View | Details | Raw Unified | Return to bug 1604
Collapse All | Expand All

(-)a/src/core/model/attribute-helper.h (-12 / +134 lines)
 Lines 22-27    Link Here 
22
22
23
#include "attribute.h"
23
#include "attribute.h"
24
#include "attribute-accessor-helper.h"
24
#include "attribute-accessor-helper.h"
25
#include "config.h"
25
#include <sstream>
26
#include <sstream>
26
#include "fatal-error.h"
27
#include "fatal-error.h"
27
28
 Lines 29-35    Link Here 
29
30
30
/**
31
/**
31
 * \ingroup attribute
32
 * \ingroup attribute
32
 * \defgroup attributehelper Attribute Helper
33
 * \defgroup attributehelper Attribute Helpers
33
 *
34
 *
34
 * All these macros can be used to generate automatically the code
35
 * All these macros can be used to generate automatically the code
35
 * for subclasses of AttributeValue, AttributeAccessor, and, AttributeChecker,
36
 * for subclasses of AttributeValue, AttributeAccessor, and, AttributeChecker,
 Lines 127-134    Link Here 
127
  return Ptr<AttributeChecker> (checker, false);
128
  return Ptr<AttributeChecker> (checker, false);
128
}
129
}
129
130
130
}
131
  
131
132
/**
132
/**
133
 * \ingroup attributehelper
133
 * \ingroup attributehelper
134
 *
134
 *
 Lines 192-199    Link Here 
192
                             Ptr<const AttributeChecker> checker);      \
192
                             Ptr<const AttributeChecker> checker);      \
193
  private:                                                              \
193
  private:                                                              \
194
    type m_value;                                                       \
194
    type m_value;                                                       \
195
  };
195
  }
196
197
196
198
/**
197
/**
199
 * \ingroup attributehelper
198
 * \ingroup attributehelper
 Lines 247-252    Link Here 
247
  class type ## Checker : public AttributeChecker {};                   \
246
  class type ## Checker : public AttributeChecker {};                   \
248
  Ptr<const AttributeChecker> Make ## type ## Checker (void)
247
  Ptr<const AttributeChecker> Make ## type ## Checker (void)
249
248
249
/**
250
 * \ingroup attributehelper
251
 *
252
 * Declare the Config::GetDefaultValue<>() specializations for \p type.
253
 *
254
 * \param type The return type.
255
 *
256
 * This macro declares the Config::GetDefaultValue<>() specialization
257
 * returning the current default configuration value of an attribute,
258
 * as a \c type.  If the attribute at the configuration path is not
259
 * compatible with the requested type, the function will raise a
260
 * fatal error.
261
 *
262
 * This macro is typically invoked in the class header file.
263
 *
264
 * \note This macro must be invoked from <tt>namespace ns3</tt>.
265
 *
266
 * \see
267
 *   GetDefaultValue<>()
268
 */
269
#define ATTRIBUTE_GETDEFAULT_DEFINE(type)                               \
270
  namespace Config {                                                    \
271
    template<> type                                                     \
272
      GetDefaultValue<type> (const std::string attributePath);          \
273
  } /* namespace Config */
274
250
275
251
/**
276
/**
252
 * \ingroup attributehelper
277
 * \ingroup attributehelper
 Lines 345-356    Link Here 
345
  Ptr<const AttributeChecker> Make ## type ## Checker (void) {          \
370
  Ptr<const AttributeChecker> Make ## type ## Checker (void) {          \
346
    return MakeSimpleAttributeChecker<type ## Value,type ## Checker>    \
371
    return MakeSimpleAttributeChecker<type ## Value,type ## Checker>    \
347
      (# type "Value", name);                                           \
372
      (# type "Value", name);                                           \
348
  }                                                                     \
373
  }
374
375
376
namespace Config {
377
  
378
/**
379
 * \ingroup config
380
 * Config::GetDefaultValue<>() helper to fetch an attribute path as
381
 * the base AttributeValue.
382
 *
383
 * \param attributePath The configuration path for the attribute.
384
 * \returns The AttributeValue.
385
 *
386
 * \internal
387
 * This is declared in attribute-helper.h to break an include cycle.
388
 * The implementation is in config.cc.
389
 */
