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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8#include "object-base.h"
9
10#include "assert.h"
13#include "log.h"
14#include "string.h"
16
17#include "ns3/core-config.h"
18
19/**
20 * @file
21 * @ingroup object
22 * ns3::ObjectBase class implementation.
23 */
24
25namespace ns3
26{
27// Explicit instantiation declaration
28
29/**
30 * @ingroup callback
31 * Explicit instantiation for ObjectBase
32 * @return A wrapper Callback
33 * \sa ns3::MakeCallback
34 */
37template class CallbackImpl<ObjectBase*>;
38
39NS_LOG_COMPONENT_DEFINE("ObjectBase");
40
42
43/**
44 * Ensure the TypeId for ObjectBase gets fully configured
45 * to anchor the inheritance tree properly.
46 *
47 * @relates ns3::ObjectBase
48 *
49 * @return The TypeId for ObjectBase.
50 */
51static TypeId
53{
55 TypeId tid = TypeId("ns3::ObjectBase");
56 tid.SetParent(tid);
57 tid.SetGroupName("Core");
58 return tid;
59}
60
63{
65 static TypeId tid = GetObjectIid();
66 return tid;
67}
68
73
74void
79
80void
82{
83 // loop over the inheritance tree back to the Object base class.
84 NS_LOG_FUNCTION(this << &attributes);
86 do // Do this tid and all parents
87 {
88 // loop over all attributes in object type
89 NS_LOG_DEBUG("construct tid=" << tid.GetName() << ", params=" << tid.GetAttributeN());
90 for (uint32_t i = 0; i < tid.GetAttributeN(); i++)
91 {
93 NS_LOG_DEBUG("try to construct \"" << tid.GetName() << "::" << info.name << "\"");
94 // is this attribute stored in this AttributeConstructionList instance ?
95 Ptr<const AttributeValue> value = attributes.Find(info.checker);
96 std::string where = "argument";
97
98 // See if this attribute should not be set here in the
99 // constructor.
100 if (!(info.flags & TypeId::ATTR_CONSTRUCT))
101 {
102 // Handle this attribute if it should not be
103 // set here.
104 if (!value)
105 {
106 // Skip this attribute if it's not in the
107 // AttributeConstructionList.
108 NS_LOG_DEBUG("skipping, not settable at construction");
109 continue;
110 }
111 else
112 {
113 // This is an error because this attribute is not
114 // settable in its constructor but is present in
115 // the AttributeConstructionList.
116 NS_FATAL_ERROR("Attribute name="
117 << info.name << " tid=" << tid.GetName()
118 << ": initial value cannot be set using attributes");
119 }
120 }
121
122 if (!value)
123 {
124 NS_LOG_DEBUG("trying to set from environment variable NS_ATTRIBUTE_DEFAULT");
125 auto [found, val] =
126 EnvironmentVariable::Get("NS_ATTRIBUTE_DEFAULT", tid.GetAttributeFullName(i));
127 if (found)
128 {
129 NS_LOG_DEBUG("found in environment: " << val);
130 value = Create<StringValue>(val);
131 where = "env var";
132 }
133 }
134
135 bool initial{false};
136 if (!value)
137 {
138 // This is guaranteed to exist
139 NS_LOG_DEBUG("falling back to initial value from tid");
140 value = info.initialValue;
141 where = "initial value";
142 initial = true;
143 }
144
145 // We have a matching attribute value, if only from the initialValue
146 if (DoSet(info.accessor, info.checker, *value) || initial)
147 {
148 // Setting from initial value may fail, e.g. setting
149 // ObjectVectorValue from ""
150 // That's ok, so we still report success since construction is complete
151 NS_LOG_DEBUG("construct \"" << tid.GetName() << "::" << info.name << "\" from "
152 << where);
153 }
154 else
155 {
156 /*
157 One would think this is an error...
158
159 but there are cases where `attributes.Find(info.checker)`
160 returns a non-null value which still fails the `DoSet()` call.
161 For example, `value` is sometimes a real `PointerValue`
162 containing 0 as the pointed-to address. Since value
163 is not null (it just contains null) the initial
164 value is not used, the DoSet fails, and we end up
165 here.
166
167 If we were adventurous we might try to fix this deep
168 below DoSet, but there be dragons.
169 */
170 /*
171 NS_ASSERT_MSG(false,
172 "Failed to set attribute '" << info.name << "' from '"
173 << value->SerializeToString(info.checker)
174 << "'");
175 */
176 }
177 }
178 tid = tid.GetParent();
179 } while (tid != ObjectBase::GetTypeId());
181}
182
183bool
186 const AttributeValue& value)
187{
188 NS_LOG_FUNCTION(this << accessor << checker << &value);
189 Ptr<AttributeValue> v = checker->CreateValidValue(value);
190 if (!v)
191 {
192 return false;
193 }
194 bool ok = accessor->Set(this, *v);
195 return ok;
196}
197
198void
199ObjectBase::SetAttribute(std::string name, const AttributeValue& value)
200{
201 NS_LOG_FUNCTION(this << name << &value);
204 if (!tid.LookupAttributeByName(name, &info))
205 {
207 "Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName());
208 }
209 if (!(info.flags & TypeId::ATTR_SET) || !info.accessor->HasSetter())
210 {
212 "Attribute name=" << name << " is not settable for this object: tid=" << tid.GetName());
213 }
214 if (!DoSet(info.accessor, info.checker, value))
215 {
216 NS_FATAL_ERROR("Attribute name=" << name << " could not be set for this object: tid="
217 << tid.GetName());
218 }
219}
220
221bool
223{
224 NS_LOG_FUNCTION(this << name << &value);
227 if (!tid.LookupAttributeByName(name, &info))
228 {
229 return false;
230 }
231 if (!(info.flags & TypeId::ATTR_SET) || !info.accessor->HasSetter())
232 {
233 return false;
234 }
235 return DoSet(info.accessor, info.checker, value);
236}
237
238void
239ObjectBase::GetAttribute(std::string name, AttributeValue& value, bool permissive) const
240{
241 NS_LOG_FUNCTION(this << name << &value);
244 if (!tid.LookupAttributeByName(name, &info, permissive))
245 {
247 "Attribute name=" << name << " does not exist for this object: tid=" << tid.GetName());
248 }
249 if (!(info.flags & TypeId::ATTR_GET) || !info.accessor->HasGetter())
250 {
252 "Attribute name=" << name << " is not gettable for this object: tid=" << tid.GetName());
253 }
254 bool ok = info.accessor->Get(this, value);
255 if (ok)
256 {
257 return;
258 }
259 auto str = dynamic_cast<StringValue*>(&value);
260 if (str == nullptr)
261 {
262 NS_FATAL_ERROR("Attribute name=" << name << " tid=" << tid.GetName()
263 << ": input value is not a string");
264 }
265 Ptr<AttributeValue> v = info.checker->Create();
266 ok = info.accessor->Get(this, *PeekPointer(v));
267 if (!ok)
268 {
269 NS_FATAL_ERROR("Attribute name=" << name << " tid=" << tid.GetName()
270 << ": could not get value");
271 }
272 str->Set(v->SerializeToString(info.checker));
273}
274
275bool
277{
278 NS_LOG_FUNCTION(this << name << &value);
281 if (!tid.LookupAttributeByName(name, &info))
282 {
283 return false;
284 }
285 if (!(info.flags & TypeId::ATTR_GET) || !info.accessor->HasGetter())
286 {
287 return false;
288 }
289 bool ok = info.accessor->Get(this, value);
290 if (ok)
291 {
292 return true;
293 }
294 auto str = dynamic_cast<StringValue*>(&value);
295 if (str == nullptr)
296 {
297 return false;
298 }
299 Ptr<AttributeValue> v = info.checker->Create();
300 ok = info.accessor->Get(this, *PeekPointer(v));
301 if (!ok)
302 {
303 return false;
304 }
305 str->Set(v->SerializeToString(info.checker));
306 return true;
307}
308
309bool
311{
312 NS_LOG_FUNCTION(this << name << &cb);
315 if (!accessor)
316 {
317 NS_LOG_DEBUG("Cannot connect trace " << name << " on object of type " << tid.GetName());
318 return false;
319 }
320 bool ok = accessor->ConnectWithoutContext(this, cb);
321 return ok;
322}
323
324bool
325ObjectBase::TraceConnect(std::string name, std::string context, const CallbackBase& cb)
326{
327 NS_LOG_FUNCTION(this << name << context << &cb);
330 if (!accessor)
331 {
332 NS_LOG_DEBUG("Cannot connect trace " << name << " on object of type " << tid.GetName());
333 return false;
334 }
335 bool ok = accessor->Connect(this, context, cb);
336 return ok;
337}
338
339bool
341{
342 NS_LOG_FUNCTION(this << name << &cb);
345 if (!accessor)
346 {
347 return false;
348 }
349 bool ok = accessor->DisconnectWithoutContext(this, cb);
350 return ok;
351}
352
353bool
354ObjectBase::TraceDisconnect(std::string name, std::string context, const CallbackBase& cb)
355{
356 NS_LOG_FUNCTION(this << name << context << &cb);
359 if (!accessor)
360 {
361 return false;
362 }
363 bool ok = accessor->Disconnect(this, context, cb);
364 return ok;
365}
366
367} // 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: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
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.
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.
static TypeId GetObjectIid()
Ensure the TypeId for ObjectBase gets fully configured to anchor the inheritance tree properly.
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.
Hold variables of type string.
Definition string.h:45
a unique identifier for an interface.
Definition type-id.h:49
@ ATTR_GET
The attribute can be read.
Definition type-id.h:54
@ ATTR_SET
The attribute can be written.
Definition type-id.h:55
@ ATTR_CONSTRUCT
The attribute can be written at construction-time.
Definition type-id.h:56
std::string GetAttributeFullName(std::size_t i) const
Get the Attribute name by index.
Definition type-id.cc:1185
std::size_t GetAttributeN() const
Get the number of attributes.
Definition type-id.cc:1170
TypeId GetParent() const
Get the parent of this TypeId.
Definition type-id.cc:1025
TypeId SetGroupName(std::string groupName)
Set the group name.
Definition type-id.cc:1009
Ptr< const TraceSourceAccessor > LookupTraceSourceByName(std::string name) const
Find a TraceSource by name.
Definition type-id.cc:1268
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition type-id.cc:1178
bool LookupAttributeByName(std::string name, AttributeInformation *info, bool permissive=false) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition type-id.cc:968
std::string GetName() const
Get the name.
Definition type-id.cc:1061
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.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#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:35
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
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:443
ns3::ObjectBase declaration and NS_OBJECT_ENSURE_REGISTERED() macro definition.
ns3::StringValue attribute value declarations.
Attribute implementation.
Definition type-id.h:86
std::string name
Attribute name.
Definition type-id.h:88
Ptr< const AttributeAccessor > accessor
Accessor object.
Definition type-id.h:98
uint32_t flags
AttributeFlags value.
Definition type-id.h:92
Ptr< const AttributeChecker > checker
Checker object.
Definition type-id.h:100
Ptr< const AttributeValue > initialValue
Configured initial value.
Definition type-id.h:96
ns3::TraceSourceAccessor and ns3::MakeTraceSourceAccessor declarations.