src/core/object.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sun, 02 Mar 2008 21:00:37 +0100
changeset 2531 b451b5fc8b57
parent 2522 7c9d1c314beb
child 2542 a9b88fdc09d6
permissions -rw-r--r--
implement context-based trace connection
mathieu@699
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@699
     2
/*
mathieu@699
     3
 * Copyright (c) 2007 INRIA, Gustavo Carneiro
mathieu@699
     4
 *
mathieu@699
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@699
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@699
     7
 * published by the Free Software Foundation;
mathieu@699
     8
 *
mathieu@699
     9
 * This program is distributed in the hope that it will be useful,
mathieu@699
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@699
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@699
    12
 * GNU General Public License for more details.
mathieu@699
    13
 *
mathieu@699
    14
 * You should have received a copy of the GNU General Public License
mathieu@699
    15
 * along with this program; if not, write to the Free Software
mathieu@699
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@699
    17
 *
mathieu@699
    18
 * Authors: Gustavo Carneiro <gjcarneiro@gmail.com>,
mathieu@699
    19
 *          Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@699
    20
 */
mathieu@706
    21
#ifndef OBJECT_H
mathieu@706
    22
#define OBJECT_H
mathieu@699
    23
mathieu@699
    24
#include <stdint.h>
mathieu@699
    25
#include <string>
mathieu@699
    26
#include "ptr.h"
mathieu@1371
    27
#include "trace-resolver.h"
mathieu@2237
    28
#include "callback.h"
mathieu@2438
    29
#include "attribute.h"
mathieu@2372
    30
#include "object-base.h"
mathieu@2489
    31
#include "attribute-helper.h"
mathieu@699
    32
mathieu@2246
    33
#define NS_OBJECT_ENSURE_REGISTERED(type)       \
mathieu@2246
    34
  static struct X##type##RegistrationClass      \
mathieu@2246
    35
  {                                             \
mathieu@2246
    36
    X##type##RegistrationClass () {             \
mathieu@2372
    37
      ns3::TypeId tid = type::GetTypeId ();     \
mathieu@2252
    38
      tid.GetParent ();                         \
mathieu@2246
    39
    }                                           \
mathieu@2246
    40
} x_##type##RegistrationVariable
mathieu@2246
    41
mathieu@2246
    42
mathieu@699
    43
