A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
attribute.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_H
20#define ATTRIBUTE_H
21
22#include "ptr.h"
23#include "simple-ref-count.h"
24
25#include <stdint.h>
26#include <string>
27
28/**
29 * \file
30 * \ingroup attributes
31 * ns3::AttributeValue, ns3::AttributeAccessor and
32 * ns3::AttributeChecker declarations.
33 */
34
35namespace ns3
36{
37
38class AttributeAccessor;
39class AttributeChecker;
40class Attribute;
41class ObjectBase;
42
43/**
44 *
45 * \ingroup core
46 * \defgroup attributes Attributes
47 *
48 * The \c ns-3 attribute system is the mechanism used in \c ns-3 to
49 * organize, document, and modify the *values* used by the various
50 * component models.
51 *
52 * Attributes also enable the tracing and statistics gathering
53 * in the simulator.
54 *
55 * See \ref attributehelper for macros to ease the declaration
56 * and definition of Attributes.
57 */
58
59/**
60 *
61 * \ingroup attributes
62 *
63 * \brief Hold a value for an Attribute.
64 *
65 * Instances of this class should always be wrapped into an Attribute object.
66 * Most subclasses of this base class are implemented by the
67 * ATTRIBUTE_HELPER_* macros.
68 */
69class AttributeValue : public SimpleRefCount<AttributeValue>
70{
71 public:
73 virtual ~AttributeValue();
74
75 /**
76 * \returns a deep copy of this class, wrapped into an Attribute object.
77 */
78 virtual Ptr<AttributeValue> Copy() const = 0;
79 /**
80 * \param [in] checker The checker associated to the attribute
81 * \returns A string representation of this value.
82 *
83 * In most cases, this method will not make any use of the checker argument.
84 * However, in a very limited set of cases, the checker argument is needed to
85 * perform proper serialization. A nice example of code which needs it is
86 * the EnumValue::SerializeToString code.
87 */
88 virtual std::string SerializeToString(Ptr<const AttributeChecker> checker) const = 0;
89 /**
90 * \param [in] value A string representation of the value
91 * \param [in] checker A pointer to the checker associated to the attribute.
92 * \returns true if the input string was correctly-formatted and could be
93 * successfully deserialized, false otherwise.
94 *
95 * Upon return of this function, this AttributeValue instance contains
96 * the deserialized value.
97 * In most cases, this method will not make any use of the checker argument.
98 * However, in a very limited set of cases, the checker argument is needed to
99 * perform proper serialization. A nice example of code which needs it is
100 * the EnumValue::SerializeToString code.
101 */
102 virtual bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) = 0;
103};
104
105/**
106 * \brief allow setting and getting the value of an attribute.
107 *
108 * \ingroup attributes
109 *
110 * The goal of this class is to hide from the user how an attribute
111 * is actually set or get to or from a class instance. Implementations
112 * of this base class are usually provided through the MakeAccessorHelper
113 * template functions, hidden behind an ATTRIBUTE_HELPER_* macro.
114 */
115class AttributeAccessor : public SimpleRefCount<AttributeAccessor>
116{
117 public:
119 virtual ~AttributeAccessor();
120
121 /**
122 * \param [in,out] object The object instance to set the value in
123 * \param [in] value The value to set
124 * \returns true if the value could be set successfully, false otherwise.
125 *
126 * This method expects that the caller has checked that the input value is
127 * valid with AttributeChecker::Check.
128 */
129 virtual bool Set(ObjectBase* object, const AttributeValue& value) const = 0;
130 /**
131 * \param [in,out] object The object instance to get the value from
132 * \param [out] attribute A pointer to where the value should be set.
133 * \returns true if the value could be read successfully, and
134 * stored in the input value, false otherwise.
135 *
136 * This method expects that the caller has checked that the input value is
137 * valid with AttributeChecker::Check.
138 */
139 virtual bool Get(const ObjectBase* object, AttributeValue& attribute) const = 0;
140
141 /**
142 * \return true if this accessor supports the Get operation, false
143 * otherwise.
144 */
145 virtual bool HasGetter() const = 0;
146 /**
147 * \return true if this accessor supports the Set operation, false
148 * otherwise.
149 */
150 virtual bool HasSetter() const = 0;
151};
152
153/**
154 * \brief Represent the type of an attribute
155 *
156 * \ingroup attributes
157 *
158 * Each type of attribute has an associated unique AttributeChecker
159 * subclass. The type of the subclass can be safely used by users
160 * to infer the type of the associated attribute. i.e., we expect
161 * binding authors to use the checker associated to an attribute
162 * to detect the type of the associated attribute.
163 *
164 * Most subclasses of this base class are implemented by the
165 * ATTRIBUTE_HELPER_HEADER and ATTRIBUTE_HELPER_CPP macros.
166 */
167class AttributeChecker : public SimpleRefCount<AttributeChecker>
168{
169 public:
171 virtual ~AttributeChecker();
172
173 /**
174 * Create a valid value from the argument value,
175 * or reinterpret the argument as a string.
176 *
177 * \param [in] value The AttributeValue to check
178 * \return Ptr to a valid value
179 */
181 /**
182 * \param [in] value A pointer to the value to check
183 * \returns true if the input value is both of the right type
184 * and if its value is within the requested range. Returns
185 * false otherwise.
186 */
187 virtual bool Check(const AttributeValue& value) const = 0;
188 /**
189 * \returns the c++ fully-qualified typename of the subclass
190 * of the ns3::AttributeValue base class which is associated
191 * to this checker.
192 *
193 * A typical return value here is FooValue where Foo is the name of the
194 * type being wrapped.
195 */
196 virtual std::string GetValueTypeName() const = 0;
197 /**
198 * \returns true if this checker has information about the underlying
199 * C++ type, false otherwise.
200 *
201 * If this method returns false, the return value of the GetUnderlyingTypeInformation
202 * method cannot be relied upon.
203 */
204 virtual bool HasUnderlyingTypeInformation() const = 0;
205 /**
206 * \returns a human-readable representation of information about
207 * the underlying C++ type.
208 */
209 virtual std::string GetUnderlyingTypeInformation() const = 0;
210 /**
211 * \returns a new instance of an AttributeValue (wrapper in an Attribute
212 * instance) which matches the type of the underlying attribute.
213 *
214 * This method is typically used to create a temporary variable prior
215 * to calling Attribute::DeserializeFromString.
216 */
217 virtual Ptr<AttributeValue> Create() const = 0;
218 /**
219 * Copy the source to the destination
220
221 * \param [in] source Source AttributeValue
222 * \param [out] destination Destination AttributeValue
223 * \return true if copy was successful
224 */
225 virtual bool Copy(const AttributeValue& source, AttributeValue& destination) const = 0;
226};
227
228/**
229 * \ingroup attributes
230 * \defgroup attribute_EmptyAttribute EmptyAttribute Attribute
231 * AttributeValue implementation for EmptyAttribute
232 */
233
234/**
235 * \brief A class for an empty attribute value.
236 *
237 * \ingroup attribute_EmptyAttribute
238 *
239 * \see AttributeValue
240 */
242{
243 public:
244 /** Default constructor. */
246
247 private:
248 /**
249 * \returns a deep copy of this class, wrapped into an Attribute object.
250 */
251 Ptr<AttributeValue> Copy() const override;
252 /**
253 * \param [in] checker The checker associated to the attribute
254 * \returns a string representation of this value.
255 *
256 * In the EmptyAttributeValue case, the string returned will be simply ""
257 */
258 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
259 /**
260 * \param [in] value A string representation of the value
261 * \param [in] checker A pointer to the checker associated to the attribute.
262 * \returns true if the input string was correctly-formatted and could be
263 * successfully deserialized, false otherwise.
264 *
265 * In the trivial case of EmptyAttributeValue, this should always return true
266 */
267 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
268};
269
270/**
271 * \brief An accessor for EmptyAttributeValue
272 *
273 * \ingroup attribute_EmptyAttribute
274 *
275 * Does nothing, since every EmptyAttributeValue is the same.
276 */
278{
279 public:
281 ~EmptyAttributeAccessor() override;
282 bool Set(ObjectBase* object, const AttributeValue& value) const override;
283 bool Get(const ObjectBase* object, AttributeValue& attribute) const override;
284 bool HasGetter() const override;
285 bool HasSetter() const override;
286};
287
288/**
289 * \ingroup attribute_EmptyAttribute
290 *
291 * \brief Create an empty AttributeAccessor.
292 *
293 * \returns The empty AttributeAccessor (runtime exception if used)
294 */
297{
299}
300
301/**
302 * \brief A checker for EmptyAttributeValue
303 *
304 * \ingroup attribute_EmptyAttribute
305 *
306 * Does nothing, since every EmptyAttributeValue does not contain anything and
307 * is, of course, valid.
308 */
310{
311 public:
313 ~EmptyAttributeChecker() override;
314 bool Check(const AttributeValue& value) const override;
315 std::string GetValueTypeName() const override;
316 bool HasUnderlyingTypeInformation() const override;
317 std::string GetUnderlyingTypeInformation() const override;
318 Ptr<AttributeValue> Create() const override;
319 bool Copy(const AttributeValue& source, AttributeValue& destination) const override;
320};
321
322/**
323 * \ingroup attribute_EmptyAttribute
324 *
325 * \brief Create an empty AttributeChecker.
326 *
327 * \returns The empty AttributeChecker (runtime exception if used)
328 */
329static inline Ptr<AttributeChecker>
331{
332 return Ptr<AttributeChecker>(new EmptyAttributeChecker(), false);
333}
334
335} // namespace ns3
336
337#endif /* ATTRIBUTE_H */
allow setting and getting the value of an attribute.
Definition: attribute.h:116
virtual bool Get(const ObjectBase *object, AttributeValue &attribute) const =0
virtual bool Set(ObjectBase *object, const AttributeValue &value) const =0
virtual bool HasSetter() const =0
virtual bool HasGetter() const =0
virtual ~AttributeAccessor()
Definition: attribute.cc:51
Represent the type of an attribute.
Definition: attribute.h:168
virtual bool HasUnderlyingTypeInformation() const =0
virtual ~AttributeChecker()
Definition: attribute.cc:59
virtual std::string GetValueTypeName() const =0
virtual bool Check(const AttributeValue &value) const =0
virtual bool Copy(const AttributeValue &source, AttributeValue &destination) const =0
Copy the source to the destination.
Ptr< AttributeValue > CreateValidValue(const AttributeValue &value) const
Create a valid value from the argument value, or reinterpret the argument as a string.
Definition: attribute.cc:64
virtual std::string GetUnderlyingTypeInformation() const =0
virtual Ptr< AttributeValue > Create() const =0
Hold a value for an Attribute.
Definition: attribute.h:70
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)=0
virtual Ptr< AttributeValue > Copy() const =0
virtual ~AttributeValue()
Definition: attribute.cc:43
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const =0
An accessor for EmptyAttributeValue.
Definition: attribute.h:278
bool Get(const ObjectBase *object, AttributeValue &attribute) const override
Definition: attribute.cc:135
bool Set(ObjectBase *object, const AttributeValue &value) const override
Definition: attribute.cc:128
~EmptyAttributeAccessor() override
Definition: attribute.cc:123
bool HasSetter() const override
Definition: attribute.cc:148
bool HasGetter() const override
Definition: attribute.cc:142
A checker for EmptyAttributeValue.
Definition: attribute.h:310
std::string GetValueTypeName() const override
Definition: attribute.cc:169
bool Check(const AttributeValue &value) const override
Definition: attribute.cc:163
Ptr< AttributeValue > Create() const override
Definition: attribute.cc:187
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition: attribute.cc:194
bool HasUnderlyingTypeInformation() const override
Definition: attribute.cc:175
~EmptyAttributeChecker() override
Definition: attribute.cc:158
std::string GetUnderlyingTypeInformation() const override
Definition: attribute.cc:181
A class for an empty attribute value.
Definition: attribute.h:242
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: attribute.cc:105
EmptyAttributeValue()
Default constructor.
Definition: attribute.cc:92
Ptr< AttributeValue > Copy() const override
Definition: attribute.cc:98
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: attribute.cc:112
Anchor the ns-3 type and attribute system.
Definition: object-base.h:173
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
A template-based reference counting class.
static Ptr< AttributeChecker > MakeEmptyAttributeChecker()
Create an empty AttributeChecker.
Definition: attribute.h:330
static Ptr< const AttributeAccessor > MakeEmptyAttributeAccessor()
Create an empty AttributeAccessor.
Definition: attribute.h:296
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ptr smart pointer declaration and implementation.
ns3::SimpleRefCount declaration and template implementation.