composable_entity.h
Go to the documentation of this file.
1 
13 #ifndef COMPOSABLE_ENTITY_H
14 #define COMPOSABLE_ENTITY_H
15 
16 namespace argos {
17  class CComposableEntity;
18 }
19 
20 #include <argos3/core/simulator/entity/entity.h>
21 #include <argos3/core/simulator/space/space.h>
22 
23 namespace argos {
24 
32  class CComposableEntity : public CEntity {
33 
34  public:
35 
37 
38  public:
39 
46 
56  const std::string& str_id);
57 
61  virtual ~CComposableEntity() {}
62 
67  virtual void Reset();
68 
75  virtual void Update();
76 
77  virtual std::string GetTypeDescription() const {
78  return "composite";
79  }
80 
87  virtual void SetEnabled(bool b_enabled);
88 
93  virtual void UpdateComponents();
94 
99  void AddComponent(CEntity& c_component);
100 
109  CEntity& RemoveComponent(const std::string& str_component);
110 
121  CEntity& GetComponent(const std::string& str_component);
122 
135  template <class E>
136  E& GetComponent(const std::string& str_component) {
137  E* pcComponent = dynamic_cast<E*>(&GetComponent(str_component));
138  if(pcComponent != NULL) {
139  return *pcComponent;
140  }
141  else {
142  THROW_ARGOSEXCEPTION("Type conversion failed for component type \"" << str_component << "\" of entity \"" << GetId());
143  }
144  }
145 
156  bool HasComponent(const std::string& str_component);
157 
166  CEntity::TMultiMap::iterator FindComponent(const std::string& str_component);
167 
173  return m_mapComponents;
174  }
175 
182  return m_vecComponents;
183  }
184 
185  private:
186 
187  CEntity::TMultiMap m_mapComponents;
188  CEntity::TVector m_vecComponents;
189 
190  };
191 
195 #define SPACE_OPERATION_ADD_COMPOSABLE_ENTITY(ENTITY) \
196  class CSpaceOperationAdd ## ENTITY : public CSpaceOperationAddEntity { \
197  public: \
198  void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
199  c_space.AddEntity(c_entity); \
200  for(size_t i = 0; i < c_entity.GetComponentVector().size(); ++i) { \
201  CallEntityOperation<CSpaceOperationAddEntity, CSpace, void>(c_space, *(c_entity.GetComponentVector()[i])); \
202  } \
203  } \
204  };
205 
206 #define SPACE_OPERATION_REMOVE_COMPOSABLE_ENTITY(ENTITY) \
207  class CSpaceOperationRemove ## ENTITY : public CSpaceOperationRemoveEntity { \
208  public: \
209  void ApplyTo(CSpace& c_space, ENTITY& c_entity) { \
210  CEntity* pcToRemove; \
211  while(!c_entity.GetComponentVector().empty()) { \
212  pcToRemove = c_entity.GetComponentVector().back(); \
213  c_entity.RemoveComponent(pcToRemove->GetTypeDescription() + "[" + pcToRemove->GetId() + "]"); \
214  CallEntityOperation<CSpaceOperationRemoveEntity, CSpace, void>(c_space, *pcToRemove); \
215  } \
216  c_space.RemoveEntity(c_entity); \
217  } \
218  };
219 
220 #define REGISTER_STANDARD_SPACE_OPERATION_ADD_COMPOSABLE(ENTITY) \
221  SPACE_OPERATION_ADD_COMPOSABLE_ENTITY(ENTITY) \
222  REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity, \
223  CSpaceOperationAdd ## ENTITY, \
224  ENTITY);
225 
226 #define REGISTER_STANDARD_SPACE_OPERATION_REMOVE_COMPOSABLE(ENTITY) \
227  SPACE_OPERATION_REMOVE_COMPOSABLE_ENTITY(ENTITY) \
228  REGISTER_SPACE_OPERATION(CSpaceOperationRemoveEntity, \
229  CSpaceOperationRemove ## ENTITY, \
230  ENTITY);
231 
232 #define REGISTER_STANDARD_SPACE_OPERATIONS_ON_COMPOSABLE(ENTITY) \
233  REGISTER_STANDARD_SPACE_OPERATION_ADD_COMPOSABLE(ENTITY) \
234  REGISTER_STANDARD_SPACE_OPERATION_REMOVE_COMPOSABLE(ENTITY)
235 
240 }
241 
242 #endif
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
Basic class for an entity that contains other entities.
virtual std::string GetTypeDescription() const
Returns a string label for this class.
virtual ~CComposableEntity()
Class destructor.
CEntity & RemoveComponent(const std::string &str_component)
Removes a component from this composable entity.
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
CComposableEntity(CComposableEntity *pc_parent)
Class constructor.
virtual void UpdateComponents()
Calls the Update() method on all the components.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
CEntity::TVector & GetComponentVector()
Returns the vector of all the components.
virtual void Update()
Updates the status of this entity.
void AddComponent(CEntity &c_component)
Adds a component to this composable entity.
CEntity::TMultiMap & GetComponentMap()
Returns the map of all the components.
bool HasComponent(const std::string &str_component)
Returns true if this composable entity has a component with the given string label.
E & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
CEntity::TMultiMap::iterator FindComponent(const std::string &str_component)
Searches for a component with the given string label.
The basic entity type.
Definition: entity.h:90
std::vector< CEntity * > TVector
A vector of entities.
Definition: entity.h:97
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
std::multimap< std::string, CEntity * > TMultiMap
A multi-map of entities.
Definition: entity.h:103