00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef ATTRIBUTE_HELPER_H
00021 #define ATTRIBUTE_HELPER_H
00022
00023 #include "attribute.h"
00024 #include "attribute-accessor-helper.h"
00025 #include <sstream>
00026 #include "fatal-error.h"
00027
00028 namespace ns3 {
00029
00030 template <typename T, typename BASE>
00031 Ptr<AttributeChecker>
00032 MakeSimpleAttributeChecker (std::string name, std::string underlying)
00033 {
00034 struct SimpleAttributeChecker : public BASE
00035 {
00036 virtual bool Check (const AttributeValue &value) const {
00037 return dynamic_cast<const T *> (&value) != 0;
00038 }
00039 virtual std::string GetValueTypeName (void) const {
00040 return m_type;
00041 }
00042 virtual bool HasUnderlyingTypeInformation (void) const {
00043 return true;
00044 }
00045 virtual std::string GetUnderlyingTypeInformation (void) const {
00046 return m_underlying;
00047 }
00048 virtual Ptr<AttributeValue> Create (void) const {
00049 return ns3::Create<T> ();
00050 }
00051 virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
00052 const T *src = dynamic_cast<const T *> (&source);
00053 T *dst = dynamic_cast<T *> (&destination);
00054 if (src == 0 || dst == 0)
00055 {
00056 return false;
00057 }
00058 *dst = *src;
00059 return true;
00060 }
00061 std::string m_type;
00062 std::string m_underlying;
00063 } *checker = new SimpleAttributeChecker ();
00064 checker->m_type = name;
00065 checker->m_underlying = underlying;
00066 return Ptr<AttributeChecker> (checker, false);
00067 }
00068
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 #define ATTRIBUTE_ACCESSOR_DEFINE(type) \
00101 template <typename T1> \
00102 Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1) \
00103 { \
00104 return MakeAccessorHelper<type##Value> (a1); \
00105 } \
00106 template <typename T1, typename T2> \
00107 Ptr<const AttributeAccessor> Make##type##Accessor (T1 a1, T2 a2) \
00108 { \
00109 return MakeAccessorHelper<type##Value> (a1, a2); \
00110 }
00111
00112 #define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name) \
00113 class name##Value : public AttributeValue \
00114 { \
00115 public: \
00116 name##Value (); \
00117 name##Value (const type &value); \
00118 void Set (const type &value); \
00119 type Get (void) const; \
00120 template <typename T> \
00121 bool GetAccessor (T &value) const { \
00122 value = T (m_value); \
00123 return true; \
00124 } \
00125 virtual Ptr<AttributeValue> Copy (void) const; \
00126 virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const; \
00127 virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker); \
00128 private: \
00129 type m_value; \
00130 };
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140 #define ATTRIBUTE_VALUE_DEFINE(type) \
00141 ATTRIBUTE_VALUE_DEFINE_WITH_NAME (type,type)
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 #define ATTRIBUTE_CONVERTER_DEFINE(type)
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 #define ATTRIBUTE_CHECKER_DEFINE(type) \
00163 class type##Checker : public AttributeChecker {}; \
00164 Ptr<const AttributeChecker> Make##type##Checker (void); \
00165
00166
00167 #define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name) \
00168 name##Value::name##Value () \
00169 : m_value () {} \
00170 name##Value::name##Value (const type &value) \
00171 : m_value (value) {} \
00172 void name##Value::Set (const type &v) { \
00173 m_value = v; \
00174 } \
00175 type name##Value::Get (void) const { \
00176 return m_value; \
00177 } \
00178 Ptr<AttributeValue> \
00179 name##Value::Copy (void) const { \
00180 return ns3::Create<name##Value> (*this); \
00181 } \
00182 std::string \
00183 name##Value::SerializeToString (Ptr<const AttributeChecker> checker) const { \
00184 std::ostringstream oss; \
00185 oss << m_value; \
00186 return oss.str (); \
00187 } \
00188 bool \
00189 name##Value::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker) { \
00190 std::istringstream iss; \
00191 iss.str (value); \
00192 iss >> m_value; \
00193 return !iss.bad () && !iss.fail (); \
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 #define ATTRIBUTE_VALUE_IMPLEMENT(type) \
00206 ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,type)
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 #define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
00217 Ptr<const AttributeChecker> Make##type##Checker (void) \
00218 { \
00219 return MakeSimpleAttributeChecker<type##Value,type##Checker> (#type "Value", #type); \
00220 } \
00221
00222 #define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name) \
00223 Ptr<const AttributeChecker> Make##type##Checker (void) \
00224 { \
00225 return MakeSimpleAttributeChecker<type##Value,type##Checker> (#type "Value", name); \
00226 } \
00227
00228
00229
00230
00231
00232
00233
00234
00235 #define ATTRIBUTE_HELPER_HEADER(type) \
00236 ATTRIBUTE_VALUE_DEFINE (type); \
00237 ATTRIBUTE_ACCESSOR_DEFINE (type); \
00238 ATTRIBUTE_CHECKER_DEFINE (type);
00239
00240
00241
00242
00243
00244
00245
00246 #define ATTRIBUTE_HELPER_CPP(type) \
00247 ATTRIBUTE_CHECKER_IMPLEMENT (type); \
00248 ATTRIBUTE_VALUE_IMPLEMENT (type);
00249
00250
00251 #endif