namespace ns3 {
mathieu@699
    44
mathieu@1340
    45
class TraceContext;
mathieu@1336
    46
class CallbackBase;
mathieu@2237
    47
class Object;
mathieu@2436
    48
class AttributeAccessor;
mathieu@2437
    49
class AttributeValue;
mathieu@2459
    50
class AttributeList;
mathieu@2463
    51
class TraceSourceAccessor;
mathieu@1336
    52
mathieu@718
    53
/**
mathieu@718
    54
 * \brief a unique identifier for an interface.
mathieu@718
    55
 *
mathieu@2372
    56
 * This class records a lot of meta-information about a 
mathieu@2372
    57
 * subclass of the Object base class:
mathieu@2372
    58
 *  - the base class of the subclass
mathieu@2372
    59
 *  - the set of accessible constructors in the subclass
mathieu@2458
    60
 *  - the set of 'attributes' accessible in the subclass
mathieu@718
    61
 */
mathieu@2250
    62
class TypeId
mathieu@699
    63
{
mathieu@699
    64
public:
mathieu@2372
    65
  enum {
mathieu@2432
    66
    ATTR_GET = 1<<0,
mathieu@2432
    67
    ATTR_SET = 1<<1,
mathieu@2432
    68
    ATTR_CONSTRUCT = 1<<2,
mathieu@2432
    69
    ATTR_SGC = ATTR_GET | ATTR_SET | ATTR_CONSTRUCT,
mathieu@2372
    70
  };
mathieu@2372
    71
mathieu@718
    72
  /**
mathieu@718
    73
   * \param name the name of the requested interface
mathieu@718
    74
   * \returns the unique id associated with the requested
mathieu@718
    75
   *          name. 
mathieu@718
    76
   *
mathieu@718
    77
   * This method cannot fail: it will crash if the input 
mathieu@718
    78
   * name is not a valid interface name.
mathieu@718
    79
   */
mathieu@2250
    80
  static TypeId LookupByName (std::string name);
mathieu@2372
    81
mathieu@2372
    82
  /**
mathieu@2372
    83
   * \returns the number of TypeId instances constructed
mathieu@2372
    84
   */
mathieu@2239
    85
  static uint32_t GetRegisteredN (void);
mathieu@2372
    86
  /**
mathieu@2372
    87
   * \param i index
mathieu@2372
    88
   * \returns the TypeId instance whose index is \i.
mathieu@2372
    89
   */
mathieu@2250
    90
  static TypeId GetRegistered (uint32_t i);
mathieu@2372
    91
mathieu@718
    92
  /**
mathieu@2372
    93
   * \param name the name of the interface to construct.
mathieu@2372
    94
   *
mathieu@2372
    95
   * No two instances can share the same name.
mathieu@2372
    96
   */
mathieu@2489
    97
  TypeId (const char * name);
mathieu@2372
    98
mathieu@2372
    99
  /**
mathieu@2372
   100
   * \returns the parent of this TypeId
mathieu@718
   101
   *
mathieu@2339
   102
   * This method cannot fail. It will return itself
mathieu@2339
   103
   * if this TypeId has no parent. i.e., it is at the top
mathieu@2339
   104
   * of the TypeId hierarchy. Currently, this is the
mathieu@2339
   105
   * case for the TypeId associated to the Object class 
mathieu@2339
   106
   * only.
mathieu@718
   107
   */
mathieu@2250
   108
  TypeId GetParent (void) const;
mathieu@1371
   109
mathieu@1371
   110
  /**
mathieu@2372
   111
   * \returns the name of the group associated to this TypeId.
mathieu@2372
   112
   */
mathieu@2372
   113
  std::string GetGroupName (void) const;
mathieu@2372
   114
  /**
mathieu@2372
   115
   * \returns the fully-qualified C++ typename of this TypeId.
mathieu@2372
   116
   */
mathieu@2372
   117
  std::string GetTypeName (void) const;
mathieu@2372
   118
mathieu@2372
   119
  /**
mathieu@1371
   120
   * \returns the name of this interface.
mathieu@1371
   121
   */
mathieu@1371
   122
  std::string GetName (void) const;
mathieu@2237
   123
mathieu@2241
   124
  /**
mathieu@2250
   125
   * \returns true if this TypeId has a constructor
mathieu@2241
   126
   */
mathieu@2241
   127
  bool HasConstructor (void) const;
mathieu@2241
   128
mathieu@2372
   129
  /**
mathieu@2458
   130
   * \returns the number of attributes associated to this TypeId
mathieu@2372
   131
   */
mathieu@2459
   132
  uint32_t GetAttributeListN (void) const;
mathieu@2372
   133
  /**
mathieu@2458
   134
   * \param i index into attribute array
mathieu@2458
   135
   * \returns the name associated to the attribute whose
mathieu@2372
   136
   *          index is \i i.
mathieu@2372
   137
   */
mathieu@2458
   138
  std::string GetAttributeName (uint32_t i) const;
mathieu@2372
   139
  /**
mathieu@2458
   140
   * \param i index into attribute array
mathieu@2458
   141
   * \returns the full name associated to the attribute whose
mathieu@2372
   142
   *          index is \i i.
mathieu@2372
   143
   */
mathieu@2458
   144
  std::string GetAttributeFullName (uint32_t i) const;
mathieu@2237
   145
mathieu@2458
   146
  Attribute GetAttributeInitialValue (uint32_t i) const;
mathieu@2424
   147
mathieu@2463
   148
  uint32_t GetTraceSourceN (void) const;
mathieu@2463
   149
  std::string GetTraceSourceName (uint32_t i) const;
mathieu@2463
   150
  std::string GetTraceSourceHelp (uint32_t i) const;
mathieu@2463
   151
  Ptr<const TraceSourceAccessor> GetTraceSourceAccessor (uint32_t i) const;
mathieu@2463
   152
mathieu@2459
   153
  Ptr<Object> CreateObject (const AttributeList &attributes) const;
mathieu@2372
   154
mathieu@2372
   155
mathieu@2372
   156
  Ptr<Object> CreateObject (void) const;
mathieu@2372
   157
  template <typename T1>
mathieu@2372
   158
  Ptr<Object> CreateObject (T1 a1) const;
mathieu@2372
   159
  template <typename T1, typename T2>
mathieu@2372
   160
  Ptr<Object> CreateObject (T1 a1, T2 a2) const;
mathieu@2372
   161
mathieu@2372
   162
mathieu@2372
   163
  /**
mathieu@2372
   164
   * \param tid the TypeId of the base class.
mathieu@2372
   165
   * \return this TypeId instance.
mathieu@2372
   166
   *
mathieu@2372
   167
   * Record in this TypeId which TypeId is the TypeId
mathieu@2372
   168
   * of the base class of the subclass.
mathieu@2372
   169
   */
mathieu@2252
   170
  TypeId SetParent (TypeId tid);
mathieu@2372
   171
  /**
mathieu@2372
   172
   * \return this TypeId instance.
mathieu@2372
   173
   *
mathieu@2372
   174
   * Record in this TypeId which TypeId is the TypeId
mathieu@2372
   175
   * of the base class of the subclass.
mathieu@2372
   176
   */
mathieu@2237
   177
  template <typename T>
mathieu@2250
   178
  TypeId SetParent (void);
mathieu@2238
   179
mathieu@2372
   180
  /**
mathieu@2372
   181
   * \param groupName the name of the group this TypeId belongs to.
mathieu@2372
   182
   * \returns this TypeId instance.
mathieu@2372
   183
   *
mathieu@2372
   184
   * The group name is purely an advisory information used to 
mathieu@2372
   185
   * group together types according to a user-specific grouping
mathieu@2372
   186
   * scheme.
mathieu@2372
   187
   */
mathieu@2372
   188
  TypeId SetGroupName (std::string groupName);
mathieu@2372
   189
  
mathieu@2372
   190
  /**
mathieu@2372
   191
   * \param typeName the fully-qualified C++ typename of this TypeId.
mathieu@2372
   192
   * \returns this TypeId instance.
mathieu@2372
   193
   */
mathieu@2372
   194
  TypeId SetTypeName (std::string typeName);
mathieu@2372
   195
mathieu@2372
   196
  /**
mathieu@2372
   197
   * \returns this TypeId instance
mathieu@2372
   198
   *
mathieu@2372
   199
   * Record in this TypeId the fact that the default constructor
mathieu@2372
   200
   * is accessible.
mathieu@2372
   201
   */
mathieu@2238
   202
  template <typename T>
mathieu@2250
   203
  TypeId AddConstructor (void);
mathieu@2372
   204
mathieu@2237
   205
  template <typename T, typename T1>
mathieu@2250
   206
  TypeId AddConstructor (void);
mathieu@2372
   207
mathieu@2237
   208
  template <typename T, typename T1, typename T2>
mathieu@2250
   209
  TypeId AddConstructor (void);
mathieu@2237
   210
mathieu@2372
   211
  /**
mathieu@2458
   212
   * \param name the name of the new attribute
mathieu@2372
   213
   * \param help some help text which describes the purpose of this
mathieu@2458
   214
   *        attribute
mathieu@2435
   215
   * \param param an instance of the associated Accessor subclass
mathieu@2372
   216
   * \returns this TypeId instance
mathieu@2372
   217
   *
mathieu@2458
   218
   * Record in this TypeId the fact that a new attribute exists.
mathieu@2372
   219
   */
mathieu@2458
   220
  TypeId AddAttribute (std::string name,
mathieu@2424
   221
                       std::string help, 
mathieu@2433
   222
                       Attribute initialValue,
mathieu@2436
   223
                       Ptr<const AttributeAccessor> spec,
mathieu@2427
   224
                       Ptr<const AttributeChecker> checker);
mathieu@2237
   225
mathieu@2372
   226
  /**
mathieu@2458
   227
   * \param name the name of the new attribute
mathieu@2372
   228
   * \param help some help text which describes the purpose of this
mathieu@2458
   229
   *        attribute
mathieu@2458
   230
   * \param flags flags which describe how this attribute can be read and/or written.
mathieu@2435
   231
   * \param param an instance of the associated Accessor subclass
mathieu@2372
   232
   * \returns this TypeId instance
mathieu@2372
   233
   *
mathieu@2458
   234
   * Record in this TypeId the fact that a new attribute exists.
mathieu@2372
   235
   */
mathieu@2458
   236
  TypeId AddAttribute (std::string name,
mathieu@2372
   237
                       std::string help, 
mathieu@2372
   238
                       uint32_t flags,
mathieu@2433
   239
                       Attribute initialValue,
mathieu@2463
   240
                       Ptr<const AttributeAccessor> accessor,
mathieu@2427
   241
                       Ptr<const AttributeChecker> checker);
mathieu@2237
   242
mathieu@2463
   243
  TypeId AddTraceSource (std::string name,
mathieu@2463
   244
                         std::string help,
mathieu@2463
   245
                         Ptr<const TraceSourceAccessor> accessor);
mathieu@2463
   246
mathieu@2471
   247
  struct AttributeInfo {
mathieu@2471
   248
    Ptr<const AttributeAccessor> accessor;
mathieu@2471
   249
    Attribute initialValue;
mathieu@2471
   250
    uint32_t flags;
mathieu@2471
   251
    Ptr<const AttributeChecker> checker;
mathieu@2471
   252
  };
mathieu@2471
   253
  /**
mathieu@2471
   254
   * \param name the name of the requested attribute
mathieu@2471
   255
   */
mathieu@2471
   256
  bool LookupAttributeByName (std::string name, struct AttributeInfo *info) const;
mathieu@2471
   257
mathieu@2471
   258
mathieu@2394
   259
  // construct an invalid TypeId.
mathieu@2394
   260
  TypeId ();
mathieu@2250
   261
  ~TypeId ();
mathieu@2489
   262
mathieu@2489
   263
  VALUE_HELPER_HEADER_1 (TypeId);
mathieu@699
   264
private:
mathieu@2372
   265
  friend class Object;
mathieu@2459
   266
  friend class AttributeList;
mathieu@2250
   267
  friend bool operator == (TypeId a, TypeId b);
mathieu@2250
   268
  friend bool operator != (TypeId a, TypeId b);
mathieu@2237
   269
mathieu@2382
   270
mathieu@2463
   271
  Ptr<const TraceSourceAccessor> LookupTraceSourceByName (std::string name) const;
mathieu@2463
   272
mathieu@2372
   273
  /**
mathieu@2458
   274
   * \param i the position of the requested attribute
mathieu@2458
   275
   * \returns the Accessor associated to the requested attribute
mathieu@2372
   276
   */
mathieu@2458
   277
  bool LookupAttributeByPosition (uint32_t i, struct AttributeInfo *info) const;
mathieu@2372
   278
  /**
mathieu@2458
   279
   * \param fullName the full name of the requested attribute
mathieu@2458
   280
   * \returns the Accessor associated to the requested attribute
mathieu@2372
   281
   */
mathieu@2458
   282
  static bool LookupAttributeByFullName (std::string fullName, struct AttributeInfo *info);
mathieu@2372
   283
mathieu@2252
   284
  explicit TypeId (uint16_t tid);
mathieu@2237
   285
  void DoAddConstructor (CallbackBase callback, uint32_t nArguments);
mathieu@2372
   286
  CallbackBase LookupConstructor (uint32_t nArguments) const;
mathieu@2458
   287
  Ptr<const AttributeAccessor> GetAttributeAccessor (uint32_t i) const;
mathieu@2458
   288
  uint32_t GetAttributeFlags (uint32_t i) const;
mathieu@2458
   289
  Ptr<const AttributeChecker> GetAttributeChecker (uint32_t i) const;
mathieu@2237
   290
  
mathieu@2252
   291
  uint16_t m_tid;
mathieu@699
   292
};
mathieu@699
   293
mathieu@2489
   294
std::ostream & operator << (std::ostream &os, TypeId tid);
mathieu@2489
   295
std::istream & operator >> (std::istream &is, TypeId &tid);
mathieu@2489
   296
mathieu@2489
   297
VALUE_HELPER_HEADER_2 (TypeId);
mathieu@2372
   298
mathieu@2372
   299
/**
mathieu@2458
   300
 * \brief a container of attributes to be used during object's construction
mathieu@2372
   301
 *        and in ns3::Object::Set.
mathieu@2372
   302
 *
mathieu@2372
   303
 */
mathieu@2459
   304
class AttributeList
mathieu@2372
   305
{
mathieu@2372
   306
public:
mathieu@2459
   307
  AttributeList ();
mathieu@2459
   308
  AttributeList (const AttributeList &o);
mathieu@2459
   309
  AttributeList &operator = (const AttributeList &o);
mathieu@2459
   310
  ~AttributeList ();
mathieu@2372
   311
  /**
mathieu@2458
   312
   * \param name the name of the attribute to set
mathieu@2372
   313
   * \param value the value to set
mathieu@2372
   314
   *
mathieu@2458
   315
   * This method checks that a attribute with the requested
mathieu@2372
   316
   * name exists and that the value specified is an acceptable
mathieu@2458
   317
   * value of that attribute. If any of these checks fails,
mathieu@2372
   318
   * the program terminates with a message.
mathieu@2372
   319
   */
mathieu@2433
   320
  bool Set (std::string name, Attribute value);
mathieu@2372
   321
mathieu@2522
   322
  bool SetWithTid (TypeId tid, std::string name, Attribute value);
mathieu@2522
   323
  bool SetWithTid (TypeId tid, uint32_t position, Attribute value);
mathieu@2372
   324
mathieu@2372
   325
  /**
mathieu@2372
   326
   * Clear the content of this instance.
mathieu@2372
   327
   */
mathieu@2372
   328
  void Reset (void);
mathieu@2372
   329
mathieu@2372
   330
  /**
mathieu@2458
   331
   * \returns the global attribute container
mathieu@2372
   332
   *
mathieu@2458
   333
   * The global attribute container can be used to specify
mathieu@2458
   334
   * a set of attribute values without having to re-specify
mathieu@2372
   335
   * them for each object when it is created. This container
mathieu@2372
   336
   * is checked only during object construction and 
mathieu@2372
   337
   * it is always checked last, after any per-object
mathieu@2372
   338
   * container is checked.
mathieu@2372
   339
   */
mathieu@2459
   340
  static AttributeList *GetGlobal (void);
mathieu@2372
   341
mathieu@2372
   342
  std::string SerializeToString (void) const;
mathieu@2372
   343
  bool DeserializeFromString (std::string value);
mathieu@2372
   344
private:
mathieu@2372
   345
  friend class Object;
mathieu@2458
   346
  struct Attr {
mathieu@2427
   347
    Ptr<const AttributeChecker> checker;
mathieu@2433
   348
    Attribute value;
mathieu@2372
   349
  };
mathieu@2458
   350
  typedef std::vector<struct Attr> Attrs;
mathieu@2458
   351
  typedef Attrs::iterator Iterator;
mathieu@2458
   352
  typedef Attrs::const_iterator CIterator;
mathieu@2372
   353
mathieu@2372
   354
mathieu@2372
   355
mathieu@2458
   356
  bool DoSet (struct TypeId::AttributeInfo *info, Attribute param);
mathieu@2433
   357
  void DoSetOne (Ptr<const AttributeChecker> checker, Attribute param);
mathieu@2458
   358
  std::string LookupAttributeFullNameByChecker (Ptr<const AttributeChecker> checker) const;
mathieu@2372
   359
mathieu@2458
   360
  Attrs m_attributes;
mathieu@2372
   361
};
mathieu@2372
   362
mathieu@2372
   363
mathieu@718
   364
/**
mathieu@718
   365
 * \brief a base class which provides memory management and object aggregation
mathieu@718
   366
 *
mathieu@718
   367
 */
mathieu@2370
   368
class Object : public ObjectBase
mathieu@699
   369
{
mathieu@699
   370
public:
mathieu@2251
   371
  static TypeId GetTypeId (void);
mathieu@699
   372
mathieu@706
   373
  Object ();
mathieu@706
   374
  virtual ~Object ();
mathieu@2372
   375
mathieu@2372
   376
  /**
mathieu@2458
   377
   * \param name the name of the attribute to set
mathieu@2458
   378
   * \param value the name of the attribute to set
mathieu@2372
   379
   *
mathieu@2458
   380
   * Set a single attribute.
mathieu@2372
   381
   */
mathieu@2460
   382
  bool SetAttribute (std::string name, Attribute value);
mathieu@2372
   383
  /**
mathieu@2458
   384
   * \param name the name of the attribute to read
mathieu@2372
   385
   * \param value a reference to the string where the value of the 
mathieu@2458
   386
   *        attribute should be stored.
mathieu@2458
   387
   * \returns true if the requested attribute was found, false otherwise.
mathieu@2372
   388
   */
mathieu@2460
   389
  bool GetAttribute (std::string name, std::string &value) const;
mathieu@2372
   390
  /**
mathieu@2458
   391
   * \param name the name of the attribute to read
mathieu@2372
   392
   * \param value a reference to the object where the value of the 
mathieu@2458
   393
   *        attribute should be stored.
mathieu@2458
   394
   * \returns true if the requested attribute was found, false otherwise.
mathieu@2372
   395
   */
mathieu@2460
   396
  Attribute GetAttribute (std::string name) const;
mathieu@2372
   397
mathieu@2463
   398
  bool TraceSourceConnect (std::string name, const CallbackBase &cb);
mathieu@2531
   399
  bool TraceSourceConnectWithContext (std::string name, std::string context, const CallbackBase &cb);
mathieu@2463
   400
  bool TraceSourceDisconnect (std::string name, const CallbackBase &cb);
mathieu@2463
   401
mathieu@2471
   402
  TypeId GetRealTypeId (void) const;
mathieu@2471
   403
mathieu@718
   404
  /**
mathieu@718
   405
   * Increment the reference count. This method should not be called
mathieu@718
   406
   * by user code. Object instances are expected to be used in conjunction
mathieu@718
   407
   * of the Ptr template which would make calling Ref unecessary and 
mathieu@718
   408
   * dangerous.
mathieu@718
   409
   */
mathieu@706
   410
  inline void Ref (void) const;
mathieu@718
   411
  /**
mathieu@718
   412
   * Decrement the reference count. This method should not be called
mathieu@718
   413
   * by user code. Object instances are expected to be used in conjunction
mathieu@718
   414
   * of the Ptr template which would make calling Ref unecessary and 
mathieu@718
   415
   * dangerous.
mathieu@718
   416
   */
mathieu@706
   417
  inline void Unref (void) const;
mathieu@718
   418
  /**
mathieu@718
   419
   * \returns a pointer to the requested interface or zero if it could not be found.
mathieu@2231
   420
   */
mathieu@2231
   421
  template <typename T>
mathieu@2257
   422
  Ptr<T> GetObject (void) const;
mathieu@2231
   423
  /**
mathieu@2252
   424
   * \param tid the interface id of the requested interface
mathieu@2231
   425
   * \returns a pointer to the requested interface or zero if it could not be found.
mathieu@718
   426
   */
mathieu@699
   427
  template <typename T>
mathieu@2257
   428
  Ptr<T> GetObject (TypeId tid) const;
mathieu@718
   429
  /**
mathieu@718
   430
   * Run the DoDispose methods of this object and all the
mathieu@718
   431
   * objects aggregated to it.
mathieu@718
   432
   * After calling this method, the object is expected to be
mathieu@718
   433
   * totally unusable except for the Ref and Unref methods.
mathieu@718
   434
   * It is an error to call Dispose twice on the same object 
mathieu@718
   435
   * instance
mathieu@718
   436
   */
mathieu@699
   437
  void Dispose (void);
mathieu@718
   438
  /**
mathieu@718
   439
   * \param other another object pointer
mathieu@718
   440
   *
mathieu@718
   441
   * This method aggregates the two objects together: after this
mathieu@2257
   442
   * method returns, it becomes possible to call GetObject
mathieu@718
   443
   * on one to get the other, and vice-versa. 
mathieu@718
   444
   */
mathieu@2258
   445
  void AggregateObject (Ptr<Object> other);
mathieu@1330
   446
mathieu@1380
   447
  /**
mathieu@1380
   448
   * \param path the path to match for the callback
mathieu@1380
   449
   * \param cb callback to connect
mathieu@1380
   450
   *
mathieu@1380
   451
   * Connect the input callback to all trace sources which
mathieu@1380
   452
   * match the input path.
mathieu@1380
   453
   *
mathieu@1380
   454
   */
mathieu@1383
   455
  void TraceConnect (std::string path, const CallbackBase &cb) const;
mathieu@1380
   456
  /**
mathieu@1380
   457
   * \param path the path to match for the callback
mathieu@1380
   458
   * \param cb callback to disconnect
mathieu@1380
   459
   *
mathieu@1380
   460
   * Disconnect the input callback from all trace sources which
mathieu@1380
   461
   * match the input path.
mathieu@1380
   462
   */
mathieu@1383
   463
  void TraceDisconnect (std::string path, const CallbackBase &cb) const;
mathieu@1380
   464
  /**
mathieu@1380
   465
   * \returns the trace resolver associated to this object.
mathieu@1380
   466
   *
mathieu@1380
   467
   * This method should be rarely called by users.
mathieu@1380
   468
   */
mathieu@1383
   469
  virtual Ptr<TraceResolver> GetTraceResolver (void) const;
mathieu@699
   470
protected:
mathieu@718
   471
  /**
mathieu@2230
   472
   * This method is called by Object::Dispose.
mathieu@2230
   473
   * Subclasses are expected to override this method and chain
mathieu@2230
   474
   * up to their parent's implementation once they are done.
mathieu@2230
   475
   */
mathieu@2230
   476
  virtual void DoDispose (void);
mathieu@2372
   477
  virtual void NotifyConstructionCompleted (void);
mathieu@2230
   478
private:
mathieu@2250
   479
  friend class TypeIdTraceResolver;
mathieu@2372
   480
mathieu@2372
   481
  template <typename T>
mathieu@2459
   482
  friend Ptr<T> CreateObject (const AttributeList &attributes);
mathieu@2372
   483
mathieu@2230
   484
  template <typename T>
mathieu@2230
   485
  friend Ptr<T> CreateObject (void);
mathieu@2230
   486
  template <typename T, typename T1>
mathieu@2230
   487
  friend Ptr<T> CreateObject (T1 a1);
mathieu@2230
   488
  template <typename T, typename T1, typename T2>
mathieu@2230
   489
  friend Ptr<T> CreateObject (T1 a1, T2 a2);
mathieu@2230
   490
  template <typename T, typename T1, typename T2, typename T3>
mathieu@2230
   491
  friend Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3);
mathieu@2230
   492
  template <typename T, typename T1, typename T2, typename T3, typename T4>
mathieu@2230
   493
  friend Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4);
