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 
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);
50 }
51 void
52 ObjectFactory::SetTypeId (const char *tid)
53 {
54  NS_LOG_FUNCTION (this << 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  NS_ASSERT (derived != 0);
96  derived->SetTypeId (m_tid);
97  derived->Construct (m_parameters);
98  Ptr<Object> object = Ptr<Object> (derived, false);
99  return object;
100 }
101 
102 std::ostream & operator << (std::ostream &os, const ObjectFactory &factory)
103 {
104  os << factory.m_tid.GetName () << "[";
105  bool first = true;
106  for (AttributeConstructionList::CIterator i = factory.m_parameters.Begin (); i != factory.m_parameters.End (); ++i)
107  {
108  os << i->name << "=" << i->value->SerializeToString (i->checker);
109  if (first)
110  {
111  os << "|";
112  }
113  }
114  os << "]";
115  return os;
116 }
117 std::istream & operator >> (std::istream &is, ObjectFactory &factory)
118 {
119  std::string v;
120  is >> v;
121  std::string::size_type lbracket, rbracket;
122  lbracket = v.find ("[");
123  rbracket = v.find ("]");
124  if (lbracket == std::string::npos && rbracket == std::string::npos)
125  {
126  factory.SetTypeId (v);
127  return is;
128  }
129  if (lbracket == std::string::npos || rbracket == std::string::npos)
130  {
131  return is;
132  }
133  NS_ASSERT (lbracket != std::string::npos);
134  NS_ASSERT (rbracket != std::string::npos);
135  std::string tid = v.substr (0, lbracket);
136  std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
137  factory.SetTypeId (tid);
138  std::string::size_type cur;
139  cur = 0;
140  while (cur != parameters.size ())
141  {
142  std::string::size_type equal = parameters.find ("=", cur);
143  if (equal == std::string::npos)
144  {
145  is.setstate (std::ios_base::failbit);
146  break;
147  }
148  else
149  {
150  std::string name = parameters.substr (cur, equal-cur);
151  struct TypeId::AttributeInformation info;
152  if (!factory.m_tid.LookupAttributeByName (name, &info))
153  {
154  is.setstate (std::ios_base::failbit);
155  break;
156  }
157  else
158  {
159  std::string::size_type next = parameters.find ("|", cur);
160  std::string value;
161  if (next == std::string::npos)
162  {
163  value = parameters.substr (equal+1, parameters.size () - (equal+1));
164  cur = parameters.size ();
165  }
166  else
167  {
168  value = parameters.substr (equal+1, next - (equal+1));
169  cur = next + 1;
170  }
171  Ptr<AttributeValue> val = info.checker->Create ();
172  bool ok = val->DeserializeFromString (value, info.checker);
173  if (!ok)
174  {
175  is.setstate (std::ios_base::failbit);
176  break;
177  }
178  else
179  {
180  factory.m_parameters.Add (name, info.checker, val);
181  }
182  }
183  }
184  }
185  return is;
186 }
187 
188 
189 ATTRIBUTE_HELPER_CPP (ObjectFactory);
190 
191 } // 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:920
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:55
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
Callback< ObjectBase * > GetConstructor(void) const
Definition: type-id.cc:722
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
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:588
std::string GetName(void) const
Definition: type-id.cc:657
void Set(std::string name, const AttributeValue &value)
std::list< struct Item >::const_iterator CIterator
instantiate subclasses of ns3::Object.
NS_LOG_COMPONENT_DEFINE("PacketLossCounter")
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)=0
void Construct(const AttributeConstructionList &attributes)
Definition: object.cc:138
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:535