A Discrete-Event Network Simulator
API
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 "abort.h"
27 
34 namespace ns3 {
35 
96 template <typename T, typename BASE>
97 Ptr<AttributeChecker>
98 MakeSimpleAttributeChecker (std::string name, std::string underlying)
99 {
104  struct SimpleAttributeChecker : public BASE
105  {
106  virtual bool Check (const AttributeValue &value) const {
107  return dynamic_cast<const T *> (&value) != 0;
108  }
109  virtual std::string GetValueTypeName (void) const {
110  return m_type;
111  }
112  virtual bool HasUnderlyingTypeInformation (void) const {
113  return true;
114  }
115  virtual std::string GetUnderlyingTypeInformation (void) const {
116  return m_underlying;
117  }
118  virtual Ptr<AttributeValue> Create (void) const {
119  return ns3::Create<T> ();
120  }
121  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
122  const T *src = dynamic_cast<const T *> (&source);
123  T *dst = dynamic_cast<T *> (&destination);
124  if (src == 0 || dst == 0)
125  {
126  return false;
127  }
128  *dst = *src;
129  return true;
130  }
131  std::string m_type; // The name of the AttributeValue type.
132  std::string m_underlying; // The underlying attribute type name.
133  } *checker = new SimpleAttributeChecker ();
134  checker->m_type = name;
135  checker->m_underlying = underlying;
136  return Ptr<AttributeChecker> (checker, false);
137 }
138 
139 }
140 
155 #define ATTRIBUTE_ACCESSOR_DEFINE(type) \
156  template <typename T1> \
157  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1) \
158  { \
159  return MakeAccessorHelper<type ## Value> (a1); \
160  } \
161  template <typename T1, typename T2> \
162  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1, T2 a2) \
163  { \
164  return MakeAccessorHelper<type ## Value> (a1, a2); \
165  }
166 
184 #define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name) \
185  class name ## Value : public AttributeValue \
186  { \
187  public: \
188  name ## Value (); \
189  name ## Value (const type &value); \
190  void Set (const type &value); \
191  type Get (void) const; \
192  template <typename T> \
193  bool GetAccessor (T &value) const { \
194  value = T (m_value); \
195  return true; \
196  } \
197  virtual Ptr<AttributeValue> Copy (void) const; \
198  virtual std::string \
199  SerializeToString (Ptr<const AttributeChecker> checker) const; \
200  virtual bool \
201  DeserializeFromString (std::string value, \
202  Ptr<const AttributeChecker> checker); \
203  private: \
204  type m_value; \
205  }
206 
207 
219 #define ATTRIBUTE_VALUE_DEFINE(name) \
220  ATTRIBUTE_VALUE_DEFINE_WITH_NAME (name,name)
221 
222 
238 #define ATTRIBUTE_CONVERTER_DEFINE(type)
239 
256 #define ATTRIBUTE_CHECKER_DEFINE(type) \
257  class type ## Checker : public AttributeChecker {}; \
258  Ptr<const AttributeChecker> Make ## type ## Checker (void)
259 
260 
277 #define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name) \
278  name ## Value::name ## Value () \
279  : m_value () {} \
280  name ## Value::name ## Value (const type &value) \
281  : m_value (value) {} \
282  void name ## Value::Set (const type &v) { \
283  m_value = v; \
284  } \
285  type name ## Value::Get (void) const { \
286  return m_value; \
287  } \
288  Ptr<AttributeValue> \
289  name ## Value::Copy (void) const { \
290  return ns3::Create<name ## Value> (*this); \
291  } \
292  std::string name ## Value::SerializeToString \
293  (Ptr<const AttributeChecker> checker) const { \
294  std::ostringstream oss; \
295  oss << m_value; \
296  return oss.str (); \
297  } \
298  bool name ## Value::DeserializeFromString \
299  (std::string value, Ptr<const AttributeChecker> checker) { \
300  std::istringstream iss; \
301  iss.str (value); \
302  iss >> m_value; \
303  NS_ABORT_MSG_UNLESS (iss.eof (), \
304  "Attribute value " << "\"" << value << "\"" \
305  << " is not properly formatted"); \
306  return !iss.bad () && !iss.fail (); \
307  }
308 
323 #define ATTRIBUTE_VALUE_IMPLEMENT(type) \
324  ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (type,type)
325 
326 
338 #define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
339  Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
340  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
341  (# type "Value", # type); \
342  } \
343 
344 
357 #define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name) \
358  Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
359  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
360  (# type "Value", name); \
361  } \
362 
363 
384 #define ATTRIBUTE_HELPER_HEADER(type) \
385  ATTRIBUTE_VALUE_DEFINE (type); \
386  ATTRIBUTE_ACCESSOR_DEFINE (type); \
387  ATTRIBUTE_CHECKER_DEFINE (type)
388 
406 #define ATTRIBUTE_HELPER_CPP(type) \
407  ATTRIBUTE_CHECKER_IMPLEMENT (type); \
408  ATTRIBUTE_VALUE_IMPLEMENT (type)
409 
410 
411 #endif /* ATTRIBUTE_HELPER_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Hold a value for an Attribute.
Definition: attribute.h:68
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T > Create(void)
Create class instances by constructors with varying numbers of arguments and return them by Ptr...
Definition: ptr.h:516
Ptr< AttributeChecker > MakeSimpleAttributeChecker(std::string name, std::string underlying)
A simple string-based attribute checker.
ns3::MakeAccessorHelper declarations and template implementations.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:688
NS_ABORT_x macro definitions.