390
Ptr<const AttributeValue>
391
GetDefaultAttributeValue (const std::string attributePath);
392
  
393
}  // namespace Config
394
349
395
350
/**
396
/**
351
 * \ingroup attributehelper
397
 * \ingroup attributehelper
352
 *
398
 *
353
 * Declare the attribute value, accessor and checkers for class \p type
399
 * Define the Config::GetDefaultValue<>() specialization for the attribute
400
 * value class \c \typeValue, for underlying class \p type.
401
 *
402
 * \param type The underlying type name.
403
 * \param typeValue The derived AttributeValue class corresponding to
404
 * \c type.
405
 *
406
 * This macro implements the Config::GetDefaultValue<>() specialization
407
 * returning the current default configuration value of an attribute
408
 * as a \c type.  If the attribute at the configuration path is not
409
 * compatible with the requested type, the function will raise a
410
 * fatal error.
411
 *
412
 * This macro is typically invoked in the source file.
413
 *
414
 * \note This macro must be invoked from <tt>namespace ns3</tt>.
415
 *
416
 * \see Config::GetDefaultValue<>()
417
 */
418
#define ATTRIBUTE_GETDEFAULT_IMPLEMENT_WITH_NAME(type,typeValue)        \
419
  namespace Config {                                                    \
420
    template<> type                                                     \
421
      GetDefaultValue<type> (const std::string attributePath)           \
422
      {                                                                 \
423
        Ptr<const AttributeValue> attrValue =                           \
424
          GetDefaultAttributeValue (attributePath);                     \
425
        Ptr<const typeValue> attr =                                     \
426
          DynamicCast<const typeValue> (attrValue);                     \
427
        if (!attr) {                                                    \
428
          NS_FATAL_ERROR ("Unable to convert attribute \""              \
429
                          << attributePath << "\" to type "             \
430
                          << #typeValue );                              \
431
        }                                                               \
432
        return attr->Get ();                                            \
433
      }                                                                 \
434
  } /* namespace Config */
435
436
437
/**
438
 * \ingroup attributehelper
439
 *
440
 * Define the Config::GetDefaultValue<>() specialization for the attribute
441
 * value class \p \<Name>Value, for underlying class \p Name.
442
 *
443
 * \param Name The underlying type name.
444
 *
445
 * This macro implements the Config::GetDefaultValue<>() specialization
446
 * returning the current default configuration value of an attribute,
447
 * as a \c Name.  If the attribute at the configuration path is not
448
 * compatible with the requested type, the function will raise a
449
 * fatal error.
450
 *
451
 * This macro is typically invoked in the source file.
452
 *
453
 * \note This macro must be invoked from <tt>namespace ns3</tt>.
454
 *
455
 * \see Config::GetDefaultValue<>()
456
 */
457
#define ATTRIBUTE_GETDEFAULT_IMPLEMENT(Name)                            \
458
  ATTRIBUTE_GETDEFAULT_IMPLEMENT_WITH_NAME(Name, Name ## Value)
459
460
/**
461
 * \ingroup attributehelper
462
 *
463
 * Declare the attribute value, accessor, checkers and
464
 * Config::GetDefaultValue<>() specialization for class \p type
354
 *
465
 *
355
 * \param type the name of the class
466
 * \param type the name of the class
356
 *
467
 *
 Lines 363-382    Link Here 
363
 *   - The AttributeChecker class \p \<type>Checker
474
 *   - The AttributeChecker class \p \<type>Checker
364
 *     and the \c MakeTypeChecker function,
475
 *     and the \c MakeTypeChecker function,
365
 *
476
 *
477
 *   - The Config::GetDefaultValue<>() specialization for getting the default
478
 *     configuration value of an attribute as a \p type,
479
 *
366
 * for class \p type.
480
 * for class \p type.
367
 *
481
 *
368
 * This macro should be invoked outside of the class
482
 * This macro should be invoked outside of the class
369
 * declaration in its public header.
483
 * declaration in its public header, normally in <tt>namespace ns3<tt>.
370
 */
484
 */
371
#define ATTRIBUTE_HELPER_HEADER(type)                                   \
485
#define ATTRIBUTE_HELPER_HEADER(type)                                   \
372
  ATTRIBUTE_VALUE_DEFINE (type);                                        \
486
  ATTRIBUTE_VALUE_DEFINE (type);                                        \
373
  ATTRIBUTE_ACCESSOR_DEFINE (type);                                     \
487
  ATTRIBUTE_ACCESSOR_DEFINE (type);                                     \
374
  ATTRIBUTE_CHECKER_DEFINE (type);
488
  ATTRIBUTE_CHECKER_DEFINE (type);                                      \
489
  ATTRIBUTE_GETDEFAULT_DEFINE (type)
375
490
376
/**
491
/**
377
 * \ingroup attributehelper
492
 * \ingroup attributehelper
378
 *
493
 *
379
 * Define the attribute value, accessor and checkers for class \p type
494
 * Define the attribute value, accessor, checkers and
495
 * Config::GetDefaultValue<>() specialization for class \p type
380
 *
496
 *
381
 * \param type the name of the class
497
 * \param type the name of the class
382
 *
498
 *
 Lines 386-398    Link Here 
386
 *
502
 *
387
 *   - The \c MakeTypeChecker function,
503
 *   - The \c MakeTypeChecker function,
388
 *
504
 *
505
 *   - The Config::GetDefaultValue<>() specialization,
506
 *
389
 * for class \p type.
507
 * for class \p type.
390
 *
508
 *
391
 * This macro should be invoked from the class implementation file.
509
 * This macro should be invoked from the class implementation file,
510
 * normally in <tt>namespace ns3</tt>.
392
 */
511
 */
393
#define ATTRIBUTE_HELPER_CPP(type)                                      \
512
#define ATTRIBUTE_HELPER_CPP(type)                                      \
394
  ATTRIBUTE_CHECKER_IMPLEMENT (type);                                   \
513
  ATTRIBUTE_CHECKER_IMPLEMENT (type);                                   \
395
  ATTRIBUTE_VALUE_IMPLEMENT (type);
514
  ATTRIBUTE_VALUE_IMPLEMENT (type);                                     \
515
  ATTRIBUTE_GETDEFAULT_IMPLEMENT (type)
396
516
517
  
518
}  // namespace ns3
397
519
398
#endif /* ATTRIBUTE_HELPER_H */
520
#endif /* ATTRIBUTE_HELPER_H */
(-)a/src/core/model/callback.h (-1 / +16 lines)
 Lines 1718-1730    Link Here 
1718
  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
1718
  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
1719
private:
1719
private:
1720
  CallbackBase m_value;                 //!< the CallbackBase
1720
  CallbackBase m_value;                 //!< the CallbackBase
1721
};
1721
1722
};  // class CallbackValue
1722
1723
1723
ATTRIBUTE_ACCESSOR_DEFINE (Callback);
1724
ATTRIBUTE_ACCESSOR_DEFINE (Callback);
1724
ATTRIBUTE_CHECKER_DEFINE (Callback);
1725
ATTRIBUTE_CHECKER_DEFINE (Callback);
1726
/*
1727
  Can't implement Config::GetDefault<> specialization for
1728
  Callback<R, T1, T2, T3, T4, T5, T6, T7, T8, T9>
1729
1730
  This ends up being a function partial specialization,
1731
  which isn't allowed.
1732
            
1733
ATTRIBUTE_GETDEFAULT_DEFINE (Callback);
1734
*/
1725
1735
1726
} // namespace ns3
1736
} // namespace ns3
1727
1737
1738
1739
/********************************************************************
1740
 *  Implementation of templates defined above
1741
 ********************************************************************/
