A Discrete-Event Network Simulator
API
object-factory.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "object-factory.h"
21 #include "log.h"
22 #include <sstream>
23 
30 namespace ns3 {
31 
32 NS_LOG_COMPONENT_DEFINE("ObjectFactory");
33 
35 {
36  NS_LOG_FUNCTION (this);
37 }
38 
39 ObjectFactory::ObjectFactory (std::string typeId)
40 {
41  NS_LOG_FUNCTION (this << typeId);
42  SetTypeId (typeId);
43 }
44 
45 void
47 {
48  NS_LOG_FUNCTION (this << tid.GetName ());
49  m_tid = tid;
50 }
51 void
52 ObjectFactory::SetTypeId (std::string tid)
53 {
54  NS_LOG_FUNCTION (this << tid);
56 }
57 void
58 ObjectFactory::SetTypeId (const char *tid)
59 {
60  NS_LOG_FUNCTION (this << tid);
62 }
63 bool
65 {
66  if (m_tid.GetUid () != 0)
67  {
68  return true;
69  }
70  return false;
71 }
72 void
73 ObjectFactory::Set (std::string name, const AttributeValue &value)
74 {
75  NS_LOG_FUNCTION (this << name << &value);
76  if (name == "")
77  {
78  return;
79  }
80 
81  struct TypeId::AttributeInformation info;
82  if (!m_tid.LookupAttributeByName (name, &info))
83  {
84  NS_FATAL_ERROR ("Invalid attribute set (" << name << ") on " << m_tid.GetName ());
85  return;
86  }
87  Ptr<AttributeValue> v = info.checker->CreateValidValue (value);
88  if (v == 0)
89  {
90  NS_FATAL_ERROR ("Invalid value for attribute set (" << name << ") on " << m_tid.GetName ());
91  return;
92  }
93  m_parameters.Add (name, info.checker, value.Copy ());
94 }
95 
96 TypeId
98 {
99  NS_LOG_FUNCTION (this);
100  return m_tid;
101 }
102 
105 {
106  NS_LOG_FUNCTION (this);
108  ObjectBase *base = cb ();
109  Object *derived = dynamic_cast<Object *> (base);
110  NS_ASSERT (derived != 0);
111  derived->SetTypeId (m_tid);
112  derived->Construct (m_parameters);
113  Ptr<Object> object = Ptr<Object> (derived, false);
114  return object;
115 }
116 
117 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
118 {
119  os << factory.m_tid.GetName () << "[";
120  bool first = true;
121  for (AttributeConstructionList::CIterator i = factory.m_parameters.Begin (); i != factory.m_parameters.End (); ++i)
122  {
123  os << i->name << "=" << i->value->SerializeToString (i->checker);
124  if (first)
125  {
126  os << "|";
127  }
128  }
129  os << "]";
130  return os;
131 }
132 std::istream & operator >> (std::istream &is, ObjectFactory &factory)
133 {
134  std::string v;
135  is >> v;
136  std::string::size_type lbracket, rbracket;
137  lbracket = v.find ("[");
138  rbracket = v.find ("]");
139  if (lbracket == std::string::npos && rbracket == std::string::npos)
140  {
141  factory.SetTypeId (v);
142  return is;
143  }
144  if (lbracket == std::string::npos || rbracket == std::string::npos)
145  {
146  return is;
147  }
148  NS_ASSERT (lbracket != std::string::npos);
149  NS_ASSERT (rbracket != std::string::npos);
150  std::string tid = v.substr (0, lbracket);
151  std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
152  factory.SetTypeId (tid);
153  std::string::size_type cur;
154  cur = 0;
155  while (cur != parameters.size ())
156  {
157  std::string::size_type equal = parameters.find ("=", cur);
158  if (equal == std::string::npos)
159  {
160  is.setstate (std::ios_base::failbit);
161  break;
162  }
163  else
164  {
165  std::string name = parameters.substr (cur, equal-cur);
166  struct TypeId::AttributeInformation info;
167  if (!factory.m_tid.LookupAttributeByName (name, &info))
168  {
169  is.setstate (std::ios_base::failbit);
170  break;
171  }
172  else
173  {
174  std::string::size_type next = parameters.find ("|", cur);
175  std::string value;
176  if (next == std::string::npos)
177  {
178  value = parameters.substr (equal+1, parameters.size () - (equal+1));
179  cur = parameters.size ();
180  }
181  else
182  {
183  value = parameters.substr (equal+1, next - (equal+1));
184  cur = next + 1;
185  }
186  Ptr<AttributeValue> val = info.checker->Create ();
187  bool ok = val->DeserializeFromString (value, info.checker);
188  if (!ok)
189  {
190  is.setstate (std::ios_base::failbit);
191  break;
192  }
193  else
194  {
195  factory.m_parameters.Add (name, info.checker, val);
196  }
197  }
198  }
199  }
200  NS_ABORT_MSG_IF (is.bad (), "Failure to parse " << parameters);
201  return is;
202 }
203 
204 
206 
207 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1053
std::string GetName(void) const
Get the name.
Definition: type-id.cc:969
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void Construct(const AttributeConstructionList &attributes)
Initialize all member variables registered as Attributes of this TypeId.
Definition: object.cc:142
Callback template class.
Definition: callback.h:1176
AttributeConstructionList m_parameters
The list of attributes and values to be used in constructing objects by this factory.
Hold a value for an Attribute.
Definition: attribute.h:68
#define ATTRIBUTE_HELPER_CPP(type)
Define the attribute value, accessor and checkers for class type.
ns3::ObjectFactory class declaration.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:877
ObjectFactory()
Default constructor.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Attribute implementation.
Definition: type-id.h:76
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
Ptr< const AttributeChecker > checker
Checker object.
Definition: type-id.h:90
virtual Ptr< AttributeValue > Copy(void) const =0
uint16_t GetUid(void) const
Get the internal id of this TypeId.
Definition: type-id.cc:1183
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
void SetTypeId(TypeId tid)
Set the TypeId of this Object.
Definition: object.cc:338
bool IsTypeIdSet(void) const
Check if the ObjectFactory has been configured with a TypeId.
TypeId GetTypeId(void) const
Get the TypeId which will be created by this ObjectFactory.
std::list< struct Item >::const_iterator CIterator
Iterator type.
Instantiate subclasses of ns3::Object.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)=0
A base class which provides memory management and object aggregation.
Definition: object.h:87
TypeId m_tid
The TypeId this factory will create.
Debug message logging.
Definition: first.py:1
void Add(std::string name, Ptr< const AttributeChecker > checker, Ptr< AttributeValue > value)
Add an Attribute to the list.
a unique identifier for an interface.
Definition: type-id.h:58
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:824