src/core/type-id.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 4318 e753d14618a3
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2671
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2671
     2
/*
mathieu@2671
     3
 * Copyright (c) 2008 INRIA
mathieu@2671
     4
 *
mathieu@2671
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2671
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2671
     7
 * published by the Free Software Foundation;
mathieu@2671
     8
 *
mathieu@2671
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2671
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2671
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2671
    12
 * GNU General Public License for more details.
mathieu@2671
    13
 *
mathieu@2671
    14
 * You should have received a copy of the GNU General Public License
mathieu@2671
    15
 * along with this program; if not, write to the Free Software
mathieu@2671
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2671
    17
 *
mathieu@2671
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2671
    19
 */
mathieu@2633
    20
#include "type-id.h"
mathieu@2633
    21
#include "singleton.h"
mathieu@2633
    22
#include "trace-source-accessor.h"
mathieu@2633
    23
#include <vector>
mathieu@2633
    24
#include <sstream>
mathieu@2633
    25
mathieu@2633
    26
/*********************************************************************
mathieu@2633
    27
 *         Helper code
mathieu@2633
    28
 *********************************************************************/
mathieu@2633
    29
mathieu@2633
    30
namespace {
mathieu@2633
    31
mathieu@2633
    32
class IidManager
mathieu@2633
    33
{
mathieu@2633
    34
public:
mathieu@2633
    35
  IidManager ();
mathieu@2633
    36
  uint16_t AllocateUid (std::string name);
mathieu@2633
    37
  void SetParent (uint16_t uid, uint16_t parent);
mathieu@2633
    38
  void SetGroupName (uint16_t uid, std::string groupName);
mathieu@2633
    39
  void AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback);
mathieu@2633
    40
  void HideFromDocumentation (uint16_t uid);
mathieu@2633
    41
  uint16_t GetUid (std::string name) const;
mathieu@2633
    42
  std::string GetName (uint16_t uid) const;
mathieu@2633
    43
  uint16_t GetParent (uint16_t uid) const;
mathieu@2633
    44
  std::string GetGroupName (uint16_t uid) const;
mathieu@2633
    45
  ns3::Callback<ns3::ObjectBase *> GetConstructor (uint16_t uid) const;
mathieu@2633
    46
  bool HasConstructor (uint16_t uid) const;
mathieu@2633
    47
  uint32_t GetRegisteredN (void) const;
mathieu@2633
    48
  uint16_t GetRegistered (uint32_t i) const;
mathieu@2633
    49
  void AddAttribute (uint16_t uid, 
mathieu@2633
    50
                     std::string name,
mathieu@2633
    51
                     std::string help, 
mathieu@2633
    52
                     uint32_t flags,
mathieu@2965
    53
                     ns3::Ptr<const ns3::AttributeValue> initialValue,
mathieu@2633
    54
                     ns3::Ptr<const ns3::AttributeAccessor> spec,
mathieu@2633
    55
                     ns3::Ptr<const ns3::AttributeChecker> checker);
mathieu@2693
    56
  uint32_t GetAttributeN (uint16_t uid) const;
mathieu@2633
    57
  std::string GetAttributeName (uint16_t uid, uint32_t i) const;
mathieu@2633
    58
  std::string GetAttributeHelp (uint16_t uid, uint32_t i) const;
mathieu@2633
    59
  uint32_t GetAttributeFlags (uint16_t uid, uint32_t i) const;
mathieu@2965
    60
  ns3::Ptr<const ns3::AttributeValue> GetAttributeInitialValue (uint16_t uid, uint32_t i) const;
mathieu@2633
    61
  ns3::Ptr<const ns3::AttributeAccessor> GetAttributeAccessor (uint16_t uid, uint32_t i) const;
mathieu@2633
    62
  ns3::Ptr<const ns3::AttributeChecker> GetAttributeChecker (uint16_t uid, uint32_t i) const;
mathieu@2633
    63
  void AddTraceSource (uint16_t uid,
mathieu@2633
    64
                       std::string name, 
mathieu@2633
    65
                       std::string help,
mathieu@2633
    66
                       ns3::Ptr<const ns3::TraceSourceAccessor> accessor);
mathieu@2633
    67
  uint32_t GetTraceSourceN (uint16_t uid) const;
mathieu@2633
    68
  std::string GetTraceSourceName (uint16_t uid, uint32_t i) const;
mathieu@2633
    69
  std::string GetTraceSourceHelp (uint16_t uid, uint32_t i) const;
mathieu@2633
    70
  ns3::Ptr<const ns3::TraceSourceAccessor> GetTraceSourceAccessor (uint16_t uid, uint32_t i) const;
mathieu@2633
    71
  bool MustHideFromDocumentation (uint16_t uid) const;
mathieu@2633
    72
mathieu@2633
    73
private:
mathieu@4318
    74
  bool HasTraceSource (uint16_t uid, std::string name);
mathieu@4320
    75
  bool HasAttribute (uint16_t uid, std::string name);
mathieu@4318
    76
mathieu@2633
    77
  struct AttributeInformation {
mathieu@2633
    78
    std::string name;
mathieu@2633
    79
    std::string help;
mathieu@2633
    80
    uint32_t flags;
mathieu@2965
    81
    ns3::Ptr<const ns3::AttributeValue> initialValue;
mathieu@2633
    82
    ns3::Ptr<const ns3::AttributeAccessor> param;
mathieu@2633
    83
    ns3::Ptr<const ns3::AttributeChecker> checker;
mathieu@2633
    84
  };
mathieu@2633
    85
  struct TraceSourceInformation {
mathieu@2633
    86
    std::string name;
mathieu@2633
    87
    std::string help;
mathieu@2633
    88
    ns3::Ptr<const ns3::TraceSourceAccessor> accessor;
mathieu@2633
    89
  };
mathieu@2633
    90
  struct IidInformation {
mathieu@2633
    91
    std::string name;
mathieu@2633
    92
    uint16_t parent;
mathieu@2633
    93
    std::string groupName;
mathieu@2633
    94
    bool hasConstructor;
mathieu@2633
    95
    ns3::Callback<ns3::ObjectBase *> constructor;
mathieu@2633
    96
    bool mustHideFromDocumentation;
mathieu@2633
    97
    std::vector<struct AttributeInformation> attributes;
mathieu@2633
    98
    std::vector<struct TraceSourceInformation> traceSources;
mathieu@2633
    99
  };
mathieu@2633
   100
  typedef std::vector<struct IidInformation>::const_iterator Iterator;
mathieu@2633
   101
mathieu@2633
   102
  struct IidManager::IidInformation *LookupInformation (uint16_t uid) const;
mathieu@2633
   103
mathieu@2633
   104
  std::vector<struct IidInformation> m_information;
mathieu@2633
   105
};
mathieu@2633
   106