mathieu@2230
   494
  template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
mathieu@2230
   495
  friend Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
mathieu@2230
   496
  template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
mathieu@2230
   497
  friend Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6);
mathieu@2230
   498
  template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
mathieu@2230
   499
  friend Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7);
mathieu@2230
   500
mathieu@2372
   501
mathieu@2436
   502
  bool DoSet (Ptr<const AttributeAccessor> spec, Attribute intialValue, 
mathieu@2433
   503
              Ptr<const AttributeChecker> checker, Attribute value);
mathieu@2257
   504
  Ptr<Object> DoGetObject (TypeId tid) const;
mathieu@2230
   505
  void DoCollectSources (std::string path, const TraceContext &context, 
mathieu@2230
   506
                         TraceResolver::SourceCollection *collection) const;
mathieu@2230
   507
  void DoTraceAll (std::ostream &os, const TraceContext &context) const;
mathieu@2230
   508
  bool Check (void) const;
mathieu@2230
   509
  bool CheckLoose (void) const;
mathieu@2372
   510
  /**
mathieu@2372
   511
   * Attempt to delete this object. This method iterates
mathieu@2372
   512
   * over all aggregated objects to check if they all 
mathieu@2372
   513
   * have a zero refcount. If yes, the object and all
mathieu@2372
   514
   * its aggregates are deleted. If not, nothing is done.
mathieu@2372
   515
   */
