A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-base.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 OBJECT_BASE_H
9#define OBJECT_BASE_H
10
11#include "callback.h"
12#include "type-id.h"
13#include "warnings.h"
14
15#include <list>
16#include <string>
17
18/**
19 * @file
20 * @ingroup object
21 * ns3::ObjectBase declaration and
22 * NS_OBJECT_ENSURE_REGISTERED() macro definition.
23 */
24
25/**
26 * @ingroup object
27 * @brief Register an Object subclass with the TypeId system.
28 *
29 * This macro should be invoked once for every class which
30 * defines a new GetTypeId method.
31 *
32 * If the class is in a namespace, then the macro call should also be
33 * in the namespace.
34 */
35#define NS_OBJECT_ENSURE_REGISTERED(type) \
36 static struct Object##type##RegistrationClass \
37 { \
38 Object##type##RegistrationClass() \
39 { \
40 NS_WARNING_PUSH_DEPRECATED; \
41 ns3::TypeId tid = type::GetTypeId(); \
42 tid.SetSize(sizeof(type)); \
43 tid.GetParent(); \
44 NS_WARNING_POP; \
45 } \
46 } Object##type##RegistrationVariable
47
48/**
49 * @ingroup object
50 * @brief Explicitly instantiate a template class with one template parameter
51 * and register the resulting instance with the TypeId system.
52 *
53 * This macro should be invoked once for every required instance of a template
54 * class with one template parameter which derives from the Object class and
55 * defines a new GetTypeId method.
56 *
57 * If the template class is in a namespace, then the macro call should also be
58 * in the namespace.
59 *
60 * @note The type names used as arguments for this macro, being used to form a
61 * class name and a variable name, CANNOT contain the scope resolution
62 * operator (::)
63 *
64 * @tparam type the template class
65 * @tparam param the first template parameter
66 */
67#define NS_OBJECT_TEMPLATE_CLASS_DEFINE(type, param) \
68 template class type<param>; \
69 template <> \
70 std::string DoGetTemplateClassName<type<param>>() \
71 { \
72 return std::string("ns3::") + std::string(#type) + std::string("<") + \
73 std::string(#param) + std::string(">"); \
74 } \
75 static struct Object##type##param##RegistrationClass \
76 { \
77 Object##type##param##RegistrationClass() \
78 { \
79 ns3::TypeId tid = type<param>::GetTypeId(); \
80 tid.SetSize(sizeof(type<param>)); \
81 tid.GetParent(); \
82 } \
83 } Object##type##param##RegistrationVariable
84
85/**
86 * @ingroup object
87 * @brief Explicitly instantiate a template class with two template parameters
88 * and register the resulting instance with the TypeId system.
89 *
90 * This macro should be invoked once for every required instance of a template
91 * class with two template parameters which derives from the Object class and
92 * defines a new GetTypeId method.
93 *
94 * If the template class is in a namespace, then the macro call should also be
95 * in the namespace.
96 *
97 * @note The type names used as arguments for this macro, being used to form a
98 * class name and a variable name, CANNOT contain the scope resolution
99 * operator (::)
100 *
101 * @tparam type the template class
102 * @tparam param1 the first template parameter
103 * @tparam param2 the second template parameter
104 */
105#define NS_OBJECT_TEMPLATE_CLASS_TWO_DEFINE(type, param1, param2) \
106 template class type<param1, param2>; \
107 template <> \
108 std::string DoGetTemplateClassName<type<param1, param2>>() \
109 { \
110 return std::string("ns3::") + std::string(#type) + std::string("<") + \
111 std::string(#param1) + std::string(",") + std::string(#param2) + std::string(">"); \
112 } \
113 static struct Object##type##param1##param2##RegistrationClass \
114 { \
115 Object##type##param1##param2##RegistrationClass() \
116 { \
117 ns3::TypeId tid = type<param1, param2>::GetTypeId(); \
118 tid.SetSize(sizeof(type<param1, param2>)); \
119 tid.GetParent(); \
120 } \
121 } Object##type##param1##param2##RegistrationVariable
122
123namespace ns3
124{
125
126/**
127 * @brief Helper function to get the name (as a string) of the type
128 * of a template class
129 * @return the name of the type of a template class as a string
130 *
131 * A specialization of this function is defined by the
132 * NS_OBJECT_TEMPLATE_CLASS_DEFINE macro.
133 */
134template <typename T>
136
137/**
138 * @brief Helper function to get the name (as a string) of the type
139 * of a template class
140 * @return the name of the type of a template class as a string
141 */
142template <typename T>
143std::string
148
149class AttributeConstructionList;
150
151/**
152 * @ingroup object
153 *
154 * @brief Anchor the ns-3 type and attribute system.
155 *
156 * Every class which wants to integrate in the ns-3 type and attribute
157 * system should derive from this base class. This base class provides:
158 * - A way to associate an ns3::TypeId to each object instance.
159 * - A way to set and get the attributes registered in the ns3::TypeId.
160 */
162{
163 public:
164 /**
165 * Get the type ID.
166 * @return The object TypeId.
167 */
168 static TypeId GetTypeId();
169
170 /**
171 * Virtual destructor.
172 */
173 virtual ~ObjectBase();
174
175 /**
176 * Get the most derived TypeId for this Object.
177 *
178 * This method is provided by ns3::Object::GetInstanceTypeId
179 * but classes which derive from ns3::ObjectBase directly
180 * have to implement it themselves. Typically, this method should
181 * simply return the output of `GetTypeId()`.
182 *
183 * @return The TypeId associated to the most-derived type
184 * of this instance.
185 */
186 virtual TypeId GetInstanceTypeId() const = 0;
187
188 /**
189 *
190 * Set a single attribute, raising fatal errors if unsuccessful.
191 *
192 * This will either succeed at setting the attribute
193 * or it will raise NS_FATAL_ERROR() on these conditions:
194 *
195 * - The attribute doesn't exist in this Object.
196 * - The attribute can't be set (no Setter).
197 * - The attribute couldn't be deserialized from the AttributeValue.
198 *
199 * @param [in] name The name of the attribute to set.
200 * @param [in] value The name of the attribute to set.
201 */
202 void SetAttribute(std::string name, const AttributeValue& value);
203 /**
204 * Set a single attribute without raising errors.
205 *
206 * If the attribute could not be set this will return \c false,
207 * but not raise any errors.
208 *
209 * @param [in] name The name of the attribute to set.
210 * @param [in] value The value to set it to.
211 * @return \c true if the requested attribute exists and could be set,
212 * \c false otherwise.
213 */
214 bool SetAttributeFailSafe(std::string name, const AttributeValue& value);
215 /**
216 * Get the value of an attribute, raising fatal errors if unsuccessful.
217 *
218 * This will either succeed at setting the attribute
219 * or it will raise NS_FATAL_ERROR() on these conditions:
220 *
221 * - The attribute doesn't exist in this Object.
222 * - The attribute can't be read (no Getter).
223 * - The attribute doesn't support string formatting.
224 * - The attribute couldn't be serialized into the AttributeValue.
225 *
226 * @param [in] name The name of the attribute to read.
227 * @param [out] value Where the result should be stored.
228 * @param [in] permissive If false (by default), will generate warnings and errors for
229 * deprecated and obsolete attributes, respectively. If set to true, warnings for deprecated
230 * attributes will be suppressed.
231 */
232 void GetAttribute(std::string name, AttributeValue& value, bool permissive = false) const;
233 /**
234 * Get the value of an attribute without raising errors.
235 *
236 * If the attribute could not be read this will return \c false,
237 * but not raise any errors.
238 *
239 * @param [in] name The name of the attribute to read.
240 * @param [out] value Where the result value should be stored.
241 * @return \c true if the requested attribute was found, \c false otherwise.
242 */
243 bool GetAttributeFailSafe(std::string name, AttributeValue& value) const;
244
245 /**
246 * Connect a TraceSource to a Callback with a context.
247 *
248 * The target trace source should be registered with TypeId::AddTraceSource.
249 *
250 * @param [in] name The name of the target trace source.
251 * @param [in] context The trace context associated to the callback.
252 * @param [in] cb The callback to connect to the trace source.
253 * @returns \c true on success, \c false if TraceSource was not found.
254 */
255 bool TraceConnect(std::string name, std::string context, const CallbackBase& cb);
256 /**
257 * Connect a TraceSource to a Callback without a context.
258 *
259 * The target trace source should be registered with TypeId::AddTraceSource.
260 *
261 * @param [in] name The name of the target trace source.
262 * @param [in] cb The callback to connect to the trace source.
263 * @returns \c true on success, \c false if TraceSource was not found.
264 */
265 bool TraceConnectWithoutContext(std::string name, const CallbackBase& cb);
266 /**
267 * Disconnect from a TraceSource a Callback previously connected
268 * with a context.
269 *
270 * The target trace source should be registered with TypeId::AddTraceSource.
271 *
272 * @param [in] name The name of the target trace source.
273 * @param [in] context The trace context associated to the callback.
274 * @param [in] cb The callback to disconnect from the trace source.
275 * @returns \c true on success, \c false if TraceSource was not found.
276 */
277 bool TraceDisconnect(std::string name, std::string context, const CallbackBase& cb);
278 /**
279 * Disconnect from a TraceSource a Callback previously connected
280 * without a context.
281 *
282 * The target trace source should be registered with TypeId::AddTraceSource.
283 *
284 * @param [in] name The name of the target trace source.
285 * @param [in] cb The callback to disconnect from the trace source.
286 * @returns \c true on success, \c false if TraceSource was not found.
287 */
288 bool TraceDisconnectWithoutContext(std::string name, const CallbackBase& cb);
289
290 protected:
291 /**
292 * Notifier called once the ObjectBase is fully constructed.
293 *
294 * This method is invoked once all member attributes have been
295 * initialized. Subclasses can override this method to be notified
296 * of this event but if they do this, they must chain up to their
297 * parent's NotifyConstructionCompleted method.
298 */
299 virtual void NotifyConstructionCompleted();
300 /**
301 * Complete construction of ObjectBase; invoked by derived classes.
302 *
303 * Invoked from subclasses to initialize all of their
304 * attribute members. This method will typically be invoked
305 * automatically from ns3::CreateObject if your class derives
306 * from ns3::Object. If you derive from ns3::ObjectBase directly,
307 * you should make sure that you invoke this method from
308 * your most-derived constructor.
309 *
310 * @param [in] attributes The attribute values used to initialize
311 * the member variables of this object's instance.
312 */
313 void ConstructSelf(const AttributeConstructionList& attributes);
314
315 private:
316 /**
317 * Attempt to set the value referenced by the accessor \pname{spec}
318 * to a valid value according to the \c checker, based on \pname{value}.
319 *
320 * @param [in] spec The accessor for the storage location.
321 * @param [in] checker The checker to use in validating the value.
322 * @param [in] value The value to attempt to store.
323 * @returns \c true if the \c value could be validated by the \pname{checker}
324 * and written to the storage location.
325 */
328 const AttributeValue& value);
329};
330
331// The following explicit template instantiation declarations prevent all the translation
332// units including this header file to implicitly instantiate the callbacks class and
333// function templates having ObjectBase as template type parameter that are required to be
334// instantiated more often (accorging to the ClangBuildAnalyzer tool).
335// These classes and functions are explicitly instantiated in object-base.cc
337extern template Callback<ObjectBase*>::Callback();
338extern template class CallbackImpl<ObjectBase*>;
339
340} // namespace ns3
341
342#endif /* OBJECT_BASE_H */
Declaration of the various callback functions.
List of Attribute name, value and checker triples used to construct Objects.
Hold a value for an Attribute.
Definition attribute.h:59
Base class for Callback class.
Definition callback.h:344
Callback template class.
Definition callback.h:422
friend class Callback
Definition callback.h:424
CallbackImpl class with varying numbers of argument types.
Definition callback.h:226
Anchor the ns-3 type and attribute system.
bool TraceConnectWithoutContext(std::string name, const CallbackBase &cb)
Connect a TraceSource to a Callback without a context.
bool TraceDisconnect(std::string name, std::string context, const CallbackBase &cb)
Disconnect from a TraceSource a Callback previously connected with a context.
bool TraceDisconnectWithoutContext(std::string name, const CallbackBase &cb)
Disconnect from a TraceSource a Callback previously connected without a context.
virtual TypeId GetInstanceTypeId() const =0
Get the most derived TypeId for this Object.
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
virtual ~ObjectBase()
Virtual destructor.
bool GetAttributeFailSafe(std::string name, AttributeValue &value) const
Get the value of an attribute without raising errors.
virtual void NotifyConstructionCompleted()
Notifier called once the ObjectBase is fully constructed.
static TypeId GetTypeId()
Get the type ID.
bool SetAttributeFailSafe(std::string name, const AttributeValue &value)
Set a single attribute without raising errors.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
bool TraceConnect(std::string name, std::string context, const CallbackBase &cb)
Connect a TraceSource to a Callback with a context.
void GetAttribute(std::string name, AttributeValue &value, bool permissive=false) const
Get the value of an attribute, raising fatal errors if unsuccessful.
bool DoSet(Ptr< const AttributeAccessor > spec, Ptr< const AttributeChecker > checker, const AttributeValue &value)
Attempt to set the value referenced by the accessor spec to a valid value according to the checker,...
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:49
template Callback< ObjectBase * > MakeCallback< ObjectBase * >(ObjectBase *(*)())
Explicit instantiation for ObjectBase.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::string DoGetTemplateClassName()
Helper function to get the name (as a string) of the type of a template class.
std::string GetTemplateClassName()
Helper function to get the name (as a string) of the type of a template class.
ns3::TypeId declaration; inline and template implementations.