dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
signal-caster.h
1 // -*- c++-mode -*-
2 // Copyright 2010 François Bleibel Thomas Moulard, Olivier Stasse
3 //
4 
5 #ifndef DYNAMIC_GRAPH_SIGNAL_CASTER_HH
6 #define DYNAMIC_GRAPH_SIGNAL_CASTER_HH
7 #include <map>
8 #include <typeinfo>
9 #include <vector>
10 
11 #include <boost/any.hpp>
12 #include <boost/format.hpp>
13 #include <boost/function/function1.hpp>
14 #include <boost/function/function2.hpp>
15 #include <boost/lexical_cast.hpp>
16 #include <boost/tuple/tuple.hpp>
17 
18 #include "dynamic-graph/exception-signal.h"
19 #include <dynamic-graph/dynamic-graph-api.h>
20 
21 namespace dynamicgraph {
33 class DYNAMIC_GRAPH_DLLAPI SignalCaster {
34 public:
35  virtual ~SignalCaster();
37  static void destroy();
41  typedef boost::function2<void, const boost::any &, std::ostream &>
43  typedef boost::function1<boost::any, std::istringstream &> caster_type;
44  typedef boost::function2<void, const boost::any &, std::ostream &>
45  tracer_type;
46 
48  static SignalCaster *getInstance(void);
50  void disp(const boost::any &object, std::ostream &os);
52  void trace(const boost::any &object, std::ostream &os);
54  boost::any cast(const std::type_info &, std::istringstream &iss);
56  void registerCast(const std::type_info &type, displayer_type displayer,
57  caster_type caster, tracer_type tracer);
59  void unregisterCast(const std::type_info &type);
61  bool existsCast(const std::type_info &type) const;
63  std::vector<std::string> listTypenames() const;
64 
65 private:
67  typedef boost::tuple<displayer_type, caster_type, tracer_type>
68  cast_functions_type;
69 
71  cast_functions_type &getCast(const std::string &type_name);
72 
75  std::map<std::string, cast_functions_type> functions_;
76 
77  std::map<std::string, const std::type_info *> type_info_;
78 
79 private:
80  explicit SignalCaster();
82  static SignalCaster *instance_;
83 };
84 
90 class DYNAMIC_GRAPH_DLLAPI SignalCastRegisterer {
91 public:
92  inline SignalCastRegisterer(const std::type_info &type,
94  SignalCaster::caster_type caster,
95  SignalCaster::tracer_type tracer) {
96  SignalCaster::getInstance()->registerCast(type, displayer, caster, tracer);
97  }
98 };
99 
104 template <typename T> void signal_disp(const T &value, std::ostream &os) {
105  SignalCaster::getInstance()->disp(value, os);
106 }
107 
108 template <typename T> T signal_cast(std::istringstream &iss) {
109  return boost::any_cast<T>(SignalCaster::getInstance()->cast(typeid(T), iss));
110 }
111 
112 template <typename T> void signal_trace(const T &value, std::ostream &os) {
113  SignalCaster::getInstance()->trace(value, os);
114 }
115 } // end of namespace dynamicgraph.
116 
117 #endif
void trace(const boost::any &object, std::ostream &os)
Traces an object using a registered trace function.
void signal_disp(const T &value, std::ostream &os)
static SignalCaster * getInstance(void)
Get a reference to the unique object of the class.
boost::function2< void, const boost::any &, std::ostream & > displayer_type
Definition: signal-caster.h:42
boost::any cast(const std::type_info &, std::istringstream &iss)
Casts an object using a registered cast function.
void disp(const boost::any &object, std::ostream &os)
Displays an object using a registered displayer function.
void registerCast(const std::type_info &type, displayer_type displayer, caster_type caster, tracer_type tracer)
Registers a cast.