entity.h
Go to the documentation of this file.
1 
13 #ifndef ENTITY_H
14 #define ENTITY_H
15 
16 namespace argos {
17  class CEntity;
18  class CComposableEntity;
19  class CSpace;
20 }
21 
22 #include <argos3/core/utility/datatypes/datatypes.h>
23 #include <argos3/core/utility/configuration/argos_configuration.h>
24 #include <argos3/core/utility/configuration/base_configurable_resource.h>
25 #include <argos3/core/utility/plugins/factory.h>
26 #include <argos3/core/utility/plugins/vtable.h>
27 
28 #include <vector>
29 #include <map>
30 #include <string>
31 
32 #if defined(__apple_build_version__ )
33 // We are on Apple, check compiler version
34 # if __clang_major__ >= 5
35  // XCode 5.0.0 or later
36 # include <unordered_map>
37 using std::unordered_map;
38 # else
39  // XCode 4.6.3 or earlier
40 # include <tr1/unordered_map>
41 using std::tr1::unordered_map;
42 # endif
43 #else
44 // We are on Linux, use c++03 standard
45 # include <tr1/unordered_map>
46 using std::tr1::unordered_map;
47 #endif
48 
49 namespace argos {
50 
90  public EnableVTableFor<CEntity> {
91 
92  public:
93 
95 
97  typedef std::vector<CEntity*> TVector;
98 
100  typedef unordered_map<std::string, CEntity*> TMap;
101 
103  typedef std::multimap<std::string, CEntity*> TMultiMap;
104 
105  public:
106 
112  CEntity(CComposableEntity* pc_parent);
113 
122  CEntity(CComposableEntity* pc_parent,
123  const std::string& str_id);
124 
128  virtual ~CEntity() {}
129 
139  virtual void Init(TConfigurationNode& t_tree);
140 
145  virtual void Reset() {}
146 
151  virtual void Destroy() {}
152 
157  inline const std::string& GetId() const {
158  return m_strId;
159  }
160 
165  std::string GetContext() const;
166 
171  inline bool HasParent() const {
172  return (m_pcParent != NULL);
173  }
174 
180 
185  const CEntity& GetRootEntity() const;
186 
193 
199  const CComposableEntity& GetParent() const;
200 
205  inline void SetParent(CComposableEntity& c_parent) {
206  m_pcParent = &c_parent;
207  }
208 
213  virtual std::string GetTypeDescription() const {
214  return "entity";
215  }
216 
221  virtual void Update() {}
222 
234  ssize_t GetIndex() const {
235  return m_nIndex;
236  }
237 
245  void SetIndex(ssize_t n_idx) {
246  m_nIndex = n_idx;
247  }
248 
255  bool IsEnabled() const {
256  return m_bEnabled;
257  }
258 
265  inline void Enable() {
266  SetEnabled(true);
267  }
268 
275  inline void Disable() {
276  SetEnabled(false);
277  }
278 
285  virtual void SetEnabled(bool b_enabled);
286 
293  return m_ptConfNode;
294  }
295 
296  private:
297 
299  CComposableEntity* m_pcParent;
300 
302  std::string m_strId;
303 
305  ssize_t m_nIndex;
306 
308  bool m_bEnabled;
309 
311  TConfigurationNode* m_ptConfNode;
312 
313  };
314 
319  bool operator()(const CEntity* pc_a, const CEntity* pc_b) const {
320  return pc_a->GetIndex() < pc_b->GetIndex();
321  }
322  bool operator()(const CEntity& c_a, const CEntity& c_b) const {
323  return c_a.GetIndex() < c_b.GetIndex();
324  }
325  };
326 
330  template <typename LABEL, typename PLUGIN, typename RETURN_TYPE>
332  public:
333  template <typename DERIVED, typename OPERATION_IMPL>
334  RETURN_TYPE Hook(PLUGIN& t_plugin, CEntity& c_entity) {
335  return Dispatch<DERIVED, OPERATION_IMPL>(t_plugin, c_entity);
336  }
337  protected:
338  template <typename DERIVED, typename OPERATION_IMPL>
339  RETURN_TYPE Dispatch(PLUGIN& t_plugin, CEntity& c_entity) {
340  /* First dispatch: cast this operation into the specific operation */
341  OPERATION_IMPL& tOperation = static_cast<OPERATION_IMPL&>(*this);
342  /* Second dispatch: cast t_base to DERIVED */
343  DERIVED& tDerived = static_cast<DERIVED&>(c_entity);
344  /* Perform visit */
345  return tOperation.ApplyTo(t_plugin, tDerived);
346  }
347  };
348 
353  bool Value;
354  SOperationOutcome() : Value(false) {}
355  SOperationOutcome(bool b_value) : Value(b_value) {}
356  bool operator()() const { return Value; }
357  };
358 
362  template <typename LABEL, typename PLUGIN, typename RETURN_TYPE>
363  class CEntityOperationInstanceHolder {
364  public:
365  ~CEntityOperationInstanceHolder() {
366  while(!m_vecOperationInstances.empty()) {
367  if(m_vecOperationInstances.back() != NULL) {
368  delete m_vecOperationInstances.back();
369  }
370  m_vecOperationInstances.pop_back();
371  }
372  }
373  template <typename DERIVED>
374  void Add(CEntityOperation<LABEL, PLUGIN, RETURN_TYPE>* pc_operation) {
375  /* Find the slot */
376  size_t unIndex = GetTag<DERIVED, CEntity>();
377  /* Does the holder have a slot for this index? */
378  if(unIndex >= m_vecOperationInstances.size()) {
379  /* No, new slots must be created
380  * Fill the slots with NULL
381  */
382  /* Create new slots up to index+1 and fill them with tDefaultFunction */
383  m_vecOperationInstances.resize(unIndex+1, NULL);
384  }
385  m_vecOperationInstances[unIndex] = pc_operation;
386  }
387  CEntityOperation<LABEL, PLUGIN, RETURN_TYPE>* operator[](size_t un_index) const {
388  if(un_index >= m_vecOperationInstances.size()) {
389  return NULL;
390  }
391  return m_vecOperationInstances[un_index];
392  }
393  private:
394  std::vector<CEntityOperation<LABEL, PLUGIN, RETURN_TYPE>*> m_vecOperationInstances;
395  };
404  template <typename LABEL, typename PLUGIN, typename RETURN_VALUE>
405  CEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE>& GetEntityOperationInstanceHolder() {
406  static CEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE> cHolder;
407  return cHolder;
408  }
417  template<typename LABEL, typename PLUGIN, typename RETURN_VALUE>
418  RETURN_VALUE CallEntityOperation(PLUGIN& t_plugin, CEntity& c_entity) {
419  typedef RETURN_VALUE (CEntityOperation<LABEL, PLUGIN, RETURN_VALUE>::*TFunction)(PLUGIN& t_plugin, CEntity&);
420  TFunction tFunction = GetVTable<LABEL, CEntity, TFunction>()[c_entity.GetTag()];
421  if(tFunction != NULL) {
422  CEntityOperation<LABEL, PLUGIN, RETURN_VALUE>* pcOperation = GetEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE>()[c_entity.GetTag()];
423  if(pcOperation != NULL) {
424  return (pcOperation->*tFunction)(t_plugin, c_entity);
425  }
426  }
427  return RETURN_VALUE();
428  }
429 
430 }
431 
432 #define REGISTER_ENTITY(CLASSNAME, \
433  LABEL, \
434  AUTHOR, \
435  VERSION, \
436  BRIEF_DESCRIPTION, \
437  LONG_DESCRIPTION, \
438  STATUS) \
439  REGISTER_SYMBOL(CEntity, \
440  CLASSNAME, \
441  LABEL, \
442  AUTHOR, \
443  VERSION, \
444  BRIEF_DESCRIPTION, \
445  LONG_DESCRIPTION, \
446  STATUS)
447 
451 #define REGISTER_ENTITY_OPERATION(LABEL, PLUGIN, OPERATION, RETURN_VALUE, DERIVED) \
452  class C ## LABEL ## PLUGIN ## OPERATION ## RETURN_VALUE ## DERIVED { \
453  typedef RETURN_VALUE (CEntityOperation<LABEL, PLUGIN, RETURN_VALUE>::*TFunction)(PLUGIN&, CEntity&); \
454  public: \
455  C ## LABEL ## PLUGIN ## OPERATION ## RETURN_VALUE ## DERIVED() { \
456  GetVTable<LABEL, CEntity, TFunction>().Add<DERIVED>(&OPERATION::Hook<DERIVED, OPERATION>); \
457  GetEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE>().Add<DERIVED>(new OPERATION()); \
458  } \
459  } c ## LABEL ## PLUGIN ## OPERATION ## RETURN_VALUE ## DERIVED;
460 
461 #endif
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
RETURN_VALUE CallEntityOperation(PLUGIN &t_plugin, CEntity &c_entity)
Calls the operation corresponding to the given context and operand Skips the function call if the ope...
Definition: entity.h:418
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Basic class for an entity that contains other entities.
The basic entity type.
Definition: entity.h:90
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
Definition: entity.h:145
void Disable()
Disables the entity.
Definition: entity.h:275
std::vector< CEntity * > TVector
A vector of entities.
Definition: entity.h:97
virtual void Update()
Updates the state of this entity.
Definition: entity.h:221
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
void SetIndex(ssize_t n_idx)
Sets the entity index.
Definition: entity.h:245
CEntity & GetRootEntity()
Returns the root entity containing this entity.
Definition: entity.cpp:115
std::string GetContext() const
Returns the context of this entity.
Definition: entity.cpp:79
std::multimap< std::string, CEntity * > TMultiMap
A multi-map of entities.
Definition: entity.h:103
TConfigurationNode * GetConfigurationNode()
Returns a pointer to the configuration node that was used to create this entity.
Definition: entity.h:292
bool IsEnabled() const
Returns true if the entity is enabled.
Definition: entity.h:255
CEntity(CComposableEntity *pc_parent)
Class constructor.
Definition: entity.cpp:18
void Enable()
Enables the entity.
Definition: entity.h:265
virtual ~CEntity()
Class destructor.
Definition: entity.h:128
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:91
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: entity.cpp:139
void SetParent(CComposableEntity &c_parent)
Sets this entity's parent.
Definition: entity.h:205
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
Definition: entity.h:151
virtual std::string GetTypeDescription() const
Returns a string label for this class.
Definition: entity.h:213
bool HasParent() const
Returns true if this entity has a parent.
Definition: entity.h:171
ssize_t GetIndex() const
Returns the entity index.
Definition: entity.h:234
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:40
unordered_map< std::string, CEntity * > TMap
A map of entities.
Definition: entity.h:100
A generic entity comparator, used in containers that must be ordered deterministically.
Definition: entity.h:318
bool operator()(const CEntity &c_a, const CEntity &c_b) const
Definition: entity.h:322
bool operator()(const CEntity *pc_a, const CEntity *pc_b) const
Definition: entity.h:319
The basic operation to be stored in the vtable.
Definition: entity.h:331
RETURN_TYPE Hook(PLUGIN &t_plugin, CEntity &c_entity)
Definition: entity.h:334
RETURN_TYPE Dispatch(PLUGIN &t_plugin, CEntity &c_entity)
Definition: entity.h:339
Type to use as return value for operation outcome.
Definition: entity.h:352
SOperationOutcome(bool b_value)
Definition: entity.h:355
bool operator()() const
Definition: entity.h:356
This class is the base of all XML-configurable ARGoS interface.
Helper to make a class hierarchy vtable-enabled.
Definition: vtable.h:136