src/core/type-id.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3997 a66553c56a8f
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
     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"
    24 #include "attribute-accessor-helper.h"
    25 #include "attribute-helper.h"
    26 #include "callback.h"
    27 #include <string>
    28 #include <stdint.h>
    29 
    30 namespace ns3 {
    31 
    32 class ObjectBase;
    33 class TraceSourceAccessor;
    34 
    35 /**
    36  * \brief a unique identifier for an interface.
    37  *
    38  * This class records a lot of meta-information about a 
    39  * subclass of the Object base class:
    40  *  - the base class of the subclass
    41  *  - the set of accessible constructors in the subclass
    42  *  - the set of 'attributes' accessible in the subclass
    43  */
    44 class TypeId
    45 {
    46 public:
    47   /**
    48    * Flags describing when a given attribute can be read or written
    49    */
    50   enum AttributeFlag {
    51     ATTR_GET = 1<<0, /**< The attribute can be read */
    52     ATTR_SET = 1<<1, /**< The attribute can be written */
    53     ATTR_CONSTRUCT = 1<<2, /**< The attribute can be written at construction-time */
    54     ATTR_SGC = ATTR_GET | ATTR_SET | ATTR_CONSTRUCT, /** The attribute can be read, and written at any time */
    55   };
    56 
    57   /**
    58    * \param name the name of the requested TypeId
    59    * \returns the unique id associated with the requested
    60    *          name. 
    61    *
    62    * This method cannot fail: it will crash if the input 
    63    * name is not a valid TypeId name.
    64    */
    65   static TypeId LookupByName (std::string name);
    66   /**
    67    * \param name the name of the requested TypeId
    68    * \param tid a pointer to the TypeId instance where the 
    69    *        result of this function should be stored.
    70    * \returns true if the requested name was found, false otherwise.
    71    */
    72   static bool LookupByNameFailSafe (std::string name, TypeId *tid);
    73 
    74   /**
    75    * \returns the number of TypeId instances registered.
    76    */
    77   static uint32_t GetRegisteredN (void);
    78   /**
    79    * \param i index
    80    * \returns the TypeId instance whose index is i.
    81    */
    82   static TypeId GetRegistered (uint32_t i);
    83 
    84   /**
    85    * \param name the name of the interface to construct.
    86    *
    87    * No two instances can share the same name. The name is expected to be
    88    * the full c++ typename of associated c++ object.
    89    */
    90   explicit TypeId (const char * name);
    91 
    92   /**
    93    * \returns the parent of this TypeId
    94    *
    95    * This method cannot fail. It will return itself
    96    * if this TypeId has no parent. i.e., it is at the top
    97    * of the TypeId hierarchy. Currently, this is the
    98    * case for the TypeId associated to the Object class 
    99    * only.
   100    */
   101   TypeId GetParent (void) const;
   102 
   103   bool HasParent (void) const;
   104 
   105   /**
   106    * \param other a parent TypeId
   107    * \returns true if the input TypeId is really a parent
   108    *          of this TypeId, false otherwise.
   109    *
   110    * Calling this method is roughly similar to calling dynamic_cast
   111    * except that you do not need object instances: you can do the check
   112    * with TypeId instances instead.
   113    */
   114   bool IsChildOf (TypeId other) const;
   115 
   116   /**
   117    * \returns the name of the group associated to this TypeId.
   118    */
   119   std::string GetGroupName (void) const;
   120 
   121   /**
   122    * \returns the name of this interface.
   123    */
   124   std::string GetName (void) const;
   125 
   126   /**
   127    * \returns true if this TypeId has a constructor
   128    */
   129   bool HasConstructor (void) const;
   130 
   131   /**
   132    * \returns the number of attributes associated to this TypeId
   133    */
   134   uint32_t GetAttributeN (void) const;
   135   /**
   136    * \param i index into attribute array
   137    * \returns the name associated to the attribute whose
   138    *          index is i.
   139    */
   140   std::string GetAttributeName (uint32_t i) const;
   141   /**
   142    * \param i index into attribute array.
   143    * \returns the help text associated to the attribute whose
   144    *          index is i.
   145    */
   146   std::string GetAttributeHelp (uint32_t i) const;
   147   /**
   148    * \param i index into attribute array
   149    * \returns the full name associated to the attribute whose
   150    *          index is i.
   151    */
   152   std::string GetAttributeFullName (uint32_t i) const;
   153 
   154   /**
   155    * \param i index into attribute array.
   156    * \returns the value with which the associated attribute 
   157    *          is initialized.
   158    */
   159   Ptr<const AttributeValue> GetAttributeInitialValue (uint32_t i) const;
   160   /**
   161    * \param i index into attribute array.
   162    * \returns the flags associated to the requested attribute.
   163    */
   164   uint32_t GetAttributeFlags (uint32_t i) const;
   165   /**
   166    * \param i index into attribute array.
   167    * \returns the checker associated to the requested attribute.
   168    */
   169   Ptr<const AttributeChecker> GetAttributeChecker (uint32_t i) const;
   170   /**
   171    * \param i index into attribute array.
   172    * \returns the accessor associated to the requested attribute.
   173    */
   174   Ptr<const AttributeAccessor> GetAttributeAccessor (uint32_t i) const;
   175 
   176   /**
   177    * \returns a callback which can be used to instanciate an object
   178    *          of this type.
   179    */
   180   Callback<ObjectBase *> GetConstructor (void) const;
   181 
   182   /**
   183    * \returns true if this TypeId should be hidden from the user, 
   184    *          false otherwise.
   185    */
   186   bool MustHideFromDocumentation (void) const;
   187 
   188 
   189   /**
   190    * \returns the number of trace sources defined in this TypeId.
   191    */
   192   uint32_t GetTraceSourceN (void) const;
   193   /**
   194    * \param i index into trace source array.
   195    * \returns the name of the requested trace source.
   196    */
   197   std::string GetTraceSourceName (uint32_t i) const;
   198   /**
   199    * \param i index into trace source array.
   200    * \returns the help text of the requested trace source.
   201    */
   202   std::string GetTraceSourceHelp (uint32_t i) const;
   203   /**
   204    * \param i index into trace source array.
   205    * \returns the accessor used to get access to the requested
   206    *          trace source.
   207    */
   208   Ptr<const TraceSourceAccessor> GetTraceSourceAccessor (uint32_t i) const;
   209 
   210 
   211   /**
   212    * \param tid the TypeId of the base class.
   213    * \return this TypeId instance.
   214    *
   215    * Record in this TypeId which TypeId is the TypeId
   216    * of the base class of the subclass.
   217    */
   218   TypeId SetParent (TypeId tid);
   219   /**
   220    * \return this TypeId instance.
   221    *
   222    * Record in this TypeId which TypeId is the TypeId
   223    * of the base class of the subclass.
   224    */
   225   template <typename T>
   226   TypeId SetParent (void);
   227 
   228   /**
   229    * \param groupName the name of the group this TypeId belongs to.
   230    * \returns this TypeId instance.
   231    *
   232    * The group name is purely an advisory information used to 
   233    * group together types according to a user-specific grouping
   234    * scheme.
   235    */
   236   TypeId SetGroupName (std::string groupName);
   237 
   238   /**
   239    * \returns this TypeId instance
   240    *
   241    * Record in this TypeId the fact that the default constructor
   242    * is accessible.
   243    */
   244   template <typename T>
   245   TypeId AddConstructor (void);
   246 
   247   /**
   248    * \param name the name of the new attribute
   249    * \param help some help text which describes the purpose of this
   250    *        attribute.
   251    * \param initialValue the initial value for this attribute.
   252    * \param accessor an instance of the associated AttributeAccessor subclass.
   253    * \param checker an instance of the associated AttributeChecker subclass.
   254    * \returns this TypeId instance
   255    *
   256    * Record in this TypeId the fact that a new attribute exists.
   257    */
   258   TypeId AddAttribute (std::string name,
   259                        std::string help, 
   260                        const AttributeValue &initialValue,
   261                        Ptr<const AttributeAccessor> accessor,
   262                        Ptr<const AttributeChecker> checker);
   263 
   264   /**
   265    * \param name the name of the new attribute
   266    * \param help some help text which describes the purpose of this
   267    *        attribute
   268    * \param flags flags which describe how this attribute can be read and/or written.
   269    * \param initialValue the initial value for this attribute.
   270    * \param accessor an instance of the associated AttributeAccessor subclass.
   271    * \param checker an instance of the associated AttributeChecker subclass.
   272    * \returns this TypeId instance
   273    *
   274    * Record in this TypeId the fact that a new attribute exists.
   275    */
   276   TypeId AddAttribute (std::string name,
   277                        std::string help, 
   278                        uint32_t flags,
   279                        const AttributeValue &initialValue,
   280                        Ptr<const AttributeAccessor> accessor,
   281                        Ptr<const AttributeChecker> checker);
   282 
   283   /**
   284    * \param name the name of the new trace source
   285    * \param help some help text which describes the purpose of this
   286    *        trace source.
   287    * \param accessor a pointer to a TraceSourceAccessor which can be
   288    *        used to connect/disconnect sinks to this trace source.
   289    * \returns this TypeId instance.
   290    */
   291   TypeId AddTraceSource (std::string name,
   292                          std::string help,
   293                          Ptr<const TraceSourceAccessor> accessor);
   294 
   295   TypeId HideFromDocumentation (void);
   296 
   297   /**
   298    * \brief store together a set of attribute properties.
   299    */
   300   struct AttributeInfo {
   301     // The accessor associated to the attribute.
   302     Ptr<const AttributeAccessor> accessor;
   303     // The initial value associated to the attribute.
   304     Ptr<const AttributeValue> initialValue;
   305     // The set of access control flags associated to the attribute.
   306     uint32_t flags;
   307     // The checker associated to the attribute.
   308     Ptr<const AttributeChecker> checker;
   309   };
   310   /**
   311    * \param name the name of the requested attribute
   312    * \param info a pointer to the TypeId::AttributeInfo data structure
   313    *        where the result value of this method will be stored.
   314    * \returns true if the requested attribute could be found, false otherwise.
   315    */
   316   bool LookupAttributeByName (std::string name, struct AttributeInfo *info) const;
   317   /**
   318    * \param name the name of the requested trace source
   319    * \returns the trace source accessor which can be used to connect and disconnect
   320    *          trace sinks with the requested trace source on an object instance.
   321    *
   322    * If no matching trace source is found, this method returns zero.
   323    */
   324   Ptr<const TraceSourceAccessor> LookupTraceSourceByName (std::string name) const;
   325 
   326 
   327   /**
   328    * \param fullName the full name of the requested attribute
   329    * \param info a pointer to the TypeId::AttributeInfo data structure
   330    *        where the result value of this method will be stored.
   331    * \returns the Accessor associated to the requested attribute
   332    */
   333   static bool LookupAttributeByFullName (std::string fullName, struct AttributeInfo *info);
   334 
   335   /**
   336    * \returns the internal integer which uniquely identifies this
   337    *          TypeId.
   338    *
   339    * This is really an internal method which users are not expected
   340    * to use.
   341    */
   342   uint16_t GetUid (void) const;
   343   /**
   344    * \param tid the internal integer which uniquely identifies 
   345    *        this TypeId.
   346    *
   347    * This method is even more internal than TypeId::GetUid. Use
   348    * at your own risk and don't be surprised that it eats raw 
   349    * babies on full-moon nights.
   350    */
   351   void SetUid (uint16_t tid);
   352 
   353   // construct an invalid TypeId.
   354   inline TypeId ();
   355   inline TypeId (const TypeId &o);
   356   inline TypeId &operator = (const TypeId &o);
   357   inline ~TypeId ();
   358 
   359 private:
   360   friend class AttributeList;
   361   friend bool operator == (TypeId a, TypeId b);
   362   friend bool operator != (TypeId a, TypeId b);
   363   friend bool operator <  (TypeId a, TypeId b);
   364 
   365 
   366   explicit TypeId (uint16_t tid);
   367   void DoAddConstructor (Callback<ObjectBase *> callback);
   368   
   369   uint16_t m_tid;
   370 };
   371 
   372 std::ostream & operator << (std::ostream &os, TypeId tid);
   373 std::istream & operator >> (std::istream &is, TypeId &tid);
   374 inline bool operator == (TypeId a, TypeId b);
   375 inline bool operator != (TypeId a, TypeId b);
   376 bool operator <  (TypeId a, TypeId b);
   377 
   378 /**
   379  * \class ns3::TypeIdValue
   380  * \brief hold objects of type ns3::TypeId
   381  */
   382 
   383 
   384 ATTRIBUTE_HELPER_HEADER (TypeId);
   385 
   386 } // namespace ns3 
   387 
   388 namespace ns3 {
   389 
   390 TypeId::TypeId ()
   391   : m_tid (0) {}
   392 TypeId::TypeId (const TypeId &o)
   393   : m_tid (o.m_tid) {}
   394 TypeId &TypeId::operator = (const TypeId &o)
   395 {
   396   m_tid = o.m_tid;
   397   return *this;
   398 }
   399 TypeId::~TypeId ()
   400 {}
   401 inline bool operator == (TypeId a, TypeId b)
   402 {
   403   return a.m_tid == b.m_tid;
   404 }
   405 
   406 inline bool operator != (TypeId a, TypeId b)
   407 {
   408   return a.m_tid != b.m_tid;
   409 }
   410 
   411 
   412 /*************************************************************************
   413  *   The TypeId implementation which depends on templates
   414  *************************************************************************/
   415 
   416 template <typename T>
   417 TypeId 
   418 TypeId::SetParent (void)
   419 {
   420   return SetParent (T::GetTypeId ());
   421 }
   422 
   423 template <typename T>
   424 TypeId 
   425 TypeId::AddConstructor (void)
   426 {
   427   struct Maker {
   428     static ObjectBase * Create () {
   429       ObjectBase * base = new T ();
   430       return base;
   431     }
   432   };
   433   Callback<ObjectBase *> cb = MakeCallback (&Maker::Create);
   434   DoAddConstructor (cb);
   435   return *this;
   436 }
   437 
   438 } // namespace ns3
   439 
   440 #endif /* TYPE_ID_H */