A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
24 namespace ns3 {
25 
26 NS_LOG_COMPONENT_DEFINE("ObjectFactory")
27  ;
28 
30 {
31  NS_LOG_FUNCTION (this);
32 }
33 
34 ObjectFactory::ObjectFactory (std::string typeId)
35 {
36  NS_LOG_FUNCTION (this << typeId);
37  SetTypeId (typeId);
38 }
39 
40 void
42 {
43  NS_LOG_FUNCTION (this << tid.GetName ());
44  m_tid = tid;
45 }
46 void
47 ObjectFactory::SetTypeId (std::string tid)
48 {
49  NS_LOG_FUNCTION (this << tid);
51 }
52 void
53 ObjectFactory::SetTypeId (const char *tid)
54 {
55  NS_LOG_FUNCTION (this << tid);
57 }
58 void
59 ObjectFactory::Set (std::string name, const AttributeValue &value)
60 {
61  NS_LOG_FUNCTION (this << name << &value);
62  if (name == "")
63  {
64  return;
65  }
66 
67  struct TypeId::AttributeInformation info;
68  if (!m_tid.LookupAttributeByName (name, &info))
69  {
70  NS_FATAL_ERROR ("Invalid attribute set (" << name << ") on " << m_tid.GetName ());
71  return;
72  }
73  Ptr<AttributeValue> v = info.checker->CreateValidValue (value);
74  if (v == 0)
75  {
76  NS_FATAL_ERROR ("Invalid value for attribute set (" << name << ") on " << m_tid.GetName ());
77  return;
78  }
79  m_parameters.Add (name, info.checker, value.Copy ());
80 }
81 
82 TypeId
84 {
85  NS_LOG_FUNCTION (this);
86  return m_tid;
87 }
88 
91 {
92  NS_LOG_FUNCTION (this);
94  ObjectBase *base = cb ();
95  Object *derived = dynamic_cast<Object *> (base);
96  NS_ASSERT (derived != 0);
97  derived->SetTypeId (m_tid);
98  derived->Construct (m_parameters);
99  Ptr<Object> object = Ptr<Object> (derived, false);
100  return object;
101 }
102 
103 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
104 {
105  os << factory.m_tid.GetName () << "[";
106  bool first = true;
107  for (AttributeConstructionList::CIterator i = factory.m_parameters.Begin (); i != factory.m_parameters.End (); ++i)
108  {
109  os << i->name << "=" << i->value->SerializeToString (i->checker);
110  if (first)
111  {
112  os << "|";
113  }
114  }
115  os << "]";
116  return os;
117 }
118 std::istream & operator >> (std::istream &is, ObjectFactory &factory)
119 {
120  std::string v;
121  is >> v;
122  std::string::size_type lbracket, rbracket;
123  lbracket = v.find ("[");
124  rbracket = v.find ("]");
125  if (lbracket == std::string::npos && rbracket == std::string::npos)
126  {
127  factory.SetTypeId (v);
128  return is;
129  }
130  if (lbracket == std::string::npos || rbracket == std::string::npos)
131  {
132  return is;
133  }
134  NS_ASSERT (lbracket != std::string::npos);
135  NS_ASSERT (rbracket != std::string::npos);
136  std::string tid = v.substr (0, lbracket);
137  std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
138  factory.SetTypeId (tid);
139  std::string::size_type cur;
140  cur = 0;
141  while (cur != parameters.size ())
142  {
143  std::string::size_type equal = parameters.find ("=", cur);
144  if (equal == std::string::npos)
145  {
146  is.setstate (std::ios_base::failbit);
147  break;
148  }
149  else
150  {
151  std::string name = parameters.substr (cur, equal-cur);
152  struct TypeId::AttributeInformation info;
153  if (!factory.m_tid.LookupAttributeByName (name, &info))
154  {
155  is.setstate (std::ios_base::failbit);
156  break;
157  }
158  else
159  {
160  std::string::size_type next = parameters.find ("|", cur);
161  std::string value;
162  if (next == std::string::npos)
163  {
164  value = parameters.substr (equal+1, parameters.size () - (equal+1));
165  cur = parameters.size ();
166  }
167  else
168  {
169  value = parameters.substr (equal+1, next - (equal+1));
170  cur = next + 1;
171  }
172  Ptr<AttributeValue> val = info.checker->Create ();
173  bool ok = val->DeserializeFromString (value, info.checker);
174  if (!ok)
175  {
176  is.setstate (std::ios_base::failbit);
177  break;
178  }
179  else
180  {
181  factory.m_parameters.Add (name, info.checker, val);
182  }
183  }
184  }
185  }
186  return is;
187 }
188 
189 
190 ATTRIBUTE_HELPER_CPP (ObjectFactory);
191 
192 } // namespace ns3
std::istream & operator>>(std::istream &is, Angles &a)
initialize a struct Angles from input
Definition: angles.cc:49
TypeId GetTypeId(void) const
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
Callback template class.
Definition: callback.h:920
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
AttributeConstructionList m_parameters
Hold a value for an Attribute.
Definition: attribute.h:56
#define NS_ASSERT(condition)
Definition: assert.h:64
void SetTypeId(TypeId tid)
implement the ns-3 type and attribute system
Definition: object-base.h:70
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Callback< ObjectBase * > GetConstructor(void) const
Definition: type-id.cc:723
void SetTypeId(TypeId tid)
Definition: object.cc:328
Ptr< Object > Create(void) const
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:43
Ptr< const AttributeChecker > checker
Definition: type-id.h:68
virtual Ptr< AttributeValue > Copy(void) const =0
bool LookupAttributeByName(std::string name, struct AttributeInformation *info) const
Definition: type-id.cc:589
std::string GetName(void) const
Definition: type-id.cc:658
void Set(std::string name, const AttributeValue &value)
std::list< struct Item >::const_iterator CIterator
instantiate subclasses of ns3::Object.
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)=0
void Construct(const AttributeConstructionList &attributes)
Definition: object.cc:139
a base class which provides memory management and object aggregation
Definition: object.h:63
ATTRIBUTE_HELPER_CPP(ObjectFactory)
void Add(std::string name, Ptr< const AttributeChecker > checker, Ptr< AttributeValue > value)
a unique identifier for an interface.
Definition: type-id.h:49
static TypeId LookupByName(std::string name)
Definition: type-id.cc:536