A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
30 NS_LOG_COMPONENT_DEFINE ("ObjectBase");
31 
32 namespace ns3 {
33 
34 NS_OBJECT_ENSURE_REGISTERED (ObjectBase);
35 
36 static TypeId
38 {
40  TypeId tid = TypeId ("ns3::ObjectBase");
41  tid.SetParent (tid);
42  return tid;
43 }
44 
45 TypeId
47 {
49  static TypeId tid = GetObjectIid ();
50  return tid;
51 }
52 
54 {
55  NS_LOG_FUNCTION (this);
56 }
57 
58 void
60 {
61  NS_LOG_FUNCTION (this);
62 }
63 
64 void
66 {
67  // loop over the inheritance tree back to the Object base class.
68  NS_LOG_FUNCTION (this << &attributes);
69  TypeId tid = GetInstanceTypeId ();
70  do {
71  // loop over all attributes in object type
72  NS_LOG_DEBUG ("construct tid="<<tid.GetName ()<<", params="<<tid.GetAttributeN ());
73  for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
74  {
75  struct TypeId::AttributeInformation info = tid.GetAttribute(i);
76  NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<<
77  info.name <<"\"");
78  // is this attribute stored in this AttributeConstructionList instance ?
79  Ptr<AttributeValue> value = attributes.Find(info.checker);
80  // See if this attribute should not be set here in the
81  // constructor.
82  if (!(info.flags & TypeId::ATTR_CONSTRUCT))
83  {
84  // Handle this attribute if it should not be
85  // set here.
86  if (value == 0)
87  {
88  // Skip this attribute if it's not in the
89  // AttributeConstructionList.
90  continue;
91  }
92  else
93  {
94  // This is an error because this attribute is not
95  // settable in its constructor but is present in
96  // the AttributeConstructionList.
97  NS_FATAL_ERROR ("Attribute name="<<info.name<<" tid="<<tid.GetName () << ": initial value cannot be set using attributes");
98  }
99  }
100  bool found = false;
101  if (value != 0)
102  {
103  // We have a matching attribute value.
104  if (DoSet (info.accessor, info.checker, *value))
105  {
106  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
107  info.name<<"\"");
108  found = true;
109  continue;
110  }
111  }
112  if (!found)
113  {
114  // No matching attribute value so we try to look at the env var.
115 #ifdef HAVE_GETENV
116  char *envVar = getenv ("NS_ATTRIBUTE_DEFAULT");
117  if (envVar != 0)
118  {
119  std::string env = std::string (envVar);
120  std::string::size_type cur = 0;
121  std::string::size_type next = 0;
122  while (next != std::string::npos)
123  {
124  next = env.find (";", cur);
125  std::string tmp = std::string (env, cur, next-cur);
126  std::string::size_type equal = tmp.find ("=");
127  if (equal != std::string::npos)
128  {
129  std::string name = tmp.substr (0, equal);
130  std::string value = tmp.substr (equal+1, tmp.size () - equal - 1);
131  if (name == tid.GetAttributeFullName (i))
132  {
133  if (DoSet (info.accessor, info.checker, StringValue (value)))
134  {
135  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
136  info.name <<"\" from env var");
137  found = true;
138  break;
139  }
140  }
141  }
142  cur = next + 1;
143  }
144  }
145 #endif /* HAVE_GETENV */
146  }
147  if (!found)
148  {
149  // No matching attribute value so we try to set the default value.
150  DoSet (info.accessor, info.checker, *info.initialValue);
151  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
152  info.name <<"\" from initial value.");
153  }
154  }
155  tid = tid.GetParent ();
156  } while (tid != ObjectBase::GetTypeId ());
158 }
159 
160 bool
163  const AttributeValue &value)
164 {
165  NS_LOG_FUNCTION (this << accessor << checker << &value);
166  Ptr<AttributeValue> v = checker->CreateValidValue (value);
167  if (v == 0)
168  {
169  return false;
170  }
171  bool ok = accessor->Set (this, *v);
172  return ok;
173 }
174 
175 void
176 ObjectBase::SetAttribute (std::string name, const AttributeValue &value)
177 {
178  NS_LOG_FUNCTION (this << name << &value);
179  struct TypeId::AttributeInformation info;
180  TypeId tid = GetInstanceTypeId ();
181  if (!tid.LookupAttributeByName (name, &info))
182  {
183  NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
184  }
185  if (!(info.flags & TypeId::ATTR_SET) ||
186  !info.accessor->HasSetter ())
187  {
188  NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<tid.GetName ());
189  }
190  if (!DoSet (info.accessor, info.checker, value))
191  {
192  NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<tid.GetName ());
193  }
194 }
195 bool
196 ObjectBase::SetAttributeFailSafe (std::string name, const AttributeValue &value)
197 {
198  NS_LOG_FUNCTION (this << name << &value);
199  struct TypeId::AttributeInformation info;
200  TypeId tid = GetInstanceTypeId ();
201  if (!tid.LookupAttributeByName (name, &info))
202  {
203  return false;
204  }
205  if (!(info.flags & TypeId::ATTR_SET) ||
206  !info.accessor->HasSetter ())
207  {
208  return false;
209  }
210  return DoSet (info.accessor, info.checker, value);
211 }
212 
213 void
214 ObjectBase::GetAttribute (std::string name, AttributeValue &value) const
215 {
216  NS_LOG_FUNCTION (this << name << &value);
217  struct TypeId::AttributeInformation info;
218  TypeId tid = GetInstanceTypeId ();
219  if (!tid.LookupAttributeByName (name, &info))
220  {
221  NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
222  }
223  if (!(info.flags & TypeId::ATTR_GET) ||
224  !info.accessor->HasGetter ())
225  {
226  NS_FATAL_ERROR ("Attribute name="<<name<<" is not gettable for this object: tid="<<tid.GetName ());
227  }
228  bool ok = info.accessor->Get (this, value);
229  if (ok)
230  {
231  return;
232  }
233  StringValue *str = dynamic_cast<StringValue *> (&value);
234  if (str == 0)
235  {
236  NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": input value is not a string");
237  }
238  Ptr<AttributeValue> v = info.checker->Create ();
239  ok = info.accessor->Get (this, *PeekPointer (v));
240  if (!ok)
241  {
242  NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": could not get value");
243  }
244  str->Set (v->SerializeToString (info.checker));
245 }
246 
247 
248 bool
249 ObjectBase::GetAttributeFailSafe (std::string name, AttributeValue &value) const
250 {
251  NS_LOG_FUNCTION (this << name << &value);
252  struct TypeId::AttributeInformation info;
253  TypeId tid = GetInstanceTypeId ();
254  if (!tid.LookupAttributeByName (name, &info))
255  {
256  return false;
257  }
258  if (!(info.flags & TypeId::ATTR_GET) ||
259  !info.accessor->HasGetter ())
260  {
261  return false;
262  }
263  bool ok = info.accessor->Get (this, value);
264  if (ok)
265  {
266  return true;
267  }
268  StringValue *str = dynamic_cast<StringValue *> (&value);
269  if (str == 0)
270  {
271  return false;
272  }
273  Ptr<AttributeValue> v = info.checker->Create ();
274  ok = info.accessor->Get (this, *PeekPointer (v));
275  if (!ok)
276  {
277  return false;
278  }
279  str->Set (v->SerializeToString (info.checker));
280  return true;
281 }
282 
283 bool
285 {
286  NS_LOG_FUNCTION (this << name << &cb);
287  TypeId tid = GetInstanceTypeId ();
289  if (accessor == 0)
290  {
291  return false;
292  }
293  bool ok = accessor->ConnectWithoutContext (this, cb);
294  return ok;
295 }
296 bool
297 ObjectBase::TraceConnect (std::string name, std::string context, const CallbackBase &cb)
298 {
299  NS_LOG_FUNCTION (this << name << context << &cb);
300  TypeId tid = GetInstanceTypeId ();
302  if (accessor == 0)
303  {
304  return false;
305  }
306  bool ok = accessor->Connect (this, context, cb);
307  return ok;
308 }
309 bool
311 {
312  NS_LOG_FUNCTION (this << name << &cb);
313  TypeId tid = GetInstanceTypeId ();
315  if (accessor == 0)
316  {
317  return false;
318  }
319  bool ok = accessor->DisconnectWithoutContext (this, cb);
320  return ok;
321 }
322 bool
323 ObjectBase::TraceDisconnect (std::string name, std::string context, const CallbackBase &cb)
324 {
325  NS_LOG_FUNCTION (this << name << context << &cb);
326  TypeId tid = GetInstanceTypeId ();
328  if (accessor == 0)
329  {
330  return false;
331  }
332  bool ok = accessor->Disconnect (this, context, cb);
333  return ok;
334 }
335 
336 
337 
338 } // namespace ns3
uint32_t GetAttributeN(void) const
Definition: type-id.cc:738
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
virtual ~ObjectBase()
Virtual destructor.
Definition: object-base.cc:53
hold variables of type string
Definition: string.h:18
void Set(const std::string &value)
Hold a value for an Attribute.
Definition: attribute.h:56
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
TypeId GetParent(void) const
Definition: type-id.cc:624
Base class for Callback class.
Definition: callback.h:848
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Definition: object-base.cc:196
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:95
static TypeId GetTypeId(void)
Get the type ID.
Definition: object-base.cc:46
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
The attribute can be written at construction-time.
Definition: type-id.h:58
The attribute can be read.
Definition: type-id.h:56
Ptr< AttributeValue > Find(Ptr< const AttributeChecker > checker) const
Ptr< const AttributeAccessor > accessor
Definition: type-id.h:67
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Definition: type-id.cc:791
bool TraceDisconnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:323
Ptr< const AttributeValue > initialValue
Definition: type-id.h:66
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:280
virtual void NotifyConstructionCompleted(void)
This method is invoked once all member attributes have been initialized.
Definition: object-base.cc:59
static TypeId GetObjectIid(void)
Definition: object-base.cc:37
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:310
Ptr< const AttributeChecker > checker
Definition: type-id.h:68
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:284
The attribute can be written.
Definition: type-id.h:57
void ConstructSelf(const AttributeConstructionList &attributes)
Definition: object-base.cc:65
std::string GetName(void) const
Definition: type-id.cc:657
void GetAttribute(std::string name, AttributeValue &value) const
Definition: object-base.cc:214
bool DoSet(Ptr< const AttributeAccessor > spec, Ptr< const AttributeChecker > checker, const AttributeValue &value)
Definition: object-base.cc:161
virtual TypeId GetInstanceTypeId(void) const =0
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:213
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:297
std::string GetAttributeFullName(uint32_t i) const
Definition: type-id.cc:751
bool GetAttributeFailSafe(std::string name, AttributeValue &attribute) const
Definition: object-base.cc:249
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
Definition: type-id.cc:745
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610