src/core/attribute-helper.h
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Sat, 04 Jul 2009 08:15:48 +0200
changeset 4654 2eaebe77d66b
parent 3094 6e787ec9c8aa
permissions -rw-r--r--
Added tag ns-3.5 for changeset c975274c9707
mathieu@2581
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2581
     2
/*
mathieu@2581
     3
 * Copyright (c) 2008 INRIA
mathieu@2581
     4
 *
mathieu@2581
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2581
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2581
     7
 * published by the Free Software Foundation;
mathieu@2581
     8
 *
mathieu@2581
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2581
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2581
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2581
    12
 * GNU General Public License for more details.
mathieu@2581
    13
 *
mathieu@2581
    14
 * You should have received a copy of the GNU General Public License
mathieu@2581
    15
 * along with this program; if not, write to the Free Software
mathieu@2581
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2581
    17
 *
mathieu@2581
    18
 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2581
    19
 */
mathieu@2582
    20
#ifndef ATTRIBUTE_HELPER_H
mathieu@2582
    21
#define ATTRIBUTE_HELPER_H
mathieu@2422
    22
mathieu@2502
    23
#include "attribute.h"
mathieu@2449
    24
#include "attribute-accessor-helper.h"
mathieu@2446
    25
#include <sstream>
mathieu@2446
    26
#include "fatal-error.h"
mathieu@2422
    27
mathieu@2599
    28
namespace ns3 {
mathieu@2599
    29
mathieu@2599
    30
template <typename T, typename BASE>
mathieu@2599
    31
Ptr<AttributeChecker>
mathieu@2969
    32
MakeSimpleAttributeChecker (std::string name, std::string underlying)
mathieu@2599
    33
{
mathieu@2599
    34
  struct SimpleAttributeChecker : public BASE
mathieu@2599
    35
  {
mathieu@2965
    36
    virtual bool Check (const AttributeValue &value) const {
mathieu@2965
    37
      return dynamic_cast<const T *> (&value) != 0;
mathieu@2599
    38
    }
mathieu@2969
    39
    virtual std::string GetValueTypeName (void) const {
mathieu@2599
    40
      return m_type;
mathieu@2599
    41
    }
mathieu@2969
    42
    virtual bool HasUnderlyingTypeInformation (void) const {
mathieu@2969
    43
      return true;
mathieu@2599
    44
    }
mathieu@2969
    45
    virtual std::string GetUnderlyingTypeInformation (void) const {
mathieu@2969
    46
      return m_underlying;
mathieu@2599
    47
    }
mathieu@2965
    48
    virtual Ptr<AttributeValue> Create (void) const {
mathieu@2965
    49
      return ns3::Create<T> ();
mathieu@2965
    50
    }
mathieu@2965
    51
    virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
mathieu@2965
    52
      const T *src = dynamic_cast<const T *> (&source);
mathieu@2965
    53
      T *dst = dynamic_cast<T *> (&destination);
mathieu@2965
    54
      if (src == 0 || dst == 0)
mathieu@2965
    55
        {
mathieu@2965
    56
          return false;
mathieu@2965
    57
        }
mathieu@2965
    58
      *dst = *src;
mathieu@2965
    59
      return true;
mathieu@2599
    60
    }
mathieu@2599
    61
    std::string m_type;
mathieu@2969
    62
    std::string m_underlying;
mathieu@2599
    63
  } *checker = new SimpleAttributeChecker ();
mathieu@2599
    64
  checker->m_type = name;
mathieu@2969
    65
  checker->m_underlying = underlying;
mathieu@2599
    66
  return Ptr<AttributeChecker> (checker, false);
mathieu@2599
    67
}
mathieu@2599
    68
mathieu@2599
    69
}
mathieu@2599
    70
mathieu@2584
    71
/**
tomh@2867
    72
 * \ingroup core
mathieu@2688
    73
 * \defgroup AttributeHelper Attribute Helper
mathieu@2584
    74
 *
mathieu@2584
    75
 * All these macros can be used to generate automatically the code
mathieu@2584
    76
 * for subclasses of AttributeValue, AttributeAccessor, and, AttributeChecker,
mathieu@2584
    77
 * which can be used to give attribute powers to a normal class. i.e.,
mathieu@2584
    78
 * the user class can then effectively be made an attribute.
mathieu@2584
    79
 *
mathieu@2584
    80
 * There are two kinds of helper macros:
mathieu@2584
    81
 *  1) The simple macros.
mathieu@2584
    82
 *  2) The more complex macros.
mathieu@2584
    83
 *
mathieu@2584
    84
 * The simple macros are implemented in terms of the complex
mathieu@2584
    85
 * macros and should generally be prefered over the complex macros:
mathieu@3093
    86
 *    - \ref ATTRIBUTE_HELPER_HEADER, and,
mathieu@2695
    87
 *    - \ref ATTRIBUTE_HELPER_CPP,
mathieu@2584
    88
 */
