A Discrete-Event Network Simulator
API
type-id.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef TYPE_ID_H
20 #define TYPE_ID_H
21 
23 #include "attribute-helper.h"
24 #include "attribute.h"
25 #include "callback.h"
26 #include "hash.h"
27 #include "trace-source-accessor.h"
28 
29 #include <stdint.h>
30 #include <string>
31 
38 namespace ns3
39 {
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,
69  };
70 
73  {
76  OBSOLETE
77  };
78 
81  {
83  std::string name;
85  std::string help;
87  uint32_t flags;
99  std::string supportMsg;
100  };
101 
104  {
106  std::string name;
108  std::string help;
110  std::string callback;
116  std::string supportMsg;
117  };
118 
120  typedef uint32_t hash_t;
121 
131  static TypeId LookupByName(std::string name);
140  static bool LookupByNameFailSafe(std::string name, TypeId* tid);
150  static TypeId LookupByHash(hash_t hash);
159  static bool LookupByHashFailSafe(hash_t hash, TypeId* tid);
160 
166  static uint16_t GetRegisteredN();
173  static TypeId GetRegistered(uint16_t i);
174 
183  explicit TypeId(const std::string& name);
184 
196  TypeId GetParent() const;
197 
203  bool HasParent() const;
204 
215  bool IsChildOf(TypeId other) const;
216 
222  std::string GetGroupName() const;
223 
229  std::string GetName() const;
230 
236  hash_t GetHash() const;
237 
243  std::size_t GetSize() const;
244 
250  bool HasConstructor() const;
251 
257  std::size_t GetAttributeN() const;
264  TypeId::AttributeInformation GetAttribute(std::size_t i) const;
271  std::string GetAttributeFullName(std::size_t i) const;
272 
280 
286  bool MustHideFromDocumentation() const;
287 
293  std::size_t GetTraceSourceN() const;
300  TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const;
301 
311  TypeId SetParent(TypeId tid);
321  template <typename T>
322  TypeId SetParent();
323 
334  TypeId SetGroupName(std::string groupName);
335 
350  TypeId SetSize(std::size_t size);
351 
359  template <typename T>
361 
384  TypeId AddAttribute(std::string name,
385  std::string help,
386  const AttributeValue& initialValue,
389  SupportLevel supportLevel = SUPPORTED,
390  const std::string& supportMsg = "");
391 
399  bool SetAttributeInitialValue(std::size_t i, Ptr<const AttributeValue> initialValue);
400 
423  TypeId AddAttribute(std::string name,
424  std::string help,
425  uint32_t flags,
426  const AttributeValue& initialValue,
429  SupportLevel supportLevel = SUPPORTED,
430  const std::string& supportMsg = "");
431 
454  TypeId AddTraceSource(std::string name,
455  std::string help,
457  std::string callback,
458  SupportLevel supportLevel = SUPPORTED,
459  const std::string& supportMsg = "");
460 
466 
476  bool LookupAttributeByName(std::string name, AttributeInformation* info) const;
500  TraceSourceInformation* info) const;
501 
510  uint16_t GetUid() const;
523  void SetUid(uint16_t uid);
524 
526  inline TypeId();
531  inline TypeId(const TypeId& o);
537  inline TypeId& operator=(const TypeId& o);
539  inline ~TypeId();
540 
541  private:
547  friend inline bool operator==(TypeId a, TypeId b);
548  friend inline bool operator!=(TypeId a, TypeId b);
549  friend bool operator<(TypeId a, TypeId b);
556  explicit TypeId(uint16_t tid);
563 
565  uint16_t m_tid;
566 };
567 
576 std::ostream& operator<<(std::ostream& os, TypeId tid);
584 std::istream& operator>>(std::istream& is, TypeId& tid);
585 
593 inline bool operator==(TypeId a, TypeId b);
594 inline bool operator!=(TypeId a, TypeId b);
595 bool operator<(TypeId a, TypeId b);
599 
600 } // namespace ns3
601 
602 namespace ns3
603 {
604 
606  : m_tid(0)
607 {
608 }
609 
611  : m_tid(o.m_tid)
612 {
613 }
614 
615 TypeId&
617 {
618  m_tid = o.m_tid;
619  return *this;
620 }
621 
623 {
624 }
625 
626 inline bool
628 {
629  return a.m_tid == b.m_tid;
630 }
631 
632 inline bool
634 {
635  return a.m_tid != b.m_tid;
636 }
637 
638 /*************************************************************************
639  * The TypeId implementation which depends on templates
640  *************************************************************************/
641 
642 template <typename T>
643 TypeId
645 {
646  return SetParent(T::GetTypeId());
647 }
648 
649 template <typename T>
650 TypeId
652 {
653  struct Maker
654  {
655  static ObjectBase* Create()
656  {
657  ObjectBase* base = new T();
658  return base;
659  }
660  };
661 
663  DoAddConstructor(cb);
664  return *this;
665 }
666 
667 } // namespace ns3
668 
669 #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.
Hold a value for an Attribute.
Definition: attribute.h:70
Callback template class.
Definition: callback.h:438
Anchor the ns-3 type and attribute system.
Definition: object-base.h:173
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
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:971
friend bool operator!=(TypeId a, TypeId b)
Comparison operator.
Definition: type-id.h:633
std::size_t GetTraceSourceN() const
Get the number of Trace sources.
Definition: type-id.cc:1123
bool SetAttributeInitialValue(std::size_t i, Ptr< const AttributeValue > initialValue)
Set the initial value of an Attribute.
Definition: type-id.cc:1076
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
bool HasParent() const
Check if this TypeId has a parent.
Definition: type-id.cc:963
TypeId SetSize(std::size_t size)
Set the size of this type.
Definition: type-id.cc:947
hash_t GetHash() const
Get the hash.
Definition: type-id.cc:999
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:1137
bool MustHideFromDocumentation() const
Check if this TypeId should not be listed in documentation.
Definition: type-id.cc:1092
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:867
TypeId::TraceSourceInformation GetTraceSource(std::size_t i) const
Get the trace source by index.
Definition: type-id.cc:1130
std::string GetGroupName() const
Get the group name.
Definition: type-id.cc:983
TypeId HideFromDocumentation()
Hide this TypeId from documentation.
Definition: type-id.cc:1151
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition: type-id.cc:1084
static uint16_t GetRegisteredN()
Get the number of registered TypeIds.
Definition: type-id.cc:879
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1115
bool HasConstructor() const
Check if this TypeId has a constructor.
Definition: type-id.cc:1014
std::size_t GetAttributeN() const
Get the number of attributes.
Definition: type-id.cc:1100
TypeId GetParent() const
Get the parent of this TypeId.
Definition: type-id.cc:955
TypeId AddConstructor()
Record in this TypeId the fact that the default constructor is accessible.
Definition: type-id.h:651
void SetUid(uint16_t uid)
Set the internal id of this TypeId.
Definition: type-id.cc:1212
uint16_t m_tid
The TypeId value.
Definition: type-id.h:565
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:939
static TypeId LookupByHash(hash_t hash)
Get a TypeId by hash.
Definition: type-id.cc:857
static TypeId GetRegistered(uint16_t i)
Get a TypeId by index.
Definition: type-id.cc:886
friend bool operator==(TypeId a, TypeId b)
Comparison operator.
Definition: type-id.h:627
std::size_t GetSize() const
Get the size of this object.
Definition: type-id.cc:1006
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1198
TypeId & operator=(const TypeId &o)
Assignment.
Definition: type-id.h:616
~TypeId()
Destructor.
Definition: type-id.h:622
friend bool operator<(TypeId a, TypeId b)
Comparison operator.
Definition: type-id.cc:1253
uint32_t hash_t
Type of hash values.
Definition: type-id.h:120
TypeId()
Default constructor.
Definition: type-id.h:605
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1108
uint16_t GetUid() const
Get the internal id of this TypeId.
Definition: type-id.cc:1205
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:844
SupportLevel
The level of support or deprecation for attributes or trace sources.
Definition: type-id.h:73
@ SUPPORTED
Attribute or trace source is currently used.
Definition: type-id.h:74
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
Definition: type-id.h:76
@ DEPRECATED
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:75
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:1029
bool LookupAttributeByName(std::string name, AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:893
std::string GetName() const
Get the name.
Definition: type-id.cc:991
TypeId SetParent()
Set the parent TypeId.
Definition: type-id.h:644
void DoAddConstructor(Callback< ObjectBase * > callback)
Implementation for AddConstructor().
Definition: type-id.cc:1022
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:442
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.
bool operator!=(Callback< R, Args... > a, Callback< R, Args... > b)
Inequality test.
Definition: callback.h:678
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
bool operator==(const EventId &a, const EventId &b)
Definition: event-id.h:157
ATTRIBUTE_HELPER_HEADER(ValueClassTest)
bool operator<(const EventId &a, const EventId &b)
Definition: event-id.h:170
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
Attribute implementation.
Definition: type-id.h:81
Ptr< const AttributeValue > originalInitialValue
Default initial value.
Definition: type-id.h:89
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:97
std::string name
Attribute name.
Definition: type-id.h:83
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:93
uint32_t flags
AttributeFlags value.
Definition: type-id.h:87
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:95
std::string supportMsg
Support message.
Definition: type-id.h:99
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:91
std::string help
Attribute help string.
Definition: type-id.h:85
TraceSource implementation.
Definition: type-id.h:104
std::string name
Trace name.
Definition: type-id.h:106
std::string supportMsg
Support message.
Definition: type-id.h:116
std::string help
Trace help string.
Definition: type-id.h:108
Ptr< const TraceSourceAccessor > accessor
Trace accessor.
Definition: type-id.h:112
std::string callback
Callback function signature type.
Definition: type-id.h:110
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:114
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.