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_nIndex(-1),
21  m_bEnabled(true),
22  m_ptConfNode(nullptr) {
23  }
24 
25  /****************************************/
26  /****************************************/
27 
29  const std::string& str_id) :
30  m_pcParent(pc_parent),
31  m_strId(str_id),
32  m_nIndex(-1),
33  m_bEnabled(true),
34  m_ptConfNode(nullptr) {
35  }
36 
37  /****************************************/
38  /****************************************/
39 
41  try {
42  /*
43  * Set the configuration node
44  */
45  m_ptConfNode = &t_tree;
46  /*
47  * Set the id of the entity from XML or type description
48  */
49  /* Was an id specified explicitly? */
50  if(NodeAttributeExists(t_tree, "id")) {
51  /* Yes, use that */
52  GetNodeAttribute(t_tree, "id", m_strId);
53  }
54  else {
55  /* No, derive it from the parent */
56  if(m_pcParent != nullptr) {
57  UInt32 unIdCount = 0;
58  while(GetParent().HasComponent(GetTypeDescription() +
59  "[" + GetTypeDescription() +
60  "_" + ToString(unIdCount) +
61  "]")) {
62  ++unIdCount;
63  }
64  m_strId = GetTypeDescription() + "_" + ToString(unIdCount);
65  }
66  else {
67  THROW_ARGOSEXCEPTION("Root entities must provide the identifier tag");
68  }
69  }
70  }
71  catch(CARGoSException& ex) {
72  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize an entity.", ex);
73  }
74  }
75 
76  /****************************************/
77  /****************************************/
78 
79  std::string CEntity::GetContext() const {
80  if(m_pcParent != nullptr) {
81  return GetParent().GetContext() + GetParent().GetId() + ".";
82  }
83  else {
84  return "";
85  }
86  }
87 
88  /****************************************/
89  /****************************************/
90 
92  if(m_pcParent != nullptr) {
93  return *m_pcParent;
94  }
95  else {
96  THROW_ARGOSEXCEPTION("Entity \"" << GetId() << "\" has no parent");
97  }
98  }
99 
100  /****************************************/
101  /****************************************/
102 
104  if(m_pcParent != nullptr) {
105  return *m_pcParent;
106  }
107  else {
108  THROW_ARGOSEXCEPTION("Entity \"" << GetId() << "\" has no parent");
109  }
110  }
111 
112  /****************************************/
113  /****************************************/
114 
116  if(m_pcParent != nullptr) {
117  return m_pcParent->GetRootEntity();
118  }
119  else {
120  return *this;
121  }
122  }
123 
124  /****************************************/
125  /****************************************/
126 
128  if(m_pcParent != nullptr) {
129  return m_pcParent->GetRootEntity();
130  }
131  else {
132  return *this;
133  }
134  }
135 
136  /****************************************/
137  /****************************************/
138 
139  void CEntity::SetEnabled(bool b_enabled) {
140  m_bEnabled = b_enabled;
141  }
142 
143  /****************************************/
144  /****************************************/
145 
147 
149 
150  /****************************************/
151  /****************************************/
152 
153 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
bool NodeAttributeExists(TConfigurationNode &t_node, const std::string &str_attribute)
Returns true if the specified attribute of a node exists.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
INIT_VTABLE_FOR(CEntity)
std::string ToString(const T &t_value)
Converts the given parameter to a std::string.
REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CEntity)
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
Basic class for an entity that contains other entities.
The basic entity type.
Definition: entity.h:90
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
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
CEntity(CComposableEntity *pc_parent)
Class constructor.
Definition: entity.cpp:18
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
virtual std::string GetTypeDescription() const
Returns a string label for this class.
Definition: entity.h:213
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:40
The exception that wraps all errors in ARGoS.