entity.h 4.37 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// -*- mode: c++ -*-
// Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
// JRL, CNRS/AIST.
//

#ifndef DYNAMIC_GRAPH_ENTITY_H
# define DYNAMIC_GRAPH_ENTITY_H
# include <iosfwd>
# include <map>
# include <sstream>
# include <string>

# include <boost/noncopyable.hpp>

# include <dynamic-graph/fwd.hh>
# include <dynamic-graph/dynamic-graph-api.h>
# include <dynamic-graph/exception-factory.h>
# include <dynamic-graph/signal-array.h>
# include <dynamic-graph/signal-base.h>
20
# include <dynamic-graph/logger.h>
Thomas Moulard's avatar
Thomas Moulard committed
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// \brief Helper macro for entity declaration.
///
/// This macro should be called in the declaration of all entities.
/// Example:
/// <code>
/// class A : public dynamicgraph::entity
/// {
///   DYNAMIC_GRAPH_ENTITY_DECL();
///
///   public:
//      // your class here
/// };
/// </code>
///
/// Caution: you *MUST* call DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN in the
/// associated source file to ensure that the attributes generated by
/// this macro are correctly initialized.
# define DYNAMIC_GRAPH_ENTITY_DECL()				\
  public:							\
  virtual const std::string& getClassName () const		\
  {								\
    return CLASS_NAME;						\
  }								\
  static const std::string CLASS_NAME

Thomas Moulard's avatar
Thomas Moulard committed
47
namespace dynamicgraph
48
{
Thomas Moulard's avatar
Thomas Moulard committed
49
50
51
52
  /// \ingroup dgraph
  ///
  /// \brief This class represents an entity, i.e. a generic
  /// computational unit that provides input and output signals.
Thomas Moulard's avatar
Thomas Moulard committed
53
  ///
Thomas Moulard's avatar
Thomas Moulard committed
54
55
  /// These signals link the entities together to form a complete
  /// computation graph.  To declare a new entity, please see the
Guilhem Saurel's avatar
Guilhem Saurel committed
56
  /// DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN macro in factory.h.
Thomas Moulard's avatar
Thomas Moulard committed
57
58
59
  class DYNAMIC_GRAPH_DLLAPI Entity : private boost::noncopyable
  {
  public:
60
    typedef std::map< std::string,SignalBase<int>* > SignalMap;
61
    typedef std::map<const std::string, command::Command*> CommandMap_t;
62

Thomas Moulard's avatar
Thomas Moulard committed
63
64
65
66
67
68
69
    explicit Entity (const std::string& name);
    virtual ~Entity  ();

    const std::string& getName  () const
    {
      return name;
    }
70
71
72
73
74
    virtual const std::string& getClassName  () const
    {
      static std::string ret("Entity");
      return ret;
    }
75
    virtual std::string getDocString () const;
76
    bool hasSignal( const std::string & signame ) const;
Thomas Moulard's avatar
Thomas Moulard committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    SignalBase<int>& getSignal (const std::string& signalName);
    const SignalBase<int>& getSignal (const std::string& signalName) const;
    std::ostream& displaySignalList(std::ostream& os) const;
    virtual std::ostream& writeGraph (std::ostream& os) const;
    virtual std::ostream& writeCompletionList (std::ostream& os) const;

    virtual void display (std::ostream& os) const;

    virtual SignalBase<int>* test ()
    {
      return 0;
    }

    virtual void test2 (SignalBase<int>*)
    {
      return ;
    }

95
96
    const std::string& getCommandList () const;
    CommandMap_t getNewStyleCommandMap();
97
    command::Command* getNewStyleCommand( const std::string& cmdName );
Thomas Moulard's avatar
Thomas Moulard committed
98

99
    SignalMap getSignalMap() const;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

    /** \name Logger related methods */
    /** \{*/
    /// \brief Send messages \param msg with level t. Add string file and line to message.
    void sendMsg(const std::string &msg,
		 MsgType t=MSG_TYPE_INFO,
		 const char *file="",
		 int line=0);

    /// \brief Specify the verbosity level of the logger.
    void setLoggerVerbosityLevel(LoggerVerbosity lv)
    {logger_.setVerbosity(lv);}

    /// \brief Get the logger's verbosity level.
    LoggerVerbosity getLoggerVerbosityLevel()
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    { return logger_.getVerbosity(); }

    /// \brief Set the time sample.
    bool setTimeSample(double t)
    { return logger_.setTimeSample(t); }

    /// \brief Get the time sample.
    double getTimeSample()
    { return logger_.getTimeSample();}

    /// \brief Set the period of the stream period
    bool setStreamPrintPeriod(double t)
    { return logger_.setStreamPrintPeriod(t); }

    /// \brief Get the period of the stream period
    double getStreamPrintPeriod()
    { return logger_.getStreamPrintPeriod();}

Thomas Moulard's avatar
Thomas Moulard committed
133
  protected:
134
    void addCommand(const std::string& name,command::Command* command);
Thomas Moulard's avatar
Thomas Moulard committed
135
136
137

    void entityRegistration ();
    void entityDeregistration ();
Thomas Moulard's avatar
Thomas Moulard committed
138
139
140
141
142
143

    void signalRegistration (const SignalArray<int>& signals);
    void signalDeregistration (const std::string& name);

    std::string name;
    SignalMap signalMap;
144
    CommandMap_t commandMap;
145
    Logger logger_;
Thomas Moulard's avatar
Thomas Moulard committed
146
147
148
149
150
151
152
  };

  DYNAMIC_GRAPH_DLLAPI std::ostream&
  operator<< (std::ostream& os, const dynamicgraph::Entity& ent);
} // end of namespace dynamicgraph

#endif //! DYNAMIC_GRAPH_ENTITY_H