A Discrete-Event Network Simulator
API
type-id.h
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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef TYPE_ID_H
21 #define TYPE_ID_H
22 
23 #include "attribute.h"
25 #include "trace-source-accessor.h"
26 #include "attribute-helper.h"
27 #include "callback.h"
28 #include "deprecated.h"
29 #include "hash.h"
30 #include <string>
31 #include <stdint.h>
32 
39 namespace ns3 {
40 
41 class ObjectBase;
42 
58 class TypeId
59 {
60 public:
63  {
64  ATTR_GET = 1 << 0,
65  ATTR_SET = 1 << 1,
66  ATTR_CONSTRUCT = 1 << 2,
68  };
71  {
74  OBSOLETE
75  };
78  {
80  std::string name;
82  std::string help;
84  uint32_t flags;
96  std::string supportMsg;
97  };
100  {
102  std::string name;
104  std::string help;
106  std::string callback;
112  std::string supportMsg;
113  };
114 
116  typedef uint32_t hash_t;
117 
127  static TypeId LookupByName (std::string name);
136  static bool LookupByNameFailSafe (std::string name, TypeId *tid);
146  static TypeId LookupByHash (hash_t hash);
155  static bool LookupByHashFailSafe (hash_t hash, TypeId *tid);
156 
162  static uint16_t GetRegisteredN (void);
169  static TypeId GetRegistered (uint16_t i);
170 
179  explicit TypeId (const std::string &name);
180 
192  TypeId GetParent (void) const;
193 
199  bool HasParent (void) const;
200 
211  bool IsChildOf (TypeId other) const;
212 
218  std::string GetGroupName (void) const;
219 
225  std::string GetName (void) const;
226 
232  hash_t GetHash (void) const;
233 
239  std::size_t GetSize (void) const;
240 
246  bool HasConstructor (void) const;
247 
253  std::size_t GetAttributeN (void) const;
260  struct TypeId::AttributeInformation GetAttribute (std::size_t i) const;
267  std::string GetAttributeFullName (std::size_t i) const;
268 
275  Callback<ObjectBase *> GetConstructor (void) const;
276 
282  bool MustHideFromDocumentation (void) const;
283 
284 
290  std::size_t GetTraceSourceN (void) const;
297  struct TypeId::TraceSourceInformation GetTraceSource (std::size_t i) const;
298 
308  TypeId SetParent (TypeId tid);
318  template <typename T>
319  TypeId SetParent (void);
320 
331  TypeId SetGroupName (std::string groupName);
332 
347  TypeId SetSize (std::size_t size);
348 
356  template <typename T>
357  TypeId AddConstructor (void);
358 
381  TypeId AddAttribute (std::string name,
382  std::string help,
383  const AttributeValue &initialValue,
384  Ptr<const AttributeAccessor> accessor,
385  Ptr<const AttributeChecker> checker,
386  SupportLevel supportLevel = SUPPORTED,
387  const std::string &supportMsg = "");
388 
396  bool SetAttributeInitialValue (std::size_t i,
397  Ptr<const AttributeValue> initialValue);
398 
421  TypeId AddAttribute (std::string name,
422  std::string help,
423  uint32_t flags,
424  const AttributeValue &initialValue,
425  Ptr<const AttributeAccessor> accessor,
426  Ptr<const AttributeChecker> checker,
427  SupportLevel supportLevel = SUPPORTED,
428  const std::string &supportMsg = "");
429 
452  TypeId AddTraceSource (std::string name,
453  std::string help,
454  Ptr<const TraceSourceAccessor> accessor,
455  std::string callback,
456  SupportLevel supportLevel = SUPPORTED,
457  const std::string &supportMsg = "");
458 
464 
474  bool LookupAttributeByName (std::string name, struct AttributeInformation *info) const;
485  Ptr<const TraceSourceAccessor> LookupTraceSourceByName (std::string name) const;
497  Ptr<const TraceSourceAccessor> LookupTraceSourceByName (std::string name, struct TraceSourceInformation *info) const;
498 
507  uint16_t GetUid (void) const;
520  void SetUid (uint16_t uid);
521 
523  inline TypeId ();
528  inline TypeId (const TypeId &o);
534  inline TypeId &operator = (const TypeId &o);
536  inline ~TypeId ();
537 
538 private:
544  friend inline bool operator == (TypeId a, TypeId b);
545  friend inline bool operator != (TypeId a, TypeId b);
546  friend bool operator < (TypeId a, TypeId b);
553  explicit TypeId (uint16_t tid);
559  void DoAddConstructor (Callback<ObjectBase *> callback);
560 
562  uint16_t m_tid;
563 };
564 
573 std::ostream & operator << (std::ostream &os, TypeId tid);
581 std::istream & operator >> (std::istream &is, TypeId &tid);
582 
590 inline bool operator == (TypeId a, TypeId b);
591 inline bool operator != (TypeId a, TypeId b);
592 bool operator < (TypeId a, TypeId b);
595 ATTRIBUTE_HELPER_HEADER (TypeId);
596 
597 } // namespace ns3
598 
599 namespace ns3 {
600 
601 TypeId::TypeId ()
602  : m_tid (0)
603 {}
604 TypeId::TypeId (const TypeId &o)
605  : m_tid (o.m_tid)
606 {}
607 TypeId &TypeId::operator = (const TypeId &o)
608 {
609  m_tid = o.m_tid;
610  return *this;
611 }
612 TypeId::~TypeId ()
613 {}
614 inline bool operator == (TypeId a, TypeId b)
615 {
616  return a.m_tid == b.m_tid;
617 }
618 
619 inline bool operator != (TypeId a, TypeId b)
620 {
621  return a.m_tid != b.m_tid;
622 }
623 
624 
625 /*************************************************************************
626  * The TypeId implementation which depends on templates
627  *************************************************************************/
628 
629 template <typename T>
630 TypeId
631 TypeId::SetParent (void)
632 {
633  return SetParent (T::GetTypeId ());
634 }
635 
636 template <typename T>
637 TypeId
638 TypeId::AddConstructor (void)
639 {
640  struct Maker
641  {
642  static ObjectBase * Create ()
643  {
644  ObjectBase * base = new T ();
645  return base;
646  }
647  };
648  Callback<ObjectBase *> cb = MakeCallback (&Maker::Create);
649  DoAddConstructor (cb);
650  return *this;
651 }
652 
653 } // namespace ns3
654 
655 #endif /* TYPE_ID_H */
ns3::MakeAccessorHelper declarations and template implementations.
Attribute helper (ATTRIBUTE_ )macros definition.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Declaration of the various callback functions.
Callback template class.
Definition: callback.h:1279
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
a unique identifier for an interface.
Definition: type-id.h:59
bool IsChildOf(TypeId other) const
Check if this TypeId is a child of another.
Definition: type-id.cc:957
TypeId AddConstructor(void)
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:638
bool SetAttributeInitialValue(std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1050
std::size_t GetTraceSourceN(void) const
Get the number of Trace sources.
Definition: type-id.cc:1097
TypeId SetParent(void)
Set the parent TypeId.
Definition: type-id.h:631
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:829
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1076
TypeId SetSize(std::size_t size)
Set the size of this type.
Definition: type-id.cc:936
bool HasParent(void) const
Check if this TypeId has a parent.
Definition: type-id.cc:950
TypeId AddTraceSource(std::string name, std::string help, Ptr< const TraceSourceAccessor > accessor, std::string callback, SupportLevel supportLevel=SUPPORTED, const std::string &supportMsg="")
Record a new TraceSource.
Definition: type-id.cc:1110
struct TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:1103
AttributeFlag
Flags describing when a given attribute can be read or written.
Definition: type-id.h:63
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
@ ATTR_SGC
The attribute can be read, and written at any time.
Definition: type-id.h:67
@ ATTR_SET
The attribute can be written.
Definition: type-id.h:65
@ ATTR_CONSTRUCT
The attribute can be written at construction-time.
Definition: type-id.h:66
static bool LookupByHashFailSafe(hash_t hash, TypeId *tid)
Get a TypeId by hash.
Definition: type-id.cc:857
static uint16_t GetRegisteredN(void)
Get the number of registered TypeIds.
Definition: type-id.cc:869
uint16_t GetUid(void) const
Get the internal id of this TypeId.
Definition: type-id.cc:1184
hash_t GetHash(void) const
Get the hash.
Definition: type-id.cc:984
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1060
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1089
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1083
std::string GetGroupName(void) const
Get the group name.
Definition: type-id.cc:968
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1190
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:929
static TypeId LookupByHash(hash_t hash)
Get a TypeId by hash.
Definition: type-id.cc:849
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
Definition: type-id.cc:875
bool HasConstructor(void) const
Check if this TypeId has a constructor.
Definition: type-id.cc:998
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1177
TypeId HideFromDocumentation(void)
Hide this TypeId from documentation.
Definition: type-id.cc:1127
bool MustHideFromDocumentation(void) const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1068
uint32_t hash_t
Type of hash values.
Definition: type-id.h:116
TypeId()
Default constructor.
Definition: type-id.h:601
TypeId GetParent(void) const
Get the parent of this TypeId.
Definition: type-id.cc:943
std::string GetName(void) const
Get the name.
Definition: type-id.cc:976
std::size_t GetSize(void) const
Get the size of this object.
Definition: type-id.cc:990
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:882
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:837
SupportLevel
The level of support or deprecation for attributes or trace sources.
Definition: type-id.h:71
@ SUPPORTED
Attribute or trace source is currently used.
Definition: type-id.h:72
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
Definition: type-id.h:74
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:73
TypeId AddAttribute(std::string name, std::string help, const AttributeValue &initialValue, Ptr< const AttributeAccessor > accessor, Ptr< const AttributeChecker > checker, SupportLevel supportLevel=SUPPORTED, const std::string &supportMsg="")
Record in this TypeId the fact that a new attribute exists.
Definition: type-id.cc:1013
NS_DEPRECATED macro definition.
ns3::Hasher, ns3::Hash32() and ns3::Hash64() function declarations.
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.h:4680
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Attribute implementation.
Definition: type-id.h:78
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition: type-id.h:86
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:94
std::string name
Attribute name.
Definition: type-id.h:80
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:90
uint32_t flags
AttributeFlags value.
Definition: type-id.h:84
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:92
std::string supportMsg
Support message.
Definition: type-id.h:96
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:88
std::string help
Attribute help string.
Definition: type-id.h:82
TraceSource implementation.
Definition: type-id.h:100
std::string name
Trace name.
Definition: type-id.h:102
std::string supportMsg
Support message.
Definition: type-id.h:112
std::string help
Trace help string.
Definition: type-id.h:104
Ptr< const TraceSourceAccessor > accessor
Trace accessor.
Definition: type-id.h:108
std::string callback
Callback function signature type.
Definition: type-id.h:106
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:110
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.