A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-factory.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-factory.h"
9
10#include "log.h"
11
12#include <sstream>
13
14/**
15 * \file
16 * \ingroup object
17 * ns3::ObjectFactory class implementation.
18 */
19
20namespace ns3
21{
22
23NS_LOG_COMPONENT_DEFINE("ObjectFactory");
24
29
30void
32{
33 NS_LOG_FUNCTION(this << tid.GetName());
34 m_tid = tid;
35}
36
37void
39{
40 NS_LOG_FUNCTION(this << tid);
42}
43
44bool
46{
47 return m_tid.GetUid() != 0;
48}
49
50void
51ObjectFactory::DoSet(const std::string& name, const AttributeValue& value)
52{
53 NS_LOG_FUNCTION(this << name << &value);
54 if (name.empty())
55 {
56 return;
57 }
58
60 if (!m_tid.LookupAttributeByName(name, &info))
61 {
62 NS_FATAL_ERROR("Invalid attribute set (" << name << ") on " << m_tid.GetName());
63 return;
64 }
65 Ptr<AttributeValue> v = info.checker->CreateValidValue(value);
66 if (!v)
67 {
68 NS_FATAL_ERROR("Invalid value for attribute set (" << name << ") on " << m_tid.GetName());
69 return;
70 }
71 m_parameters.Add(name, info.checker, value.Copy());
72}
73
76{
77 NS_LOG_FUNCTION(this);
78 return m_tid;
79}
80
83{
84 NS_LOG_FUNCTION(this);
86 m_tid.GetUid(),
87 "ObjectFactory::Create - can't use an ObjectFactory without setting a TypeId first.");
89 ObjectBase* base = cb();
90 auto derived = dynamic_cast<Object*>(base);
91 NS_ASSERT(derived != nullptr);
92 derived->SetTypeId(m_tid);
93 derived->Construct(m_parameters);
94 Ptr<Object> object = Ptr<Object>(derived, false);
95 return object;
96}
97
98std::ostream&
99operator<<(std::ostream& os, const ObjectFactory& factory)
100{
101 os << factory.m_tid.GetName() << "[";
102 bool first = true;
103 for (auto i = factory.m_parameters.Begin(); i != factory.m_parameters.End(); ++i)
104 {
105 os << i->name << "=" << i->value->SerializeToString(i->checker);
106 if (first)
107 {
108 os << "|";
109 }
110 }
111 os << "]";
112 return os;
113}
114
115std::istream&
116operator>>(std::istream& is, ObjectFactory& factory)
117{
118 std::string v;
119 is >> v;
120 std::string::size_type lbracket;
121 std::string::size_type rbracket;
122 lbracket = v.find('[');
123 rbracket = v.find(']');
124 if (lbracket == std::string::npos && rbracket == std::string::npos)
125 {
126 factory.SetTypeId(v);
127 return is;
128 }
129 if (lbracket == std::string::npos || rbracket == std::string::npos)
130 {
131 return is;
132 }
133 NS_ASSERT(lbracket != std::string::npos);
134 NS_ASSERT(rbracket != std::string::npos);
135 std::string tid = v.substr(0, lbracket);
136 std::string parameters = v.substr(lbracket + 1, rbracket - (lbracket + 1));
137 factory.SetTypeId(tid);
138 std::string::size_type cur;
139 cur = 0;
140 while (cur != parameters.size())
141 {
142 std::string::size_type equal = parameters.find('=', cur);
143 if (equal == std::string::npos)
144 {
145 is.setstate(std::ios_base::failbit);
146 break;
147 }
148 else
149 {
150 std::string name = parameters.substr(cur, equal - cur);
152 if (!factory.m_tid.LookupAttributeByName(name, &info))
153 {
154 is.setstate(std::ios_base::failbit);
155 break;
156 }
157 else
158 {
159 std::string::size_type next = parameters.find('|', cur);
160 std::string value;
161 if (next == std::string::npos)
162 {
163 value = parameters.substr(equal + 1, parameters.size() - (equal + 1));
164 cur = parameters.size();
165 }
166 else
167 {
168 value = parameters.substr(equal + 1, next - (equal + 1));
169 cur = next + 1;
170 }
171 Ptr<AttributeValue> val = info.checker->Create();
172 bool ok = val->DeserializeFromString(value, info.checker);
173 if (!ok)
174 {
175 is.setstate(std::ios_base::failbit);
176 break;
177 }
178 else
179 {
180 factory.m_parameters.Add(name, info.checker, val);
181 }
182 }
183 }
184 }
185 NS_ABORT_MSG_IF(is.bad(), "Failure to parse " << parameters);
186 return is;
187}
188
190
191} // namespace ns3
void Add(std::string name, Ptr< const AttributeChecker > checker, Ptr< AttributeValue > value)
Add an Attribute to the list.
Hold a value for an Attribute.
Definition attribute.h:59
Callback template class.
Definition callback.h:422
Anchor the ns-3 type and attribute system.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
TypeId GetTypeId() const
Get the TypeId which will be created by this ObjectFactory.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
TypeId m_tid
The TypeId this factory will create.
ObjectFactory()
Default constructor.
void DoSet(const std::string &name, const AttributeValue &value)
Set an attribute to be set during construction.
AttributeConstructionList m_parameters
The list of attributes and values to be used in constructing objects by this factory.
bool IsTypeIdSet() const
Check if the ObjectFactory has been configured with a TypeId.
A base class which provides memory management and object aggregation.
Definition object.h:78
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition type-id.cc:1154
uint16_t GetUid() const
Get the internal id of this TypeId.
Definition type-id.cc:1275
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
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Debug message logging.
Definition first.py:1
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
std::istream & operator>>(std::istream &is, Angles &a)
Definition angles.cc:172
ns3::ObjectFactory class declaration.
Attribute implementation.
Definition type-id.h:70
Ptr< const AttributeChecker > checker
Checker object.
Definition type-id.h:84