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
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
src
core
model
object-factory.cc
Generated on Fri Dec 21 2012 19:00:32 for ns-3 by
1.8.1.2