A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
object-base.cc
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"
23 #include "attribute-construction-list.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
37 GetObjectIid (void)
38 {
40  TypeId tid = TypeId ("ns3::ObjectBase");
41  tid.SetParent (tid);
42  return tid;
43 }
44 
45 TypeId
46 ObjectBase::GetTypeId (void)
47 {
49  static TypeId tid = GetObjectIid ();
50  return tid;
51 }
52 
53 ObjectBase::~ObjectBase ()
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  if (!(info.flags & TypeId::ATTR_CONSTRUCT))
79  {
80  continue;
81  }
82  bool found = false;
83  // is this attribute stored in this AttributeConstructionList instance ?
84  Ptr<AttributeValue> value = attributes.Find(info.checker);
85  if (value != 0)
86  {
87  // We have a matching attribute value.
88  if (DoSet (info.accessor, info.checker, *value))
89  {
90  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
91  info.name<<"\"");
92  found = true;
93  continue;
94  }
95  }
96  if (!found)
97  {
98  // No matching attribute value so we try to look at the env var.
99 #ifdef HAVE_GETENV
100  char *envVar = getenv ("NS_ATTRIBUTE_DEFAULT");
101  if (envVar != 0)
102  {
103  std::string env = std::string (envVar);
104  std::string::size_type cur = 0;
105  std::string::size_type next = 0;
106  while (next != std::string::npos)
107  {
108  next = env.find (";", cur);
109  std::string tmp = std::string (env, cur, next-cur);
110  std::string::size_type equal = tmp.find ("=");
111  if (equal != std::string::npos)
112  {
113  std::string name = tmp.substr (0, equal);
114  std::string value = tmp.substr (equal+1, tmp.size () - equal - 1);
115  if (name == tid.GetAttributeFullName (i))
116  {
117  if (DoSet (info.accessor, info.checker, StringValue (value)))
118  {
119  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
120  info.name <<"\" from env var");
121  found = true;
122  break;
123  }
124  }
125  }
126  cur = next + 1;
127  }
128  }
129 #endif /* HAVE_GETENV */
130  }
131  if (!found)
132  {
133  // No matching attribute value so we try to set the default value.
134  DoSet (info.accessor, info.checker, *info.initialValue);
135  NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<<
136  info.name <<"\" from initial value.");
137  }
138  }
139  tid = tid.GetParent ();
140  } while (tid != ObjectBase::GetTypeId ());
142 }
143 
144 bool
145 ObjectBase::DoSet (Ptr<const AttributeAccessor> accessor,
147  const AttributeValue &value)
148 {
149  NS_LOG_FUNCTION (this << accessor << checker << &value);
150  Ptr<AttributeValue> v = checker->CreateValidValue (value);
151  if (v == 0)
152  {
153  return false;
154  }
155  bool ok = accessor->Set (this, *v);
156  return ok;
157 }
158 
159 void
160 ObjectBase::SetAttribute (std::string name, const AttributeValue &value)
161 {
162  NS_LOG_FUNCTION (this << name << &value);
163  struct TypeId::AttributeInformation info;
164  TypeId tid = GetInstanceTypeId ();
165  if (!tid.LookupAttributeByName (name, &info))
166  {
167  NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
168  }
169  if (!(info.flags & TypeId::ATTR_SET) ||
170  !info.accessor->HasSetter ())
171  {
172  NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<tid.GetName ());
173  }
174  if (!DoSet (info.accessor, info.checker, value))
175  {
176  NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<tid.GetName ());
177  }
178 }
179 bool
180 ObjectBase::SetAttributeFailSafe (std::string name, const AttributeValue &value)
181 {
182  NS_LOG_FUNCTION (this << name << &value);
183  struct TypeId::AttributeInformation info;
184  TypeId tid = GetInstanceTypeId ();
185  if (!tid.LookupAttributeByName (name, &info))
186  {
187  return false;
188  }
189  if (!(info.flags & TypeId::ATTR_SET) ||
190  !info.accessor->HasSetter ())
191  {
192  return false;
193  }
194  return DoSet (info.accessor, info.checker, value);
195 }
196 
197 void
198 ObjectBase::GetAttribute (std::string name, AttributeValue &value) const
199 {
200  NS_LOG_FUNCTION (this << name << &value);
201  struct TypeId::AttributeInformation info;
202  TypeId tid = GetInstanceTypeId ();
203  if (!tid.LookupAttributeByName (name, &info))
204  {
205  NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ());
206  }
207  if (!(info.flags & TypeId::ATTR_GET) ||
208  !info.accessor->HasGetter ())
209  {
210  NS_FATAL_ERROR ("Attribute name="<<name<<" is not gettable for this object: tid="<<tid.GetName ());
211  }
212  bool ok = info.accessor->Get (this, value);
213  if (ok)
214  {
215  return;
216  }
217  StringValue *str = dynamic_cast<StringValue *> (&value);
218  if (str == 0)
219  {
220  NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": input value is not a string");
221  }
222  Ptr<AttributeValue> v = info.checker->Create ();
223  ok = info.accessor->Get (this, *PeekPointer (v));
224  if (!ok)
225  {
226  NS_FATAL_ERROR ("Attribute name="<<name<<" tid="<<tid.GetName () << ": could not get value");
227  }
228  str->Set (v->SerializeToString (info.checker));
229 }
230 
231 
232 bool
233 ObjectBase::GetAttributeFailSafe (std::string name, AttributeValue &value) const
234 {
235  NS_LOG_FUNCTION (this << name << &value);
236  struct TypeId::AttributeInformation info;
237  TypeId tid = GetInstanceTypeId ();
238  if (!tid.LookupAttributeByName (name, &info))
239  {
240  return false;
241  }
242  if (!(info.flags & TypeId::ATTR_GET) ||
243  !info.accessor->HasGetter ())
244  {
245  return false;
246  }
247  bool ok = info.accessor->Get (this, value);
248  if (ok)
249  {
250  return true;
251  }
252  StringValue *str = dynamic_cast<StringValue *> (&value);
253  if (str == 0)
254  {
255  return false;
256  }
257  Ptr<AttributeValue> v = info.checker->Create ();
258  ok = info.accessor->Get (this, *PeekPointer (v));
259  if (!ok)
260  {
261  return false;
262  }
263  str->Set (v->SerializeToString (info.checker));
264  return true;
265 }
266 
267 bool
269 {
270  NS_LOG_FUNCTION (this << name << &cb);
271  TypeId tid = GetInstanceTypeId ();
273  if (accessor == 0)
274  {
275  return false;
276  }
277  bool ok = accessor->ConnectWithoutContext (this, cb);
278  return ok;
279 }
280 bool
281 ObjectBase::TraceConnect (std::string name, std::string context, const CallbackBase &cb)
282 {
283  NS_LOG_FUNCTION (this << name << context << &cb);
284  TypeId tid = GetInstanceTypeId ();
286  if (accessor == 0)
287  {
288  return false;
289  }
290  bool ok = accessor->Connect (this, context, cb);
291  return ok;
292 }
293 bool
295 {
296  NS_LOG_FUNCTION (this << name << &cb);
297  TypeId tid = GetInstanceTypeId ();
299  if (accessor == 0)
300  {
301  return false;
302  }
303  bool ok = accessor->DisconnectWithoutContext (this, cb);
304  return ok;
305 }
306 bool
307 ObjectBase::TraceDisconnect (std::string name, std::string context, const CallbackBase &cb)
308 {
309  NS_LOG_FUNCTION (this << name << context << &cb);
310  TypeId tid = GetInstanceTypeId ();
312  if (accessor == 0)
313  {
314  return false;
315  }
316  bool ok = accessor->Disconnect (this, context, cb);
317  return ok;
318 }
319 
320 
321 
322 } // namespace ns3
uint32_t GetAttributeN(void) const
Definition: type-id.cc:592
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
hold variables of type string
Definition: string.h:19
Hold a value for an Attribute.
Definition: attribute.h:51
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
TypeId GetParent(void) const
Definition: type-id.cc:485
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Definition: object-base.cc:180
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Definition: type-id.cc:645
bool TraceDisconnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:307
virtual void NotifyConstructionCompleted(void)
Definition: object-base.cc:59
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:294
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Definition: object-base.cc:268
void ConstructSelf(const AttributeConstructionList &attributes)
Definition: object-base.cc:65
std::string GetName(void) const
Definition: type-id.cc:518
virtual Ptr< AttributeValue > Create(void) const =0
void GetAttribute(std::string name, AttributeValue &value) const
Definition: object-base.cc:198
virtual TypeId GetInstanceTypeId(void) const =0
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Definition: object-base.cc:281
std::string GetAttributeFullName(uint32_t i) const
Definition: type-id.cc:605
bool GetAttributeFailSafe(std::string name, AttributeValue &attribute) const
Definition: object-base.cc:233
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
Definition: type-id.cc:599
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
a unique identifier for an interface.
Definition: type-id.h:44