mathieu@2230
   516
  void MaybeDelete (void) const;
mathieu@2230
   517
  /**
mathieu@2252
   518
   * \param tid an TypeId
mathieu@718
   519
   *
mathieu@2372
   520
   * Invoked from ns3::CreateObject only.
mathieu@2372
   521
   * Initialize the m_tid member variable to
mathieu@2372
   522
   * keep track of the type of this object instance.
mathieu@718
   523
   */
mathieu@2252
   524
  void SetTypeId (TypeId tid);
mathieu@2372
   525
  /**
mathieu@2458
   526
   * \param attributes the attribute values used to initialize 
mathieu@2372
   527
   *        the member variables of this object's instance.
mathieu@2372
   528
   *
mathieu@2372
   529
   * Invoked from ns3::CreateObject only.
mathieu@2372
   530
   * Initialize all the member variables which were
mathieu@2372
   531
   * registered with the associated TypeId.
mathieu@2372
   532
   */
mathieu@2459
   533
  void Construct (const AttributeList &attributes);
mathieu@2230
   534
mathieu@2372
   535
  /**
mathieu@2372
   536
   * The reference count for this object. Each aggregate
mathieu@2372
   537
   * has an individual reference count. When the global
mathieu@2372
   538
   * reference count (the sum of all reference counts) 
mathieu@2372
   539
   * reaches zero, the object and all its aggregates is 
mathieu@2372
   540
   * deleted.
mathieu@2372
   541
   */
