src/core/value-helper.h
changeset 2439 4a0b22a3f5fa
parent 2437 44e34ffee8e3
child 2442 78446ab94ef5
equal deleted inserted replaced
2438:e2ac9f9aeeb9 2439:4a0b22a3f5fa
     1 #ifndef VALUE_HELPER_H
     1 #ifndef VALUE_HELPER_H
     2 #define VALUE_HELPER_H
     2 #define VALUE_HELPER_H
     3 
     3 
     4 #include "class-value-helper.h"
     4 #include "class-value-helper.h"
     5 
     5 
     6 #define VALUE_HELPER_HEADER_1(type) \
       
     7   type (Attribute value); \
       
     8   operator Attribute () const;
       
     9 
     6 
    10 #define VALUE_HELPER_HEADER_2(type)					\
     7 #define ATTRIBUTE_ACCESSOR_DEFINE(type)					\
    11   class type##Value : public AttributeValue {};				\
       
    12   class type##Accessor : public AttributeAccessor {};			\
     8   class type##Accessor : public AttributeAccessor {};			\
    13   Ptr<const AttributeChecker> Make##type##Checker (void);		\
       
    14   template <typename T1>						\
     9   template <typename T1>						\
    15   Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1)		\
    10   Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1)		\
    16   {									\
    11   {									\
    17     return MakeClassValueHelperAccessor< type ,				\
    12     return MakeAccessorHelper<type##Accessor,type##Value> (a1);		\
    18       type##Value, type##Accessor> (a1);				\
       
    19   }									\
    13   }									\
    20   template <typename T1, typename T2>					\
    14   template <typename T1, typename T2>					\
    21   Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1, T2 a2)	\
    15   Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1, T2 a2)	\
    22   {									\
    16   {									\
    23     return MakeClassValueHelperAccessor<type,				\
    17     return MakeAccessorHelper<type##Accessor,type##Value> (a1, a2);	\
    24       type##Value,type##Accessor> (a1, a2);				\
       
    25   }
    18   }
    26 
    19 
    27 #define VALUE_HELPER_CPP(type)						\
    20 #define ATTRIBUTE_VALUE_DEFINE(type)					\
       
    21   class type##Value : public AttributeValue				\
       
    22   {									\
       
    23   public:								\
       
    24     type##Value (const type &value);					\
       
    25     void Set (const type &value);					\
       
    26     type Get (void) const;						\
       
    27     virtual Attribute Copy (void) const;				\
       
    28     virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const; \
       
    29     virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker); \
       
    30     type##Value (Attribute value);					\
       
    31     operator Attribute () const;					\
       
    32   private:								\
       
    33     type m_value;							\
       
    34   };
       
    35 
       
    36 #define ATTRIBUTE_CONVERTER_DEFINE(type)	\
       
    37   type (Attribute value);			\
       
    38   operator Attribute () const;
       
    39 
       
    40 #define ATTRIBUTE_CHECKER_DEFINE(type)				\
       
    41   Ptr<const AttributeChecker> Make##type##Checker (void);	\
       
    42 
       
    43 #define ATTRIBUTE_VALUE_IMPLEMENT(type)					\
       
    44   type##Value::type##Value (const type &value)				\
       
    45   : m_value (value) {}							\
       
    46   void type##Value::Set (const type &v) {				\
       
    47     m_value = v;							\
       
    48   }									\
       
    49   type type##Value::Get (void) const {					\
       
    50     return m_value;							\
       
    51   }									\
       
    52   Attribute								\
       
    53   type##Value::Copy (void) const {					\
       
    54     return Attribute::Create<type##Value> (*this);			\
       
    55   }									\
       
    56   std::string								\
       
    57   type##Value::SerializeToString (Ptr<const AttributeChecker> checker) const { \
       
    58     std::ostringstream oss;						\
       
    59     oss << m_value;							\
       
    60     return oss.str ();							\
       
    61   }									\
       
    62   bool									\
       
    63   type##Value::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker) { \
       
    64     std::istringstream iss;						\
       
    65     iss.str (value);							\
       
    66     iss >> m_value;							\
       
    67     return !iss.bad () && !iss.fail ();					\
       
    68   }									\
       
    69   type##Value::type##Value (Attribute value)				\
       
    70   {									\
       
    71     type##Value *v = value.DynCast<type##Value *> ();			\
       
    72     if (v == 0)								\
       
    73       {									\
       
    74 	NS_FATAL_ERROR ("Unexpected type of value. Expected \"" << #type << "Value\""); \
       
    75       }									\
       
    76     m_value = v->Get ();						\
       
    77   }									\
       
    78   type##Value::operator Attribute () const				\
       
    79   {									\
       
    80     return Attribute::Create<type##Value> (*this);			\
       
    81   }
       
    82 
       
    83 #define ATTRIBUTE_CHECKER_IMPLEMENT(type)				\
    28   Ptr<const AttributeChecker> Make##type##Checker (void)		\
    84   Ptr<const AttributeChecker> Make##type##Checker (void)		\
    29   {									\
    85   {									\
    30     return MakeSimpleAttributeChecker<type> ();				\
    86     return MakeSimpleAttributeChecker<type> ();				\
    31   }									\
    87   }									\
       
    88 
       
    89 #define ATTRIBUTE_CONVERTER_IMPLEMENT(type)				\
    32   type::type (Attribute value)						\
    90   type::type (Attribute value)						\
    33   {									\
    91   {									\
    34     *this = ClassValueHelperExtractFrom<type,type##Value> (value);	\
    92     const type##Value *v = value.DynCast<const type##Value *> ();	\
       
    93     if (v == 0)								\
       
    94       {									\
       
    95       NS_FATAL_ERROR ("Unexpected type of value. Expected \"" << #type << "Value\""); \
       
    96       }									\
       
    97     *this = v->Get ();							\
    35   }									\
    98   }									\
    36   type::operator Attribute () const					\
    99   type::operator Attribute () const					\
    37   {									\
   100   {									\
    38     return ClassValueHelperConvertTo<type,type##Value> (this);		\
   101     return Attribute::Create<type##Value> (*this);			\
    39   }
   102   }
    40 
   103 
       
   104 
       
   105 #define VALUE_HELPER_HEADER_1(type) \
       
   106   ATTRIBUTE_CONVERTER_DEFINE (type)
       
   107 
       
   108 #define VALUE_HELPER_HEADER_2(type)					\
       
   109   ATTRIBUTE_VALUE_DEFINE (type);					\
       
   110   ATTRIBUTE_ACCESSOR_DEFINE (type);					\
       
   111   ATTRIBUTE_CHECKER_DEFINE (type);
       
   112 
       
   113 #define VALUE_HELPER_CPP(type)						\
       
   114   ATTRIBUTE_CHECKER_IMPLEMENT (type);					\
       
   115   ATTRIBUTE_CONVERTER_IMPLEMENT (type);					\
       
   116   ATTRIBUTE_VALUE_IMPLEMENT (type);
       
   117 
       
   118 
       
   119 
    41 #endif /* VALUE_HELPER_H */
   120 #endif /* VALUE_HELPER_H */