ARGoS  3
A parallel, multi-engine simulator for swarm robotics
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 
94  ENABLE_VTABLE();
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 
229  bool IsEnabled() const {
230  return m_bEnabled;
231  }
232 
239  inline void Enable() {
240  SetEnabled(true);
241  }
242 
249  inline void Disable() {
250  SetEnabled(false);
251  }
252 
259  virtual void SetEnabled(bool b_enabled);
260 
261  private:
262 
264  CComposableEntity* m_pcParent;
265 
267  std::string m_strId;
268 
270  bool m_bEnabled;
271 
272  };
273 
277  template <typename LABEL, typename PLUGIN, typename RETURN_TYPE>
279  public:
280  template <typename DERIVED, typename OPERATION_IMPL>
281  RETURN_TYPE Hook(PLUGIN& t_plugin, CEntity& c_entity) {
282  return Dispatch<DERIVED, OPERATION_IMPL>(t_plugin, c_entity);
283  }
284  protected:
285  template <typename DERIVED, typename OPERATION_IMPL>
286  RETURN_TYPE Dispatch(PLUGIN& t_plugin, CEntity& c_entity) {
287  /* First dispatch: cast this operation into the specific operation */
288  OPERATION_IMPL& tOperation = static_cast<OPERATION_IMPL&>(*this);
289  /* Second dispatch: cast t_base to DERIVED */
290  DERIVED& tDerived = static_cast<DERIVED&>(c_entity);
291  /* Perform visit */
292  return tOperation.ApplyTo(t_plugin, tDerived);
293  }
294  };
295 
300  bool Value;
301  SOperationOutcome() : Value(false) {}
302  SOperationOutcome(bool b_value) : Value(b_value) {}
303  bool operator()() const { return Value; }
304  };
305 
309  template <typename LABEL, typename PLUGIN, typename RETURN_TYPE>
310  class CEntityOperationInstanceHolder {
311  public:
312  ~CEntityOperationInstanceHolder() {
313  while(!m_vecOperationInstances.empty()) {
314  if(m_vecOperationInstances.back() != NULL) {
315  delete m_vecOperationInstances.back();
316  }
317  m_vecOperationInstances.pop_back();
318  }
319  }
320  template <typename DERIVED>
321  void Add(CEntityOperation<LABEL, PLUGIN, RETURN_TYPE>* pc_operation) {
322  /* Find the slot */
323  size_t unIndex = GetTag<DERIVED, CEntity>();
324  /* Does the holder have a slot for this index? */
325  if(unIndex >= m_vecOperationInstances.size()) {
326  /* No, new slots must be created
327  * Fill the slots with NULL
328  */
329  /* Create new slots up to index+1 and fill them with tDefaultFunction */
330  m_vecOperationInstances.resize(unIndex+1, NULL);
331  }
332  m_vecOperationInstances[unIndex] = pc_operation;
333  }
334  CEntityOperation<LABEL, PLUGIN, RETURN_TYPE>* operator[](size_t un_index) const {
335  if(un_index >= m_vecOperationInstances.size()) {
336  return NULL;
337  }
338  return m_vecOperationInstances[un_index];
339  }
340  private:
341  std::vector<CEntityOperation<LABEL, PLUGIN, RETURN_TYPE>*> m_vecOperationInstances;
342  };
351  template <typename LABEL, typename PLUGIN, typename RETURN_VALUE>
352  CEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE>& GetEntityOperationInstanceHolder() {
353  static CEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE> cHolder;
354  return cHolder;
355  }
364  template<typename LABEL, typename PLUGIN, typename RETURN_VALUE>
365  RETURN_VALUE CallEntityOperation(PLUGIN& t_plugin, CEntity& c_entity) {
366  typedef RETURN_VALUE (CEntityOperation<LABEL, PLUGIN, RETURN_VALUE>::*TFunction)(PLUGIN& t_plugin, CEntity&);
367  TFunction tFunction = GetVTable<LABEL, CEntity, TFunction>()[c_entity.GetTag()];
368  if(tFunction != NULL) {
369  CEntityOperation<LABEL, PLUGIN, RETURN_VALUE>* pcOperation = GetEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE>()[c_entity.GetTag()];
370  if(pcOperation != NULL) {
371  return (pcOperation->*tFunction)(t_plugin, c_entity);
372  }
373  }
374  return RETURN_VALUE();
375  }
376 
377 }
378 
379 #define REGISTER_ENTITY(CLASSNAME, \
380  LABEL, \
381  AUTHOR, \
382  VERSION, \
383  BRIEF_DESCRIPTION, \
384  LONG_DESCRIPTION, \
385  STATUS) \
386  REGISTER_SYMBOL(CEntity, \
387  CLASSNAME, \
388  LABEL, \
389  AUTHOR, \
390  VERSION, \
391  BRIEF_DESCRIPTION, \
392  LONG_DESCRIPTION, \
393  STATUS)
394 
398 #define REGISTER_ENTITY_OPERATION(LABEL, PLUGIN, OPERATION, RETURN_VALUE, DERIVED) \
399  class C ## LABEL ## PLUGIN ## OPERATION ## RETURN_VALUE ## DERIVED { \
400  typedef RETURN_VALUE (CEntityOperation<LABEL, PLUGIN, RETURN_VALUE>::*TFunction)(PLUGIN&, CEntity&); \
401  public: \
402  C ## LABEL ## PLUGIN ## OPERATION ## RETURN_VALUE ## DERIVED() { \
403  GetVTable<LABEL, CEntity, TFunction>().Add<DERIVED>(&OPERATION::Hook<DERIVED, OPERATION>); \
404  GetEntityOperationInstanceHolder<LABEL, PLUGIN, RETURN_VALUE>().Add<DERIVED>(new OPERATION()); \
405  } \
406  } c ## LABEL ## PLUGIN ## OPERATION ## RETURN_VALUE ## DERIVED;
407 
408 #endif
argos::CBaseConfigurableResource
This class is the base of all XML-configurable ARGoS interface.
Definition: base_configurable_resource.h:23
argos::CEntityOperation
The basic operation to be stored in the vtable.
Definition: entity.h:278
argos::CEntity::GetId
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CComposableEntity
Basic class for an entity that contains other entities.
Definition: composable_entity.h:32
argos::CEntity::ENABLE_VTABLE
ENABLE_VTABLE()
argos::CEntity::HasParent
bool HasParent() const
Returns true if this entity has a parent.
Definition: entity.h:171
argos::CEntity::Enable
void Enable()
Enables the entity.
Definition: entity.h:239
argos::CEntity::~CEntity
virtual ~CEntity()
Class destructor.
Definition: entity.h:128
argos::CallEntityOperation
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:365
argos::CEntity::Destroy
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
Definition: entity.h:151
argos::CEntity::Update
virtual void Update()
Updates the state of this entity.
Definition: entity.h:221
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CEntity::GetRootEntity
CEntity & GetRootEntity()
Returns the root entity containing this entity.
Definition: entity.cpp:107
argos::CEntity::GetTypeDescription
virtual std::string GetTypeDescription() const
Returns a string label for this class.
Definition: entity.h:213
argos::SOperationOutcome::SOperationOutcome
SOperationOutcome(bool b_value)
Definition: entity.h:302
argos::SOperationOutcome::Value
bool Value
Definition: entity.h:300
argos::CEntity::TMap
unordered_map< std::string, CEntity * > TMap
A map of entities.
Definition: entity.h:100
argos::CEntity::SetEnabled
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: entity.cpp:131
argos::CEntity::CEntity
CEntity(CComposableEntity *pc_parent)
Class constructor.
Definition: entity.cpp:18
argos::SOperationOutcome::SOperationOutcome
SOperationOutcome()
Definition: entity.h:301
argos::CEntity
The basic entity type.
Definition: entity.h:89
argos::CEntity::SetParent
void SetParent(CComposableEntity &c_parent)
Sets this entity's parent.
Definition: entity.h:205
argos::CEntity::GetParent
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:83
argos::CEntity::Reset
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
argos::CEntity::Disable
void Disable()
Disables the entity.
Definition: entity.h:249
argos::CEntity::GetContext
std::string GetContext() const
Returns the context of this entity.
Definition: entity.cpp:71
argos::SOperationOutcome
Type to use as return value for operation outcome.
Definition: entity.h:299
argos::CEntity::IsEnabled
bool IsEnabled() const
Returns true if the entity is enabled.
Definition: entity.h:229
argos::CEntityOperation::Dispatch
RETURN_TYPE Dispatch(PLUGIN &t_plugin, CEntity &c_entity)
Definition: entity.h:286
argos::CEntity::TMultiMap
std::multimap< std::string, CEntity * > TMultiMap
A multi-map of entities.
Definition: entity.h:103
argos::CEntityOperation::Hook
RETURN_TYPE Hook(PLUGIN &t_plugin, CEntity &c_entity)
Definition: entity.h:281
argos::EnableVTableFor
Helper to make a class hierarchy vtable-enabled.
Definition: vtable.h:136
argos::CEntity::TVector
std::vector< CEntity * > TVector
A vector of entities.
Definition: entity.h:97
argos::CEntity::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:36
argos::SOperationOutcome::operator()
bool operator()() const
Definition: entity.h:303