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