1742
1728
namespace ns3 {
1743
namespace ns3 {
1729
1744
1730
template <typename T>
1745
template <typename T>
(-)a/src/core/model/config.cc (+44 lines)
 Lines 776-781    Link Here 
776
  return Singleton<ConfigImpl>::Get ()->GetRootNamespaceObject (i);
776
  return Singleton<ConfigImpl>::Get ()->GetRootNamespaceObject (i);
777
}
777
}
778
778
779
/**
780
 * \ingroup config
781
 * GetDefaultValue<>() helper to fetch the TypeId::AttributeInformation.
782
 *
783
 * \param attributePath The configuration path for the attribute.
784
 * \returns The TypeId::AttributeInformation.
785
 *
786
 * This is internal to the Config system.
787
 */
788
Ptr<struct TypeId::AttributeInformation>
789
GetAttributeInformation(const std::string attributePath)
790
{
791
  NS_LOG_FUNCTION (attributePath);
792
  // Attribute name is last token
793
  size_t colon = attributePath.rfind ("::");
794
  const std::string typeName = attributePath.substr (0, colon);
795
  NS_LOG_DEBUG ("typeName: '" << typeName << "', colon: " << colon);
796
  
797
  TypeId tid;
798
  if (!TypeId::LookupByNameFailSafe (typeName, &tid))
799
    {
800
      NS_FATAL_ERROR ("Unknown type=" << typeName);
801
    }
802
803
  const std::string attrName = attributePath.substr (colon + 2);
804
  Ptr<struct TypeId::AttributeInformation> info =
805
    Create<struct TypeId::AttributeInformation> ();
806
  if (!tid.LookupAttributeByName (attrName, PeekPointer (info)) )
807
    {
808
      NS_FATAL_ERROR ("Attribute not found: " << attributePath);
809
    }
810
  return info;
811
}
812
813
Ptr<const AttributeValue>
814
GetDefaultAttributeValue (const std::string attributePath)
815
{
816
  Ptr<struct TypeId::AttributeInformation> info =
817
    GetAttributeInformation (attributePath);
818
  Ptr<const AttributeValue> attr = info->initialValue;
819
  return attr;
820
}
821
822
779
} // namespace Config
823
} // namespace Config
780
824
781
} // namespace ns3
825
} // namespace ns3
(-)a/src/core/model/config.h (+36 lines)
 Lines 247-252    Link Here 
