A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-base.cc
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#include "object-base.h"
20
21#include "assert.h"
24#include "log.h"
25#include "string.h"
27
28#include "ns3/core-config.h"
29
30/**
31 * \file
32 * \ingroup object
33 * ns3::ObjectBase class implementation.
34 */
35
36namespace ns3
37{
38// Explicit instantiation declaration
39
40/**
41 * \ingroup callback
42 * Explicit instantiation for ObjectBase
43 * \return A wrapper Callback
44 * \sa ns3::MakeCallback
45 */
48template class CallbackImpl<ObjectBase*>;
49
50NS_LOG_COMPONENT_DEFINE("ObjectBase");
51
53
54/**
55 * Ensure the TypeId for ObjectBase gets fully configured
56 * to anchor the inheritance tree properly.
57 *
58 * \relates ns3::ObjectBase
59 *
60 * \return The TypeId for ObjectBase.
61 */
62static TypeId
64{
66 TypeId tid = TypeId("ns3::ObjectBase");
67 tid.SetParent(tid);
68 tid.SetGroupName("Core");
69 return tid;
70}
71
74{
76 static TypeId tid = GetObjectIid();
77 return tid;
78}
79
81{
82 NS_LOG_FUNCTION(this);
83}
84
85void
87{
88 NS_LOG_FUNCTION(this);
89}
90
91void
93{
94 // loop over the inheritance tree back to the Object base class.
95 NS_LOG_FUNCTION(this << &attributes);
97 do // Do this tid and all parents
98 {
99 // loop over all attributes in object type
100 NS_LOG_DEBUG("construct tid=" << tid.GetName() << ", params=" << tid.GetAttributeN());
101 for (uint32_t i = 0; i < tid.GetAttributeN(); i++)
102 {
104 NS_LOG_DEBUG("try to construct \"" << tid.GetName() << "::" << info.name << "\"");
105 // is this attribute stored in this AttributeConstructionList instance ?
106 Ptr<const AttributeValue> value = attributes.Find(info.checker);
107 std::string where = "argument";
108
109 // See if this attribute should not be set here in the
110 // constructor.
111 if (!(info.flags & TypeId::ATTR_CONSTRUCT))
112 {
113 // Handle this attribute if it should not be
114 // set here.
115 if (!value)
116 {
117 // Skip this attribute if it's not in the
118 // AttributeConstructionList.
119 NS_LOG_DEBUG("skipping, not settable at construction");
120 continue;
121 }
122 else
123 {
124 // This is an error because this attribute is not
125 // settable in its constructor but is present in
126 // the AttributeConstructionList.
127 NS_FATAL_ERROR("Attribute name="
128 << info.name << " tid=" << tid.GetName()
129 << ": initial value cannot be set using attributes");
130 }
131 }
132
133 if (!value)
134 {
135 NS_LOG_DEBUG("trying to set from environment variable NS_ATTRIBUTE_DEFAULT");
136 auto [found, val] =
137 EnvironmentVariable::Get("NS_ATTRIBUTE_DEFAULT", tid.GetAttributeFullName(i));
138 if (found)
139 {
140 NS_LOG_DEBUG("found in environment: " << val);
141 value = Create<StringValue>(val);
142 where = "env var";
143 }
144 }
145
146 bool initial{false};
147 if (!value)
148 {
149 // This is guaranteed to exist
150 NS_LOG_DEBUG("falling back to initial value from tid");
151 value = info.initialValue;
152 where = "initial value";
153 initial = true;
154 }
155
156 // We have a matching attribute value, if only from the initialValue
157 if (DoSet(info.accessor, info.checker, *value) || initial)
158 {
159 // Setting from initial value may fail, e.g. setting
160 // ObjectVectorValue from ""
161 // That's ok, so we still report success since construction is complete
162 NS_LOG_DEBUG("construct \"" << tid.GetName() << "::" << info.name << "\" from "
163 << where);
164 }
165 else
166 {
167 /*
168 One would think this is an error...
169
170 but there are cases where `attributes.Find(info.checker)`
171 returns a non-null value which still fails the `DoSet()` call.
172 For example, `value` is sometimes a real `PointerValue`
173 containing 0 as the pointed-to address. Since value
174 is not null (it just contains null) the initial
175 value is not used, the DoSet fails, and we end up
176 here.
177
178 If we were adventurous we might try to fix this deep
179 below DoSet, but there be dragons.
180 */
181 /*
182 NS_ASSERT_MSG(false,
183 "Failed to set attribute '" << info.name << "' from '"
184 << value->SerializeToString(info.checker)
185 << "'");
186 */
187 }
188
189 } // for i attributes
190 tid = tid.GetParent();
191 } while (tid != ObjectBase::GetTypeId());
193}
194
195bool
198 const AttributeValue& value)
199{
200 NS_LOG_FUNCTION(this << accessor << checker << &value);
201 Ptr<AttributeValue> v = checker->CreateValidValue(value);
202 if (!v)
203 {
204 return false;
205 }
206 bool ok = accessor->Set(this, *v);
207 return ok;
208}
209
210void
211ObjectBase::SetAttribute(std::string name, const AttributeValue& value)
212{
213 NS_LOG_FUNCTION(this << name << &value);
216 if (!tid.LookupAttributeByName(name, &info))
217 {
219 "Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName());
220 }
221 if (!(info.flags & TypeId::ATTR_SET) || !info.accessor->HasSetter())
222 {
224 "Attribute name=" << name << " is not settable for this object: tid=" << tid.GetName());
225 }
226 if (!DoSet(info.accessor, info.checker, value))
227 {
228 NS_FATAL_ERROR("Attribute name=" << name << " could not be set for this object: tid="
229 << tid.GetName());
230 }
231}
232
233bool
235{
236 NS_LOG_FUNCTION(this << name << &value);
239 if (!tid.LookupAttributeByName(name, &info))
240 {
241 return false;
242 }
243 if (!(info.flags & TypeId::ATTR_SET) || !info.accessor->HasSetter())
244 {
245 return false;
246 }
247 return DoSet(info.accessor, info.checker, value);
248}
249
250void
251ObjectBase::GetAttribute(std::string name, AttributeValue& value) const
252{
253 NS_LOG_FUNCTION(this << name << &value);
256 if (!tid.LookupAttributeByName(name, &info))
257 {
259 "Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName());
260 }
261 if (!(info.flags & TypeId::ATTR_GET) || !info.accessor->HasGetter())
262 {
264 "Attribute name=" << name << " is not gettable for this object: tid=" << tid.GetName());
265 }
266 bool ok = info.accessor->Get(this, value);
267 if (ok)
268 {
269 return;
270 }
271 auto str = dynamic_cast<StringValue*>(&value);
272 if (str == nullptr)
273 {
274 NS_FATAL_ERROR("Attribute name=" << name << " tid=" << tid.GetName()
275 << ": input value is not a string");
276 }
277 Ptr<AttributeValue> v = info.checker->Create();
278 ok = info.accessor->Get(this, *PeekPointer(v));
279 if (!ok)
280 {
281 NS_FATAL_ERROR("Attribute name=" << name << " tid=" << tid.GetName()
282 << ": could not get value");
283 }
284 str->Set(v->SerializeToString(info.checker));
285}
286
287bool
289{
290 NS_LOG_FUNCTION(this << name << &value);
293 if (!tid.LookupAttributeByName(name, &info))
294 {
295 return false;
296 }
297 if (!(info.flags & TypeId::ATTR_GET) || !info.accessor->HasGetter())
298 {
299 return false;
300 }
301 bool ok = info.accessor->Get(this, value);
302 if (ok)
303 {
304 return true;
305 }
306 auto str = dynamic_cast<StringValue*>(&value);
307 if (str == nullptr)
308 {
309 return false;
310 }
311 Ptr<AttributeValue> v = info.checker->Create();
312 ok = info.accessor->Get(this, *PeekPointer(v));
313 if (!ok)
314 {
315 return false;
316 }
317 str->Set(v->SerializeToString(info.checker));
318 return true;
319}
320
321bool
323{
324 NS_LOG_FUNCTION(this << name << &cb);
327 if (!accessor)
328 {
329 NS_LOG_DEBUG("Cannot connect trace " << name << " on object of type " << tid.GetName());
330 return false;
331 }
332 bool ok = accessor->ConnectWithoutContext(this, cb);
333 return ok;
334}
335
336bool
337ObjectBase::TraceConnect(std::string name, std::string context, const CallbackBase& cb)
338{
339 NS_LOG_FUNCTION(this << name << context << &cb);
342 if (!accessor)
343 {
344 NS_LOG_DEBUG("Cannot connect trace " << name << " on object of type " << tid.GetName());
345 return false;
346 }
347 bool ok = accessor->Connect(this, context, cb);
348 return ok;
349}
350
351bool
353{
354 NS_LOG_FUNCTION(this << name << &cb);
357 if (!accessor)
358 {
359 return false;
360 }
361 bool ok = accessor->DisconnectWithoutContext(this, cb);
362 return ok;
363}
364
365bool
366ObjectBase::TraceDisconnect(std::string name, std::string context, const CallbackBase& cb)
367{
368 NS_LOG_FUNCTION(this << name << context << &cb);
371 if (!accessor)
372 {
373 return false;
374 }
375 bool ok = accessor->Disconnect(this, context, cb);
376 return ok;
377}
378
379} // namespace ns3
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ns3::AttributeConstructionList declaration.
List of Attribute name, value and checker triples used to construct Objects.
Ptr< AttributeValue > Find(Ptr< const AttributeChecker > checker) const
Find an Attribute in the list from its AttributeChecker.
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
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
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
static TypeId GetObjectIid()
Ensure the TypeId for ObjectBase gets fully configured to anchor the inheritance tree properly.
Definition: object-base.cc:63
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
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:64
@ ATTR_SET
The attribute can be written.
Definition: type-id.h:65
@ ATTR_CONSTRUCT
The attribute can be written at construction-time.
Definition: type-id.h:66
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition: type-id.cc:1116
std::size_t GetAttributeN() const
Get the number of attributes.
Definition: type-id.cc:1101
TypeId GetParent() const
Get the parent of this TypeId.
Definition: type-id.cc:956
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition: type-id.cc:940
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition: type-id.cc:1199
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1109
bool LookupAttributeByName(std::string name, AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:894
std::string GetName() const
Get the name.
Definition: type-id.cc:992
Class Environment declaration.
template Callback< ObjectBase * > MakeCallback< ObjectBase * >(ObjectBase *(*)())
Explicit instantiation for ObjectBase.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:454
ns3::ObjectBase declaration and NS_OBJECT_ENSURE_REGISTERED() macro definition.
ns3::StringValue attribute value declarations.
Attribute implementation.
Definition: type-id.h:81
std::string name
Attribute name.
Definition: type-id.h:83
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition: type-id.h:93
uint32_t flags
AttributeFlags value.
Definition: type-id.h:87
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:95
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition: type-id.h:91
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.