mathieu@2633
   107
IidManager::IidManager ()
mathieu@2633
   108
{}
mathieu@2633
   109
mathieu@2633
   110
uint16_t 
mathieu@2633
   111
IidManager::AllocateUid (std::string name)
mathieu@2633
   112
{
mathieu@2633
   113
  uint16_t j = 1;
mathieu@2633
   114
  for (Iterator i = m_information.begin (); i != m_information.end (); i++)
mathieu@2633
   115
    {
mathieu@2633
   116
      if (i->name == name)
mathieu@2633
   117
        {
mathieu@2633
   118
          NS_FATAL_ERROR ("Trying to allocate twice the same uid: " << name);
mathieu@2633
   119
          return 0;
mathieu@2633
   120
        }
mathieu@2633
   121
      j++;
mathieu@2633
   122
    }
mathieu@2633
   123
  struct IidInformation information;
mathieu@2633
   124
  information.name = name;
mathieu@2633
   125
  information.parent = 0;
mathieu@2633
   126
  information.groupName = "";
mathieu@2633
   127
  information.hasConstructor = false;
mathieu@2633
   128
  information.mustHideFromDocumentation = false;
mathieu@2633
   129
  m_information.push_back (information);
mathieu@2633
   130
  uint32_t uid = m_information.size ();
mathieu@2633
   131
  NS_ASSERT (uid <= 0xffff);
mathieu@2633
   132
  return uid;
mathieu@2633
   133
}
mathieu@2633
   134
mathieu@2633
   135
struct IidManager::IidInformation *
mathieu@2633
   136
IidManager::LookupInformation (uint16_t uid) const
mathieu@2633
   137
{
mathieu@3910
   138
  NS_ASSERT (uid <= m_information.size () && uid != 0);
mathieu@2633
   139
  return const_cast<struct IidInformation *> (&m_information[uid-1]);
mathieu@2633
   140
}
mathieu@2633
   141
mathieu@2633
   142
void 
mathieu@2633
   143
IidManager::SetParent (uint16_t uid, uint16_t parent)
mathieu@2633
   144
{
mathieu@2633
   145
  NS_ASSERT (parent <= m_information.size ());
mathieu@2633
   146
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   147
  information->parent = parent;
mathieu@2633
   148
}
mathieu@2633
   149
void 
mathieu@2633
   150
IidManager::SetGroupName (uint16_t uid, std::string groupName)
mathieu@2633
   151
{
mathieu@2633
   152
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   153
  information->groupName = groupName;
mathieu@2633
   154
}
mathieu@2633
   155
void
mathieu@2633
   156
IidManager::HideFromDocumentation (uint16_t uid)
mathieu@2633
   157
{
mathieu@2633
   158
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   159
  information->mustHideFromDocumentation = true;
mathieu@2633
   160
}
mathieu@2633
   161
mathieu@2633
   162
void 
mathieu@2633
   163
IidManager::AddConstructor (uint16_t uid, ns3::Callback<ns3::ObjectBase *> callback)
mathieu@2633
   164
{
mathieu@2633
   165
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   166
  if (information->hasConstructor)
mathieu@2633
   167
    {
mathieu@2633
   168
      NS_FATAL_ERROR (information->name<<" already has a constructor.");
mathieu@2633
   169
    }
mathieu@2633
   170
  information->hasConstructor = true;
mathieu@2633
   171
  information->constructor = callback;
mathieu@2633
   172
}
mathieu@2633
   173
mathieu@2633
   174
uint16_t 
mathieu@2633
   175
