A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
attribute-helper.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #ifndef ATTRIBUTE_HELPER_H
21 #define ATTRIBUTE_HELPER_H
22 
23 #include "attribute.h"
25 #include <sstream>
26 #include "fatal-error.h"
27 
28 namespace ns3 {
29 
58 template <typename T, typename BASE>
59 Ptr<AttributeChecker>
60 MakeSimpleAttributeChecker (std::string name, std::string underlying)
61 {
62  struct SimpleAttributeChecker : public BASE
63  {
64  virtual bool Check (const AttributeValue &value) const {
65  return dynamic_cast<const T *> (&value) != 0;
66  }
67  virtual std::string GetValueTypeName (void) const {
68  return m_type;
69  }
70  virtual bool HasUnderlyingTypeInformation (void) const {
71  return true;
72  }
73  virtual std::string GetUnderlyingTypeInformation (void) const {
74  return m_underlying;
75  }
76  virtual Ptr<AttributeValue> Create (void) const {
77  return ns3::Create<T> ();
78  }
79  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
80  const T *src = dynamic_cast<const T *> (&source);
81  T *dst = dynamic_cast<T *> (&destination);
82  if (src == 0 || dst == 0)
83  {
84  return false;
85  }
86  *dst = *src;
87  return true;
88  }
89  std::string m_type;
90  std::string m_underlying;
91  } *checker = new SimpleAttributeChecker ();
92  checker->m_type = name;
93  checker->m_underlying = underlying;
94  return Ptr<AttributeChecker> (checker, false);
95 }
96 
97 }
98 
113 #define ATTRIBUTE_ACCESSOR_DEFINE(type) \
114  template <typename T1> \
115  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1) \
116  { \
117  return MakeAccessorHelper<type ## Value> (a1); \
118  } \
119  template <typename T1, typename T2> \
120  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1, T2 a2) \
121  { \
122  return MakeAccessorHelper<type ## Value> (a1, a2); \
123  }
124 
141 #define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name) \
142  class name ## Value : public AttributeValue \
143  { \
144  public: \
145  name ## Value (); \
146  name ## Value (const type &value); \
147  void Set (const type &value); \
148  type Get (void) const; \
149  template <typename T> \
150  bool GetAccessor (T &value) const { \
151  value = T (m_value); \
152  return true; \
153  } \
154  virtual Ptr<AttributeValue> Copy (void) const; \
155  virtual std::string \
156  SerializeToString (Ptr<const AttributeChecker> checker) const; \
157  virtual bool \
158  DeserializeFromString (std::string value, \
159  Ptr<const AttributeChecker> checker); \
160  private: \
161  type m_value; \
162  };
163 
164 
176 #define ATTRIBUTE_VALUE_DEFINE(Name) \
177  ATTRIBUTE_VALUE_DEFINE_WITH_NAME (Name,Name)
178 
179 
195 #define ATTRIBUTE_CONVERTER_DEFINE(type)
196 
213 #define ATTRIBUTE_CHECKER_DEFINE(type) \
214  class type ## Checker : public AttributeChecker {}; \
215  Ptr<const AttributeChecker> Make ## type ## Checker (void)
216 
217 
234 #define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name) \
235  name ## Value::name ## Value () \
236  : m_value () {} \
237  name ## Value::name ## Value (const type &value) \
238  : m_value (value) {} \
239  void name ## Value::Set (const type &v) { \
240  m_value = v; \
241  } \
242  type name ## Value::Get (void) const { \
243  return m_value; \
244  } \
245  Ptr<AttributeValue> \
246  name ## Value::Copy (void) const { \
247  return ns3::Create<name ## Value> (*this); \
248  } \
249  std::string name ## Value::SerializeToString \
250  (Ptr<const AttributeChecker> checker) const { \
251  std::ostringstream oss; \
252  oss << m_value; \
253  return oss.str (); \
254  } \
255  bool name ## Value::DeserializeFromString \
256  (std::string value, Ptr<const AttributeChecker> checker) { \
257  std::istringstream iss; \
258  iss.str (value); \
259  iss >> m_value; \
260  return !iss.bad () && !iss.fail (); \
261  }
262 
277 #define ATTRIBUTE_VALUE_IMPLEMENT(type) \
278  ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (type,type)
279 
280 
292 #define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
293  Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
294  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
295  (# type "Value", # type); \
296  } \
297 
298 
311 #define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name) \
312  Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
313  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
314  (# type "Value", name); \
315  } \
316 
317 
338 #define ATTRIBUTE_HELPER_HEADER(type) \
339  ATTRIBUTE_VALUE_DEFINE (type); \
340  ATTRIBUTE_ACCESSOR_DEFINE (type); \
341  ATTRIBUTE_CHECKER_DEFINE (type);
342 
360 #define ATTRIBUTE_HELPER_CPP(type) \
361  ATTRIBUTE_CHECKER_IMPLEMENT (type); \
362  ATTRIBUTE_VALUE_IMPLEMENT (type);
363 
364 
365 #endif /* ATTRIBUTE_HELPER_H */
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Hold a value for an Attribute.
Definition: attribute.h:56
Ptr< T > Create(void)
Definition: ptr.h:232
Ptr< AttributeChecker > MakeSimpleAttributeChecker(std::string name, std::string underlying)
A simple string-based attribute checker.
Ptr< T > Copy(Ptr< T > object)
Definition: ptr.h:388