1 #include "object-base.h" |
1 #include "object-base.h" |
2 |
2 #include "log.h" |
3 ns3::ObjectBase::~ObjectBase () {} |
3 #include "trace-source-accessor.h" |
|
4 #include "string.h" |
|
5 |
|
6 NS_LOG_COMPONENT_DEFINE ("ObjectBase"); |
|
7 |
|
8 namespace ns3 { |
|
9 |
|
10 static TypeId |
|
11 GetObjectIid (void) |
|
12 { |
|
13 TypeId tid = TypeId ("ns3::ObjectBase"); |
|
14 tid.SetParent (tid); |
|
15 return tid; |
|
16 } |
|
17 |
|
18 TypeId |
|
19 ObjectBase::GetTypeId (void) |
|
20 { |
|
21 static TypeId tid = GetObjectIid (); |
|
22 return tid; |
|
23 } |
|
24 |
|
25 ObjectBase::~ObjectBase () |
|
26 {} |
|
27 |
|
28 void |
|
29 ObjectBase::NotifyConstructionCompleted (void) |
|
30 {} |
|
31 |
|
32 void |
|
33 ObjectBase::ConstructSelf (const AttributeList &attributes) |
|
34 { |
|
35 // loop over the inheritance tree back to the Object base class. |
|
36 TypeId tid = GetInstanceTypeId (); |
|
37 do { |
|
38 // loop over all attributes in object type |
|
39 NS_LOG_DEBUG ("construct tid="<<tid.GetName ()<<", params="<<tid.GetAttributeListN ()); |
|
40 for (uint32_t i = 0; i < tid.GetAttributeListN (); i++) |
|
41 { |
|
42 Ptr<const AttributeAccessor> paramSpec = tid.GetAttributeAccessor (i); |
|
43 Attribute initial = tid.GetAttributeInitialValue (i); |
|
44 Ptr<const AttributeChecker> checker = tid.GetAttributeChecker (i); |
|
45 NS_LOG_DEBUG ("try to construct \""<< tid.GetName ()<<"::"<< |
|
46 tid.GetAttributeName (i)<<"\""); |
|
47 if (!(tid.GetAttributeFlags (i) & TypeId::ATTR_CONSTRUCT)) |
|
48 { |
|
49 continue; |
|
50 } |
|
51 bool found = false; |
|
52 // is this attribute stored in this AttributeList instance ? |
|
53 for (AttributeList::Attrs::const_iterator j = attributes.m_attributes.begin (); |
|
54 j != attributes.m_attributes.end (); j++) |
|
55 { |
|
56 if (j->checker == checker) |
|
57 { |
|
58 // We have a matching attribute value. |
|
59 DoSet (paramSpec, initial, checker, j->value); |
|
60 NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< |
|
61 tid.GetAttributeName (i)<<"\""); |
|
62 found = true; |
|
63 break; |
|
64 } |
|
65 } |
|
66 if (!found) |
|
67 { |
|
68 // is this attribute stored in the global instance instance ? |
|
69 for (AttributeList::Attrs::const_iterator j = AttributeList::GetGlobal ()->m_attributes.begin (); |
|
70 j != AttributeList::GetGlobal ()->m_attributes.end (); j++) |
|
71 { |
|
72 if (j->checker == checker) |
|
73 { |
|
74 // We have a matching attribute value. |
|
75 DoSet (paramSpec, initial, checker, j->value); |
|
76 NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< |
|
77 tid.GetAttributeName (i)<<"\" from global"); |
|
78 found = true; |
|
79 break; |
|
80 } |
|
81 } |
|
82 } |
|
83 if (!found) |
|
84 { |
|
85 // No matching attribute value so we set the default value. |
|
86 paramSpec->Set (this, initial); |
|
87 NS_LOG_DEBUG ("construct \""<< tid.GetName ()<<"::"<< |
|
88 tid.GetAttributeName (i)<<"\" from initial value."); |
|
89 } |
|
90 } |
|
91 tid = tid.GetParent (); |
|
92 } while (tid != ObjectBase::GetTypeId ()); |
|
93 NotifyConstructionCompleted (); |
|
94 } |
|
95 |
|
96 bool |
|
97 ObjectBase::DoSet (Ptr<const AttributeAccessor> spec, Attribute initialValue, |
|
98 Ptr<const AttributeChecker> checker, Attribute value) |
|
99 { |
|
100 bool ok = checker->Check (value); |
|
101 if (!ok) |
|
102 { |
|
103 // attempt to convert to string |
|
104 const StringValue *str = value.DynCast<const StringValue *> (); |
|
105 if (str == 0) |
|
106 { |
|
107 return false; |
|
108 } |
|
109 // attempt to convert back from string. |
|
110 Attribute v = checker->Create (); |
|
111 ok = v.DeserializeFromString (str->Get ().Get (), checker); |
|
112 if (!ok) |
|
113 { |
|
114 return false; |
|
115 } |
|
116 ok = checker->Check (v); |
|
117 if (!ok) |
|
118 { |
|
119 return false; |
|
120 } |
|
121 value = v; |
|
122 } |
|
123 ok = spec->Set (this, value); |
|
124 return ok; |
|
125 } |
|
126 void |
|
127 ObjectBase::SetAttribute (std::string name, Attribute value) |
|
128 { |
|
129 struct TypeId::AttributeInfo info; |
|
130 TypeId tid = GetInstanceTypeId (); |
|
131 if (!tid.LookupAttributeByName (name, &info)) |
|
132 { |
|
133 NS_FATAL_ERROR ("Attribute name="<<name<<" does not exist for this object: tid="<<tid.GetName ()); |
|
134 } |
|
135 if (!(info.flags & TypeId::ATTR_SET)) |
|
136 { |
|
137 NS_FATAL_ERROR ("Attribute name="<<name<<" is not settable for this object: tid="<<tid.GetName ()); |
|
138 } |
|
139 if (!DoSet (info.accessor, info.initialValue, info.checker, value)) |
|
140 { |
|
141 NS_FATAL_ERROR ("Attribute name="<<name<<" could not be set for this object: tid="<<tid.GetName ()); |
|
142 } |
|
143 } |
|
144 bool |
|
145 ObjectBase::SetAttributeFailSafe (std::string name, Attribute value) |
|
146 { |
|
147 struct TypeId::AttributeInfo info; |
|
148 TypeId tid = GetInstanceTypeId (); |
|
149 if (!tid.LookupAttributeByName (name, &info)) |
|
150 { |
|
151 return false; |
|
152 } |
|
153 if (!(info.flags & TypeId::ATTR_SET)) |
|
154 { |
|
155 return false; |
|
156 } |
|
157 return DoSet (info.accessor, info.initialValue, info.checker, value); |
|
158 } |
|
159 bool |
|
160 ObjectBase::GetAttribute (std::string name, std::string &value) const |
|
161 { |
|
162 struct TypeId::AttributeInfo info; |
|
163 TypeId tid = GetInstanceTypeId (); |
|
164 if (!tid.LookupAttributeByName (name, &info)) |
|
165 { |
|
166 return false; |
|
167 } |
|
168 if (!(info.flags & TypeId::ATTR_GET)) |
|
169 { |
|
170 return false; |
|
171 } |
|
172 Attribute v = info.checker->Create (); |
|
173 bool ok = info.accessor->Get (this, v); |
|
174 if (ok) |
|
175 { |
|
176 value = v.SerializeToString (info.checker); |
|
177 } |
|
178 return ok; |
|
179 } |
|
180 |
|
181 Attribute |
|
182 ObjectBase::GetAttribute (std::string name) const |
|
183 { |
|
184 struct TypeId::AttributeInfo info; |
|
185 TypeId tid = GetInstanceTypeId (); |
|
186 if (!tid.LookupAttributeByName (name, &info)) |
|
187 { |
|
188 return Attribute (); |
|
189 } |
|
190 if (!(info.flags & TypeId::ATTR_GET)) |
|
191 { |
|
192 return Attribute (); |
|
193 } |
|
194 Attribute value = info.checker->Create (); |
|
195 bool ok = info.accessor->Get (this, value); |
|
196 if (!ok) |
|
197 { |
|
198 return Attribute (); |
|
199 } |
|
200 return value; |
|
201 } |
|
202 |
|
203 bool |
|
204 ObjectBase::TraceConnectWithoutContext (std::string name, const CallbackBase &cb) |
|
205 { |
|
206 TypeId tid = GetInstanceTypeId (); |
|
207 Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name); |
|
208 if (accessor == 0) |
|
209 { |
|
210 return false; |
|
211 } |
|
212 bool ok = accessor->ConnectWithoutContext (this, cb); |
|
213 return ok; |
|
214 } |
|
215 bool |
|
216 ObjectBase::TraceConnectWithoutContext (std::string name, std::string context, const CallbackBase &cb) |
|
217 { |
|
218 TypeId tid = GetInstanceTypeId (); |
|
219 Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name); |
|
220 if (accessor == 0) |
|
221 { |
|
222 return false; |
|
223 } |
|
224 bool ok = accessor->Connect (this, context, cb); |
|
225 return ok; |
|
226 } |
|
227 bool |
|
228 ObjectBase::TraceDisconnectWithoutContext (std::string name, const CallbackBase &cb) |
|
229 { |
|
230 TypeId tid = GetInstanceTypeId (); |
|
231 Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name); |
|
232 if (accessor == 0) |
|
233 { |
|
234 return false; |
|
235 } |
|
236 bool ok = accessor->DisconnectWithoutContext (this, cb); |
|
237 return ok; |
|
238 } |
|
239 bool |
|
240 ObjectBase::TraceDisconnectWithoutContext (std::string name, std::string context, const CallbackBase &cb) |
|
241 { |
|
242 TypeId tid = GetInstanceTypeId (); |
|
243 Ptr<const TraceSourceAccessor> accessor = tid.LookupTraceSourceByName (name); |
|
244 if (accessor == 0) |
|
245 { |
|
246 return false; |
|
247 } |
|
248 bool ok = accessor->Disconnect (this, context, cb); |
|
249 return ok; |
|
250 } |
|
251 |
|
252 |
|
253 |
|
254 } // namespace ns3 |