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 void
91 {
92 }
93 
94 TypeId
96 {
97  NS_LOG_FUNCTION (this);
98  return m_tid;
99 }
100 
103 {
104  NS_LOG_FUNCTION (this);
106  ObjectBase *base = cb ();
107  Object *derived = dynamic_cast<Object *> (base);
108  NS_ASSERT (derived != 0);
109  derived->SetTypeId (m_tid);
110  derived->Construct (m_parameters);
111  Ptr<Object> object = Ptr<Object> (derived, false);
112  return object;
113 }
114 
115 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
116 {
117  os << factory.m_tid.GetName () << "[";
118  bool first = true;
119  for (AttributeConstructionList::CIterator i = factory.m_parameters.Begin (); i != factory.m_parameters.End (); ++i)
120  {
121  os << i->name << "=" << i->value->SerializeToString (i->checker);
122  if (first)
123  {
124  os << "|";
125  }
126  }
127  os << "]";
128  return os;
129 }
130 std::istream & operator >> (std::istream &is, ObjectFactory &factory)
131 {
132  std::string v;
133  is >> v;
134  std::string::size_type lbracket, rbracket;
135  lbracket = v.find ("[");
136  rbracket = v.find ("]");
137  if (lbracket == std::string::npos && rbracket == std::string::npos)
138  {
139  factory.SetTypeId (v);
140  return is;
141  }
142  if (lbracket == std::string::npos || rbracket == std::string::npos)
143  {
144  return is;
145  }
146  NS_ASSERT (lbracket != std::string::npos);
147  NS_ASSERT (rbracket != std::string::npos);
148  std::string tid = v.substr (0, lbracket);
149  std::string parameters = v.substr (lbracket + 1,rbracket - (lbracket + 1));
150  factory.SetTypeId (tid);
151  std::string::size_type cur;
152  cur = 0;
153  while (cur != parameters.size ())
154  {
155  std::string::size_type equal = parameters.find ("=", cur);
156  if (equal == std::string::npos)
157  {
158  is.setstate (std::ios_base::failbit);
159  break;
160  }
161  else
162  {
163  std::string name = parameters.substr (cur, equal - cur);
164  struct TypeId::AttributeInformation info;
165  if (!factory.m_tid.LookupAttributeByName (name, &info))
166  {
167  is.setstate (std::ios_base::failbit);
168  break;
169  }
170  else
171  {
172  std::string::size_type next = parameters.find ("|", cur);
173  std::string value;
174  if (next == std::string::npos)
175  {
176  value = parameters.substr (equal + 1, parameters.size () - (equal + 1));
177  cur = parameters.size ();
178  }
179  else
180  {
181  value = parameters.substr (equal + 1, next - (equal + 1));
182  cur = next + 1;
183  }
184  Ptr<AttributeValue> val = info.checker->Create ();
185  bool ok = val->DeserializeFromString (value, info.checker);
186  if (!ok)
187  {
188  is.setstate (std::ios_base::failbit);
189  break;
190  }
191  else
192  {
193  factory.m_parameters.Add (name, info.checker, val);
194  }
195  }
196  }
197  }
198  NS_ABORT_MSG_IF (is.bad (), "Failure to parse " << parameters);
199  return is;
200 }
201 
202 
204 
205 } // 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
void Set(void)
Base case to stop the recursion performed by the templated version of this method.
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