IidManager::GetUid (std::string name) const
mathieu@2633
   176
{
mathieu@2633
   177
  uint32_t j = 1;
mathieu@2633
   178
  for (Iterator i = m_information.begin (); i != m_information.end (); i++)
mathieu@2633
   179
    {
mathieu@2633
   180
      if (i->name == name)
mathieu@2633
   181
        {
mathieu@2633
   182
          NS_ASSERT (j <= 0xffff);
mathieu@2633
   183
          return j;
mathieu@2633
   184
        }
mathieu@2633
   185
      j++;
mathieu@2633
   186
    }
mathieu@2633
   187
  return 0;
mathieu@2633
   188
}
mathieu@2633
   189
std::string 
mathieu@2633
   190
IidManager::GetName (uint16_t uid) const
mathieu@2633
   191
{
mathieu@2633
   192
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   193
  return information->name;
mathieu@2633
   194
}
mathieu@2633
   195
uint16_t 
mathieu@2633
   196
IidManager::GetParent (uint16_t uid) const
mathieu@2633
   197
{
mathieu@2633
   198
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   199
  return information->parent;
mathieu@2633
   200
}
mathieu@2633
   201
std::string 
mathieu@2633
   202
IidManager::GetGroupName (uint16_t uid) const
mathieu@2633
   203
{
mathieu@2633
   204
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   205
  return information->groupName;
mathieu@2633
   206
}
mathieu@2633
   207
mathieu@2633
   208
ns3::Callback<ns3::ObjectBase *> 
mathieu@2633
   209
IidManager::GetConstructor (uint16_t uid) const
mathieu@2633
   210
{
mathieu@2633
   211
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   212
  if (!information->hasConstructor)
mathieu@2633
   213
    {
mathieu@2633
   214
      NS_FATAL_ERROR ("Requested constructor for "<<information->name<<" but it does not have one.");
mathieu@2633
   215
    }
mathieu@2633
   216
  return information->constructor;
mathieu@2633
   217
}
mathieu@2633
   218
mathieu@2633
   219
bool 
mathieu@2633
   220
IidManager::HasConstructor (uint16_t uid) const
mathieu@2633
   221
{
mathieu@2633
   222
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   223
  return information->hasConstructor;
mathieu@2633
   224
}
mathieu@2633
   225
mathieu@2633
   226
uint32_t 
mathieu@2633
   227
IidManager::GetRegisteredN (void) const
mathieu@2633
   228
{
mathieu@2633
   229
  return m_information.size ();
mathieu@2633
   230
}
mathieu@2633
   231
uint16_t 
mathieu@2633
   232
IidManager::GetRegistered (uint32_t i) const
mathieu@2633
   233
{
mathieu@2633
   234
  return i + 1;
mathieu@2633
   235
}
mathieu@2633
   236
mathieu@4320
   237
bool
mathieu@4320
   238
IidManager::HasAttribute (uint16_t uid,
mathieu@4320
   239
                          std::string name)
mathieu@4320
   240
{
mathieu@4320
   241
  struct IidInformation *information  = LookupInformation (uid);
mathieu@4320
   242
  while (true)
mathieu@4320
   243
    {
mathieu@4320
   244
      for (std::vector<struct AttributeInformation>::const_iterator i = information->attributes.begin ();
mathieu@4320
   245
           i != information->attributes.end (); ++i)
mathieu@4320
   246
        {
mathieu@4320
   247
          if (i->name == name)
mathieu@4320
   248
            {
mathieu@4320
   249
              return true;
mathieu@4320
   250
            }
mathieu@4320
   251
        }
mathieu@4320
   252
      struct IidInformation *parent = LookupInformation (information->parent);
mathieu@4320
   253
      if (parent == information)
mathieu@4320
   254
        {
mathieu@4320
   255
          // top of inheritance tree
mathieu@4320
   256
          return false;
mathieu@4320
   257
        }
mathieu@4320
   258
      // check parent
mathieu@4320
   259
      information = parent;
mathieu@4320
   260
    }
mathieu@4320
   261
  return false;
mathieu@4320
   262
}
mathieu@4320
   263
mathieu@2633
   264
void 
mathieu@2633
   265
IidManager::AddAttribute (uint16_t uid, 
mathieu@2633
   266
                          std::string name,
mathieu@2633
   267
                          std::string help, 
mathieu@2633
   268
                          uint32_t flags,
mathieu@2965
   269
                          ns3::Ptr<const ns3::AttributeValue> initialValue,
mathieu@2633
   270
                          ns3::Ptr<const ns3::AttributeAccessor> spec,
mathieu@2633
   271
                          ns3::Ptr<const ns3::AttributeChecker> checker)
mathieu@2633
   272
{
mathieu@2633
   273
  struct IidInformation *information = LookupInformation (uid);
mathieu@4320
   274
  if (HasAttribute (uid, name))
mathieu@2633
   275
    {
mathieu@4320
   276
      NS_FATAL_ERROR ("Attribute \"" << name << "\" already registered on tid=\"" << 
mathieu@4320
   277
                      information->name << "\"");
mathieu@2633
   278
    }
mathieu@2633
   279
  struct AttributeInformation param;
mathieu@2633
   280
  param.name = name;
mathieu@2633
   281
  param.help = help;
mathieu@2633
   282
  param.flags = flags;
mathieu@2633
   283
  param.initialValue = initialValue;
mathieu@2633
   284
  param.param = spec;
mathieu@2633
   285
  param.checker = checker;
mathieu@2633
   286
  information->attributes.push_back (param);
mathieu@2633
   287
}
mathieu@2633
   288