247
 */
247
 */
248
Ptr<Object> GetRootNamespaceObject (uint32_t i);
248
Ptr<Object> GetRootNamespaceObject (uint32_t i);
249
249
250
/**
251
 * \ingroup config
252
 * Get the current default value for an attribute.
253
 *
254
 * The current default is the initial value which would be
255
 * used by new instances of the type, including any
256
 * CommandLine or environment variable configuration.
257
 *
258
 * \tparam R The type to return.
259
 * \param attributePath The configuration path for the attribute.
260
 * \returns The default value.
261
 *
262
 * GetDefaultValue<>() will raise a fatal error if the attribute
263
 * can't be converted to the desired type.
264
 *
265
 * \internal
266
 *
267
 * GetDefaultValue<>() proceeds by first looking up the default value as a base
268
 * AttributeValue, then attempting to upcast to the derived AttributeValue
269
 * type associated with the requested return type, then finally
270
 * getting the default value from the derived AttributeValue.
271
 *
272
 * For example, if the desired return type is an ns3::DataRate, the
273
 * base AttributeValue will be upcast to a DataRateValue.  If this fails
274
 * GetDefaultValue<>() will raise a fatal error.
275
 *
276
 * There is no generic template declaration; only specializations
277
 * of this template exist.  Specializations are declared and implemented
278
 * by the ATTRIBUTE_GETDEFAULT_DEFINE() and ATTRIBUTE_GETDEFAULT_IMPLEMENT()
279
 * macros, which are normally invoked by the ATTRIBUTE_HELPER_HEADER() and
280
 * ATTRIBUTE_HELPER_CPP() macros.
281
 */
