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
34namespace ns3 {
35
96template <typename T, typename BASE>
97Ptr<AttributeChecker>
98MakeSimpleAttributeChecker (std::string name, std::string underlying)
99{
104 struct SimpleAttributeChecker : public BASE
105 {
106 virtual bool Check (const AttributeValue &value) const
107 {
108 return dynamic_cast<const T *> (&value) != 0;
109 }
110 virtual std::string GetValueTypeName (void) const
111 {
112 return m_type;
113 }
114 virtual bool HasUnderlyingTypeInformation (void) const
115 {
116 return true;
117 }
118 virtual std::string GetUnderlyingTypeInformation (void) const
119 {
120 return m_underlying;
121 }
122 virtual Ptr<AttributeValue> Create (void) const
123 {
124 return ns3::Create<T> ();
125 }
126 virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const
127 {
128 const T *src = dynamic_cast<const T *> (&source);
129 T *dst = dynamic_cast<T *> (&destination);
130 if (src == 0 || dst == 0)
131 {
132 return false;
133 }
134 *dst = *src;
135 return true;
136 }
137 std::string m_type; // The name of the AttributeValue type.
138 std::string m_underlying; // The underlying attribute type name.
139 } *checker = new SimpleAttributeChecker ();
140 checker->m_type = name;
141 checker->m_underlying = underlying;
142 return Ptr<AttributeChecker> (checker, false);
143}
144
145}
146
161#define ATTRIBUTE_ACCESSOR_DEFINE(type) \
162 template <typename T1> \
163 Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1) \
164 { \
165 return MakeAccessorHelper<type ## Value> (a1); \
166 } \
167 template <typename T1, typename T2> \
168 Ptr<const AttributeAccessor> Make ## type ## Accessor (T1 a1, T2 a2) \
169 { \
170 return MakeAccessorHelper<type ## Value> (a1, a2); \
171 }
172
190#define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type,name) \
191 class name ## Value : public AttributeValue \
192 { \
193 public: \
194 name ## Value (); \
195 name ## Value (const type &value); \
196 void Set (const type &value); \
197 type Get (void) const; \
198 template <typename T> \
199 bool GetAccessor (T & value) const { \
200 value = T (m_value); \
201 return true; \
202 } \
203 virtual Ptr<AttributeValue> Copy (void) const; \
204 virtual std::string \
205 SerializeToString (Ptr<const AttributeChecker> checker) const; \
206 virtual bool \
207 DeserializeFromString (std::string value, \
208 Ptr<const AttributeChecker> checker); \
209 private: \
210 type m_value; \
211 }
212
213
225#define ATTRIBUTE_VALUE_DEFINE(name) \
226 ATTRIBUTE_VALUE_DEFINE_WITH_NAME (name,name)
227
228
244#define ATTRIBUTE_CONVERTER_DEFINE(type)
245
262#define ATTRIBUTE_CHECKER_DEFINE(type) \
263 class type ## Checker : public AttributeChecker {}; \
264 Ptr<const AttributeChecker> Make ## type ## Checker (void)
265
266
283#define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type,name) \
284 name ## Value::name ## Value () \
285 : m_value () {} \
286 name ## Value::name ## Value (const type &value) \
287 : m_value (value) {} \
288 void name ## Value::Set (const type &v) { \
289 m_value = v; \
290 } \
291 type name ## Value::Get (void) const { \
292 return m_value; \
293 } \
294 Ptr<AttributeValue> \
295 name ## Value::Copy (void) const { \
296 return ns3::Create<name ## Value> (*this); \
297 } \
298 std::string name ## Value::SerializeToString \
299 (Ptr<const AttributeChecker> checker) const { \
300 std::ostringstream oss; \
301 oss << m_value; \
302 return oss.str (); \
303 } \
304 bool name ## Value::DeserializeFromString \
305 (std::string value, Ptr<const AttributeChecker> checker) { \
306 std::istringstream iss; \
307 iss.str (value); \
308 iss >> m_value; \
309 NS_ABORT_MSG_UNLESS (iss.eof (), \
310 "Attribute value " << "\"" << value << "\"" << \
311 " is not properly formatted"); \
312 return !iss.bad () && !iss.fail (); \
313 }
314
329#define ATTRIBUTE_VALUE_IMPLEMENT(type) \
330 ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME (type,type)
331
332
344#define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
345 Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
346 return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
347 (# type "Value", # type); \
348 } \
349
363#define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type,name) \
364 Ptr<const AttributeChecker> Make ## type ## Checker (void) { \
365 return MakeSimpleAttributeChecker<type ## Value,type ## Checker> \
366 (# type "Value", name); \
367 } \
368
390#define ATTRIBUTE_HELPER_HEADER(type) \
391 ATTRIBUTE_VALUE_DEFINE (type); \
392 ATTRIBUTE_ACCESSOR_DEFINE (type); \
393 ATTRIBUTE_CHECKER_DEFINE (type)
394
412#define ATTRIBUTE_HELPER_CPP(type) \
413 ATTRIBUTE_CHECKER_IMPLEMENT (type); \
414 ATTRIBUTE_VALUE_IMPLEMENT (type)
415
416
417#endif /* ATTRIBUTE_HELPER_H */
NS_ABORT_x macro definitions.
ns3::MakeAccessorHelper declarations and template implementations.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Hold a value for an Attribute.
Definition: attribute.h:69
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
Ptr< AttributeChecker > MakeSimpleAttributeChecker(std::string name, std::string underlying)
A simple string-based attribute checker.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition: ptr.h:409
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:555