mathieu@2584
    89
mathieu@2584
    90
/**
mathieu@2584
    91
 * \ingroup AttributeHelper
mathieu@2584
    92
 * \param type the name of the class
mathieu@2584
    93
 *
mathieu@2584
    94
 * This macro defines and generates the code for the implementation 
mathieu@2584
    95
 * of the MakeXXXAccessor template functions. This macro is typically
mathieu@2584
    96
 * invoked in a class header to allow users of this class to view and
mathieu@2584
    97
 * use the template functions defined here. This macro is implemented
mathieu@2584
    98
 * through the helper templates functions ns3::MakeAccessorHelper<>.
mathieu@2584
    99
 */
mathieu@2439
   100
#define ATTRIBUTE_ACCESSOR_DEFINE(type)					\
mathieu@2436
   101
  template <typename T1>						\
mathieu@2436
   102
  Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1)		\
mathieu@2436
   103
  {									\
mathieu@2520
   104
    return MakeAccessorHelper<type##Value> (a1);			\
mathieu@2436
   105
  }									\
mathieu@2436
   106
  template <typename T1, typename T2>					\
mathieu@2436
   107
  Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1, T2 a2)	\
mathieu@2436
   108
  {									\
mathieu@2520
   109
    return MakeAccessorHelper<type##Value> (a1, a2);			\
mathieu@2422
   110
  }
mathieu@2422
   111
mathieu@2969
   112
#define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name)                     \
mathieu@2965
   113
  class name##Value : public AttributeValue				\
mathieu@2965
   114
  {									\
mathieu@2965
   115
  public:								\
mathieu@2965
   116
    name##Value ();							\
mathieu@2965
   117
    name##Value (const type &value);					\
mathieu@2965
   118
    void Set (const type &value);					\
mathieu@2965
   119
    type Get (void) const;						\
mathieu@3763
   120
    template <typename T>                                               \
mathieu@3763
   121
    bool GetAccessor (T &value) const {                                 \
mathieu@3763
   122
      value = T (m_value);                                              \
mathieu@3763
   123
      return true;                                                      \
mathieu@3763
   124
    }                                                                   \
mathieu@2965
   125
    virtual Ptr<AttributeValue> Copy (void) const;                      \
mathieu@2965
   126
    virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const; \
mathieu@2965
   127
    virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker); \
mathieu@2965
   128
  private:								\
mathieu@2965
   129
    type m_value;							\
mathieu@2965
   130
  };
mathieu@2965
   131
mathieu@2965
   132
mathieu@2584
   133
/**
mathieu@2584
   134
 * \ingroup AttributeHelper
mathieu@2584
   135
 * \param type the name of the class.
mathieu@2584
   136
 *
mathieu@2584
   137
 * This macro defines the class XXXValue associated to class XXX.
mathieu@2584
   138
 * This macro is typically invoked in a class header.
mathieu@2584
   139
 */
mathieu@2439
   140
#define ATTRIBUTE_VALUE_DEFINE(type)					\
mathieu@2965
   141
  ATTRIBUTE_VALUE_DEFINE_WITH_NAME (type,type)
mathieu@2965
   142
mathieu@2439
   143
mathieu@2584
   144
/**
mathieu@2584
   145
 * \ingroup AttributeHelper
mathieu@2584
   146
 * \param type the name of the class
mathieu@2584
   147
 *
mathieu@2584
   148
 * This macro defines the conversion operators for class XXX to and
mathieu@2584
   149
 * from instances of type Attribute.
mathieu@2584
   150
 * Typically invoked from xxx.h.
mathieu@2584
   151
 */
mathieu@2965
   152
#define ATTRIBUTE_CONVERTER_DEFINE(type)
mathieu@2439
   153
mathieu@2584
   154
/**
mathieu@2584
   155
 * \ingroup AttributeHelper
mathieu@2584
   156
 * \param type the name of the class
mathieu@2584
   157
 *
mathieu@2584
   158
 * This macro defines the XXXChecker class and the associated
mathieu@2584
   159
 * MakeXXXChecker function.
mathieu@2584
   160
 * Typically invoked from xxx.h.
mathieu@2584
   161
 */
mathieu@2439
   162
#define ATTRIBUTE_CHECKER_DEFINE(type)				\
mathieu@2473
   163
  class type##Checker : public AttributeChecker {};		\
mathieu@2439
   164
  Ptr<const AttributeChecker> Make##type##Checker (void);	\
mathieu@2439
   165
mathieu@2965
   166
mathieu@2965
   167
#define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name)                  \
mathieu@2965
   168
  name##Value::name##Value ()						\
mathieu@2520
   169
    : m_value () {}							\
mathieu@2965
   170
  name##Value::name##Value (const type &value)				\
mathieu@2439
   171
  : m_value (value) {}							\