mathieu@2633
   289
mathieu@2633
   290
uint32_t 
mathieu@2693
   291
IidManager::GetAttributeN (uint16_t uid) const
mathieu@2633
   292
{
mathieu@2633
   293
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   294
  return information->attributes.size ();
mathieu@2633
   295
}
mathieu@2633
   296
std::string 
mathieu@2633
   297
IidManager::GetAttributeName (uint16_t uid, uint32_t i) const
mathieu@2633
   298
{
mathieu@2633
   299
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   300
  NS_ASSERT (i < information->attributes.size ());
mathieu@2633
   301
  return information->attributes[i].name;
mathieu@2633
   302
}
mathieu@2633
   303
std::string 
mathieu@2633
   304
IidManager::GetAttributeHelp (uint16_t uid, uint32_t i) const
mathieu@2633
   305
{
mathieu@2633
   306
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   307
  NS_ASSERT (i < information->attributes.size ());
mathieu@2633
   308
  return information->attributes[i].help;
mathieu@2633
   309
}
mathieu@2633
   310
uint32_t
mathieu@2633
   311
IidManager::GetAttributeFlags (uint16_t uid, uint32_t i) const
mathieu@2633
   312
{
mathieu@2633
   313
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   314
  NS_ASSERT (i < information->attributes.size ());
mathieu@2633
   315
  return information->attributes[i].flags;
mathieu@2633
   316
}
mathieu@2965
   317
ns3::Ptr<const ns3::AttributeValue> 
mathieu@2633
   318
IidManager::GetAttributeInitialValue (uint16_t uid, uint32_t i) const
mathieu@2633
   319
{
mathieu@2633
   320
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   321
  NS_ASSERT (i < information->attributes.size ());
mathieu@2633
   322
  return information->attributes[i].initialValue;
mathieu@2633
   323
}
mathieu@2633
   324
ns3::Ptr<const ns3::AttributeAccessor>
mathieu@2633
   325
IidManager::GetAttributeAccessor (uint16_t uid, uint32_t i) const
mathieu@2633
   326
{
mathieu@2633
   327
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   328
  NS_ASSERT (i < information->attributes.size ());
mathieu@2633
   329
  return information->attributes[i].param;
mathieu@2633
   330
}
mathieu@2633
   331
ns3::Ptr<const ns3::AttributeChecker>
mathieu@2633
   332
IidManager::GetAttributeChecker (uint16_t uid, uint32_t i) const
mathieu@2633
   333
{
mathieu@2633
   334
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   335
  NS_ASSERT (i < information->attributes.size ());
mathieu@2633
   336
  return information->attributes[i].checker;
mathieu@2633
   337
}
mathieu@2633
   338
mathieu@4318
   339
bool
mathieu@4318
   340
IidManager::HasTraceSource (uint16_t uid,
mathieu@4318
   341
                            std::string name)
mathieu@4318
   342
{
mathieu@4318
   343
  struct IidInformation *information  = LookupInformation (uid);
mathieu@4318
   344
  while (true)
mathieu@4318
   345
    {
mathieu@4318
   346
      for (std::vector<struct TraceSourceInformation>::const_iterator i = information->traceSources.begin ();
mathieu@4318
   347
           i != information->traceSources.end (); ++i)
mathieu@4318
   348
        {
mathieu@4318
   349
          if (i->name == name)
mathieu@4318
   350
            {
mathieu@4318
   351
              return true;
mathieu@4318
   352
            }
mathieu@4318
   353
        }
mathieu@4318
   354
      struct IidInformation *parent = LookupInformation (information->parent);
mathieu@4318
   355
      if (parent == information)
mathieu@4318
   356
        {
mathieu@4318
   357
          // top of inheritance tree
mathieu@4318
   358
          return false;
mathieu@4318
   359
        }
mathieu@4318
   360
      // check parent
mathieu@4318
   361
      information = parent;
mathieu@4318
   362
    }
mathieu@4318
   363
  return false;
mathieu@4318
   364
}
mathieu@4318
   365
mathieu@2633
   366
void 
mathieu@2633
   367
IidManager::AddTraceSource (uint16_t uid,
mathieu@2633
   368
                            std::string name, 
mathieu@2633
   369
                            std::string help,
mathieu@2633
   370
                            ns3::Ptr<const ns3::TraceSourceAccessor> accessor)
mathieu@2633
   371
{
mathieu@2633
   372
  struct IidInformation *information  = LookupInformation (uid);
mathieu@4318
   373
  if (HasTraceSource (uid, name))
mathieu@4318
   374
    {
mathieu@4318
   375
      NS_FATAL_ERROR ("Trace source \"" << name << "\" already registered on tid=\"" << 
mathieu@4318
   376
                      information->name << "\"");
mathieu@4318
   377
    }
mathieu@2633
   378
  struct TraceSourceInformation source;
mathieu@2633
   379
  source.name = name;
mathieu@2633
   380
  source.help = help;
mathieu@2633
   381
  source.accessor = accessor;
mathieu@2633
   382
  information->traceSources.push_back (source);
mathieu@2633
   383
}
mathieu@2633
   384
