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 
109 #define ATTRIBUTE_ACCESSOR_DEFINE(type) \
110  template <typename T1> \
111  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1) \
112  { \
113  return MakeAccessorHelper<type ## Value> (a1); \
114  } \
115  template <typename T1, typename T2> \
116  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1, T2 a2) \
117  { \
118  return MakeAccessorHelper<type ## Value> (a1, a2); \
119  }
120 
125 #define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name) \
126  class name ## Value : public AttributeValue \
127  { \
128 public: \
129  name ## Value (); \
130  name ## Value (const type &value); \
131  void Set (const type &value); \
132  type Get (void) const; \
133  template <typename T> \
134  bool GetAccessor (T &value) const { \
135  value = T (m_value); \
136  return true; \
137  } \
138  virtual Ptr<AttributeValue> Copy (void) const; \
139  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const; \
140  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker); \
141 private: \
142  type m_value; \
143  };
144 
145 
153 #define ATTRIBUTE_VALUE_DEFINE(type) \
154  ATTRIBUTE_VALUE_DEFINE_WITH_NAME (type,type)
155 
156 
165 #define ATTRIBUTE_CONVERTER_DEFINE(type)
166 
175 #define ATTRIBUTE_CHECKER_DEFINE(type) \
176  class type ## Checker : public AttributeChecker {}; \
177  Ptr<const AttributeChecker> Make ## type ## Checker (void); \
178 
179 
184 #define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name) \
185  name ## Value::name ## Value () \
186  : m_value () {} \
187  name ## Value::name ## Value (const type &value) \
188  : m_value (value) {} \
189  void name ## Value::Set (const type &v) { \
190  m_value = v; \
191  } \
192  type name ## Value::Get (void) const { \
193  return m_value; \
194  } \
195  Ptr<AttributeValue> \
196  name ## Value::Copy (void) const { \
197  return ns3::Create<name ## Value> (*this); \
198  } \
199  std::string \
200  name ## Value::SerializeToString (Ptr<const AttributeChecker> checker) const { \
201  std::ostringstream oss; \
202  oss << m_value; \
203  return oss.str (); \
204  } \
205  bool \
206  name ## Value::DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker) { \
207  std::istringstream iss; \
208  iss.str (value); \
209  iss >> m_value; \
210  return !iss.bad () && !iss.fail (); \
211  }
212 
222 #define ATTRIBUTE_VALUE_IMPLEMENT(type) \
223  ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (type,type)
224 
225 
233 #define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
234  Ptr<const AttributeChecker> Make ## type ## Checker (void) \
235  { \
236  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> (# type "Value", # type); \
237  } \
238 
239 
243 #define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name) \
244  Ptr<const AttributeChecker> Make ## type ## Checker (void) \
245  { \
246  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> (# type "Value", name); \
247  } \
248 
249 
256 #define ATTRIBUTE_HELPER_HEADER(type) \
257  ATTRIBUTE_VALUE_DEFINE (type); \
258  ATTRIBUTE_ACCESSOR_DEFINE (type); \
259  ATTRIBUTE_CHECKER_DEFINE (type);
260 
267 #define ATTRIBUTE_HELPER_CPP(type) \
268  ATTRIBUTE_CHECKER_IMPLEMENT (type); \
269  ATTRIBUTE_VALUE_IMPLEMENT (type);
270 
271 
272 #endif /* ATTRIBUTE_HELPER_H */
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
Hold a value for an Attribute.
Definition: attribute.h:56
Ptr< T > Create(void)
Definition: ptr.h:231
Ptr< AttributeChecker > MakeSimpleAttributeChecker(std::string name, std::string underlying)
A simple string-based attribute checker.
Ptr< T > Copy(Ptr< T > object)
Definition: ptr.h:387