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 void
64 ObjectFactory::Set (std::string name, const AttributeValue &value)
65 {
66  NS_LOG_FUNCTION (this << name << &value);
67  if (name == "")
68  {
69  return;
70  }
71 
72  struct TypeId::AttributeInformation info;
73  if (!m_tid.LookupAttributeByName (name, &info))
74  {
75  NS_FATAL_ERROR ("Invalid attribute set (" << name << ") on " << m_tid.GetName ());
76  return;
77  }
78  Ptr<AttributeValue> v = info.checker->CreateValidValue (value);
79  if (v == 0)
80  {
81  NS_FATAL_ERROR ("Invalid value for attribute set (" << name << ") on " << m_tid.GetName ());
82  return;
83  }
84  m_parameters.Add (name, info.checker, value.Copy ());
85 }
86 
87 TypeId
89 {
90  NS_LOG_FUNCTION (this);
91  return m_tid;
92 }
93 
96 {
97  NS_LOG_FUNCTION (this);
99  ObjectBase *base = cb ();
100  Object *derived = dynamic_cast<Object *> (base);
101  NS_ASSERT (derived != 0);
102  derived->SetTypeId (m_tid);
103  derived->Construct (m_parameters);
104  Ptr<Object> object = Ptr<Object> (derived, false);
105  return object;
106 }
107 
108 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
109 {
110  os << factory.m_tid.GetName () << "[";
111  bool first = true;
112  for (AttributeConstructionList::CIterator i = factory.m_parameters.Begin (); i != factory.m_parameters.End (); ++i)
113  {
114  os << i->name << "=" << i->value->SerializeToString (i->checker);
115  if (first)
116  {
117  os << "|";
118  }
119  }
120  os << "]";
121  return os;
122 }
123 std::istream & operator >> (std::istream &is, ObjectFactory &factory)
124 {
125  std::string v;
126  is >> v;
127  std::string::size_type lbracket, rbracket;
128  lbracket = v.find ("[");
129  rbracket = v.find ("]");
130  if (lbracket == std::string::npos && rbracket == std::string::npos)
131  {
132  factory.SetTypeId (v);
133  return is;
134  }
135  if (lbracket == std::string::npos || rbracket == std::string::npos)
136  {
137  return is;
138  }
139  NS_ASSERT (lbracket != std::string::npos);
140  NS_ASSERT (rbracket != std::string::npos);
141  std::string tid = v.substr (0, lbracket);
142  std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
143  factory.SetTypeId (tid);
144  std::string::size_type cur;
145  cur = 0;
146  while (cur != parameters.size ())
147  {
148  std::string::size_type equal = parameters.find ("=", cur);
149  if (equal == std::string::npos)
150  {
151  is.setstate (std::ios_base::failbit);
152  break;
153  }
154  else
155  {
156  std::string name = parameters.substr (cur, equal-cur);
157  struct TypeId::AttributeInformation info;
158  if (!factory.m_tid.LookupAttributeByName (name, &info))
159  {
160  is.setstate (std::ios_base::failbit);
161  break;
162  }
163  else
164  {
165  std::string::size_type next = parameters.find ("|", cur);
166  std::string value;
167  if (next == std::string::npos)
168  {
169  value = parameters.substr (equal+1, parameters.size () - (equal+1));
170  cur = parameters.size ();
171  }
172  else
173  {
174  value = parameters.substr (equal+1, next - (equal+1));
175  cur = next + 1;
176  }
177  Ptr<AttributeValue> val = info.checker->Create ();
178  bool ok = val->DeserializeFromString (value, info.checker);
179  if (!ok)
180  {
181  is.setstate (std::ios_base::failbit);
182  break;
183  }
184  else
185  {
186  factory.m_parameters.Add (name, info.checker, val);
187  }
188  }
189  }
190  }
191  NS_ABORT_MSG_IF (is.bad (), "Failure to parse " << parameters);
192  return is;
193 }
194 
195 
197 
198 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:48
TypeId GetTypeId(void) const
Get the TypeId which will be created by this ObjectFactory.
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:201
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:118
Callback< ObjectBase * > GetConstructor(void) const
Get the constructor callback.
Definition: type-id.cc:1042
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
Every class exported by the ns3 library is enclosed in the ns3 namespace.
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Find an Attribute by name, retrieving the associated AttributeInformation.
Definition: type-id.cc:866
std::string GetName(void) const
Get the name.
Definition: type-id.cc:958
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
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:813