uint32_t 
mathieu@2633
   385
IidManager::GetTraceSourceN (uint16_t uid) const
mathieu@2633
   386
{
mathieu@2633
   387
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   388
  return information->traceSources.size ();
mathieu@2633
   389
}
mathieu@2633
   390
std::string 
mathieu@2633
   391
IidManager::GetTraceSourceName (uint16_t uid, uint32_t i) const
mathieu@2633
   392
{
mathieu@2633
   393
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   394
  NS_ASSERT (i < information->traceSources.size ());
mathieu@2633
   395
  return information->traceSources[i].name;
mathieu@2633
   396
}
mathieu@2633
   397
std::string 
mathieu@2633
   398
IidManager::GetTraceSourceHelp (uint16_t uid, uint32_t i) const
mathieu@2633
   399
{
mathieu@2633
   400
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   401
  NS_ASSERT (i < information->traceSources.size ());
mathieu@2633
   402
  return information->traceSources[i].help;
mathieu@2633
   403
}
mathieu@2633
   404
ns3::Ptr<const ns3::TraceSourceAccessor> 
mathieu@2633
   405
IidManager::GetTraceSourceAccessor (uint16_t uid, uint32_t i) const
mathieu@2633
   406
{
mathieu@2633
   407
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   408
  NS_ASSERT (i < information->traceSources.size ());
mathieu@2633
   409
  return information->traceSources[i].accessor;
mathieu@2633
   410
}
mathieu@2633
   411
bool 
mathieu@2633
   412
IidManager::MustHideFromDocumentation (uint16_t uid) const
mathieu@2633
   413
{
mathieu@2633
   414
  struct IidInformation *information = LookupInformation (uid);
mathieu@2633
   415
  return information->mustHideFromDocumentation;
mathieu@2633
   416
}
mathieu@2633
   417
mathieu@2633
   418
} // anonymous namespace
mathieu@2633
   419
mathieu@2633
   420
namespace ns3 {
mathieu@2633
   421
mathieu@2633
   422
/*********************************************************************
mathieu@2633
   423
 *         The TypeId class
mathieu@2633
   424
 *********************************************************************/
mathieu@2633
   425
mathieu@2633
   426
TypeId::TypeId (const char *name)
mathieu@2633
   427
{
mathieu@2633
   428
  uint16_t uid = Singleton<IidManager>::Get ()->AllocateUid (name);
mathieu@2633
   429
  NS_ASSERT (uid != 0);
mathieu@2633
   430
  m_tid = uid;
mathieu@2633
   431
}
mathieu@2633
   432
mathieu@2633
   433
mathieu@2633
   434
TypeId::TypeId (uint16_t tid)
mathieu@2633
   435
  : m_tid (tid)
mathieu@2633
   436
{}
mathieu@2633
   437
TypeId 
mathieu@2633
   438
TypeId::LookupByName (std::string name)
mathieu@2633
   439
{
mathieu@2633
   440
  uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
tomh@3125
   441
  NS_ASSERT_MSG (uid != 0, "Assert in TypeId::LookupByName: " << name << " not found");
mathieu@2633
   442
  return TypeId (uid);
mathieu@2633
   443
}
mathieu@2633
   444
bool
mathieu@2633
   445
TypeId::LookupByNameFailSafe (std::string name, TypeId *tid)
mathieu@2633
   446
{
mathieu@2633
   447
  uint16_t uid = Singleton<IidManager>::Get ()->GetUid (name);
mathieu@2633
   448
  if (uid == 0)
mathieu@2633
   449
    {
mathieu@2633
   450
      return false;
mathieu@2633
   451
    }
mathieu@2633
   452
  *tid = TypeId (uid);
mathieu@2633
   453
  return true;
mathieu@2633
   454
}
mathieu@2633
   455
mathieu@2633
   456
bool
mathieu@2633
   457
TypeId::LookupAttributeByFullName (std::string fullName, struct TypeId::AttributeInfo *info)
mathieu@2633
   458
{
mathieu@2633
   459
  std::string::size_type pos = fullName.rfind ("::");
mathieu@2633
   460
  if (pos == std::string::npos)
mathieu@2633
   461
    {
mathieu@2633
   462
      return 0;
mathieu@2633
   463
    }
mathieu@2633
   464
  std::string tidName = fullName.substr (0, pos);
mathieu@2633
   465
  std::string paramName = fullName.substr (pos+2, fullName.size () - (pos+2));
mathieu@2633
   466
  TypeId tid;
mathieu@2633
   467
  bool ok = LookupByNameFailSafe (tidName, &tid);
mathieu@2633
   468
  if (!ok)
mathieu@2633
   469
    {
mathieu@2633
   470
      return false;
mathieu@2633
   471
    }
mathieu@2633
   472
  return tid.LookupAttributeByName (paramName, info);
mathieu@2633
   473
}
mathieu@2633
   474
uint32_t 
mathieu@2633
   475
TypeId::GetRegisteredN (void)
mathieu@2633
   476
{
mathieu@2633
   477
  return Singleton<IidManager>::Get ()->GetRegisteredN ();
mathieu@2633
   478
}
mathieu@2633
   479
TypeId 
mathieu@2633
   480
TypeId::GetRegistered (uint32_t i)
mathieu@2633
   481
{
mathieu@2633
   482
  return TypeId (Singleton<IidManager>::Get ()->GetRegistered (i));
mathieu@2633
   483
}
mathieu@2633
   484
mathieu@2633
   485
bool
mathieu@2633
   486
TypeId::LookupAttributeByName (std::string name, struct TypeId::AttributeInfo *info) const
mathieu@2633
   487
{
mathieu@2633
   488
  TypeId tid;
mathieu@2633
   489
  TypeId nextTid = *this;
mathieu@2633
   490
  do {
mathieu@2633
   491
    tid = nextTid;
mathieu@2693
   492
    for (uint32_t i = 0; i < tid.GetAttributeN (); i++)
mathieu@2633
   493
      {
mathieu@2633
   494
        std::string paramName = tid.GetAttributeName (i);
mathieu@2633
   495
        if (paramName == name)
mathieu@2633
   496
          {
mathieu@2633
   497
            info->accessor = tid.GetAttributeAccessor (i);
mathieu@2633
   498
            info->flags = tid.GetAttributeFlags (i);
mathieu@2633
   499
            info->initialValue = tid.GetAttributeInitialValue (i);
mathieu@2633
   500
            info->checker = tid.GetAttributeChecker (i);
mathieu@2633
   501
            return true;
mathieu@2633
   502
          }
mathieu@2633
   503
      }
mathieu@2633
   504
    nextTid = tid.GetParent ();
mathieu@2633
   505
  } while (nextTid != tid);
mathieu@2633
   506
  return false;
mathieu@2633
   507
}
mathieu@2633
   508
mathieu@2633
   509
TypeId 
mathieu@2633
   510
TypeId::SetParent (TypeId tid)
mathieu@2633
   511
{
mathieu@2633
   512
  Singleton<IidManager>::Get ()->SetParent (m_tid, tid.m_tid);
mathieu@2633
   513
  return *this;
mathieu@2633
   514
}
mathieu@2633
   515
TypeId 
mathieu@2633
   516
TypeId::SetGroupName (std::string groupName)
mathieu@2633
   517
{
mathieu@2633
   518
  Singleton<IidManager>::Get ()->SetGroupName (m_tid, groupName);
mathieu@2633
   519
  return *this;
mathieu@2633
   520
}
mathieu@2633
   521
TypeId 
mathieu@2633
   522
TypeId::GetParent (void) const
mathieu@2633
   523
{
mathieu@2633
   524
  uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_tid);
mathieu@2633
   525
  return TypeId (parent);
mathieu@2633
   526
}
mathieu@2641
   527
