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
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
40
ObjectFactory::SetTypeId
(
TypeId
tid)
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
82
ObjectFactory::GetTypeId
(
void
)
const
83
{
84
NS_LOG_FUNCTION
(
this
);
85
return
m_tid
;
86
}
87
88
Ptr<Object>
89
ObjectFactory::Create
(
void
)
const
90
{
91
NS_LOG_FUNCTION
(
this
);
92
Callback<ObjectBase *>
cb =
m_tid
.
GetConstructor
();
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
src
core
model
object-factory.cc
Generated on Fri Aug 30 2013 01:42:47 for ns-3 by
1.8.1.2