ARGoS  3
A parallel, multi-engine simulator for swarm robotics
entity.cpp
Go to the documentation of this file.
1 
8 #include "entity.h"
9 #include "composable_entity.h"
10 #include <argos3/core/utility/logging/argos_log.h>
11 #include <argos3/core/simulator/space/space.h>
12 
13 namespace argos {
14 
15  /****************************************/
16  /****************************************/
17 
19  m_pcParent(pc_parent),
20  m_bEnabled(true) {
21  }
22 
23  /****************************************/
24  /****************************************/
25 
27  const std::string& str_id) :
28  m_pcParent(pc_parent),
29  m_strId(str_id),
30  m_bEnabled(true) {
31  }
32 
33  /****************************************/
34  /****************************************/
35 
37  try {
38  /*
39  * Set the id of the entity from XML or type description
40  */
41  /* Was an id specified explicitly? */
42  if(NodeAttributeExists(t_tree, "id")) {
43  /* Yes, use that */
44  GetNodeAttribute(t_tree, "id", m_strId);
45  }
46  else {
47  /* No, derive it from the parent */
48  if(m_pcParent != NULL) {
49  UInt32 unIdCount = 0;
50  while(GetParent().HasComponent(GetTypeDescription() +
51  "[" + GetTypeDescription() +
52  "_" + ToString(unIdCount) +
53  "]")) {
54  ++unIdCount;
55  }
56  m_strId = GetTypeDescription() + "_" + ToString(unIdCount);
57  }
58  else {
59  THROW_ARGOSEXCEPTION("Root entities must provide the identifier tag");
60  }
61  }
62  }
63  catch(CARGoSException& ex) {
64  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize an entity.", ex);
65  }
66  }
67 
68  /****************************************/
69  /****************************************/
70 
71  std::string CEntity::GetContext() const {
72  if(m_pcParent != NULL) {
73  return GetParent().GetContext() + GetParent().GetId() + ".";
74  }
75  else {
76  return "";
77  }
78  }
79 
80  /****************************************/
81  /****************************************/
82 
84  if(m_pcParent != NULL) {
85  return *m_pcParent;
86  }
87  else {
88  THROW_ARGOSEXCEPTION("Entity \"" << GetId() << "\" has no parent");
89  }
90  }
91 
92  /****************************************/
93  /****************************************/
94 
95  const CComposableEntity& CEntity::GetParent() const {
96  if(m_pcParent != NULL) {
97  return *m_pcParent;
98  }
99  else {
100  THROW_ARGOSEXCEPTION("Entity \"" << GetId() << "\" has no parent");
101  }
102  }
103 
104  /****************************************/
105  /****************************************/
106 
108  if(m_pcParent != NULL) {
109  return m_pcParent->GetRootEntity();
110  }
111  else {
112  return *this;
113  }
114  }
115 
116  /****************************************/
117  /****************************************/
118 
119  const CEntity& CEntity::GetRootEntity() const {
120  if(m_pcParent != NULL) {
121  return m_pcParent->GetRootEntity();
122  }
123  else {
124  return *this;
125  }
126  }
127 
128  /****************************************/
129  /****************************************/
130 
131  void CEntity::SetEnabled(bool b_enabled) {
132  m_bEnabled = b_enabled;
133  }
134 
135  /****************************************/
136  /****************************************/
137 
139 
141 
142  /****************************************/
143  /****************************************/
144 
145 }
entity.h
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::CARGoSException
The exception that wraps all errors in ARGoS.
Definition: argos_exception.h:61
argos::REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY
REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CEntity)
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::INIT_VTABLE_FOR
INIT_VTABLE_FOR(CEntity)
THROW_ARGOSEXCEPTION_NESTED
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
Definition: argos_exception.h:115
THROW_ARGOSEXCEPTION
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
Definition: argos_exception.h:111
composable_entity.h
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::CEntity
The basic entity type.
Definition: entity.h:89
argos::ToString
std::string ToString(const T &t_value)
Converts the given parameter to a std::string.
Definition: string_utilities.h:36
argos::CEntity::GetParent
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:83
UInt32
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
argos::GetNodeAttribute
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
Definition: argos_configuration.h:208
argos::CEntity::GetContext
std::string GetContext() const
Returns the context of this entity.
Definition: entity.cpp:71
argos::NodeAttributeExists
bool NodeAttributeExists(TConfigurationNode &t_node, const std::string &str_attribute)
Returns true if the specified attribute of a node exists.
Definition: argos_configuration.h:172
argos::CEntity::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:36