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