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 void
41 {
42  NS_LOG_FUNCTION (this << tid.GetName ());
43  m_tid = tid;
44 }
45 void
46 ObjectFactory::SetTypeId (std::string tid)
47 {
48  NS_LOG_FUNCTION (this << tid);
50 }
51 void
52 ObjectFactory::SetTypeId (const char *tid)
53 {
54  NS_LOG_FUNCTION (this << tid);
56 }
57 bool
59 {
60  if (m_tid.GetUid () != 0)
61  {
62  return true;
63  }
64  return false;
65 }
66 void
67 ObjectFactory::DoSet (const std::string &name, const AttributeValue &value)
68 {
69  NS_LOG_FUNCTION (this << name << &value);
70  if (name == "")
71  {
72  return;
73  }
74 
75  struct TypeId::AttributeInformation info;
76  if (!m_tid.LookupAttributeByName (name, &info))
77  {
78  NS_FATAL_ERROR ("Invalid attribute set (" << name << ") on " << m_tid.GetName ());
79  return;
80  }
81  Ptr<AttributeValue> v = info.checker->CreateValidValue (value);
82  if (v == 0)
83  {
84  NS_FATAL_ERROR ("Invalid value for attribute set (" << name << ") on " << m_tid.GetName ());
85  return;
86  }
87  m_parameters.Add (name, info.checker, value.Copy ());
88 }
89 
90 TypeId
92 {
93  NS_LOG_FUNCTION (this);
94  return m_tid;
95 }
96 
99 {
100  NS_LOG_FUNCTION (this);
102  ObjectBase *base = cb ();
103  Object *derived = dynamic_cast<Object *> (base);
104  NS_ASSERT (derived != 0);
105  derived->SetTypeId (m_tid);
106  derived->Construct (m_parameters);
107  Ptr<Object> object = Ptr<Object> (derived, false);
108  return object;
109 }
110 
111 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
112 {
113  os << factory.m_tid.GetName () << "[";
114  bool first = true;
115  for (AttributeConstructionList::CIterator i = factory.m_parameters.Begin (); i != factory.m_parameters.End (); ++i)
116  {
117  os << i->name << "=" << i->value->SerializeToString (i->checker);
118  if (first)
119  {
120  os << "|";
121  }
122  }
123  os << "]";
124  return os;
125 }
126 std::istream & operator >> (std::istream &is, ObjectFactory &factory)
127 {
128  std::string v;
129  is >> v;
130  std::string::size_type lbracket, rbracket;
131  lbracket = v.find ("[");
132  rbracket = v.find ("]");
133  if (lbracket == std::string::npos && rbracket == std::string::npos)
134  {
135  factory.SetTypeId (v);
136  return is;
137  }
138  if (lbracket == std::string::npos || rbracket == std::string::npos)
139  {
140  return is;
141  }
142  NS_ASSERT (lbracket != std::string::npos);
143  NS_ASSERT (rbracket != std::string::npos);
144  std::string tid = v.substr (0, lbracket);
145  std::string parameters = v.substr (lbracket + 1,rbracket - (lbracket + 1));
146  factory.SetTypeId (tid);
147  std::string::size_type cur;
148  cur = 0;
149  while (cur != parameters.size ())
150  {
151  std::string::size_type equal = parameters.find ("=", cur);
152  if (equal == std::string::npos)
153  {
154  is.setstate (std::ios_base::failbit);
155  break;
156  }
157  else
158  {
159  std::string name = parameters.substr (cur, equal - cur);
160  struct TypeId::AttributeInformation info;
161  if (!factory.m_tid.LookupAttributeByName (name, &info))
162  {
163  is.setstate (std::ios_base::failbit);
164  break;
165  }
166  else
167  {
168  std::string::size_type next = parameters.find ("|", cur);
169  std::string value;
170  if (next == std::string::npos)
171  {
172  value = parameters.substr (equal + 1, parameters.size () - (equal + 1));
173  cur = parameters.size ();
174  }
175  else
176  {
177  value = parameters.substr (equal + 1, next - (equal + 1));
178  cur = next + 1;
179  }
180  Ptr<AttributeValue> val = info.checker->Create ();
181  bool ok = val->DeserializeFromString (value, info.checker);
182  if (!ok)
183  {
184  is.setstate (std::ios_base::failbit);
185  break;
186  }
187  else
188  {
189  factory.m_parameters.Add (name, info.checker, val);
190  }
191  }
192  }
193  }
194  NS_ABORT_MSG_IF (is.bad (), "Failure to parse " << parameters);
195  return is;
196 }
197 
198 
200 
201 } // 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:1061
std::string GetName(void) const
Get the name.
Definition: type-id.cc:977
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:1278
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:205
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:165
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:883
ObjectFactory()
Default constructor.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Attribute implementation.
Definition: type-id.h:77
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:92
virtual Ptr< AttributeValue > Copy(void) const =0
uint16_t GetUid(void) const
Get the internal id of this TypeId.
Definition: type-id.cc:1185
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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
void DoSet(const std::string &name, const AttributeValue &value)
Set an attribute to be set during construction.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830