A Discrete-Event Network Simulator
API
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 
31 namespace ns3
32 {
33 
34 NS_LOG_COMPONENT_DEFINE("ObjectFactory");
35 
37 {
38  NS_LOG_FUNCTION(this);
39 }
40 
41 void
43 {
44  NS_LOG_FUNCTION(this << tid.GetName());
45  m_tid = tid;
46 }
47 
48 void
49 ObjectFactory::SetTypeId(std::string tid)
50 {
51  NS_LOG_FUNCTION(this << tid);
53 }
54 
55 bool
57 {
58  if (m_tid.GetUid() != 0)
59  {
60  return true;
61  }
62  return false;
63 }
64 
65 void
66 ObjectFactory::DoSet(const std::string& name, const AttributeValue& value)
67 {
68  NS_LOG_FUNCTION(this << name << &value);
69  if (name.empty())
70  {
71  return;
72  }
73 
74  struct TypeId::AttributeInformation info;
75  if (!m_tid.LookupAttributeByName(name, &info))
76  {
77  NS_FATAL_ERROR("Invalid attribute set (" << name << ") on " << m_tid.GetName());
78  return;
79  }
80  Ptr<AttributeValue> v = info.checker->CreateValidValue(value);
81  if (!v)
82  {
83  NS_FATAL_ERROR("Invalid value for attribute set (" << name << ") on " << m_tid.GetName());
84  return;
85  }
86  m_parameters.Add(name, info.checker, value.Copy());
87 }
88 
89 TypeId
91 {
92  NS_LOG_FUNCTION(this);
93  return m_tid;
94 }
95 
98 {
99  NS_LOG_FUNCTION(this);
101  ObjectBase* base = cb();
102  Object* derived = dynamic_cast<Object*>(base);
103  NS_ASSERT(derived != nullptr);
104  derived->SetTypeId(m_tid);
105  derived->Construct(m_parameters);
106  Ptr<Object> object = Ptr<Object>(derived, false);
107  return object;
108 }
109 
110 std::ostream&
111 operator<<(std::ostream& os, const ObjectFactory& factory)
112 {
113  os << factory.m_tid.GetName() << "[";
114  bool first = true;
116  i != factory.m_parameters.End();
117  ++i)
118  {
119  os << i->name << "=" << i->value->SerializeToString(i->checker);
120  if (first)
121  {
122  os << "|";
123  }
124  }
125  os << "]";
126  return os;
127 }
128 
129 std::istream&
130 operator>>(std::istream& is, ObjectFactory& factory)
131 {
132  std::string v;
133  is >> v;
134  std::string::size_type lbracket;
135  std::string::size_type rbracket;
136  lbracket = v.find('[');
137  rbracket = v.find(']');
138  if (lbracket == std::string::npos && rbracket == std::string::npos)
139  {
140  factory.SetTypeId(v);
141  return is;
142  }
143  if (lbracket == std::string::npos || rbracket == std::string::npos)
144  {
145  return is;
146  }
147  NS_ASSERT(lbracket != std::string::npos);
148  NS_ASSERT(rbracket != std::string::npos);
149  std::string tid = v.substr(0, lbracket);
150  std::string parameters = v.substr(lbracket + 1, rbracket - (lbracket + 1));
151  factory.SetTypeId(tid);
152  std::string::size_type cur;
153  cur = 0;
154  while (cur != parameters.size())
155  {
156  std::string::size_type equal = parameters.find('=', cur);
157  if (equal == std::string::npos)
158  {
159  is.setstate(std::ios_base::failbit);
160  break;
161  }
162  else
163  {
164  std::string name = parameters.substr(cur, equal - cur);
165  struct TypeId::AttributeInformation info;
166  if (!factory.m_tid.LookupAttributeByName(name, &info))
167  {
168  is.setstate(std::ios_base::failbit);
169  break;
170  }
171  else
172  {
173  std::string::size_type next = parameters.find('|', cur);
174  std::string value;
175  if (next == std::string::npos)
176  {
177  value = parameters.substr(equal + 1, parameters.size() - (equal + 1));
178  cur = parameters.size();
179  }
180  else
181  {
182  value = parameters.substr(equal + 1, next - (equal + 1));
183  cur = next + 1;
184  }
185  Ptr<AttributeValue> val = info.checker->Create();
186  bool ok = val->DeserializeFromString(value, info.checker);
187  if (!ok)
188  {
189  is.setstate(std::ios_base::failbit);
190  break;
191  }
192  else
193  {
194  factory.m_parameters.Add(name, info.checker, val);
195  }
196  }
197  }
198  }
199  NS_ABORT_MSG_IF(is.bad(), "Failure to parse " << parameters);
200  return is;
201 }
202 
204 
205 } // namespace ns3
void Add(std::string name, Ptr< const AttributeChecker > checker, Ptr< AttributeValue > value)
Add an Attribute to the list.
std::list< struct Item >::const_iterator CIterator
Iterator type.
Hold a value for an Attribute.
Definition: attribute.h:70
Callback template class.
Definition: callback.h:443
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:60
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:839
Callback< ObjectBase * > GetConstructor() const
Get the constructor callback.
Definition: type-id.cc:1088
uint16_t GetUid() const
Get the internal id of this TypeId.
Definition: type-id.cc:1209
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:897
std::string GetName() const
Get the name.
Definition: type-id.cc:995
#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 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.
ATTRIBUTE_HELPER_CPP(Length)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:153
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:129
value
Definition: second.py:41
ns3::ObjectFactory class declaration.
Attribute implementation.
Definition: type-id.h:82
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:96