mathieu@706
   542
  mutable uint32_t m_count;
mathieu@2372
   543
  /**
mathieu@2372
   544
   * Identifies the type of this object instance.
mathieu@2372
   545
   */
mathieu@2252
   546
  TypeId m_tid;
mathieu@2372
   547
  /**
mathieu@2372
   548
   * Set to true when the DoDispose method of the object
mathieu@2372
   549
   * has run, false otherwise.
mathieu@2372
   550
   */
mathieu@713
   551
  bool m_disposed;
mathieu@1383
   552
  mutable bool m_collecting;
mathieu@2372
   553
  /**
mathieu@2372
   554
   * A pointer to the next aggregate object. This is a circular
mathieu@2372
   555
   * linked list of aggregated objects: the last one points
mathieu@2372
   556
   * back to the first one. If an object is not aggregated to
mathieu@2372
   557
   * any other object, the value of this field is equal to the
mathieu@2372
   558
   * value of the 'this' pointer.
mathieu@2372
   559
   */
mathieu@706
   560
  Object *m_next;
mathieu@699
   561
};
mathieu@699
   562
mathieu@699
   563
} // namespace ns3
mathieu@699
   564
mathieu@699
   565
namespace ns3 {
mathieu@699
   566
mathieu@2372
   567
/*************************************************************************
mathieu@2372
   568
 *   The TypeId implementation which depends on templates
mathieu@2372
   569
 *************************************************************************/
mathieu@2372
   570
mathieu@2238
   571
template <typename T>
mathieu@2250
   572
TypeId 
mathieu@2250
   573
TypeId::SetParent (void)
mathieu@2238
   574
{
mathieu@2251
   575
  return SetParent (T::GetTypeId ());
mathieu@2238
   576
}
mathieu@2237
   577
mathieu@2237
   578
template <typename T>
mathieu@2250
   579
TypeId 
mathieu@2250
   580
TypeId::AddConstructor (void)
mathieu@2237
   581
{
mathieu@2237
   582
  struct Maker {
mathieu@2459
   583
    static Ptr<Object> Create (const AttributeList &attributes) {
mathieu@2458
   584
      return ns3::CreateObject<T> (attributes);
mathieu@2237
   585
    }
mathieu@2237
   586
  };
mathieu@2237
   587
  CallbackBase cb = MakeCallback (&Maker::Create);
mathieu@2237
   588
  DoAddConstructor (cb, 0);
mathieu@2237
   589
  return *this;
mathieu@2237
   590
}
mathieu@2237
   591
template <typename T, typename T1>
mathieu@2250
   592
TypeId 
mathieu@2250
   593
TypeId::AddConstructor (void)
mathieu@2237
   594
{
mathieu@2237
   595
  struct Maker {
mathieu@2237
   596
    static Ptr<Object> Create (T1 a1) {
mathieu@2244
   597
      return ns3::CreateObject<T,T1> (a1);
mathieu@2237
   598
    }
mathieu@2237
   599
  };
mathieu@2237
   600
  CallbackBase cb = MakeCallback (&Maker::Create);
mathieu@2237
   601
  DoAddConstructor (cb, 1);
mathieu@2237
   602
  return *this;
mathieu@2237
   603
}
mathieu@2237
   604
template <typename T, typename T1, typename T2>
mathieu@2250
   605
TypeId 
mathieu@2250
   606
TypeId::AddConstructor (void)
mathieu@2237
   607
{
mathieu@2237
   608
  struct Maker {
mathieu@2237
   609
    static Ptr<Object> Create (T1 a1, T2 a2) {
mathieu@2244
   610
      return ns3::CreateObject<T,T1,T2> (a1, a2);
mathieu@2237
   611
    }
mathieu@2237
   612
  };
mathieu@2237
   613
  CallbackBase cb = MakeCallback (&Maker::Create);
mathieu@2237
   614
  DoAddConstructor (cb, 2);
mathieu@2237
   615
  return *this;
mathieu@2237
   616
}
mathieu@2237
   617
template <typename T1>
mathieu@2237
   618
Ptr<Object> 
mathieu@2372
   619
TypeId::CreateObject (T1 a1) const
mathieu@2237
   620
{
mathieu@2237
   621
  CallbackBase cb = LookupConstructor (1);
mathieu@2237
   622
  Callback<Ptr<Object>,T1> realCb;
mathieu@2237
   623
  realCb.Assign (cb);
mathieu@2237
   624
  Ptr<Object> object = realCb (a1);
mathieu@2237
   625
  return object;
mathieu@2237
   626
}
mathieu@2237
   627
template <typename T1, typename T2>
mathieu@2237
   628
Ptr<Object> 
mathieu@2372
   629
TypeId::CreateObject (T1 a1, T2 a2) const
mathieu@2237
   630
{
mathieu@2237
   631
  CallbackBase cb = LookupConstructor (2);
mathieu@2237
   632
  Callback<Ptr<Object>,T1,T2> realCb;
mathieu@2237
   633
  realCb.Assign (cb);
mathieu@2237
   634
  Ptr<Object> object = realCb (a1,a2);
mathieu@2237
   635
  return object;
mathieu@2237
   636
}
mathieu@2237
   637
mathieu@2372
   638
/*************************************************************************
mathieu@2372
   639
 *   The Object implementation which depends on templates
mathieu@2372
   640
 *************************************************************************/
mathieu@2237
   641
mathieu@701
   642
void
mathieu@706
   643
Object::Ref (void) const
mathieu@701
   644
{
mathieu@701
   645
  m_count++;
mathieu@701
   646
}
mathieu@701
   647
void
mathieu@706
   648
Object::Unref (void) const
mathieu@701
   649
{
mathieu@701
   650
  NS_ASSERT (Check ());
mathieu@701
   651
  m_count--;
mathieu@701
   652
  if (m_count == 0)
mathieu@701
   653
    {
mathieu@701
   654
      MaybeDelete ();
mathieu@701
   655
    }
mathieu@701
   656
}
mathieu@701
   657
mathieu@699
   658
template <typename T>
mathieu@699
   659
Ptr<T> 
mathieu@2257
   660
Object::GetObject () const
mathieu@2231
   661
{
mathieu@2257
   662
  Ptr<Object> found = DoGetObject (T::GetTypeId ());
mathieu@2231
   663
  if (found != 0)
mathieu@2231
   664
    {
mathieu@2231
   665
      return Ptr<T> (dynamic_cast<T *> (PeekPointer (found)));
mathieu@2231
   666
    }
mathieu@2231
   667
  return 0;
mathieu@2231
   668
}
mathieu@2231
   669
mathieu@2231
   670
template <typename T>
mathieu@2231
   671
Ptr<T> 
mathieu@2257
   672
Object::GetObject (TypeId tid) const
mathieu@699
   673
{
mathieu@2257
   674
  Ptr<Object> found = DoGetObject (tid);
mathieu@699
   675
  if (found != 0)
mathieu@699
   676
    {
mathieu@699
   677
      return Ptr<T> (dynamic_cast<T *> (PeekPointer (found)));
mathieu@699
   678
    }
mathieu@699
   679
  return 0;
mathieu@699
   680
}
mathieu@699
   681
mathieu@2372
   682
/*************************************************************************
mathieu@2372
   683
 *   The helper functions which need templates.
mathieu@2372
   684
 *************************************************************************/
mathieu@2372
   685
mathieu@2372
   686
mathieu@2372
   687
template <typename T>
mathieu@2459
   688
Ptr<T> CreateObject (const AttributeList &attributes)
mathieu@2372
   689
{
mathieu@2372
   690
  Ptr<T> p = Ptr<T> (new T (), false);
mathieu@2372
   691
  p->SetTypeId (T::GetTypeId ());
mathieu@2458
   692
  p->Object::Construct (attributes);
mathieu@2372
   693
  return p;  
mathieu@2372
   694
}
mathieu@2372
   695
mathieu@2230
   696
template <typename T>
mathieu@2230
   697
Ptr<T> CreateObject (void)
mathieu@2230
   698
{
mathieu@2230
   699
  Ptr<T> p = Ptr<T> (new T (), false);
mathieu@2251
   700
  p->SetTypeId (T::GetTypeId ());
mathieu@2459
   701
  p->Object::Construct (AttributeList ());
mathieu@2230
   702
  return p;
mathieu@2230
   703
}
mathieu@2230
   704
mathieu@2230
   705
template <typename T, typename T1>
mathieu@2230
   706
Ptr<T> CreateObject (T1 a1)
mathieu@2230
   707
{
mathieu@2230
   708
  Ptr<T> p = Ptr<T> (new T (a1), false);
mathieu@2251
   709
  p->SetTypeId (T::GetTypeId ());
mathieu@2459
   710
  p->Object::Construct (AttributeList ());
mathieu@2230
   711
  return p;
mathieu@2230
   712
}
mathieu@2230
   713
mathieu@2230
   714
template <typename T, typename T1, typename T2>
mathieu@2230
   715
Ptr<T> CreateObject (T1 a1, T2 a2)
mathieu@2230
   716
{
mathieu@2230
   717
  Ptr<T> p = Ptr<T> (new T (a1, a2), false);
mathieu@2251
   718
  p->SetTypeId (T::GetTypeId ());
mathieu@2459
   719
  p->Object::Construct (AttributeList ());
mathieu@2230
   720
  return p;
mathieu@2230
   721
}
mathieu@2230
   722
template <typename T, typename T1, typename T2, typename T3>
mathieu@2230
   723
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3)
mathieu@2230
   724
{
mathieu@2230
   725
  Ptr<T> p = Ptr<T> (new T (a1, a2, a3), false);
mathieu@2251
   726
  p->SetTypeId (T::GetTypeId ());
mathieu@2230
   727
  return p;
mathieu@2230
   728
}
mathieu@2230
   729
