A Discrete-Event Network Simulator
API
object-base.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "object-base.h"
21 #include "log.h"
22 #include "trace-source-accessor.h"
24 #include "string.h"
25 #include "ns3/core-config.h"
26 
27 #include <cstdlib> // getenv
28 #include <cstring> // strlen
29 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("ObjectBase");
39 
40 NS_OBJECT_ENSURE_REGISTERED (ObjectBase);
41 
50 static TypeId
52 {
54  TypeId tid = TypeId ("ns3::ObjectBase");
55  tid.SetParent (tid);
56  tid.SetGroupName ("Core");
57  return tid;
58 }
59 
60 TypeId
62 {
64  static TypeId tid = GetObjectIid ();
65  return tid;
66 }
67 
69 {
70  NS_LOG_FUNCTION (this);
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION (this);
77 }
78 
79 void
81 {
82  // loop over the inheritance tree back to the Object base class.
83  NS_LOG_FUNCTION (this << &attributes);
84  TypeId tid = GetInstanceTypeId ();
85  do
86  {
87  // loop over all attributes in object type
88  NS_LOG_DEBUG ("construct tid=" << tid.GetName () << ", params=" << tid.GetAttributeN ());
89  for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
90  {
91  struct TypeId::AttributeInformation info = tid.GetAttribute (i);
92  NS_LOG_DEBUG ("try to construct \"" << tid.GetName () << "::" <<
93  info.name << "\"");
94  // is this attribute stored in this AttributeConstructionList instance ?
95  Ptr<AttributeValue> value = attributes.Find (info.checker);
96  // See if this attribute should not be set here in the
97  // constructor.
98  if (!(info.flags & TypeId::ATTR_CONSTRUCT))
99  {
100  // Handle this attribute if it should not be
101  // set here.
102  if (value == 0)
103  {
104  // Skip this attribute if it's not in the
105  // AttributeConstructionList.
106  continue;
107  }
108  else
109  {
110  // This is an error because this attribute is not
111  // settable in its constructor but is present in
112  // the AttributeConstructionList.
113  NS_FATAL_ERROR ("Attribute name=" << info.name << " tid=" << tid.GetName () << ": initial value cannot be set using attributes");
114  }
115  }
116 
117  if (value != 0)
118  {
119  // We have a matching attribute value.
120  if (DoSet (info.accessor, info.checker, *value))
121  {
122  NS_LOG_DEBUG ("construct \"" << tid.GetName () << "::" <<
123  info.name << "\"");
124  continue;
125  }
126  }
127 
128  // No matching attribute value so we try to look at the env var.
129  const char *envVar = getenv ("NS_ATTRIBUTE_DEFAULT");
130  if (envVar != 0 && std::strlen (envVar) > 0)
131  {
132  std::string env = envVar;
133  std::string::size_type cur = 0;
134  std::string::size_type next = 0;
135  while (next != std::string::npos)
136  {
137  next = env.find (";", cur);
138  std::string tmp = std::string (env, cur, next - cur);
139  std::string::size_type equal = tmp.find ("=");
140  if (equal != std::string::npos)
141  {
142  std::string name = tmp.substr (0, equal);
143  std::string envval = tmp.substr (equal + 1, tmp.size () - equal - 1);
144  if (name == tid.GetAttributeFullName (i))
145  {
146  if (DoSet (info.accessor, info.checker, StringValue (envval)))
147  {
148  NS_LOG_DEBUG ("construct \"" << tid.GetName () << "::" <<
149  info.name << "\" from env var");
150  break;
151  }
152  }
153  }
154  cur = next + 1;
155  }
156  }
157 
158  // No matching attribute value so we try to set the default value.
159  DoSet (info.accessor, info.checker, *info.initialValue);
160  NS_LOG_DEBUG ("construct \"" << tid.GetName () << "::" <<
161  info.name << "\" from initial value.");
162  }
163  tid = tid.GetParent ();
164  }
165  while (tid != ObjectBase::GetTypeId ());
167 }
168 
169 bool
172  const AttributeValue &value)
173 {
174  NS_LOG_FUNCTION (this << accessor << checker << &value);
175  Ptr<AttributeValue> v = checker->CreateValidValue (value);
176  if (v == 0)
177  {
178  return false;
179  }
180  bool ok = accessor->Set (this, *v);
181  return ok;
182 }
183 
184 void
185 ObjectBase::SetAttribute (std::string name, const AttributeValue &value)
186 {
187  NS_LOG_FUNCTION (this << name << &value);
188  struct TypeId::AttributeInformation info;
189  TypeId tid = GetInstanceTypeId ();
190  if (!tid.LookupAttributeByName (name, &info))
191  {
192  NS_FATAL_ERROR ("Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName ());
193  }
194  if (!(info.flags & TypeId::ATTR_SET)
195  || !info.accessor->HasSetter ())
196  {
197  NS_FATAL_ERROR ("Attribute name=" << name << " is not settable for this object: tid=" << tid.GetName ());
198  }
199  if (!DoSet (info.accessor, info.checker, value))
200  {
201  NS_FATAL_ERROR ("Attribute name=" << name << " could not be set for this object: tid=" << tid.GetName ());
202  }
203 }
204 bool
205 ObjectBase::SetAttributeFailSafe (std::string name, const AttributeValue &value)
206 {
207  NS_LOG_FUNCTION (this << name << &value);
208  struct TypeId::AttributeInformation info;
209  TypeId tid = GetInstanceTypeId ();
210  if (!tid.LookupAttributeByName (name, &info))
211  {
212  return false;
213  }
214  if (!(info.flags & TypeId::ATTR_SET)
215  || !info.accessor->HasSetter ())
216  {
217  return false;
218  }
219  return DoSet (info.accessor, info.checker, value);
220 }
221 
222 void
223 ObjectBase::GetAttribute (std::string name, AttributeValue &value) const
224 {
225  NS_LOG_FUNCTION (this << name << &value);
226  struct TypeId::AttributeInformation info;
227  TypeId tid = GetInstanceTypeId ();
228  if (!tid.LookupAttributeByName (name, &info))
229  {
230  NS_FATAL_ERROR ("Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName ());
231  }
232  if (!(info.flags & TypeId::ATTR_GET)
233  || !info.accessor->HasGetter ())
234  {
235  NS_FATAL_ERROR ("Attribute name=" << name << " is not gettable for this object: tid=" << tid.GetName ());
236  }
237  bool ok = info.accessor->Get (this, value);
238  if (ok)
239  {
240  return;
241  }
242  StringValue *str = dynamic_cast<StringValue *> (&value);
243  if (str == 0)
244  {
245  NS_FATAL_ERROR ("Attribute name=" << name << " tid=" << tid.GetName () << ": input value is not a string");
246  }
247  Ptr<AttributeValue> v = info.checker->Create ();
248  ok = info.accessor->Get (this, *PeekPointer (v));
249  if (!ok)
250  {
251  NS_FATAL_ERROR ("Attribute name=" << name << " tid=" << tid.GetName () << ": could not get value");
252  }
253  str->Set (v->SerializeToString (info.checker));
254 }
255 
256 
257 bool
258 ObjectBase::GetAttributeFailSafe (std::string name, AttributeValue &value) const
259 {
260  NS_LOG_FUNCTION (this << name << &value);
261  struct TypeId::AttributeInformation info;
262  TypeId tid = GetInstanceTypeId ();
263  if (!tid.LookupAttributeByName (name, &info))
264  {
265  return false;
266  }
267  if (!(info.flags & TypeId::ATTR_GET)
268  || !info.accessor->HasGetter ())
269  {
270  return false;
271  }
272  bool ok = info.accessor->Get (this, value);
273  if (ok)
274  {
275  return true;
276  }
277  StringValue *str = dynamic_cast<StringValue *> (&value);
278  if (str == 0)
279  {
280  return false;
281  }
282  Ptr<AttributeValue> v = info.checker->Create ();
283  ok = info.accessor->Get (this, *PeekPointer (v));
284  if (!ok)
285  {
286  return false;
287  }
288  str->Set (v->SerializeToString (info.checker));
289  return true;
290 }
291 
292 bool
294 {
295  NS_LOG_FUNCTION (this << name << &cb);
296  TypeId tid = GetInstanceTypeId ();
298  if (accessor == 0)
299  {
300  return false;
301  }
302  bool ok = accessor->ConnectWithoutContext (this, cb);
303  return ok;
304 }
305 bool
306 ObjectBase::TraceConnect (std::string name, std::string context, const CallbackBase &cb)
307 {
308  NS_LOG_FUNCTION (this << name << context << &cb);
309  TypeId tid = GetInstanceTypeId ();
311  if (accessor == 0)
312  {
313  return false;
314  }
315  bool ok = accessor->Connect (this, context, cb);
316  return ok;
317 }
318 bool
320 {
321  NS_LOG_FUNCTION (this << name << &cb);
322  TypeId tid = GetInstanceTypeId ();
324  if (accessor == 0)
325  {
326  return false;
327  }
328  bool ok = accessor->DisconnectWithoutContext (this, cb);
329  return ok;
330 }
331 bool
332 ObjectBase::TraceDisconnect (std::string name, std::string context, const CallbackBase &cb)
333 {
334  NS_LOG_FUNCTION (this << name << context << &cb);
335  TypeId tid = GetInstanceTypeId ();
337  if (accessor == 0)
338  {
339  return false;
340  }
341  bool ok = accessor->Disconnect (this, context, cb);
342  return ok;
343 }
344 
345 
346 
347 } // namespace ns3
std::string GetName(void) const
Get the name.
Definition: type-id.cc:977
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1178
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
virtual ~ObjectBase()
Virtual destructor.
Definition: object-base.cc:68
Hold variables of type string.
Definition: string.h:41
void Set(const std::string &value)
Set the value.
Definition: string.cc:31
ns3::StringValue attribute value declarations.
Hold a value for an Attribute.
Definition: attribute.h:68
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:411
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Base class for Callback class.
Definition: callback.h:1195
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Set a single attribute without raising errors.
Definition: object-base.cc:205
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
static TypeId GetTypeId(void)
Get the type ID.
Definition: object-base.cc:61
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
The attribute can be written at construction-time.
Definition: type-id.h:66
bool GetAttributeFailSafe(std::string name, AttributeValue &value) const
Get the value of an attribute without raising erros.
Definition: object-base.cc:258
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:944
The attribute can be read.
Definition: type-id.h:64
ns3::AttributeConstructionList declaration.
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:90
ns3::ObjectBase declaration and NS_OBJECT_ENSURE_REGISTERED() madro definition.
static TypeId GetObjectIid(void)
Ensure the TypeId for ObjectBase gets fully configured to anchor the inheritance tree properly...
Definition: object-base.cc:51
bool TraceDisconnect(std::string name, std::string context, const CallbackBase &cb)
Disconnect from a TraceSource a Callback previously connected with a context.
Definition: object-base.cc:332
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:88
List of Attribute name, value and checker triples used to construct Objects.
virtual void NotifyConstructionCompleted(void)
Notifier called once the ObjectBase is fully constructed.
Definition: object-base.cc:74
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Disconnect from a TraceSource a Callback previously connected without a context.
Definition: object-base.cc:319
Attribute implementation.
Definition: type-id.h:77
uint32_t flags
AttributeFlags value.
Definition: type-id.h:84
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:930
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:92
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
Definition: object-base.cc:293
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string name
Attribute name.
Definition: type-id.h:80
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.
The attribute can be written.
Definition: type-id.h:65
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1090
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
Definition: object-base.cc:80
bool DoSet(Ptr< const AttributeAccessor > spec, Ptr< const AttributeChecker > checker, const AttributeValue &value)
Attempt to set the value referenced by the accessor spec to a valid value according to the checker...
Definition: object-base.cc:170
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1077
virtual TypeId GetInstanceTypeId(void) const =0
Get the most derived TypeId for this Object.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1084
Ptr< AttributeValue > Find(Ptr< const AttributeChecker > checker) const
Find an Attribute in the list from its AttributeChecker.
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
Definition: object-base.cc:306
Debug message logging.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
a unique identifier for an interface.
Definition: type-id.h:58
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923