A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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 if (m_type.rfind("ns3::", 0) == 0)
115 {
116 // m_type already starts with "ns3::"
117 return m_type;
118 }
119 return "ns3::" + m_type;
120 }
121
122 bool HasUnderlyingTypeInformation() const override
123 {
124 return true;
125 }
126
127 std::string GetUnderlyingTypeInformation() const override
128 {
129 return m_underlying;
130 }
131
132 Ptr<AttributeValue> Create() const override
133 {
134 return ns3::Create<T>();
135 }
136
137 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
138 {
139 const T* src = dynamic_cast<const T*>(&source);
140 T* dst = dynamic_cast<T*>(&destination);
141 if (src == nullptr || dst == nullptr)
142 {
143 return false;
144 }
145 *dst = *src;
146 return true;
147 }
148
149 std::string m_type; // The name of the AttributeValue type.
150 std::string m_underlying; // The underlying attribute type name.
151 }* checker = new SimpleAttributeChecker();
152
153 checker->m_type = name;
154 checker->m_underlying = underlying;
155 return Ptr<AttributeChecker>(checker, false);
156}
157
158} // namespace ns3
159
174#define ATTRIBUTE_ACCESSOR_DEFINE(type) \
175 template <typename T1> \
176 Ptr<const AttributeAccessor> Make##type##Accessor(T1 a1) \
177 { \
178 return MakeAccessorHelper<type##Value>(a1); \
179 } \
180 template <typename T1, typename T2> \
181 Ptr<const AttributeAccessor> Make##type##Accessor(T1 a1, T2 a2) \
182 { \
183 return MakeAccessorHelper<type##Value>(a1, a2); \
184 }
185
203#define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type, name) \
204 class name##Value : public AttributeValue \
205 { \
206 public: \
207 name##Value(); \
208 name##Value(const type& value); \
209 void Set(const type& value); \
210 type Get() const; \
211 template <typename T> \
212 bool GetAccessor(T& value) const \
213 { \
214 value = T(m_value); \
215 return true; \
216 } \
217 Ptr<AttributeValue> Copy() const override; \
218 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override; \
219 bool DeserializeFromString(std::string value, \
220 Ptr<const AttributeChecker> checker) override; \
221 \
222 private: \
223 type m_value; \
224 }
225
237#define ATTRIBUTE_VALUE_DEFINE(name) ATTRIBUTE_VALUE_DEFINE_WITH_NAME(name, name)
238
254#define ATTRIBUTE_CONVERTER_DEFINE(type)
255
272#define ATTRIBUTE_CHECKER_DEFINE(type) \
273 class type##Checker : public AttributeChecker \
274 { \
275 }; \
276 Ptr<const AttributeChecker> Make##type##Checker()
277
294#define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, name) \
295 name##Value::name##Value() \
296 : m_value() \
297 { \
298 } \
299 name##Value::name##Value(const type& value) \
300 : m_value(value) \
301 { \
302 } \
303 void name##Value::Set(const type& v) \
304 { \
305 m_value = v; \
306 } \
307 type name##Value::Get() const \
308 { \
309 return m_value; \
310 } \
311 Ptr<AttributeValue> name##Value::Copy() const \
312 { \
313 return ns3::Create<name##Value>(*this); \
314 } \
315 std::string name##Value::SerializeToString(Ptr<const AttributeChecker> checker) const \
316 { \
317 std::ostringstream oss; \
318 oss << m_value; \
319 return oss.str(); \
320 } \
321 bool name##Value::DeserializeFromString(std::string value, \
322 Ptr<const AttributeChecker> checker) \
323 { \
324 std::istringstream iss; \
325 iss.str(value); \
326 iss >> m_value; \
327 NS_ABORT_MSG_UNLESS(iss.eof(), \
328 "Attribute value " \
329 << "\"" << value << "\"" \
330 << " is not properly formatted"); \
331 return !iss.bad() && !iss.fail(); \
332 }
333
348#define ATTRIBUTE_VALUE_IMPLEMENT(type) ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, type)
349
361#define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
362 Ptr<const AttributeChecker> Make##type##Checker() \
363 { \
364 return MakeSimpleAttributeChecker<type##Value, type##Checker>(#type "Value", #type); \
365 }
366
380#define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type, name) \
381 Ptr<const AttributeChecker> Make##type##Checker() \
382 { \
383 return MakeSimpleAttributeChecker<type##Value, type##Checker>(#type "Value", name); \
384 }
385
407#define ATTRIBUTE_HELPER_HEADER(type) \
408 ATTRIBUTE_VALUE_DEFINE(type); \
409 ATTRIBUTE_ACCESSOR_DEFINE(type); \
410 ATTRIBUTE_CHECKER_DEFINE(type)
411
429#define ATTRIBUTE_HELPER_CPP(type) \
430 ATTRIBUTE_CHECKER_IMPLEMENT(type); \
431 ATTRIBUTE_VALUE_IMPLEMENT(type)
432
433#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:77
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:442
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:610