mathieu@2230
   730
template <typename T, typename T1, typename T2, typename T3, typename T4>
mathieu@2230
   731
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4)
mathieu@2230
   732
{
mathieu@2230
   733
  Ptr<T> p = Ptr<T> (new T (a1, a2, a3, a4), false);
mathieu@2251
   734
  p->SetTypeId (T::GetTypeId ());
mathieu@2230
   735
  return p;
mathieu@2230
   736
}
mathieu@2230
   737
mathieu@2230
   738
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5>
mathieu@2230
   739
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5)
mathieu@2230
   740
{
mathieu@2230
   741
  Ptr<T> p = Ptr<T> (new T (a1, a2, a3, a4, a5), false);
mathieu@2251
   742
  p->SetTypeId (T::GetTypeId ());
mathieu@2230
   743
  return p;
mathieu@2230
   744
}
mathieu@2230
   745
mathieu@2230
   746
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
mathieu@2230
   747
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6)
mathieu@2230
   748
{
mathieu@2230
   749
  Ptr<T> p = Ptr<T> (new T (a1, a2, a3, a4, a5, a6), false);
mathieu@2251
   750
  p->SetTypeId (T::GetTypeId ());
mathieu@2230
   751
  return p;
mathieu@2230
   752
}
mathieu@2230
   753
