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 
37 static TypeId
39 {
41  TypeId tid = TypeId ("ns3::ObjectBase");
42  tid.SetParent (tid);
43  return tid;
44 }
45 
46 TypeId
48 {
50  static TypeId tid = GetObjectIid ();
51  return tid;
52 }
53 
55 {
56  NS_LOG_FUNCTION (this);
57 }
58 
59 void
61 {
62  NS_LOG_FUNCTION (this);
63 }
64 
65 void
67 {
68  // loop over the inheritance tree back to the Object base class.
69  NS_LOG_FUNCTION (this << &attributes);
70  TypeId tid = GetInstanceTypeId ();
71  do {
72  // loop over all attributes in object type
73  NS_LOG_DEBUG ("construct tid="<<tid.GetName ()<<", params="<<tid.GetAttributeN ());
74  for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
75  {
76  struct TypeId::AttributeInformation info = tid.GetAttribute(i);
77  NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<<
78  info.name <<"\"");
79  if (!(info.flags & TypeId::ATTR_CONSTRUCT))
80  {
81  continue;
82  }
83  bool found = false;
84  // is this attribute stored in this AttributeConstructionList instance ?
85  Ptr<AttributeValue> value = attributes.Find(info.checker);
86  if (value != 0)
87  {
88  // We have a matching attribute value.
89  if (DoSet (info.accessor, info.checker, *value))
90  {
91  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
92  info.name<<"\"");
93  found = true;
94  continue;
95  }
96  }
97  if (!found)
98  {
99  // No matching attribute value so we try to look at the env var.
100 #ifdef HAVE_GETENV
101  char *envVar = getenv ("NS_ATTRIBUTE_DEFAULT");
102  if (envVar != 0)
103  {
104  std::string env = std::string (envVar);
105  std::string::size_type cur = 0;
106  std::string::size_type next = 0;
107  while (next != std::string::npos)
108  {
109  next = env.find (";", cur);
110  std::string tmp = std::string (env, cur, next-cur);
111  std::string::size_type equal = tmp.find ("=");
112  if (equal != std::string::npos)
113  {
114  std::string name = tmp.substr (0, equal);
115  std::string value = tmp.substr (equal+1, tmp.size () - equal - 1);
116  if (name == tid.GetAttributeFullName (i))
117  {
118  if (DoSet (info.accessor, info.checker, StringValue (value)))
119  {
120  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
121  info.name <<"\" from env var");
122  found = true;
123  break;
124  }
125  }
126  }
127  cur = next + 1;
128  }
129  }
130 #endif /* HAVE_GETENV */
131  }
132  if (!found)
133  {
134  // No matching attribute value so we try to set the default value.
135  DoSet (info.accessor, info.checker, *info.initialValue);
136  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
137  info.name <<"\" from initial value.");
138  }
139  }
140  tid = tid.GetParent ();
141  } while (tid != ObjectBase::GetTypeId ());
143 }
144 
145 bool
148  const AttributeValue &value)
149 {
150  NS_LOG_FUNCTION (this << accessor << checker << &value);
151  Ptr<AttributeValue> v = checker->CreateValidValue (value);
152  if (v == 0)
153  {
154  return false;
155  }
156  bool ok = accessor->Set (this, *v);
157  return ok;
158 }
159 
160 void
161 ObjectBase::SetAttribute (std::string name, const AttributeValue &value)
162 {
163  NS_LOG_FUNCTION (this << name << &value);
164  struct TypeId::AttributeInformation info;
165  TypeId tid = GetInstanceTypeId ();
166  if (!tid.LookupAttributeByName (name, &info))
167  {
168  NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
169  }
170  if (!(info.flags & TypeId::ATTR_SET) ||
171  !info.accessor->HasSetter ())
172  {
173  NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<tid.GetName ());
174  }
175  if (!DoSet (info.accessor, info.checker, value))
176  {
177  NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<tid.GetName ());
178  }
179 }
180 bool
181 ObjectBase::SetAttributeFailSafe (std::string name, const AttributeValue &value)
182 {
183  NS_LOG_FUNCTION (this << name << &value);
184  struct TypeId::AttributeInformation info;
185  TypeId tid = GetInstanceTypeId ();
186  if (!tid.LookupAttributeByName (name, &info))
187  {
188  return false;
189  }
190  if (!(info.flags & TypeId::ATTR_SET) ||
191  !info.accessor->HasSetter ())
192  {
193  return false;
194  }
195  return DoSet (info.accessor, info.checker, value);
196 }
197 
198 void
199 ObjectBase::GetAttribute (std::string name, AttributeValue &value) const
200 {
201  NS_LOG_FUNCTION (this << name << &value);
202  struct TypeId::AttributeInformation info;
203  TypeId tid = GetInstanceTypeId ();
204  if (!tid.LookupAttributeByName (name, &info))
205  {
206  NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
207  }
208  if (!(info.flags & TypeId::ATTR_GET) ||
209  !info.accessor->HasGetter ())
210  {
211  NS_FATAL_ERROR ("Attribute name="<<name<<" is not gettable for this object: tid="<<tid.GetName ());
212  }
213  bool ok = info.accessor->Get (this, value);
214  if (ok)
215  {
216  return;
217  }
218  StringValue *str = dynamic_cast<StringValue *> (&value);
219  if (str == 0)
220  {
221  NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": input value is not a string");
222  }
223  Ptr<AttributeValue> v = info.checker->Create ();
224  ok = info.accessor->Get (this, *PeekPointer (v));
225  if (!ok)
226  {
227  NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": could not get value");
228  }
229  str->Set (v->SerializeToString (info.checker));
230 }
231 
232 
233 bool
234 ObjectBase::GetAttributeFailSafe (std::string name, AttributeValue &value) const
235 {
236  NS_LOG_FUNCTION (this << name << &value);
237  struct TypeId::AttributeInformation info;
238  TypeId tid = GetInstanceTypeId ();
239  if (!tid.LookupAttributeByName (name, &info))
240  {
241  return false;
242  }
243  if (!(info.flags & TypeId::ATTR_GET) ||
244  !info.accessor->HasGetter ())
245  {
246  return false;
247  }
248  bool ok = info.accessor->Get (this, value);
249  if (ok)
250  {
251  return true;
252  }
253  StringValue *str = dynamic_cast<StringValue *> (&value);
254  if (str == 0)
255  {
256  return false;
257  }
258  Ptr<AttributeValue> v = info.checker->Create ();
259  ok = info.accessor->Get (this, *PeekPointer (v));
260  if (!ok)
261  {
262  return false;
263  }
264  str->Set (v->SerializeToString (info.checker));
265  return true;
266 }
267 
268 bool
270 {
271  NS_LOG_FUNCTION (this << name << &cb);
272  TypeId tid = GetInstanceTypeId ();
274  if (accessor == 0)
275  {
276  return false;
277  }
278  bool ok = accessor->ConnectWithoutContext (this, cb);
279  return ok;
280 }
281 bool
282 ObjectBase::TraceConnect (std::string name, std::string context, const CallbackBase &cb)
283 {
284  NS_LOG_FUNCTION (this << name << context << &cb);
285  TypeId tid = GetInstanceTypeId ();
287  if (accessor == 0)
288  {
289  return false;
290  }
291  bool ok = accessor->Connect (this, context, cb);
292  return ok;
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->DisconnectWithoutContext (this, cb);
305  return ok;
306 }
307 bool
308 ObjectBase::TraceDisconnect (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->Disconnect (this, context, cb);
318  return ok;
319 }
320 
321 
322 
323 } // namespace ns3
uint32_t GetAttributeN(void) const
Definition: type-id.cc:739
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
virtual ~ObjectBase()
Virtual destructor.
Definition: object-base.cc:54
hold variables of type string
Definition: string.h:19
void Set(const std::string &value)
Hold a value for an Attribute.
Definition: attribute.h:56
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
TypeId GetParent(void) const
Definition: type-id.cc:625
Base class for Callback class.
Definition: callback.h:844
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Definition: object-base.cc:181
static TypeId GetTypeId(void)
Get the type ID.
Definition: object-base.cc:47
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log.h:309
The attribute can be written at construction-time.
Definition: type-id.h:58
NS_LOG_COMPONENT_DEFINE("ObjectBase")
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
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:792
bool TraceDisconnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:308
Ptr< const AttributeValue > initialValue
Definition: type-id.h:66
T * PeekPointer(const Ptr< T > &p)
Definition: ptr.h:279
virtual void NotifyConstructionCompleted(void)
This method is invoked once all member attributes have been initialized.
Definition: object-base.cc:60
static TypeId GetObjectIid(void)
Definition: object-base.cc:38
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:295
Ptr< const AttributeChecker > checker
Definition: type-id.h:68
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:269
The attribute can be written.
Definition: type-id.h:57
void ConstructSelf(const AttributeConstructionList &attributes)
Definition: object-base.cc:66
std::string GetName(void) const
Definition: type-id.cc:658
void GetAttribute(std::string name, AttributeValue &value) const
Definition: object-base.cc:199
bool DoSet(Ptr< const AttributeAccessor > spec, Ptr< const AttributeChecker > checker, const AttributeValue &value)
Definition: object-base.cc:146
virtual TypeId GetInstanceTypeId(void) const =0
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:282
std::string GetAttributeFullName(uint32_t i) const
Definition: type-id.cc:752
bool GetAttributeFailSafe(std::string name, AttributeValue &attribute) const
Definition: object-base.cc:234
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
Definition: type-id.cc:746
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611