282
template <typename R>
283
R
284
GetDefaultValue (const std::string attributePath);
285
250
} // namespace Config
286
} // namespace Config
251
287
252
} // namespace ns3
288
} // namespace ns3
(-)a/src/core/model/double.cc (+1 lines)
 Lines 27-32    Link Here 
27
NS_LOG_COMPONENT_DEFINE ("Double");
27
NS_LOG_COMPONENT_DEFINE ("Double");
28
28
29
ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (double, Double);
29
ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (double, Double);
30
ATTRIBUTE_GETDEFAULT_IMPLEMENT_WITH_NAME (double, DoubleValue);
30
31
31
namespace internal {
32
namespace internal {
32
33
(-)a/src/core/model/double.h (+1 lines)
 Lines 34-39    Link Here 
34
 */
34
 */
35
ATTRIBUTE_VALUE_DEFINE_WITH_NAME (double, Double);
35
ATTRIBUTE_VALUE_DEFINE_WITH_NAME (double, Double);
36
ATTRIBUTE_ACCESSOR_DEFINE (Double);
36
ATTRIBUTE_ACCESSOR_DEFINE (Double);
37
ATTRIBUTE_GETDEFAULT_DEFINE (double);
37
38
38
template <typename T>
39
template <typename T>
39
Ptr<const AttributeChecker> MakeDoubleChecker (void);
40
Ptr<const AttributeChecker> MakeDoubleChecker (void);
(-)a/src/core/model/integer.cc (+1 lines)
 Lines 27-32    Link Here 
27
NS_LOG_COMPONENT_DEFINE ("Integer");
27
NS_LOG_COMPONENT_DEFINE ("Integer");
28
28
29
ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (int64_t, Integer);
29
ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (int64_t, Integer);
30
ATTRIBUTE_GETDEFAULT_IMPLEMENT_WITH_NAME (int64_t, IntegerValue);
30
31
31
namespace internal {
32
namespace internal {
32
33
(-)a/src/core/model/integer.h (+1 lines)
 Lines 37-42    Link Here 
37
 */
37
 */
38
ATTRIBUTE_VALUE_DEFINE_WITH_NAME (int64_t, Integer);
38
ATTRIBUTE_VALUE_DEFINE_WITH_NAME (int64_t, Integer);
39
ATTRIBUTE_ACCESSOR_DEFINE (Integer);
39
ATTRIBUTE_ACCESSOR_DEFINE (Integer);
40
ATTRIBUTE_GETDEFAULT_DEFINE (int64_t);
40
41
41
template <typename T>
42
template <typename T>
42
Ptr<const AttributeChecker> MakeIntegerChecker (void);
43
Ptr<const AttributeChecker> MakeIntegerChecker (void);
(-)a/src/core/model/nstime.h (+1 lines)
 Lines 919-924    Link Here 
919
 * \brief Attribute for objects of type ns3::Time
919
 * \brief Attribute for objects of type ns3::Time
920
 */
920
 */
921
ATTRIBUTE_VALUE_DEFINE (Time);
921
ATTRIBUTE_VALUE_DEFINE (Time);
922
ATTRIBUTE_GETDEFAULT_DEFINE (Time);
922
923
923
/**
924
/**
924
 *  Attribute accessor function for Time
925
 *  Attribute accessor function for Time
(-)a/src/core/model/type-id.h (-2 / +3 lines)
 Lines 60-66    Link Here 
60
    ATTR_CONSTRUCT = 1<<2, /**< The attribute can be written at construction-time */
60
    ATTR_CONSTRUCT = 1<<2, /**< The attribute can be written at construction-time */
61
    ATTR_SGC = ATTR_GET | ATTR_SET | ATTR_CONSTRUCT, /**< The attribute can be read, and written at any time */
61
    ATTR_SGC = ATTR_GET | ATTR_SET | ATTR_CONSTRUCT, /**< The attribute can be read, and written at any time */
62
  };
62
  };
63
  struct AttributeInformation {
63
  struct AttributeInformation : public SimpleRefCount<AttributeInformation>
64
  {
64
    std::string name;
65
    std::string name;
65
    std::string help;
66
    std::string help;
66
    uint32_t flags;
67
    uint32_t flags;
 Lines 412-418    Link Here 
412
 * \brief hold objects of type ns3::TypeId
413
 * \brief hold objects of type ns3::TypeId
413
 */
414
 */
414
415
415
416
  
416
ATTRIBUTE_HELPER_HEADER (TypeId);
417
ATTRIBUTE_HELPER_HEADER (TypeId);
417
418
418
} // namespace ns3 
419
} // namespace ns3 
(-)a/src/core/test/attribute-test-suite.cc (-2 / +14 lines)
 Lines 72-79    Link Here 
72
{
72
{
73
  return is;
73
  return is;
74
}
74
}
75
ATTRIBUTE_HELPER_HEADER (ValueClassTest);
75
76
ATTRIBUTE_HELPER_CPP (ValueClassTest);
76
ATTRIBUTE_VALUE_DEFINE (ValueClassTest);
77
ATTRIBUTE_ACCESSOR_DEFINE (ValueClassTest);
78
ATTRIBUTE_CHECKER_DEFINE (ValueClassTest);
79
80
namespace ns3 {
81
ATTRIBUTE_GETDEFAULT_DEFINE (ValueClassTest);
82
}
83
84
ATTRIBUTE_CHECKER_IMPLEMENT (ValueClassTest);
85
ATTRIBUTE_VALUE_IMPLEMENT (ValueClassTest);
86
namespace ns3 {
87
ATTRIBUTE_GETDEFAULT_IMPLEMENT (ValueClassTest);
88
}
77
89
78
class Derived : public Object
90
class Derived : public Object
79
{
91
{
(-)a/src/mesh/model/dot11s/ie-dot11s-id.cc (-2 / +5 lines)
 Lines 151-158    Link Here 
151
  return is;
151
  return is;
152
}
152
}
153
153
154
ATTRIBUTE_HELPER_CPP (IeMeshId);
154
ATTRIBUTE_VALUE_IMPLEMENT (IeMeshId);
155
155
ATTRIBUTE_CHECKER_IMPLEMENT (IeMeshId);
156
156
157
} // namespace dot11s
157
} // namespace dot11s
158
159
ATTRIBUTE_GETDEFAULT_IMPLEMENT (ns3::dot11s::IeMeshId);
160
158
} // namespace ns3
161
} // namespace ns3
(-)a/src/mesh/model/dot11s/ie-dot11s-id.h (-5 / +6 lines)
 Lines 58-69    Link Here 
58
58
59
std::ostream &operator << (std::ostream &os, const IeMeshId &meshId);
59
std::ostream &operator << (std::ostream &os, const IeMeshId &meshId);
60
60
61
/**
61
ATTRIBUTE_VALUE_DEFINE (IeMeshId);
62
 * \class ns3::IeMeshIdValue
62
ATTRIBUTE_ACCESSOR_DEFINE (IeMeshId);
63
 * \brief hold objects of type ns3::IeMeshId
63
ATTRIBUTE_CHECKER_DEFINE (IeMeshId);
64
 */
65
64
66
ATTRIBUTE_HELPER_HEADER (IeMeshId);
67
} // namespace dot11s
65
} // namespace dot11s
66
  
67
ATTRIBUTE_GETDEFAULT_DEFINE (ns3::dot11s::IeMeshId);
68
  
68
} // namespace ns3
69
} // namespace ns3
69
#endif /* MESH_ID_H */
70
#endif /* MESH_ID_H */

Return to bug 1604