mathieu@2230
   754
template <typename T, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
mathieu@2230
   755
Ptr<T> CreateObject (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6, T7 a7)
mathieu@2230
   756
{
mathieu@2230
   757
  Ptr<T> p = Ptr<T> (new T (a1, a2, a3, a4, a5, a6, a7), false);
mathieu@2251
   758
  p->SetTypeId (T::GetTypeId ());
mathieu@2230
   759
  return p;
mathieu@2230
   760
}
mathieu@2230
   761
mathieu@2230
   762
mathieu@2372
   763
template <typename T>
mathieu@2372
   764
Ptr<T> 
mathieu@2460
   765
CreateObjectWith (std::string n1 = "", Attribute v1 = Attribute (),
mathieu@2489
   766
                  std::string n2 = "", Attribute v2 = Attribute (),
mathieu@2489
   767
                  std::string n3 = "", Attribute v3 = Attribute (),
mathieu@2489
   768
                  std::string n4 = "", Attribute v4 = Attribute (),
mathieu@2489
   769
                  std::string n5 = "", Attribute v5 = Attribute (),
mathieu@2489
   770
                  std::string n6 = "", Attribute v6 = Attribute (),
mathieu@2489
   771
                  std::string n7 = "", Attribute v7 = Attribute (),
mathieu@2489
   772
                  std::string n8 = "", Attribute v8 = Attribute (),
mathieu@2489
   773
                  std::string n9 = "", Attribute v9 = Attribute ())