bool 
mathieu@3070
   528
TypeId::HasParent (void) const
mathieu@3070
   529
{
mathieu@3070
   530
  uint16_t parent = Singleton<IidManager>::Get ()->GetParent (m_tid);
mathieu@3070
   531
  return parent != m_tid;
mathieu@3070
   532
}
mathieu@3070
   533
bool 
mathieu@2641
   534
TypeId::IsChildOf (TypeId other) const
mathieu@2641
   535
{
mathieu@2641
   536
  TypeId tmp = *this;
mathieu@2645
   537
  while (tmp != other && tmp != tmp.GetParent ())
mathieu@2641
   538
    {
mathieu@2641
   539
      tmp = tmp.GetParent ();
mathieu@2641
   540
    }
mathieu@2939
   541
  return tmp == other && *this != other;
mathieu@2641
   542
}
mathieu@2633
   543
std::string 
mathieu@2633
   544
TypeId::GetGroupName (void) const
mathieu@2633
   545
{
mathieu@2633
   546
  std::string groupName = Singleton<IidManager>::Get ()->GetGroupName (m_tid);
mathieu@2633
   547
  return groupName;
mathieu@2633
   548
}
mathieu@2633
   549
mathieu@2633
   550
std::string 
mathieu@2633
   551
TypeId::GetName (void) const
mathieu@2633
   552
{
mathieu@2633
   553
  std::string name = Singleton<IidManager>::Get ()->GetName (m_tid);
mathieu@2633
   554
  return name;
mathieu@2633
   555
}
mathieu@2633
   556
mathieu@2633
   557
bool 
mathieu@2633
   558
TypeId::HasConstructor (void) const
mathieu@2633
   559
{
mathieu@2633
   560
  bool hasConstructor = Singleton<IidManager>::Get ()->HasConstructor (m_tid);
mathieu@2633
   561
  return hasConstructor;
mathieu@2633
   562
}
mathieu@2633
   563
mathieu@2633
   564
void
mathieu@2633
   565
TypeId::DoAddConstructor (Callback<ObjectBase *> cb)
mathieu@2633
   566
{
mathieu@2633
   567
  Singleton<IidManager>::Get ()->AddConstructor (m_tid, cb);
mathieu@2633
   568
}
mathieu@2633
   569
mathieu@2633
   570
TypeId 
mathieu@2633
   571
