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 #ifdef HAVE_STDLIB_H
27 #include <cstdlib>
28 #endif
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 #ifdef HAVE_GETENV
129  // No matching attribute value so we try to look at the env var.
130  char *envVar = getenv ("NS_ATTRIBUTE_DEFAULT");
131  if (envVar != 0)
132  {
133  std::string env = std::string (envVar);
134  std::string::size_type cur = 0;
135  std::string::size_type next = 0;
136  while (next != std::string::npos)
137  {
138  next = env.find (";", cur);
139  std::string tmp = std::string (env, cur, next - cur);
140  std::string::size_type equal = tmp.find ("=");
141  if (equal != std::string::npos)
142  {
143  std::string name = tmp.substr (0, equal);
144  std::string envval = tmp.substr (equal + 1, tmp.size () - equal - 1);
145  if (name == tid.GetAttributeFullName (i))
146  {
147  if (DoSet (info.accessor, info.checker, StringValue (envval)))
148  {
149  NS_LOG_DEBUG ("construct \"" << tid.GetName () << "::" <<
150  info.name << "\" from env var");
151  break;
152  }
153  }
154  }
155  cur = next + 1;
156  }
157  }
158 #endif /* HAVE_GETENV */
159 
160  // No matching attribute value so we try to set the default value.
161  DoSet (info.accessor, info.checker, *info.initialValue);
162  NS_LOG_DEBUG ("construct \"" << tid.GetName () << "::" <<
163  info.name << "\" from initial value.");
164  }
165  tid = tid.GetParent ();
166  }
167  while (tid != ObjectBase::GetTypeId ());
169 }
170 
171 bool
174  const AttributeValue &value)
175 {
176  NS_LOG_FUNCTION (this << accessor << checker << &value);
177  Ptr<AttributeValue> v = checker->CreateValidValue (value);
178  if (v == 0)
179  {
180  return false;
181  }
182  bool ok = accessor->Set (this, *v);
183  return ok;
184 }
185 
186 void
187 ObjectBase::SetAttribute (std::string name, const AttributeValue &value)
188 {
189  NS_LOG_FUNCTION (this << name << &value);
190  struct TypeId::AttributeInformation info;
191  TypeId tid = GetInstanceTypeId ();
192  if (!tid.LookupAttributeByName (name, &info))
193  {
194  NS_FATAL_ERROR ("Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName ());
195  }
196  if (!(info.flags & TypeId::ATTR_SET)
197  || !info.accessor->HasSetter ())
198  {
199  NS_FATAL_ERROR ("Attribute name=" << name << " is not settable for this object: tid=" << tid.GetName ());
200  }
201  if (!DoSet (info.accessor, info.checker, value))
202  {
203  NS_FATAL_ERROR ("Attribute name=" << name << " could not be set for this object: tid=" << tid.GetName ());
204  }
205 }
206 bool
207 ObjectBase::SetAttributeFailSafe (std::string name, const AttributeValue &value)
208 {
209  NS_LOG_FUNCTION (this << name << &value);
210  struct TypeId::AttributeInformation info;
211  TypeId tid = GetInstanceTypeId ();
212  if (!tid.LookupAttributeByName (name, &info))
213  {
214  return false;
215  }
216  if (!(info.flags & TypeId::ATTR_SET)
217  || !info.accessor->HasSetter ())
218  {
219  return false;
220  }
221  return DoSet (info.accessor, info.checker, value);
222 }
223 
224 void
225 ObjectBase::GetAttribute (std::string name, AttributeValue &value) const
226 {
227  NS_LOG_FUNCTION (this << name << &value);
228  struct TypeId::AttributeInformation info;
229  TypeId tid = GetInstanceTypeId ();
230  if (!tid.LookupAttributeByName (name, &info))
231  {
232  NS_FATAL_ERROR ("Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName ());
233  }
234  if (!(info.flags & TypeId::ATTR_GET)
235  || !info.accessor->HasGetter ())
236  {
237  NS_FATAL_ERROR ("Attribute name=" << name << " is not gettable for this object: tid=" << tid.GetName ());
238  }
239  bool ok = info.accessor->Get (this, value);
240  if (ok)
241  {
242  return;
243  }
244  StringValue *str = dynamic_cast<StringValue *> (&value);
245  if (str == 0)
246  {
247  NS_FATAL_ERROR ("Attribute name=" << name << " tid=" << tid.GetName () << ": input value is not a string");
248  }
249  Ptr<AttributeValue> v = info.checker->Create ();
250  ok = info.accessor->Get (this, *PeekPointer (v));
251  if (!ok)
252  {
253  NS_FATAL_ERROR ("Attribute name=" << name << " tid=" << tid.GetName () << ": could not get value");
254  }
255  str->Set (v->SerializeToString (info.checker));
256 }
257 
258 
259 bool
260 ObjectBase::GetAttributeFailSafe (std::string name, AttributeValue &value) const
261 {
262  NS_LOG_FUNCTION (this << name << &value);
263  struct TypeId::AttributeInformation info;
264  TypeId tid = GetInstanceTypeId ();
265  if (!tid.LookupAttributeByName (name, &info))
266  {
267  return false;
268  }
269  if (!(info.flags & TypeId::ATTR_GET)
270  || !info.accessor->HasGetter ())
271  {
272  return false;
273  }
274  bool ok = info.accessor->Get (this, value);
275  if (ok)
276  {
277  return true;
278  }
279  StringValue *str = dynamic_cast<StringValue *> (&value);
280  if (str == 0)
281  {
282  return false;
283  }
284  Ptr<AttributeValue> v = info.checker->Create ();
285  ok = info.accessor->Get (this, *PeekPointer (v));
286  if (!ok)
287  {
288  return false;
289  }
290  str->Set (v->SerializeToString (info.checker));
291  return true;
292 }
293 
294 bool
296 {
297  NS_LOG_FUNCTION (this << name << &cb);
298  TypeId tid = GetInstanceTypeId ();
300  if (accessor == 0)
301  {
302  return false;
303  }
304  bool ok = accessor->ConnectWithoutContext (this, cb);
305  return ok;
306 }
307 bool
308 ObjectBase::TraceConnect (std::string name, std::string context, const CallbackBase &cb)
309 {
310  NS_LOG_FUNCTION (this << name << context << &cb);
311  TypeId tid = GetInstanceTypeId ();
313  if (accessor == 0)
314  {
315  return false;
316  }
317  bool ok = accessor->Connect (this, context, cb);
318  return ok;
319 }
320 bool
322 {
323  NS_LOG_FUNCTION (this << name << &cb);
324  TypeId tid = GetInstanceTypeId ();
326  if (accessor == 0)
327  {
328  return false;
329  }
330  bool ok = accessor->DisconnectWithoutContext (this, cb);
331  return ok;
332 }
333 bool
334 ObjectBase::TraceDisconnect (std::string name, std::string context, const CallbackBase &cb)
335 {
336  NS_LOG_FUNCTION (this << name << context << &cb);
337  TypeId tid = GetInstanceTypeId ();
339  if (accessor == 0)
340  {
341  return false;
342  }
343  bool ok = accessor->Disconnect (this, context, cb);
344  return ok;
345 }
346 
347 
348 
349 } // namespace ns3
std::string GetName(void) const
Get the name.
Definition: type-id.cc:976
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:1185
#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:566
#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:1194
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Set a single attribute without raising errors.
Definition: object-base.cc:207
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
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:260
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:943
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:334
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:321
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:929
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:295
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:1089
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:172
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1076
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:1083
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:308
Debug message logging.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:187
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:225
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922