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/**
13 * @file
14 * @ingroup object
15 * ns3::ObjectFactory class implementation.
16 */
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("ObjectFactory");
22
27
28void
30{
31 NS_LOG_FUNCTION(this << tid.GetName());
32 m_tid = tid;
33}
34
35void
37{
38 NS_LOG_FUNCTION(this << tid);
40}
41
42bool
44{
45 return m_tid.GetUid() != 0;
46}
47
48void
49ObjectFactory::DoSet(const std::string& name, const AttributeValue& value)
50{
51 NS_LOG_FUNCTION(this << name << &value);
52 if (name.empty())
53 {
54 return;
55 }
56
58 if (!m_tid.LookupAttributeByName(name, &info))
59 {
60 NS_FATAL_ERROR("Invalid attribute set (" << name << ") on " << m_tid.GetName());
61 return;
62 }
63 Ptr<AttributeValue> v = info.checker->CreateValidValue(value);
64 if (!v)
65 {
66 NS_FATAL_ERROR("Invalid value for attribute set (" << name << ") on " << m_tid.GetName());
67 return;
68 }
69 m_parameters.Add(name, info.checker, value.Copy());
70}
71
74{
75 NS_LOG_FUNCTION(this);
76 return m_tid;
77}
78
81{
82 NS_LOG_FUNCTION(this);
84 m_tid.GetUid(),
85 "ObjectFactory::Create - can't use an ObjectFactory without setting a TypeId first.");
86 Callback<ObjectBase*> cb = m_tid.GetConstructor();
87 ObjectBase* base = cb();
88 auto derived = dynamic_cast<Object*>(base);
89 NS_ASSERT(derived != nullptr);
90 derived->SetTypeId(m_tid);
91 derived->Construct(m_parameters);
92 Ptr<Object> object = Ptr<Object>(derived, false);
93 return object;
94}
95
96std::ostream&
97operator<<(std::ostream& os, const ObjectFactory& factory)
98{
99 os << factory.m_tid.GetName() << "[";
100 bool first = true;
101 for (auto i = factory.m_parameters.Begin(); i != factory.m_parameters.End(); ++i)
102 {
103 os << i->name << "=" << i->value->SerializeToString(i->checker);
104 if (first)
105 {
106 os << "|";
107 }
108 }
109 os << "]";
110 return os;
111}
112
113std::istream&
114operator>>(std::istream& is, ObjectFactory& factory)
115{
116 std::string v;
117 is >> v;
118 std::string::size_type lbracket;
119 std::string::size_type rbracket;
120 lbracket = v.find('[');
121 rbracket = v.find(']');
122 if (lbracket == std::string::npos && rbracket == std::string::npos)
123 {
124 factory.SetTypeId(v);
125 return is;
126 }
127 if (lbracket == std::string::npos || rbracket == std::string::npos)
128 {
129 return is;
130 }
131 NS_ASSERT(lbracket != std::string::npos);
132 NS_ASSERT(rbracket != std::string::npos);
133 std::string tid = v.substr(0, lbracket);
134 std::string parameters = v.substr(lbracket + 1, rbracket - (lbracket + 1));
135 factory.SetTypeId(tid);
136 std::string::size_type cur;
137 cur = 0;
138 while (cur != parameters.size())
139 {
140 std::string::size_type equal = parameters.find('=', cur);
141 if (equal == std::string::npos)
142 {
143 is.setstate(std::ios_base::failbit);
144 break;
145 }
146 else
147 {
148 std::string name = parameters.substr(cur, equal - cur);
150 if (!factory.m_tid.LookupAttributeByName(name, &info))
151 {
152 is.setstate(std::ios_base::failbit);
153 break;
154 }
155 else
156 {
157 std::string::size_type next = parameters.find('|', cur);
158 std::string value;
159 if (next == std::string::npos)
160 {
161 value = parameters.substr(equal + 1, parameters.size() - (equal + 1));
162 cur = parameters.size();
163 }
164 else
165 {
166 value = parameters.substr(equal + 1, next - (equal + 1));
167 cur = next + 1;
168 }
169 Ptr<AttributeValue> val = info.checker->Create();
170 bool ok = val->DeserializeFromString(value, info.checker);
171 if (!ok)
172 {
173 is.setstate(std::ios_base::failbit);
174 break;
175 }
176 else
177 {
178 factory.m_parameters.Add(name, info.checker, val);
179 }
180 }
181 }
182 }
183 NS_ABORT_MSG_IF(is.bad(), "Failure to parse " << parameters);
184 return is;
185}
186
188
189} // namespace ns3
uint32_t v
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:428
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:81
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
a unique identifier for an interface.
Definition type-id.h:49
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:870
bool LookupAttributeByName(std::string name, AttributeInformation *info, bool permissive=false) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition type-id.cc:966
std::string GetName() const
Get the name.
Definition type-id.cc:1059
#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:194
#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:86
Ptr< const AttributeChecker > checker
Checker object.
Definition type-id.h:100