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 one template parameter
88 * and register the resulting instance with the TypeId system.
89 *
90 * This version of the macro allows the user to specify a namespace
91 * for the template class and a different namespace for the template parameter.
92 *
93 * The `nst` parameter is the namespace of the templated class, which must be
94 * in the `ns3` namespace and can be in a nested namespace, e.g., `ns3::aodv`.
95 * Adding the leading `ns3` namespace is optional for nested namespace, while for
96 * classes in the `ns3` namespace is mandatory.
97 *
98 * The `nsp` parameter is the namespace of the template parameter, and can be
99 * any namespace, including `std`, `ns3`, or a nested namespace like `ns3::aodv`.
100 * Also in this case, adding the leading `ns3` namespace is optional for nested
101 * namespace, while for objects in the `ns3` namespace is mandatory.
102 * The parameter can also be in the `std` namespace, e.g., `std::uint16_t` will have
103 * `nsp` set to `std` and `param` to `uint16_t`.
104 *
105 * The result of the function ``GetTemplateClassName`` depends on these values, and
106 * typically will be something like `ns3::nst::type<nsp::param>`.
107 *
108 * @sa NS_OBJECT_TEMPLATE_CLASS_DEFINE
109 *
110 * @note This macro must be used in the `ns3` namespace.
111 *
112 * @param nst the namespace of the template class
113 * @param type the template class
114 * @param nsp the namespace of the parameter (typically `ns3`)
115 * @param param the first template parameter
116 */
117#define NS_OBJECT_TEMPLATE_CLASS_WITH_NS_DEFINE(nst, type, nsp, param) \
118 template class nst::type<nsp::param>; \
119 template <> \
120 std::string DoGetTemplateClassName<nst::type<nsp::param>>() \
121 { \
122 constexpr std::string_view prefix = "ns3::"; \
123 std::string cName = std::string(#nst) + "::" + #type; \
124 if (cName.starts_with(prefix)) \
125 cName.erase(0, prefix.size()); \
126 std::string pName = std::string(#nsp) + "::" + #param; \
127 if (pName.starts_with(prefix)) \
128 pName.erase(0, prefix.size()); \
129 return std::string("ns3::") + cName + std::string("<") + pName + std::string(">"); \
130 } \
131 namespace nst \
132 { \
133 static struct Object##type##nsp##param##RegistrationClass \
134 { \
135 Object##type##nsp##param##RegistrationClass() \
136 { \
137 ns3::TypeId tid = type<nsp::param>::GetTypeId(); \
138 tid.SetSize(sizeof(type<nsp::param>)); \
139 tid.GetParent(); \
140 } \
141 } Object##type##nsp##param##RegistrationVariable; \
142 }
143
144/**
145 * @ingroup object
146 * @brief Explicitly instantiate a template class with two template parameters
147 * and register the resulting instance with the TypeId system.
148 *
149 * This macro should be invoked once for every required instance of a template
150 * class with two template parameters which derives from the Object class and
151 * defines a new GetTypeId method.
152 *
153 * If the template class is in a namespace, then the macro call should also be
154 * in the namespace.
155 *
156 * @note The type names used as arguments for this macro, being used to form a
157 * class name and a variable name, CANNOT contain the scope resolution
158 * operator (::)
159 *
160 * @tparam type the template class
161 * @tparam param1 the first template parameter
162 * @tparam param2 the second template parameter
163 */
164#define NS_OBJECT_TEMPLATE_CLASS_TWO_DEFINE(type, param1, param2) \
165 template class type<param1, param2>; \
166 template <> \
167 std::string DoGetTemplateClassName<type<param1, param2>>() \
168 { \
169 return std::string("ns3::") + std::string(#type) + std::string("<") + \
170 std::string(#param1) + std::string(",") + std::string(#param2) + std::string(">"); \
171 } \
172 static struct Object##type##param1##param2##RegistrationClass \
173 { \
174 Object##type##param1##param2##RegistrationClass() \
175 { \
176 ns3::TypeId tid = type<param1, param2>::GetTypeId(); \
177 tid.SetSize(sizeof(type<param1, param2>)); \
178 tid.GetParent(); \
179 } \
180 } Object##type##param1##param2##RegistrationVariable
181
182namespace ns3
183{
184
185/**
186 * @brief Helper function to get the name (as a string) of the type
187 * of a template class
188 * @return the name of the type of a template class as a string
189 *
190 * A specialization of this function is defined by the
191 * NS_OBJECT_TEMPLATE_CLASS_DEFINE macro.
192 */
193template <typename T>
195
196/**
197 * @brief Helper function to get the name (as a string) of the type
198 * of a template class
199 * @return the name of the type of a template class as a string
200 */
201template <typename T>
202std::string
207
208class AttributeConstructionList;
209
210/**
211 * @ingroup object
212 *
213 * @brief Anchor the ns-3 type and attribute system.
214 *
215 * Every class which wants to integrate in the ns-3 type and attribute
216 * system should derive from this base class. This base class provides:
217 * - A way to associate an ns3::TypeId to each object instance.
218 * - A way to set and get the attributes registered in the ns3::TypeId.
219 */
221{
222 public:
223 /**
224 * Get the type ID.
225 * @return The object TypeId.
226 */
227 static TypeId GetTypeId();
228
229 /**
230 * Virtual destructor.
231 */
232 virtual ~ObjectBase();
233
234 /**
235 * Get the most derived TypeId for this Object.
236 *
237 * This method is provided by ns3::Object::GetInstanceTypeId
238 * but classes which derive from ns3::ObjectBase directly
239 * have to implement it themselves. Typically, this method should
240 * simply return the output of `GetTypeId()`.
241 *
242 * @return The TypeId associated to the most-derived type
243 * of this instance.
244 */
245 virtual TypeId GetInstanceTypeId() const = 0;
246
247 /**
248 *
249 * Set a single attribute, raising fatal errors if unsuccessful.
250 *
251 * This will either succeed at setting the attribute
252 * or it will raise NS_FATAL_ERROR() on these conditions:
253 *
254 * - The attribute doesn't exist in this Object.
255 * - The attribute can't be set (no Setter).
256 * - The attribute couldn't be deserialized from the AttributeValue.
257 *
258 * @param [in] name The name of the attribute to set.
259 * @param [in] value The name of the attribute to set.
260 */
261 void SetAttribute(std::string name, const AttributeValue& value);
262 /**
263 * Set a single attribute without raising errors.
264 *
265 * If the attribute could not be set this will return \c false,
266 * but not raise any errors.
267 *
268 * @param [in] name The name of the attribute to set.
269 * @param [in] value The value to set it to.
270 * @return \c true if the requested attribute exists and could be set,
271 * \c false otherwise.
272 */
273 bool SetAttributeFailSafe(std::string name, const AttributeValue& value);
274 /**
275 * Get the value of an attribute, raising fatal errors if unsuccessful.
276 *
277 * This will either succeed at setting the attribute
278 * or it will raise NS_FATAL_ERROR() on these conditions:
279 *
280 * - The attribute doesn't exist in this Object.
281 * - The attribute can't be read (no Getter).
282 * - The attribute doesn't support string formatting.
283 * - The attribute couldn't be serialized into the AttributeValue.
284 *
285 * @param [in] name The name of the attribute to read.
286 * @param [out] value Where the result should be stored.
287 * @param [in] permissive If false (by default), will generate warnings and errors for
288 * deprecated and obsolete attributes, respectively. If set to true, warnings for deprecated
289 * attributes will be suppressed.
290 */
291 void GetAttribute(std::string name, AttributeValue& value, bool permissive = false) const;
292 /**
293 * Get the value of an attribute without raising errors.
294 *
295 * If the attribute could not be read this will return \c false,
296 * but not raise any errors.
297 *
298 * @param [in] name The name of the attribute to read.
299 * @param [out] value Where the result value should be stored.
300 * @return \c true if the requested attribute was found, \c false otherwise.
301 */
302 bool GetAttributeFailSafe(std::string name, AttributeValue& value) const;
303
304 /**
305 * Connect a TraceSource to a Callback with a context.
306 *
307 * The target trace source should be registered with TypeId::AddTraceSource.
308 *
309 * @param [in] name The name of the target trace source.
310 * @param [in] context The trace context associated to the callback.
311 * @param [in] cb The callback to connect to the trace source.
312 * @returns \c true on success, \c false if TraceSource was not found.
313 */
314 bool TraceConnect(std::string name, std::string context, const CallbackBase& cb);
315 /**
316 * Connect a TraceSource to a Callback without a context.
317 *
318 * The target trace source should be registered with TypeId::AddTraceSource.
319 *
320 * @param [in] name The name of the target trace source.
321 * @param [in] cb The callback to connect to the trace source.
322 * @returns \c true on success, \c false if TraceSource was not found.
323 */
324 bool TraceConnectWithoutContext(std::string name, const CallbackBase& cb);
325 /**
326 * Disconnect from a TraceSource a Callback previously connected
327 * with a context.
328 *
329 * The target trace source should be registered with TypeId::AddTraceSource.
330 *
331 * @param [in] name The name of the target trace source.
332 * @param [in] context The trace context associated to the callback.
333 * @param [in] cb The callback to disconnect from the trace source.
334 * @returns \c true on success, \c false if TraceSource was not found.
335 */
336 bool TraceDisconnect(std::string name, std::string context, const CallbackBase& cb);
337 /**
338 * Disconnect from a TraceSource a Callback previously connected
339 * without a context.
340 *
341 * The target trace source should be registered with TypeId::AddTraceSource.
342 *
343 * @param [in] name The name of the target trace source.
344 * @param [in] cb The callback to disconnect from the trace source.
345 * @returns \c true on success, \c false if TraceSource was not found.
346 */
347 bool TraceDisconnectWithoutContext(std::string name, const CallbackBase& cb);
348
349 protected:
350 /**
351 * Notifier called once the ObjectBase is fully constructed.
352 *
353 * This method is invoked once all member attributes have been
354 * initialized. Subclasses can override this method to be notified
355 * of this event but if they do this, they must chain up to their
356 * parent's NotifyConstructionCompleted method.
357 */
358 virtual void NotifyConstructionCompleted();
359 /**
360 * Complete construction of ObjectBase; invoked by derived classes.
361 *
362 * Invoked from subclasses to initialize all of their
363 * attribute members. This method will typically be invoked
364 * automatically from ns3::CreateObject if your class derives
365 * from ns3::Object. If you derive from ns3::ObjectBase directly,
366 * you should make sure that you invoke this method from
367 * your most-derived constructor.
368 *
369 * @param [in] attributes The attribute values used to initialize
370 * the member variables of this object's instance.
371 */
372 void ConstructSelf(const AttributeConstructionList& attributes);
373
374 private:
375 /**
376 * Attempt to set the value referenced by the accessor \pname{spec}
377 * to a valid value according to the \c checker, based on \pname{value}.
378 *
379 * @param [in] spec The accessor for the storage location.
380 * @param [in] checker The checker to use in validating the value.
381 * @param [in] value The value to attempt to store.
382 * @returns \c true if the \c value could be validated by the \pname{checker}
383 * and written to the storage location.
384 */
387 const AttributeValue& value);
388};
389
390// The following explicit template instantiation declarations prevent all the translation
391// units including this header file to implicitly instantiate the callbacks class and
392// function templates having ObjectBase as template type parameter that are required to be
393// instantiated more often (accorging to the ClangBuildAnalyzer tool).
394// These classes and functions are explicitly instantiated in object-base.cc
396extern template Callback<ObjectBase*>::Callback();
397extern template class CallbackImpl<ObjectBase*>;
398
399} // namespace ns3
400
401#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:347
Callback template class.
Definition callback.h:428
friend class Callback
Definition callback.h:430
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.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:50
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.