A Discrete-Event Network Simulator
API
attribute-helper.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef ATTRIBUTE_HELPER_H
20#define ATTRIBUTE_HELPER_H
21
22#include "abort.h"
24#include "attribute.h"
25
26#include <sstream>
27
34namespace ns3
35{
36
97template <typename T, typename BASE>
98Ptr<AttributeChecker>
99MakeSimpleAttributeChecker(std::string name, std::string underlying)
100{
105 struct SimpleAttributeChecker : public BASE
106 {
107 bool Check(const AttributeValue& value) const override
108 {
109 return dynamic_cast<const T*>(&value) != nullptr;
110 }
111
112 std::string GetValueTypeName() const override
113 {
114 return m_type;
115 }
116
117 bool HasUnderlyingTypeInformation() const override
118 {
119 return true;
120 }
121
122 std::string GetUnderlyingTypeInformation() const override
123 {
124 return m_underlying;
125 }
126
127 Ptr<AttributeValue> Create() const override
128 {
129 return ns3::Create<T>();
130 }
131
132 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
133 {
134 const T* src = dynamic_cast<const T*>(&source);
135 T* dst = dynamic_cast<T*>(&destination);
136 if (src == nullptr || dst == nullptr)
137 {
138 return false;
139 }
140 *dst = *src;
141 return true;
142 }
143
144 std::string m_type; // The name of the AttributeValue type.
145 std::string m_underlying; // The underlying attribute type name.
146 }* checker = new SimpleAttributeChecker();
147
148 checker->m_type = name;
149 checker->m_underlying = underlying;
150 return Ptr<AttributeChecker>(checker, false);
151}
152
153} // namespace ns3
154
169#define ATTRIBUTE_ACCESSOR_DEFINE(type) \
170 template <typename T1> \
171 Ptr<const AttributeAccessor> Make##type##Accessor(T1 a1) \
172 { \
173 return MakeAccessorHelper<type##Value>(a1); \
174 } \
175 template <typename T1, typename T2> \
176 Ptr<const AttributeAccessor> Make##type##Accessor(T1 a1, T2 a2) \
177 { \
178 return MakeAccessorHelper<type##Value>(a1, a2); \
179 }
180
198#define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type, name) \
199 class name##Value : public AttributeValue \
200 { \
201 public: \
202 name##Value(); \
203 name##Value(const type& value); \
204 void Set(const type& value); \
205 type Get() const; \
206 template <typename T> \
207 bool GetAccessor(T& value) const \
208 { \
209 value = T(m_value); \
210 return true; \
211 } \
212 Ptr<AttributeValue> Copy() const override; \
213 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override; \
214 bool DeserializeFromString(std::string value, \
215 Ptr<const AttributeChecker> checker) override; \
216 \
217 private: \
218 type m_value; \
219 }
220
232#define ATTRIBUTE_VALUE_DEFINE(name) ATTRIBUTE_VALUE_DEFINE_WITH_NAME(name, name)
233
249#define ATTRIBUTE_CONVERTER_DEFINE(type)
250
267#define ATTRIBUTE_CHECKER_DEFINE(type) \
268 class type##Checker : public AttributeChecker \
269 { \
270 }; \
271 Ptr<const AttributeChecker> Make##type##Checker()
272
289#define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, name) \
290 name##Value::name##Value() \
291 : m_value() \
292 { \
293 } \
294 name##Value::name##Value(const type& value) \
295 : m_value(value) \
296 { \
297 } \
298 void name##Value::Set(const type& v) \
299 { \
300 m_value = v; \
301 } \
302 type name##Value::Get() const \
303 { \
304 return m_value; \
305 } \
306 Ptr<AttributeValue> name##Value::Copy() const \
307 { \
308 return ns3::Create<name##Value>(*this); \
309 } \
310 std::string name##Value::SerializeToString(Ptr<const AttributeChecker> checker) const \
311 { \
312 std::ostringstream oss; \
313 oss << m_value; \
314 return oss.str(); \
315 } \
316 bool name##Value::DeserializeFromString(std::string value, \
317 Ptr<const AttributeChecker> checker) \
318 { \
319 std::istringstream iss; \
320 iss.str(value); \
321 iss >> m_value; \
322 NS_ABORT_MSG_UNLESS(iss.eof(), \
323 "Attribute value " \
324 << "\"" << value << "\"" \
325 << " is not properly formatted"); \
326 return !iss.bad() && !iss.fail(); \
327 }
328
343#define ATTRIBUTE_VALUE_IMPLEMENT(type) ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, type)
344
356#define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
357 Ptr<const AttributeChecker> Make##type##Checker() \
358 { \
359 return MakeSimpleAttributeChecker<type##Value, type##Checker>(#type "Value", #type); \
360 }
361
375#define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type, name) \
376 Ptr<const AttributeChecker> Make##type##Checker() \
377 { \
378 return MakeSimpleAttributeChecker<type##Value, type##Checker>(#type "Value", name); \
379 }
380
402#define ATTRIBUTE_HELPER_HEADER(type) \
403 ATTRIBUTE_VALUE_DEFINE(type); \
404 ATTRIBUTE_ACCESSOR_DEFINE(type); \
405 ATTRIBUTE_CHECKER_DEFINE(type)
406
424#define ATTRIBUTE_HELPER_CPP(type) \
425 ATTRIBUTE_CHECKER_IMPLEMENT(type); \
426 ATTRIBUTE_VALUE_IMPLEMENT(type)
427
428#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:70
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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:481
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:649
value
Definition: second.py:41