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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#ifndef ATTRIBUTE_HELPER_H
9#define ATTRIBUTE_HELPER_H
10
11#include "abort.h"
13#include "attribute.h"
14
15#include <sstream>
16
17/**
18 * @file
19 * @ingroup attributehelper
20 * Attribute helper (\c ATTRIBUTE_ )macros definition.
21 */
22
23namespace ns3
24{
25
26/**
27 * @ingroup attributes
28 * @defgroup attributehelper Attribute Helper
29 *
30 * All these macros can be used to generate automatically the code
31 * for subclasses of AttributeValue, AttributeAccessor, and, AttributeChecker,
32 * which can be used to give attribute powers to a normal class. i.e.,
33 * the user class can then effectively be made an attribute.
34 *
35 * There are two kinds of helper macros:
36 * -# The simple macros.
37 * - ATTRIBUTE_HELPER_HEADER(type)
38 * - ATTRIBUTE_HELPER_CPP(type)
39 * -# The more complex macros.
40 *
41 * The simple macros are implemented in terms of the complex
42 * macros and should generally be preferred over the complex macros.
43 *
44 * @note
45 * Because these macros generate class and function definitions, it's
46 * difficult to document the results directly. Instead, we use a
47 * set of functions in print-introspected-doxygen.cc to generate
48 * most of the API documentation. When introducing a new AttributeValue
49 * subclass (whether via these macros or by hand), the new type must also
50 * be added to the attributes[] list in PrintAttributeImplementations() in
51 * utils/print-introspected-doxygen.cc. Otherwise the introspected Doxygen
52 * documentation for the new value type, its accessors and its checker will
53 * not be generated.
54 */
55
56/**
57 * @ingroup attributehelper
58 * @defgroup attributeimpl Attribute Implementation
59 *
60 * These are the internal implementation functions for the Attribute
61 * system.
62 *
63 * Module code shouldn't need to call these directly. Instead,
64 * see \ref attributehelper.
65 *
66 * There are three versions of DoMakeAccessorHelperOne:
67 * - With a member variable: DoMakeAccessorHelperOne(U T::*)
68 * - With a class get functor: DoMakeAccessorHelperOne(U(T::*)() const)
69 * - With a class set method: DoMakeAccessorHelperOne(void(T::*)(U))
70 *
71 * There are two pairs of DoMakeAccessorHelperTwo (four total):
72 * - Taking two arguments, a set method and get functor, in either order,
73 * - With set methods returning \c void or \c bool.
74 */
75
76/**
77 * @ingroup attributeimpl
78 *
79 * A simple string-based attribute checker
80 *
81 * @tparam T @explicit The specific AttributeValue type used to represent
82 * the Attribute.
83 * @tparam BASE @explicit The AttributeChecker type corresponding to \pname{T}.
84 * @param [in] name The name of the AttributeValue type, essentially the
85 * string form of \pname{T}.
86 * @param [in] underlying Underlying type name.
87 * @return Ptr to AttributeChecker.
88 */
89template <typename T, typename BASE>
91MakeSimpleAttributeChecker(std::string name, std::string underlying)
92{
93 /**
94 * String-based AttributeChecker implementation.
95 * @extends AttributeChecker
96 */
97 struct SimpleAttributeChecker : public BASE
98 {
99 bool Check(const AttributeValue& value) const override
100 {
101 return dynamic_cast<const T*>(&value) != nullptr;
102 }
103
104 std::string GetValueTypeName() const override
105 {
106 if (m_type.rfind("ns3::", 0) == 0)
107 {
108 // m_type already starts with "ns3::"
109 return m_type;
110 }
111 return "ns3::" + m_type;
112 }
113
114 bool HasUnderlyingTypeInformation() const override
115 {
116 return true;
117 }
118
119 std::string GetUnderlyingTypeInformation() const override
120 {
121 return m_underlying;
122 }
123
124 Ptr<AttributeValue> Create() const override
125 {
126 return ns3::Create<T>();
127 }
128
129 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
130 {
131 const T* src = dynamic_cast<const T*>(&source);
132 T* dst = dynamic_cast<T*>(&destination);
133 if (src == nullptr || dst == nullptr)
134 {
135 return false;
136 }
137 *dst = *src;
138 return true;
139 }
140
141 std::string m_type; // The name of the AttributeValue type.
142 std::string m_underlying; // The underlying attribute type name.
143 }* checker = new SimpleAttributeChecker();
144
145 checker->m_type = name;
146 checker->m_underlying = underlying;
147 return Ptr<AttributeChecker>(checker, false);
148}
149
150} // namespace ns3
151
152/**
153 * @ingroup attributehelper
154 *
155 * Define the attribute accessor functions \c MakeTypeAccessor
156 * for class \pname{type}.
157 *
158 * @param [in] type The name of the class
159 *
160 * This macro defines and generates the code for the implementation
161 * of the \c MakeTypeAccessor template functions. This macro is typically
162 * invoked in a class header to allow users of this class to view and
163 * use the template functions defined here. This macro is implemented
164 * through the helper templates functions ns3::MakeAccessorHelper<>.
165 */
166#define ATTRIBUTE_ACCESSOR_DEFINE(type) \
167 template <typename T1> \
168 Ptr<const AttributeAccessor> Make##type##Accessor(T1 a1) \
169 { \
170 return MakeAccessorHelper<type##Value>(a1); \
171 } \
172 template <typename T1, typename T2> \
173 Ptr<const AttributeAccessor> Make##type##Accessor(T1 a1, T2 a2) \
174 { \
175 return MakeAccessorHelper<type##Value>(a1, a2); \
176 }
177
178/**
179 * @ingroup attributehelper
180 *
181 * Declare the attribute value class \pname{nameValue}
182 * for underlying class \pname{type}.
183 *
184 * @param [in] type The underlying type name token
185 * @param [in] name The token to use in defining the accessor name.
186 *
187 * This macro declares the class \c typeValue associated with class \c type.
188 * This macro is typically invoked in the class header file.
189 *
190 * This can be used directly for things like plain old data,
191 * such as \c std::string, to create the attribute value class
192 * StringValue, as in
193 * `ATTRIBUTE_VALUE_DEFINE_WITH_NAME(std::string, String);`
194 */
195#define ATTRIBUTE_VALUE_DEFINE_WITH_NAME(type, name) \
196 class name##Value : public AttributeValue \
197 { \
198 public: \
199 name##Value() = default; \
200 name##Value(const type& value); \
201 void Set(const type& value); \
202 type Get() const; \
203 template <typename T> \
204 bool GetAccessor(T& value) const \
205 { \
206 value = T(m_value); \
207 return true; \
208 } \
209 Ptr<AttributeValue> Copy() const override; \
210 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override; \
211 bool DeserializeFromString(std::string value, \
212 Ptr<const AttributeChecker> checker) override; \
213 \
214 private: \
215 type m_value; \
216 }
217
218/**
219 * @ingroup attributehelper
220 *
221 * Declare the attribute value class \pname{nameValue}
222 * for the class \pname{name}
223 *
224 * @param [in] name The name of the class.
225 *
226 * This macro declares the class \c nameValue associated to class \c name.
227 * This macro is typically invoked in the class header file.
228 */
229#define ATTRIBUTE_VALUE_DEFINE(name) ATTRIBUTE_VALUE_DEFINE_WITH_NAME(name, name)
230
231/**
232 * @ingroup attributehelper
233 *
234 * Define the conversion operators class \pname{type} and
235 * Attribute instances.
236 *
237 * @param [in] type The name of the class
238 *
239 * This macro defines the conversion operators for class \c type to and
240 * from instances of \c typeAttribute.
241 * Typically invoked in the class header file.
242 *
243 * @internal
244 * This appears to be unused in the current code base.
245 */
246#define ATTRIBUTE_CONVERTER_DEFINE(type)
247
248/**
249 * @ingroup attributehelper
250 *
251 * Declare the AttributeChecker class \pname{typeChecker}
252 * and the \c MaketypeChecker function for class \pname{type}.
253 *
254 * @param [in] type The name of the class
255 *
256 * This macro declares the \pname{typeChecker} class and the associated
257 * \c MaketypeChecker function.
258 *
259 * (Note that the \pname{typeChecker} class needs no implementation
260 * since it just inherits all its implementation from AttributeChecker.)
261 *
262 * Typically invoked in the class header file.
263 */
264#define ATTRIBUTE_CHECKER_DEFINE(type) \
265 class type##Checker : public AttributeChecker \
266 { \
267 }; \
268 Ptr<const AttributeChecker> Make##type##Checker()
269
270/**
271 * @ingroup attributehelper
272 *
273 * Define the class methods belonging to
274 * the attribute value class \pname{nameValue}
275 * of the underlying class \pname{type}.
276 *
277 * @param [in] type The underlying type name
278 * @param [in] name The token to use in defining the accessor name.
279 *
280 * This macro implements the \pname{typeValue} class methods
281 * (including the \pname{typeValue}%::%SerializeToString
282 * and \pname{typeValue}%::%DeserializeFromString methods).
283 *
284 * Typically invoked in the source file
285 */
286#define ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, name) \
287 name##Value::name##Value(const type& value) \
288 : m_value(value) \
289 { \
290 } \
291 void name##Value::Set(const type& v) \
292 { \
293 m_value = v; \
294 } \
295 type name##Value::Get() const \
296 { \
297 return m_value; \
298 } \
299 Ptr<AttributeValue> name##Value::Copy() const \
300 { \
301 return ns3::Create<name##Value>(*this); \
302 } \
303 std::string name##Value::SerializeToString(Ptr<const AttributeChecker> checker) const \
304 { \
305 std::ostringstream oss; \
306 oss << m_value; \
307 return oss.str(); \
308 } \
309 bool name##Value::DeserializeFromString(std::string value, \
310 Ptr<const AttributeChecker> checker) \
311 { \
312 if (value.empty()) \
313 { \
314 m_value = type(); \
315 return true; \
316 } \
317 std::istringstream iss; \
318 iss.str(value); \
319 iss >> m_value; \
320 NS_ABORT_MSG_UNLESS(iss.eof(), \
321 "Attribute value \"" << value << "\" is not properly formatted"); \
322 return !iss.bad() && !iss.fail(); \
323 }
324
325/**
326 * @ingroup attributehelper
327 *
328 * Define the class methods belonging to
329 * attribute value class \pname{typeValue} for class \pname{type}.
330 *
331 * @param [in] type The name of the class.
332 *
333 * This macro implements the \pname{typeValue} class methods
334 * (including the \pname{typeValue}%::%SerializeToString
335 * and \pname{typeValue}%::%DeserializeFromString methods).
336 *
337 * Typically invoked in the source file.
338 */
339#define ATTRIBUTE_VALUE_IMPLEMENT(type) ATTRIBUTE_VALUE_IMPLEMENT_WITH_NAME(type, type)
340
341/**
342 * @ingroup attributehelper
343 *
344 * Define the \c MaketypeChecker function for class \pname{type}.
345 *
346 * @param [in] type The name of the class
347 *
348 * This macro implements the \c MaketypeChecker function.
349 *
350 * Typically invoked in the source file..
351 */
352#define ATTRIBUTE_CHECKER_IMPLEMENT(type) \
353 Ptr<const AttributeChecker> Make##type##Checker() \
354 { \
355 return MakeSimpleAttributeChecker<type##Value, type##Checker>(#type "Value", #type); \
356 }
357
358/**
359 * @ingroup attributehelper
360 *
361 * Define the \c MaketypeChecker function for class \pname{type}.
362 *
363 * @param [in] type The name of the class.
364 * @param [in] name The string name of the underlying type.
365 *
366 * This macro implements the \c MaketypeChecker function
367 * for class \pname{type}.
368 *
369 * Typically invoked in the source file..
370 */
371#define ATTRIBUTE_CHECKER_IMPLEMENT_WITH_NAME(type, name) \
372 Ptr<const AttributeChecker> Make##type##Checker() \
373 { \
374 return MakeSimpleAttributeChecker<type##Value, type##Checker>(#type "Value", name); \
375 }
376
377/**
378 * @ingroup attributehelper
379 *
380 * Declare the attribute value, accessor and checkers for class \pname{type}
381 *
382 * @param [in] type The name of the class
383 *
384 * This macro declares:
385 *
386 * - The attribute value class \pname{typeValue},
387 *
388 * - The attribute accessor functions \c MaketypeAccessor,
389 *
390 * - The AttributeChecker class \pname{typeChecker},
391 * and the \c MaketypeChecker function,
392 *
393 * for class \pname{type}.
394 *
395 * This macro should be invoked outside of the class
396 * declaration in its public header.
397 */
398#define ATTRIBUTE_HELPER_HEADER(type) \
399 ATTRIBUTE_VALUE_DEFINE(type); \
400 ATTRIBUTE_ACCESSOR_DEFINE(type); \
401 ATTRIBUTE_CHECKER_DEFINE(type)
402
403/**
404 * @ingroup attributehelper
405 *
406 * Define the attribute value, accessor and checkers for class \pname{type}
407 *
408 * @param [in] type The name of the class
409 *
410 * This macro implements
411 *
412 * - The \pname{typeValue} class methods,
413 *
414 * - The \c MaketypeChecker function,
415 *
416 * for class \pname{type}.
417 *
418 * This macro should be invoked from the class implementation file.
419 */
420#define ATTRIBUTE_HELPER_CPP(type) \
421 ATTRIBUTE_CHECKER_IMPLEMENT(type); \
422 ATTRIBUTE_VALUE_IMPLEMENT(type)
423
424#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:59
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
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:492
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:667