TypeId::AddAttribute (std::string name,
mathieu@2633
   572
                      std::string help, 
mathieu@2965
   573
                      const AttributeValue &initialValue,
mathieu@2633
   574
                      Ptr<const AttributeAccessor> param,
mathieu@2633
   575
                      Ptr<const AttributeChecker> checker)
mathieu@2633
   576
{
mathieu@2965
   577
  Singleton<IidManager>::Get ()->AddAttribute (m_tid, name, help, ATTR_SGC, initialValue.Copy (), param, checker);
mathieu@2633
   578
  return *this;
mathieu@2633
   579
}
mathieu@2633
   580
mathieu@2633
   581
TypeId 
mathieu@2633
   582
TypeId::AddAttribute (std::string name,
mathieu@2633
   583
                      std::string help, 
mathieu@2633
   584
                      uint32_t flags,
mathieu@2965
   585
                      const AttributeValue &initialValue,
mathieu@2633
   586
                      Ptr<const AttributeAccessor> param,
mathieu@2633
   587
                      Ptr<const AttributeChecker> checker)
mathieu@2633
   588
{
mathieu@2965
   589
  Singleton<IidManager>::Get ()->AddAttribute (m_tid, name, help, flags, initialValue.Copy (), param, checker);
mathieu@2633
   590
  return *this;
mathieu@2633
   591
}
mathieu@2633
   592
mathieu@2633
   593
Callback<ObjectBase *> 
mathieu@2633
   594
TypeId::GetConstructor (void) const
mathieu@2633
   595
{
mathieu@2633
   596
  Callback<ObjectBase *>  cb = Singleton<IidManager>::Get ()->GetConstructor (m_tid);
mathieu@2633
   597
  return cb;
mathieu@2633
   598
}
mathieu@2633
   599
mathieu@2633
   600
bool 
mathieu@2633
   601
TypeId::MustHideFromDocumentation (void) const
mathieu@2633
   602
{
mathieu@2633
   603
  bool mustHide = Singleton<IidManager>::Get ()->MustHideFromDocumentation (m_tid);
mathieu@2633
   604
  return mustHide;
mathieu@2633
   605
}
mathieu@2633
   606
mathieu@2633
   607
uint32_t 
mathieu@2693
   608
TypeId::GetAttributeN (void) const
mathieu@2633
   609
{
mathieu@2693
   610
  uint32_t n = Singleton<IidManager>::Get ()->GetAttributeN (m_tid);
mathieu@2633
   611
  return n;
mathieu@2633
   612
}
mathieu@2633
   613
std::string 
mathieu@2633
   614
TypeId::GetAttributeName (uint32_t i) const
mathieu@2633
   615
{
mathieu@2633
   616
  std::string name = Singleton<IidManager>::Get ()->GetAttributeName (m_tid, i);
mathieu@2633
   617
  return name;
mathieu@2633
   618
}
mathieu@2633
   619
std::string 
mathieu@2633
   620
TypeId::GetAttributeHelp (uint32_t i) const
mathieu@2633
   621
{
mathieu@2633
   622
  std::string help = Singleton<IidManager>::Get ()->GetAttributeHelp (m_tid, i);
mathieu@2633
   623
  return help;
mathieu@2633
   624
}
mathieu@2633
   625
std::string 
mathieu@2633
   626
TypeId::GetAttributeFullName (uint32_t i) const
mathieu@2633
   627
{
mathieu@2633
   628
  return GetName () + "::" + GetAttributeName (i);
mathieu@2633
   629
}
mathieu@2965
   630
Ptr<const AttributeValue>
mathieu@2633
   631
TypeId::GetAttributeInitialValue (uint32_t i) const
mathieu@2633
   632
{
mathieu@2965
   633
  Ptr<const AttributeValue> value = Singleton<IidManager>::Get ()->GetAttributeInitialValue (m_tid, i);
mathieu@2633
   634
  return value;
mathieu@2633
   635
}
mathieu@2633
   636
Ptr<const AttributeAccessor>
mathieu@2633
   637
TypeId::GetAttributeAccessor (uint32_t i) const
mathieu@2633
   638
{
mathieu@2633
   639
  // Used exclusively by the Object class.
mathieu@2633
   640
  Ptr<const AttributeAccessor> param = Singleton<IidManager>::Get ()->GetAttributeAccessor (m_tid, i);
mathieu@2633
   641
  return param;
mathieu@2633
   642
}
mathieu@2633
   643
uint32_t 
mathieu@2633
   644
TypeId::GetAttributeFlags (uint32_t i) const
mathieu@2633
   645
{
mathieu@2633
   646
  // Used exclusively by the Object class.
mathieu@2633
   647
  uint32_t flags = Singleton<IidManager>::Get ()->GetAttributeFlags (m_tid, i);
mathieu@2633
   648
  return flags;
mathieu@2633
   649
}
mathieu@2633
   650
Ptr<const AttributeChecker>
mathieu@2633
   651
TypeId::GetAttributeChecker (uint32_t i) const
mathieu@2633
   652
{
mathieu@2633
   653
  // Used exclusively by the Object class.
mathieu@2633
   654
  Ptr<const AttributeChecker> checker = Singleton<IidManager>::Get ()->GetAttributeChecker (m_tid, i);
mathieu@2633
   655
  return checker;
mathieu@2633
   656
}
mathieu@2633
   657
