A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
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
ObjectFactory::ObjectFactory
()
29
{
30
}
31
32
ObjectFactory::ObjectFactory
(std::string typeId)
33
{
34
SetTypeId
(typeId);
35
}
36
37
void
38
ObjectFactory::SetTypeId
(
TypeId
tid)
39
{
40
m_tid
= tid;
41
}
42
void
43
ObjectFactory::SetTypeId
(std::string tid)
44
{
45
m_tid
=
TypeId::LookupByName
(tid);
46
}
47
void
48
ObjectFactory::SetTypeId
(
const
char
*tid)
49
{
50
m_tid
=
TypeId::LookupByName
(tid);
51
}
52
void
53
ObjectFactory::Set
(std::string name,
const
AttributeValue
&value)
54
{
55
if
(name ==
""
)
56
{
57
return
;
58
}
59
60
struct
TypeId::AttributeInformation
info;
61
if
(!
m_tid
.
LookupAttributeByName
(name, &info))
62
{
63
NS_FATAL_ERROR
(
"Invalid attribute set ("
<< name <<
") on "
<<
m_tid
.
GetName
());
64
return
;
65
}
66
Ptr<AttributeValue>
v = info.
checker
->
CreateValidValue
(value);
67
if
(v == 0)
68
{
69
NS_FATAL_ERROR
(
"Invalid value for attribute set ("
<< name <<
") on "
<<
m_tid
.
GetName
());
70
return
;
71
}
72
m_parameters
.
Add
(name, info.
checker
, value.
Copy
());
73
}
74
75
TypeId
76
ObjectFactory::GetTypeId
(
void
)
const
77
{
78
return
m_tid
;
79
}
80
81
Ptr<Object>
82
ObjectFactory::Create
(
void
)
const
83
{
84
Callback<ObjectBase *>
cb =
m_tid
.
GetConstructor
();
85
ObjectBase
*base = cb ();
86
Object
*derived =
dynamic_cast<
Object
*
>
(base);
87
derived->
SetTypeId
(
m_tid
);
88
derived->
Construct
(
m_parameters
);
89
Ptr<Object>
object
=
Ptr<Object>
(derived,
false
);
90
return
object;
91
}
92
93
std::ostream &
operator <<
(std::ostream &os,
const
ObjectFactory
&factory)
94
{
95
os << factory.
m_tid
.
GetName
() <<
"["
;
96
bool
first =
true
;
97
for
(
AttributeConstructionList::CIterator
i = factory.
m_parameters
.
Begin
(); i != factory.
m_parameters
.
End
(); ++i)
98
{
99
os << i->name <<
"="
<< i->value->SerializeToString (i->checker);
100
if
(first)
101
{
102
os <<
"|"
;
103
}
104
}
105
os <<
"]"
;
106
return
os;
107
}
108
std::istream &
operator >>
(std::istream &is,
ObjectFactory
&factory)
109
{
110
std::string v;
111
is >> v;
112
std::string::size_type lbracket, rbracket;
113
lbracket = v.find (
"["
);
114
rbracket = v.find (
"]"
);
115
if
(lbracket == std::string::npos && rbracket == std::string::npos)
116
{
117
factory.
SetTypeId
(v);
118
return
is;
119
}
120
if
(lbracket == std::string::npos || rbracket == std::string::npos)
121
{
122
NS_LOG_DEBUG
(
"Error while parsing factory specification: mismatching brackets. \""
<< v <<
"\""
);
123
return
is;
124
}
125
NS_ASSERT
(lbracket != std::string::npos);
126
NS_ASSERT
(rbracket != std::string::npos);
127
std::string tid = v.substr (0, lbracket);
128
std::string parameters = v.substr (lbracket+1,rbracket-(lbracket+1));
129
factory.
SetTypeId
(tid);
130
std::string::size_type cur;
131
cur = 0;
132
while
(cur != parameters.size ())
133
{
134
std::string::size_type equal = parameters.find (
"="
, cur);
135
if
(equal == std::string::npos)
136
{
137
is.setstate (std::ios_base::failbit);
138
NS_LOG_DEBUG
(
"Error while parsing serialized attribute: \""
<< parameters <<
"\""
);
139
break
;
140
}
141
else
142
{
143
std::string name = parameters.substr (cur, equal-cur);
144
struct
TypeId::AttributeInformation
info;
145
if
(!factory.
m_tid
.
LookupAttributeByName
(name, &info))
146
{
147
is.setstate (std::ios_base::failbit);
148
NS_LOG_DEBUG
(
"Error while parsing serialized attribute: name does not exist: \""
<< name <<
"\""
);
149
break
;
150
}
151
else
152
{
153
std::string::size_type next = parameters.find (
"|"
, cur);
154
std::string value;
155
if
(next == std::string::npos)
156
{
157
value = parameters.substr (equal+1, parameters.size () - (equal+1));
158
cur = parameters.size ();
159
}
160
else
161
{
162
value = parameters.substr (equal+1, next - (equal+1));
163
cur = next + 1;
164
}
165
Ptr<AttributeValue>
val = info.
checker
->
Create
();
166
bool
ok = val->
DeserializeFromString
(value, info.
checker
);
167
if
(!ok)
168
{
169
is.setstate (std::ios_base::failbit);
170
NS_LOG_DEBUG
(
"Error while parsing serialized attribute: value invalid: \""
<< value <<
"\""
);
171
break
;
172
}
173
else
174
{
175
factory.
m_parameters
.
Add
(name, info.
checker
, val);
176
}
177
}
178
}
179
}
180
return
is;
181
}
182
183
184
ATTRIBUTE_HELPER_CPP
(ObjectFactory);
185
186
}
// namespace ns3
src
core
model
object-factory.cc
Generated on Tue Oct 9 2012 16:45:34 for ns-3 by
1.8.1.2