mathieu@2965
   172
  void name##Value::Set (const type &v) {				\
mathieu@2439
   173
    m_value = v;							\
mathieu@2439
   174
  }									\
mathieu@2965
   175
  type name##Value::Get (void) const {					\
mathieu@2439
   176
    return m_value;							\
mathieu@2439
   177
  }									\
mathieu@2965
   178
  Ptr<AttributeValue>                                                   \
mathieu@2965
   179
  name##Value::Copy (void) const {					\
mathieu@2965
   180
    return ns3::Create<name##Value> (*this);                            \
mathieu@2965
   181
  }                                                                     \
mathieu@2965
   182
  std::string								\
mathieu@2965
   183
  name##Value::SerializeToString (Ptr<const AttributeChecker> checker) const { \
mathieu@2965
   184
    std::ostringstream oss;						\
mathieu@2965
   185
    oss << m_value;							\
mathieu@2965
   186
    return oss.str ();							\
mathieu@2439
   187
  }									\
mathieu@2965
   188
  bool									\
mathieu@2965
   189
  name##Value::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker) { \
mathieu@2965
   190
    std::istringstream iss;						\
mathieu@2965
   191
    iss.str (value);							\
mathieu@2965
   192
    iss >> m_value;							\
mathieu@2965
   193
    return !iss.bad () && !iss.fail ();					\
mathieu@2439
   194
  }
mathieu@2439
   195
mathieu@2584
   196
/**
mathieu@2584
   197
 * \ingroup AttributeHelper
mathieu@2584
   198
 * \param type the name of the class.
mathieu@2584
   199
 *
mathieu@2584
   200
 * This macro implements the XXXValue class (including the 
mathieu@2584
   201
 * XXXValue::SerializeToString and XXXValue::DeserializeFromString 
mathieu@2584
   202
 * methods).
mathieu@2584
   203
 * Typically invoked from xxx.cc.
mathieu@2584
   204
 */
mathieu@2515
   205
#define ATTRIBUTE_VALUE_IMPLEMENT(type)					\
mathieu@2965
   206
  ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,type)
mathieu@2965
   207
mathieu@2515
   208
mathieu@2584
   209
/**
mathieu@2584
   210
 * \ingroup AttributeHelper
mathieu@2584
   211
 * \param type the name of the class
mathieu@2584
   212
 *
mathieu@2584
   213
 * This macro implements the MakeXXXChecker function.
mathieu@2584
   214
 * Typically invoked from xxx.cc.
mathieu@2584
   215
 */
mathieu@2439
   216
#define ATTRIBUTE_CHECKER_IMPLEMENT(type)				\
mathieu@2436
   217
  Ptr<const AttributeChecker> Make##type##Checker (void)		\
mathieu@2427
   218
  {									\
mathieu@2969
   219
    return MakeSimpleAttributeChecker<type##Value,type##Checker> (#type "Value", #type); \
mathieu@2427
   220
  }									\
mathieu@2439
   221
mathieu@2969
   222
#define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name)                    \
mathieu@2969
   223
  Ptr<const AttributeChecker> Make##type##Checker (void)		\
mathieu@2969
   224
  {									\
mathieu@2969
   225
    return MakeSimpleAttributeChecker<type##Value,type##Checker> (#type "Value", name); \
mathieu@2969
   226
  }									\
mathieu@2969
   227
mathieu@2584
   228
/**
mathieu@2584
   229
 * \ingroup AttributeHelper
mathieu@2584
   230
 * \param type the name of the class
mathieu@2584
   231
 *
mathieu@2584
   232
 * This macro should be invoked outside of the class
mathieu@2584
   233
 * declaration in its public header.
mathieu@2584
   234
 */
mathieu@3094
   235
#define ATTRIBUTE_HELPER_HEADER(type)					\
mathieu@2439
   236
  ATTRIBUTE_VALUE_DEFINE (type);					\
mathieu@2439
   237
  ATTRIBUTE_ACCESSOR_DEFINE (type);					\
mathieu@2439
   238
  ATTRIBUTE_CHECKER_DEFINE (type);
mathieu@2439
   239
mathieu@2584
   240
/**
mathieu@2584
   241
 * \ingroup AttributeHelper
mathieu@2584
   242
 * \param type the name of the class
mathieu@2584
   243
 *
mathieu@2584
   244
 * This macro should be invoked from the class implementation file.
mathieu@2584
   245
 */
mathieu@2584
   246
#define ATTRIBUTE_HELPER_CPP(type)                                      \
mathieu@2439
   247
  ATTRIBUTE_CHECKER_IMPLEMENT (type);					\
mathieu@2439
   248
  ATTRIBUTE_VALUE_IMPLEMENT (type);
mathieu@2439
   249
mathieu@2439
   250
mathieu@2582
   251
#endif /* ATTRIBUTE_HELPER_H */