mathieu@2633
   658
uint32_t 
mathieu@2633
   659
TypeId::GetTraceSourceN (void) const
mathieu@2633
   660
{
mathieu@2633
   661
  return Singleton<IidManager>::Get ()->GetTraceSourceN (m_tid);
mathieu@2633
   662
}
mathieu@2633
   663
std::string 
mathieu@2633
   664
TypeId::GetTraceSourceName (uint32_t i) const
mathieu@2633
   665
{
mathieu@2633
   666
  return Singleton<IidManager>::Get ()->GetTraceSourceName (m_tid, i);
mathieu@2633
   667
}
mathieu@2633
   668
std::string 
mathieu@2633
   669
TypeId::GetTraceSourceHelp (uint32_t i) const
mathieu@2633
   670
{
mathieu@2633
   671
  return Singleton<IidManager>::Get ()->GetTraceSourceHelp (m_tid, i);
mathieu@2633
   672
}
mathieu@2633
   673
Ptr<const TraceSourceAccessor> 
mathieu@2633
   674
TypeId::GetTraceSourceAccessor (uint32_t i) const
mathieu@2633
   675
{
mathieu@2633
   676
  return Singleton<IidManager>::Get ()->GetTraceSourceAccessor (m_tid, i);
mathieu@2633
   677
}
mathieu@2633
   678
mathieu@2633
   679
TypeId 
mathieu@2633
   680
TypeId::AddTraceSource (std::string name,
mathieu@2633
   681
                        std::string help,
mathieu@2633
   682
                        Ptr<const TraceSourceAccessor> accessor)
mathieu@2633
   683
{
mathieu@2633
   684
  Singleton<IidManager>::Get ()->AddTraceSource (m_tid, name, help, accessor);
mathieu@2633
   685
  return *this;
mathieu@2633
   686
}
mathieu@2633
   687
mathieu@2633
   688
TypeId 
mathieu@2633
   689
TypeId::HideFromDocumentation (void)
mathieu@2633
   690
{
mathieu@2633
   691
  Singleton<IidManager>::Get ()->HideFromDocumentation (m_tid);
mathieu@2633
   692
  return *this;
mathieu@2633
   693
}
mathieu@2633
   694
mathieu@2633
   695
mathieu@2633
   696
Ptr<const TraceSourceAccessor> 
mathieu@2633
   697
TypeId::LookupTraceSourceByName (std::string name) const
mathieu@2633
   698
{
mathieu@2633
   699
  TypeId tid;
mathieu@2633
   700
  TypeId nextTid = *this;
mathieu@2633
   701
  do {
mathieu@2633
   702
    tid = nextTid;
mathieu@2633
   703
    for (uint32_t i = 0; i < tid.GetTraceSourceN (); i++)
mathieu@2633
   704
      {
mathieu@2633
   705
        std::string srcName = tid.GetTraceSourceName (i);
mathieu@2633
   706
        if (srcName == name)
mathieu@2633
   707
          {
mathieu@2633
   708
            return tid.GetTraceSourceAccessor (i);
mathieu@2633
   709
          }
mathieu@2633
   710
      }
mathieu@2633
   711
    nextTid = tid.GetParent ();
mathieu@2633
   712
  } while (nextTid != tid);
mathieu@2633
   713
  return 0;
mathieu@2633
   714
}
mathieu@2633
   715
mathieu@2644
   716
uint16_t 
mathieu@2644
   717
TypeId::GetUid (void) const
mathieu@2644
   718
{
mathieu@2644
   719
  return m_tid;
mathieu@2644
   720
}
mathieu@2644
   721
void 
mathieu@2644
   722
TypeId::SetUid (uint16_t tid)
mathieu@2644
   723
{
mathieu@2644
   724
  m_tid = tid;
mathieu@2644
   725
}
mathieu@2644
   726
mathieu@2633
   727
std::ostream & operator << (std::ostream &os, TypeId tid)
mathieu@2633
   728
{
mathieu@2633
   729
  os << tid.GetName ();
mathieu@2633
   730
  return os;
mathieu@2633
   731
}
mathieu@2633
   732
std::istream & operator >> (std::istream &is, TypeId &tid)
mathieu@2633
   733
{
mathieu@2633
   734
  std::string tidString;
mathieu@2633
   735
  is >> tidString;
mathieu@2633
   736
  bool ok = TypeId::LookupByNameFailSafe (tidString, &tid);
mathieu@2633
   737
  if (!ok)
mathieu@2633
   738
    {
mathieu@2633
   739
      is.setstate (std::ios_base::badbit);
mathieu@2633
   740
    }
mathieu@2633
   741
  return is;
mathieu@2633
   742
}
mathieu@2633
   743
mathieu@2633
   744
mathieu@2633
   745
ATTRIBUTE_HELPER_CPP (TypeId);
mathieu@2633
   746
mathieu@2939
   747
bool operator < (TypeId a, TypeId b)
mathieu@2939
   748
{
mathieu@2939
   749
  return a.m_tid < b.m_tid;
mathieu@2939
   750
}
mathieu@2939
   751
mathieu@2633
   752
} // namespace ns3