mathieu@2372
   774
              
mathieu@2372
   775
{
mathieu@2459
   776
  AttributeList attributes;
mathieu@2458
   777
  attributes.SetWithTid (T::GetTypeId (), n1, v1);
mathieu@2458
   778
  attributes.SetWithTid (T::GetTypeId (), n2, v2);
mathieu@2489
   779
  attributes.SetWithTid (T::GetTypeId (), n3, v3);
mathieu@2489
   780
  attributes.SetWithTid (T::GetTypeId (), n4, v4);
mathieu@2489
   781
  attributes.SetWithTid (T::GetTypeId (), n5, v5);
mathieu@2489
   782
  attributes.SetWithTid (T::GetTypeId (), n6, v6);
mathieu@2489
   783
  attributes.SetWithTid (T::GetTypeId (), n7, v7);
mathieu@2489
   784
  attributes.SetWithTid (T::GetTypeId (), n8, v8);
mathieu@2489
   785
  attributes.SetWithTid (T::GetTypeId (), n9, v9);
mathieu@2458
   786
  return CreateObject<T> (attributes);
mathieu@2372
   787
}
mathieu@2372
   788
mathieu@699
   789
} // namespace ns3
mathieu@699
   790
mathieu@706
   791
#endif /* OBJECT_H */
mathieu@699
   792