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 {
100  /* String-based AttributeChecker implementation. */
101  struct SimpleAttributeChecker : public BASE
102  {
103  virtual bool Check (const AttributeValue &value) const {
104  return dynamic_cast<const T *> (&value) != 0;
105  }
106  virtual std::string GetValueTypeName (void) const {
107  return m_type;
108  }
109  virtual bool HasUnderlyingTypeInformation (void) const {
110  return true;
111  }
112  virtual std::string GetUnderlyingTypeInformation (void) const {
113  return m_underlying;
114  }
115  virtual Ptr<AttributeValue> Create (void) const {
116  return ns3::Create<T> ();
117  }
118  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
119  const T *src = dynamic_cast<const T *> (&source);
120  T *dst = dynamic_cast<T *> (&destination);
121  if (src == 0 || dst == 0)
122  {
123  return false;
124  }
125  *dst = *src;
126  return true;
127  }
128  std::string m_type; // The name of the AttributeValue type.
129  std::string m_underlying; // The underlying attribute type name.
130  } *checker = new SimpleAttributeChecker ();
131  checker->m_type = name;
132  checker->m_underlying = underlying;
133  return Ptr<AttributeChecker> (checker, false);
134 }
135 
136 }
137 
152 #define ATTRIBUTE_ACCESSOR_DEFINE(type) \
153  template <typename T1> \
154  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1) \
155  { \
156  return MakeAccessorHelper<type ## Value> (a1); \
157  } \
158  template <typename T1, typename T2> \
159  Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1, T2 a2) \
160  { \
161  return MakeAccessorHelper<type ## Value> (a1, a2); \
162  }
163 
180 #define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name) \
181  class name ## Value : public AttributeValue \
182  { \
183  public: \
184  name ## Value (); \
185  name ## Value (const type &value); \
186  void Set (const type &value); \
187  type Get (void) const; \
188  template <typename T> \
189  bool GetAccessor (T &value) const { \
190  value = T (m_value); \
191  return true; \
192  } \
193  virtual Ptr<AttributeValue> Copy (void) const; \
194  virtual std::string \
195  SerializeToString (Ptr<const AttributeChecker> checker) const; \
196  virtual bool \
197  DeserializeFromString (std::string value, \
198  Ptr<const AttributeChecker> checker); \
199  private: \
200  type m_value; \
201  };
202 
203 
215 #define ATTRIBUTE_VALUE_DEFINE(Name) \
216  ATTRIBUTE_VALUE_DEFINE_WITH_NAME (Name,Name)
217 
218 
234 #define ATTRIBUTE_CONVERTER_DEFINE(type)
235 
252 #define ATTRIBUTE_CHECKER_DEFINE(type) \
253  class type ## Checker : public AttributeChecker {}; \
254  Ptr<const AttributeChecker> Make ## type ## Checker (void)
255 
256 
273 #define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name) \
274  name ## Value::name ## Value () \
275  : m_value () {} \
276  name ## Value::name ## Value (const type &value) \
277  : m_value (value) {} \
278  void name ## Value::Set (const type &v) { \
279  m_value = v; \
280  } \
281  type name ## Value::Get (void) const { \
282  return m_value; \
283  } \
284  Ptr<AttributeValue> \
285  name ## Value::Copy (void) const { \
286  return ns3::Create<name ## Value> (*this); \
287  } \
288  std::string name ## Value::SerializeToString \
289  (Ptr<const AttributeChecker> checker) const { \
290  std::ostringstream oss; \
291  oss << m_value; \
292  return oss.str (); \
293  } \
294  bool name ## Value::DeserializeFromString \
295  (std::string value, Ptr<const AttributeChecker> checker) { \
296  std::istringstream iss; \
297  iss.str (value); \
298  iss >> m_value; \
299  NS_ABORT_MSG_UNLESS (iss.eof (), "Attribute value " << "\"" << value << "\"" << " is not properly formatted"); \
300  return !iss.bad () && !iss.fail (); \
301  }
302 
317 #define ATTRIBUTE_VALUE_IMPLEMENT(type) \
318  ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (type,type)
319 
320 
332 #define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
333  Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
334  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
335  (# type "Value", # type); \
336  } \
337 
338 
351 #define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name) \
352  Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
353  return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
354  (# type "Value", name); \
355  } \
356 
357 
378 #define ATTRIBUTE_HELPER_HEADER(type) \
379  ATTRIBUTE_VALUE_DEFINE (type); \
380  ATTRIBUTE_ACCESSOR_DEFINE (type); \
381  ATTRIBUTE_CHECKER_DEFINE (type);
382 
400 #define ATTRIBUTE_HELPER_CPP(type) \
401  ATTRIBUTE_CHECKER_IMPLEMENT (type); \
402  ATTRIBUTE_VALUE_IMPLEMENT (type);
403 
404 
405 #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:514
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:686
NS_ABORT_x macro definitions.