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 * 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-factory.h"
20
21#include "log.h"
22
23#include <sstream>
24
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("ObjectFactory");
35
37{
38 NS_LOG_FUNCTION(this);
39}
40
41void
43{
44 NS_LOG_FUNCTION(this << tid.GetName());
45 m_tid = tid;
46}
47
48void
50{
51 NS_LOG_FUNCTION(this << tid);
53}
54
55bool
57{
58 return m_tid.GetUid() != 0;
59}
60
61void
62ObjectFactory::DoSet(const std::string& name, const AttributeValue& value)
63{
64 NS_LOG_FUNCTION(this << name << &value);
65 if (name.empty())
66 {
67 return;
68 }
69
71 if (!m_tid.LookupAttributeByName(name, &info))
72 {
73 NS_FATAL_ERROR("Invalid attribute set (" << name << ") on " << m_tid.GetName());
74 return;
75 }
76 Ptr<AttributeValue> v = info.checker->CreateValidValue(value);
77 if (!v)
78 {
79 NS_FATAL_ERROR("Invalid value for attribute set (" << name << ") on " << m_tid.GetName());
80 return;
81 }
82 m_parameters.Add(name, info.checker, value.Copy());
83}
84
87{
88 NS_LOG_FUNCTION(this);
89 return m_tid;
90}
91
94{
95 NS_LOG_FUNCTION(this);
97 ObjectBase* base = cb();
98 Object* derived = dynamic_cast<Object*>(base);
99 NS_ASSERT(derived != nullptr);
100 derived->SetTypeId(m_tid);
101 derived->Construct(m_parameters);
102 Ptr<Object> object = Ptr<Object>(derived, false);
103 return object;
104}
105
106std::ostream&
107operator<<(std::ostream& os, const ObjectFactory& factory)
108{
109 os << factory.m_tid.GetName() << "[";
110 bool first = true;
112 i != factory.m_parameters.End();
113 ++i)
114 {
115 os << i->name << "=" << i->value->SerializeToString(i->checker);
116 if (first)
117 {
118 os << "|";
119 }
120 }
121 os << "]";
122 return os;
123}
124
125std::istream&
126operator>>(std::istream& is, ObjectFactory& factory)
127{
128 std::string v;
129 is >> v;
130 std::string::size_type lbracket;
131 std::string::size_type rbracket;
132 lbracket = v.find('[');
133 rbracket = v.find(']');
134 if (lbracket == std::string::npos && rbracket == std::string::npos)
135 {
136 factory.SetTypeId(v);
137 return is;
138 }
139 if (lbracket == std::string::npos || rbracket == std::string::npos)
140 {
141 return is;
142 }
143 NS_ASSERT(lbracket != std::string::npos);
144 NS_ASSERT(rbracket != std::string::npos);
145 std::string tid = v.substr(0, lbracket);
146 std::string parameters = v.substr(lbracket + 1, rbracket - (lbracket + 1));
147 factory.SetTypeId(tid);
148 std::string::size_type cur;
149 cur = 0;
150 while (cur != parameters.size())
151 {
152 std::string::size_type equal = parameters.find('=', cur);
153 if (equal == std::string::npos)
154 {
155 is.setstate(std::ios_base::failbit);
156 break;
157 }
158 else
159 {
160 std::string name = parameters.substr(cur, equal - cur);
162 if (!factory.m_tid.LookupAttributeByName(name, &info))
163 {
164 is.setstate(std::ios_base::failbit);
165 break;
166 }
167 else
168 {
169 std::string::size_type next = parameters.find('|', cur);
170 std::string value;
171 if (next == std::string::npos)
172 {
173 value = parameters.substr(equal + 1, parameters.size() - (equal + 1));
174 cur = parameters.size();
175 }
176 else
177 {
178 value = parameters.substr(equal + 1, next - (equal + 1));
179 cur = next + 1;
180 }
181 Ptr<AttributeValue> val = info.checker->Create();
182 bool ok = val->DeserializeFromString(value, info.checker);
183 if (!ok)
184 {
185 is.setstate(std::ios_base::failbit);
186 break;
187 }
188 else
189 {
190 factory.m_parameters.Add(name, info.checker, val);
191 }
192 }
193 }
194 }
195 NS_ABORT_MSG_IF(is.bad(), "Failure to parse " << parameters);
196 return is;
197}
198
200
201} // namespace ns3
void Add(std::string name, Ptr< const AttributeChecker > checker, Ptr< AttributeValue > value)
Add an Attribute to the list.
std::list< Item >::const_iterator CIterator
Iterator type.
Hold a value for an Attribute.
Definition: attribute.h:70
Callback template class.
Definition: callback.h:438
Anchor the ns-3 type and attribute system.
Definition: object-base.h:173
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:89
void Construct(const AttributeConstructionList &attributes)
Initialize all member variables registered as Attributes of this TypeId.
Definition: object.cc:144
void SetTypeId(TypeId tid)
Set the TypeId of this Object.
Definition: object.cc:345
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:840
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition: type-id.cc:1089
uint16_t GetUid() const
Get the internal id of this TypeId.
Definition: type-id.cc:1210
bool LookupAttributeByName(std::string name, AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:898
std::string GetName() const
Get the name.
Definition: type-id.cc:996
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#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.
Definition: fatal-error.h:179
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:129
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
ns3::ObjectFactory class declaration.
Attribute implementation.
Definition: type-id.h:81
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:95