A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
type-id.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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "type-id.h"
21 #include "singleton.h"
22 #include "trace-source-accessor.h"
23 #include "log.h"
24 #include <vector>
25 #include <sstream>
26 
27 /*********************************************************************
28  * Helper code
29  *********************************************************************/
30 
31 NS_LOG_COMPONENT_DEFINE ("TypeID");
32 
33 namespace {
34 
36 {
37 public:
38  IidManager ();
39  uint16_t AllocateUid (std::string name);
40  void SetParent (uint16_t uid, uint16_t parent);
41  void SetGroupName (uint16_t uid, std::string groupName);
42  void AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback);
43  void HideFromDocumentation (uint16_t uid);
44  uint16_t GetUid (std::string name) const;
45  std::string GetName (uint16_t uid) const;
46  uint16_t GetParent (uint16_t uid) const;
47  std::string GetGroupName (uint16_t uid) const;
48  ns3::Callback<ns3::ObjectBase *> GetConstructor (uint16_t uid) const;
49  bool HasConstructor (uint16_t uid) const;
50  uint32_t GetRegisteredN (void) const;
51  uint16_t GetRegistered (uint32_t i) const;
52  void AddAttribute (uint16_t uid,
53  std::string name,
54  std::string help,
55  uint32_t flags,
59  void SetAttributeInitialValue(uint16_t uid,
60  uint32_t i,
62  uint32_t GetAttributeN (uint16_t uid) const;
63  struct ns3::TypeId::AttributeInformation GetAttribute(uint16_t uid, uint32_t i) const;
64  void AddTraceSource (uint16_t uid,
65  std::string name,
66  std::string help,
68  uint32_t GetTraceSourceN (uint16_t uid) const;
69  struct ns3::TypeId::TraceSourceInformation GetTraceSource(uint16_t uid, uint32_t i) const;
70  bool MustHideFromDocumentation (uint16_t uid) const;
71 
72 private:
73  bool HasTraceSource (uint16_t uid, std::string name);
74  bool HasAttribute (uint16_t uid, std::string name);
75 
76  struct IidInformation {
77  std::string name;
78  uint16_t parent;
79  std::string groupName;
80  bool hasConstructor;
82  bool mustHideFromDocumentation;
83  std::vector<struct ns3::TypeId::AttributeInformation> attributes;
84  std::vector<struct ns3::TypeId::TraceSourceInformation> traceSources;
85  };
86  typedef std::vector<struct IidInformation>::const_iterator Iterator;
87 
88  struct IidManager::IidInformation *LookupInformation (uint16_t uid) const;
89 
90  std::vector<struct IidInformation> m_information;
91 };
92 
93 IidManager::IidManager ()
94 {
95  NS_LOG_FUNCTION (this);
96 }
97 
98 uint16_t
99 IidManager::AllocateUid (std::string name)
100 {
101  NS_LOG_FUNCTION (this << name);
102  uint16_t j = 1;
103  for (Iterator i = m_information.begin (); i != m_information.end (); i++)
104  {
105  if (i->name == name)
106  {
107  NS_FATAL_ERROR ("Trying to allocate twice the same uid: " << name);
108  return 0;
109  }
110  j++;
111  }
112  struct IidInformation information;
113  information.name = name;
114  information.parent = 0;
115  information.groupName = "";
116  information.hasConstructor = false;
117  information.mustHideFromDocumentation = false;
118  m_information.push_back (information);
119  uint32_t uid = m_information.size ();
120  NS_ASSERT (uid <= 0xffff);
121  return uid;
122 }
123 
124 struct IidManager::IidInformation *
125 IidManager::LookupInformation (uint16_t uid) const
126 {
127  NS_LOG_FUNCTION (this << uid);
128  NS_ASSERT (uid <= m_information.size () && uid != 0);
129  return const_cast<struct IidInformation *> (&m_information[uid-1]);
130 }
131 
132 void
133 IidManager::SetParent (uint16_t uid, uint16_t parent)
134 {
135  NS_LOG_FUNCTION (this << uid << parent);
136  NS_ASSERT (parent <= m_information.size ());
137  struct IidInformation *information = LookupInformation (uid);
138  information->parent = parent;
139 }
140 void
141 IidManager::SetGroupName (uint16_t uid, std::string groupName)
142 {
143  NS_LOG_FUNCTION (this << uid << groupName);
144  struct IidInformation *information = LookupInformation (uid);
145  information->groupName = groupName;
146 }
147 void
148 IidManager::HideFromDocumentation (uint16_t uid)
149 {
150  NS_LOG_FUNCTION (this << uid);
151  struct IidInformation *information = LookupInformation (uid);
152  information->mustHideFromDocumentation = true;
153 }
154 
155 void
156 IidManager::AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback)
157 {
158  NS_LOG_FUNCTION (this << uid << &callback);
159  struct IidInformation *information = LookupInformation (uid);
160  if (information->hasConstructor)
161  {
162  NS_FATAL_ERROR (information->name<<" already has a constructor.");
163  }
164  information->hasConstructor = true;
165  information->constructor = callback;
166 }
167 
168 uint16_t
169 IidManager::GetUid (std::string name) const
170 {
171  NS_LOG_FUNCTION (this << name);
172  uint32_t j = 1;
173  for (Iterator i = m_information.begin (); i != m_information.end (); i++)
174  {
175  if (i->name == name)
176  {
177  NS_ASSERT (j <= 0xffff);
178  return j;
179  }
180  j++;
181  }
182  return 0;
183 }
184 std::string
185 IidManager::GetName (uint16_t uid) const
186 {
187  NS_LOG_FUNCTION (this << uid);
188  struct IidInformation *information = LookupInformation (uid);
189  return information->name;
190 }
191 uint16_t
192 IidManager::GetParent (uint16_t uid) const
193 {
194  NS_LOG_FUNCTION (this << uid);
195  struct IidInformation *information = LookupInformation (uid);
196  return information->parent;
197 }
198 std::string
199 IidManager::GetGroupName (uint16_t uid) const
200 {
201  NS_LOG_FUNCTION (this << uid);
202  struct IidInformation *information = LookupInformation (uid);
203  return information->groupName;
204 }
205 
207 IidManager::GetConstructor (uint16_t uid) const
208 {
209  NS_LOG_FUNCTION (this << uid);
210  struct IidInformation *information = LookupInformation (uid);
211  if (!information->hasConstructor)
212  {
213  NS_FATAL_ERROR ("Requested constructor for "<<information->name<<" but it does not have one.");
214  }
215  return information->constructor;
216 }
217 
218 bool
219 IidManager::HasConstructor (uint16_t uid) const
220 {
221  NS_LOG_FUNCTION (this << uid);
222  struct IidInformation *information = LookupInformation (uid);
223  return information->hasConstructor;
224 }
225 
226 uint32_t
227 IidManager::GetRegisteredN (void) const
228 {
229  NS_LOG_FUNCTION (this);
230  return m_information.size ();
231 }
232 uint16_t
233 IidManager::GetRegistered (uint32_t i) const
234 {
235  NS_LOG_FUNCTION (this << i);
236  return i + 1;
237 }
238 
239 bool
240 IidManager::HasAttribute (uint16_t uid,
241  std::string name)
242 {
243  NS_LOG_FUNCTION (this << uid << name);
244  struct IidInformation *information = LookupInformation (uid);
245  while (true)
246  {
247  for (std::vector<struct ns3::TypeId::AttributeInformation>::const_iterator i = information->attributes.begin ();
248  i != information->attributes.end (); ++i)
249  {
250  if (i->name == name)
251  {
252  return true;
253  }
254  }
255  struct IidInformation *parent = LookupInformation (information->parent);
256  if (parent == information)
257  {
258  // top of inheritance tree
259  return false;
260  }
261  // check parent
262  information = parent;
263  }
264  return false;
265 }
266 
267 void
268 IidManager::AddAttribute (uint16_t uid,
269  std::string name,
270  std::string help,
271  uint32_t flags,
275 {
276  NS_LOG_FUNCTION (this << uid << name << help << flags << initialValue << accessor << checker);
277  struct IidInformation *information = LookupInformation (uid);
278  if (HasAttribute (uid, name))
279  {
280  NS_FATAL_ERROR ("Attribute \"" << name << "\" already registered on tid=\"" <<
281  information->name << "\"");
282  }
284  info.name = name;
285  info.help = help;
286  info.flags = flags;
287  info.initialValue = initialValue;
288  info.originalInitialValue = initialValue;
289  info.accessor = accessor;
290  info.checker = checker;
291  information->attributes.push_back (info);
292 }
293 void
294 IidManager::SetAttributeInitialValue(uint16_t uid,
295  uint32_t i,
297 {
298  NS_LOG_FUNCTION (this << uid << i << initialValue);
299  struct IidInformation *information = LookupInformation (uid);
300  NS_ASSERT (i < information->attributes.size ());
301  information->attributes[i].initialValue = initialValue;
302 }
303 
304 
305 
306 uint32_t
307 IidManager::GetAttributeN (uint16_t uid) const
308 {
309  NS_LOG_FUNCTION (this << uid);
310  struct IidInformation *information = LookupInformation (uid);
311  return information->attributes.size ();
312 }
314 IidManager::GetAttribute(uint16_t uid, uint32_t i) const
315 {
316  NS_LOG_FUNCTION (this << uid << i);
317  struct IidInformation *information = LookupInformation (uid);
318  NS_ASSERT (i < information->attributes.size ());
319  return information->attributes[i];
320 }
321 
322 bool
323 IidManager::HasTraceSource (uint16_t uid,
324  std::string name)
325 {
326  NS_LOG_FUNCTION (this << uid << name);
327  struct IidInformation *information = LookupInformation (uid);
328  while (true)
329  {
330  for (std::vector<struct ns3::TypeId::TraceSourceInformation>::const_iterator i = information->traceSources.begin ();
331  i != information->traceSources.end (); ++i)
332  {
333  if (i->name == name)
334  {
335  return true;
336  }
337  }
338  struct IidInformation *parent = LookupInformation (information->parent);
339  if (parent == information)
340  {
341  // top of inheritance tree
342  return false;
343  }
344  // check parent
345  information = parent;
346  }
347  return false;
348 }
349 
350 void
351 IidManager::AddTraceSource (uint16_t uid,
352  std::string name,
353  std::string help,
355 {
356  NS_LOG_FUNCTION (this << uid << name << help << accessor);
357  struct IidInformation *information = LookupInformation (uid);
358  if (HasTraceSource (uid, name))
359  {
360  NS_FATAL_ERROR ("Trace source \"" << name << "\" already registered on tid=\"" <<
361  information->name << "\"");
362  }
364  source.name = name;
365  source.help = help;
366  source.accessor = accessor;
367  information->traceSources.push_back (source);
368 }
369 uint32_t
370 IidManager::GetTraceSourceN (uint16_t uid) const
371 {
372  NS_LOG_FUNCTION (this << uid);
373  struct IidInformation *information = LookupInformation (uid);
374  return information->traceSources.size ();
375 }
377 IidManager::GetTraceSource(uint16_t uid, uint32_t i) const
378 {
379  NS_LOG_FUNCTION (this << uid << i);
380  struct IidInformation *information = LookupInformation (uid);
381  NS_ASSERT (i < information->traceSources.size ());
382  return information->traceSources[i];
383 }
384 bool
385 IidManager::MustHideFromDocumentation (uint16_t uid) const
386 {
387  NS_LOG_FUNCTION (this << uid);
388  struct IidInformation *information = LookupInformation (uid);
389  return information->mustHideFromDocumentation;
390 }
391 
392 } // anonymous namespace
393 
394 namespace ns3 {
395 
396 /*********************************************************************
397  * The TypeId class
398  *********************************************************************/
399 
400 TypeId::TypeId (const char *name)
401 {
402  NS_LOG_FUNCTION (this << name);
403  uint16_t uid = Singleton<IidManager>::Get ()->AllocateUid (name);
404  NS_ASSERT (uid != 0);
405  m_tid = uid;
406 }
407 
408 
409 TypeId::TypeId (uint16_t tid)
410  : m_tid (tid)
411 {
412  NS_LOG_FUNCTION (this << tid);
413 }
414 TypeId
415 TypeId::LookupByName (std::string name)
416 {
417  NS_LOG_FUNCTION (name);
418  uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
419  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
420  return TypeId (uid);
421 }
422 bool
423 TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
424 {
425  NS_LOG_FUNCTION (name << tid);
426  uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
427  if (uid == 0)
428  {
429  return false;
430  }
431  *tid = TypeId (uid);
432  return true;
433 }
434 
435 uint32_t
437 {
439  return Singleton<IidManager>::Get ()->GetRegisteredN ();
440 }
441 TypeId
443 {
444  NS_LOG_FUNCTION (i);
446 }
447 
448 bool
449 TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInformation *info) const
450 {
451  NS_LOG_FUNCTION (this << name << info);
452  TypeId tid;
453  TypeId nextTid = *this;
454  do {
455  tid = nextTid;
456  for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
457  {
458  struct TypeId::AttributeInformation tmp = tid.GetAttribute(i);
459  if (tmp.name == name)
460  {
461  *info = tmp;
462  return true;
463  }
464  }
465  nextTid = tid.GetParent ();
466  } while (nextTid != tid);
467  return false;
468 }
469 
470 TypeId
472 {
473  NS_LOG_FUNCTION (this << tid);
474  Singleton<IidManager>::Get ()->SetParent (m_tid, tid.m_tid);
475  return *this;
476 }
477 TypeId
478 TypeId::SetGroupName (std::string groupName)
479 {
480  NS_LOG_FUNCTION (this << groupName);
481  Singleton<IidManager>::Get ()->SetGroupName (m_tid, groupName);
482  return *this;
483 }
484 TypeId
485 TypeId::GetParent (void) const
486 {
487  NS_LOG_FUNCTION (this);
488  uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_tid);
489  return TypeId (parent);
490 }
491 bool
492 TypeId::HasParent (void) const
493 {
494  NS_LOG_FUNCTION (this);
495  uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_tid);
496  return parent != m_tid;
497 }
498 bool
500 {
501  NS_LOG_FUNCTION (this << other);
502  TypeId tmp = *this;
503  while (tmp != other && tmp != tmp.GetParent ())
504  {
505  tmp = tmp.GetParent ();
506  }
507  return tmp == other && *this != other;
508 }
509 std::string
511 {
512  NS_LOG_FUNCTION (this);
513  std::string groupName = Singleton<IidManager>::Get ()->GetGroupName (m_tid);
514  return groupName;
515 }
516 
517 std::string
518 TypeId::GetName (void) const
519 {
520  NS_LOG_FUNCTION (this);
521  std::string name = Singleton<IidManager>::Get ()->GetName (m_tid);
522  return name;
523 }
524 
525 bool
527 {
528  NS_LOG_FUNCTION (this);
529  bool hasConstructor = Singleton<IidManager>::Get ()->HasConstructor (m_tid);
530  return hasConstructor;
531 }
532 
533 void
534 TypeId::DoAddConstructor (Callback<ObjectBase *> cb)
535 {
536  NS_LOG_FUNCTION (this << &cb);
537  Singleton<IidManager>::Get ()->AddConstructor (m_tid, cb);
538 }
539 
540 TypeId
541 TypeId::AddAttribute (std::string name,
542  std::string help,
543  const AttributeValue &initialValue,
546 {
547  NS_LOG_FUNCTION (this << name << help << &initialValue << accessor << checker);
548  Singleton<IidManager>::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC, initialValue.Copy (), accessor, checker);
549  return *this;
550 }
551 
552 TypeId
553 TypeId::AddAttribute (std::string name,
554  std::string help,
555  uint32_t flags,
556  const AttributeValue &initialValue,
559 {
560  NS_LOG_FUNCTION (this << name << help << flags << &initialValue << accessor << checker);
561  Singleton<IidManager>::Get ()->AddAttribute (m_tid, name, help, flags, initialValue.Copy (), accessor, checker);
562  return *this;
563 }
564 
565 bool
567  Ptr<const AttributeValue> initialValue)
568 {
569  NS_LOG_FUNCTION (this << i << initialValue);
570  Singleton<IidManager>::Get ()->SetAttributeInitialValue (m_tid, i, initialValue);
571  return true;
572 }
573 
574 
577 {
578  NS_LOG_FUNCTION (this);
579  Callback<ObjectBase *> cb = Singleton<IidManager>::Get ()->GetConstructor (m_tid);
580  return cb;
581 }
582 
583 bool
585 {
586  NS_LOG_FUNCTION (this);
587  bool mustHide = Singleton<IidManager>::Get ()->MustHideFromDocumentation (m_tid);
588  return mustHide;
589 }
590 
591 uint32_t
593 {
594  NS_LOG_FUNCTION (this);
595  uint32_t n = Singleton<IidManager>::Get ()->GetAttributeN (m_tid);
596  return n;
597 }
599 TypeId::GetAttribute(uint32_t i) const
600 {
601  NS_LOG_FUNCTION (this << i);
602  return Singleton<IidManager>::Get ()->GetAttribute(m_tid, i);
603 }
604 std::string
606 {
607  NS_LOG_FUNCTION (this << i);
609  return GetName () + "::" + info.name;
610 }
611 
612 uint32_t
614 {
615  NS_LOG_FUNCTION (this);
616  return Singleton<IidManager>::Get ()->GetTraceSourceN (m_tid);
617 }
619 TypeId::GetTraceSource(uint32_t i) const
620 {
621  NS_LOG_FUNCTION (this << i);
622  return Singleton<IidManager>::Get ()->GetTraceSource(m_tid, i);
623 }
624 
625 TypeId
626 TypeId::AddTraceSource (std::string name,
627  std::string help,
629 {
630  NS_LOG_FUNCTION (this << name << help << accessor);
631  Singleton<IidManager>::Get ()->AddTraceSource (m_tid, name, help, accessor);
632  return *this;
633 }
634 
635 TypeId
636 TypeId::HideFromDocumentation (void)
637 {
638  NS_LOG_FUNCTION (this);
639  Singleton<IidManager>::Get ()->HideFromDocumentation (m_tid);
640  return *this;
641 }
642 
643 
644 Ptr<const TraceSourceAccessor>
645 TypeId::LookupTraceSourceByName (std::string name) const
646 {
647  NS_LOG_FUNCTION (this << name);
648  TypeId tid;
649  TypeId nextTid = *this;
650  do {
651  tid = nextTid;
652  for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++)
653  {
654  struct TypeId::TraceSourceInformation info = tid.GetTraceSource (i);
655  if (info.name == name)
656  {
657  return info.accessor;
658  }
659  }
660  nextTid = tid.GetParent ();
661  } while (nextTid != tid);
662  return 0;
663 }
664 
665 uint16_t
666 TypeId::GetUid (void) const
667 {
668  NS_LOG_FUNCTION (this);
669  return m_tid;
670 }
671 void
672 TypeId::SetUid (uint16_t tid)
673 {
674  NS_LOG_FUNCTION (this << tid);
675  m_tid = tid;
676 }
677 
678 std::ostream & operator << (std::ostream &os, TypeId tid)
679 {
680  os << tid.GetName ();
681  return os;
682 }
683 std::istream & operator >> (std::istream &is, TypeId &tid)
684 {
685  std::string tidString;
686  is >> tidString;
687  bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
688  if (!ok)
689  {
690  is.setstate (std::ios_base::badbit);
691  }
692  return is;
693 }
694 
695 
696 ATTRIBUTE_HELPER_CPP (TypeId);
697 
698 bool operator < (TypeId a, TypeId b)
699 {
700  return a.m_tid < b.m_tid;
701 }
702 
703 } // namespace ns3
uint32_t GetAttributeN(void) const
Definition: type-id.cc:592
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:49
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:311
TypeId SetParent(void)
Definition: type-id.h:381
Hold a value for an Attribute.
Definition: attribute.h:51
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
TypeId GetParent(void) const
Definition: type-id.cc:485
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Definition: type-id.cc:423
#define NS_LOG_FUNCTION_NOARGS()
Definition: log.h:275
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Callback< ObjectBase * > GetConstructor(void) const
Definition: type-id.cc:576
bool HasConstructor(void) const
Definition: type-id.cc:526
bool MustHideFromDocumentation(void) const
Definition: type-id.cc:584
a template singleton
Definition: singleton.h:37
static uint32_t GetRegisteredN(void)
Definition: type-id.cc:436
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Definition: type-id.cc:645
uint32_t GetTraceSourceN(void) const
Definition: type-id.cc:613
static TypeId GetRegistered(uint32_t i)
Definition: type-id.cc:442
TypeId SetGroupName(std::string groupName)
Definition: type-id.cc:478
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:43
TypeId AddAttribute(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker)
Definition: type-id.cc:541
virtual Ptr< AttributeValue > Copy(void) const =0
void SetUid(uint16_t tid)
Definition: type-id.cc:672
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Definition: type-id.cc:449
#define ATTRIBUTE_HELPER_CPP(type)
std::string GetName(void) const
Definition: type-id.cc:518
uint16_t GetUid(void) const
Definition: type-id.cc:666
#define NS_ASSERT_MSG(condition, message)
Definition: assert.h:86
bool SetAttributeInitialValue(uint32_t i, Ptr< const AttributeValue > initialValue)
Definition: type-id.cc:566
TypeId AddTraceSource(std::string name, std::string help, Ptr< const TraceSourceAccessor > accessor)
Definition: type-id.cc:626
std::string GetAttributeFullName(uint32_t i) const
Definition: type-id.cc:605
bool IsChildOf(TypeId other) const
Definition: type-id.cc:499
struct TypeId::TraceSourceInformation GetTraceSource(uint32_t i) const
Definition: type-id.cc:619
std::string GetGroupName(void) const
Definition: type-id.cc:510
struct TypeId::AttributeInformation GetAttribute(uint32_t i) const
Definition: type-id.cc:599
a unique identifier for an interface.
Definition: type-id.h:44
static TypeId LookupByName(std::string name)
Definition: type-id.cc:415