23 #include "attribute.h" |
23 #include "attribute.h" |
24 #include "object.h" |
24 #include "object.h" |
25 |
25 |
26 namespace ns3 { |
26 namespace ns3 { |
27 |
27 |
28 class Pointer |
28 class PointerValue : public AttributeValue |
29 { |
29 { |
30 public: |
30 public: |
31 Pointer (); |
31 PointerValue (); |
32 |
32 |
33 Pointer (Ptr<Object> object); |
33 PointerValue (Ptr<Object> object); |
34 |
34 |
35 void SetObject (Ptr<Object> object); |
35 void SetObject (Ptr<Object> object); |
36 |
36 |
37 Ptr<Object> GetObject (void) const; |
37 Ptr<Object> GetObject (void) const; |
38 |
38 |
39 template <typename T> |
39 template <typename T> |
40 Pointer (const Ptr<T> &object); |
40 PointerValue (const Ptr<T> &object); |
41 |
41 |
42 template <typename T> |
42 template <typename T> |
43 void Set (const Ptr<T> &object); |
43 void Set (const Ptr<T> &object); |
44 |
44 |
45 template <typename T> |
45 template <typename T> |
46 Ptr<T> Get (void) const; |
46 Ptr<T> Get (void) const; |
47 |
47 |
48 template <typename T> |
48 template <typename T> |
49 operator Ptr<T> () const; |
49 operator Ptr<T> () const; |
50 |
50 |
51 ATTRIBUTE_CONVERTER_DEFINE (Pointer); |
51 virtual Ptr<AttributeValue> Copy (void) const; |
|
52 virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const; |
|
53 virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker); |
|
54 |
52 private: |
55 private: |
53 Ptr<Object> m_value; |
56 Ptr<Object> m_value; |
54 }; |
57 }; |
55 |
58 |
56 std::ostream & operator << (std::ostream &os, const Pointer &pointer); |
|
57 std::istream & operator >> (std::istream &is, Pointer &pointer); |
|
58 |
|
59 ATTRIBUTE_VALUE_DEFINE (Pointer); |
|
60 |
|
61 template <typename T, typename U> |
59 template <typename T, typename U> |
62 Ptr<const AttributeAccessor> |
60 Ptr<const AttributeAccessor> |
63 MakePointerAccessor (Ptr<U> T::*memberVariable); |
61 MakePointerAccessor (Ptr<U> T::*memberVariable); |
64 template <typename T, typename U> |
62 template <typename T, typename U> |
65 Ptr<const AttributeAccessor> |
63 Ptr<const AttributeAccessor> |
92 namespace internal { |
90 namespace internal { |
93 |
91 |
94 template <typename T> |
92 template <typename T> |
95 class APointerChecker : public PointerChecker |
93 class APointerChecker : public PointerChecker |
96 { |
94 { |
97 virtual bool Check (Attribute val) const { |
95 virtual bool Check (const AttributeValue &val) const { |
98 const PointerValue *value = val.DynCast<const PointerValue *> (); |
96 const PointerValue *value = dynamic_cast<const PointerValue *> (&val); |
99 if (value == 0) |
97 if (value == 0) |
100 { |
98 { |
101 return false; |
99 return false; |
102 } |
100 } |
103 if (value->Get ().GetObject () == 0) |
101 if (value->GetObject () == 0) |
104 { |
102 { |
105 return true; |
103 return true; |
106 } |
104 } |
107 T *ptr = dynamic_cast<T*> (PeekPointer (value->Get ().GetObject ())); |
105 T *ptr = dynamic_cast<T*> (PeekPointer (value->GetObject ())); |
108 if (ptr == 0) |
106 if (ptr == 0) |
109 { |
107 { |
110 return false; |
108 return false; |
111 } |
109 } |
112 return true; |
110 return true; |
120 return false; |
118 return false; |
121 } |
119 } |
122 virtual std::string GetTypeConstraints (void) const { |
120 virtual std::string GetTypeConstraints (void) const { |
123 return ""; |
121 return ""; |
124 } |
122 } |
125 virtual Attribute Create (void) const { |
123 virtual Ptr<AttributeValue> Create (void) const { |
126 return Attribute (ns3::Create<PointerValue> ()); |
124 return ns3::Create<PointerValue> (); |
|
125 } |
|
126 virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const { |
|
127 const PointerValue *src = dynamic_cast<const PointerValue *> (&source); |
|
128 PointerValue *dst = dynamic_cast<PointerValue *> (&destination); |
|
129 if (src == 0 || dst == 0) |
|
130 { |
|
131 return false; |
|
132 } |
|
133 *dst = *src; |
|
134 return true; |
127 } |
135 } |
128 virtual TypeId GetPointeeTypeId (void) const { |
136 virtual TypeId GetPointeeTypeId (void) const { |
129 return T::GetTypeId (); |
137 return T::GetTypeId (); |
130 } |
138 } |
131 }; |
139 }; |
138 template <typename T, typename U> |
146 template <typename T, typename U> |
139 class PointerAccessor : public AttributeAccessor |
147 class PointerAccessor : public AttributeAccessor |
140 { |
148 { |
141 public: |
149 public: |
142 virtual ~PointerAccessor () {} |
150 virtual ~PointerAccessor () {} |
143 virtual bool Set (ObjectBase * object, Attribute val) const { |
151 virtual bool Set (ObjectBase * object, const AttributeValue &val) const { |
144 T *obj = dynamic_cast<T *> (object); |
152 T *obj = dynamic_cast<T *> (object); |
145 if (obj == 0) |
153 if (obj == 0) |
146 { |
154 { |
147 return false; |
155 return false; |
148 } |
156 } |
149 const PointerValue *value = val.DynCast<const PointerValue *> (); |
157 const PointerValue *value = dynamic_cast<const PointerValue *> (&val); |
150 if (value == 0) |
158 if (value == 0) |
151 { |
159 { |
152 return false; |
160 return false; |
153 } |
161 } |
154 Ptr<U> ptr = dynamic_cast<U*> (PeekPointer (value->Get ().GetObject ())); |
162 Ptr<U> ptr = dynamic_cast<U*> (PeekPointer (value->GetObject ())); |
155 if (ptr == 0) |
163 if (ptr == 0) |
156 { |
164 { |
157 return false; |
165 return false; |
158 } |
166 } |
159 DoSet (obj, ptr); |
167 DoSet (obj, ptr); |
160 return true; |
168 return true; |
161 } |
169 } |
162 virtual bool Get (const ObjectBase * object, Attribute val) const { |
170 virtual bool Get (const ObjectBase * object, AttributeValue &val) const { |
163 const T *obj = dynamic_cast<const T *> (object); |
171 const T *obj = dynamic_cast<const T *> (object); |
164 if (obj == 0) |
172 if (obj == 0) |
165 { |
173 { |
166 return false; |
174 return false; |
167 } |
175 } |
168 PointerValue *value = val.DynCast<PointerValue *> (); |
176 PointerValue *value = dynamic_cast<PointerValue *> (&val); |
169 if (value == 0) |
177 if (value == 0) |
170 { |
178 { |
171 return false; |
179 return false; |
172 } |
180 } |
173 value->Set (Pointer (DoGet (obj))); |
181 value->Set (DoGet (obj)); |
174 return true; |
182 return true; |
175 } |
183 } |
176 private: |
184 private: |
177 virtual void DoSet (T *object, Ptr<U> value) const = 0; |
185 virtual void DoSet (T *object, Ptr<U> value) const = 0; |
178 virtual Ptr<U> DoGet (const T *object) const = 0; |
186 virtual Ptr<U> DoGet (const T *object) const = 0; |
180 |
188 |
181 } // namespace internal |
189 } // namespace internal |
182 |
190 |
183 |
191 |
184 template <typename T> |
192 template <typename T> |
185 Pointer::Pointer (const Ptr<T> &object) |
193 PointerValue::PointerValue (const Ptr<T> &object) |
186 { |
194 { |
187 m_value = object; |
195 m_value = object; |
188 } |
196 } |
189 |
197 |
190 template <typename T> |
198 template <typename T> |
191 void |
199 void |
192 Pointer::Set (const Ptr<T> &object) |
200 PointerValue::Set (const Ptr<T> &object) |
193 { |
201 { |
194 m_value = object; |
202 m_value = object; |
195 } |
203 } |
196 |
204 |
197 template <typename T> |
205 template <typename T> |
198 Ptr<T> |
206 Ptr<T> |
199 Pointer::Get (void) const |
207 PointerValue::Get (void) const |
200 { |
208 { |
201 T *v = dynamic_cast<T *> (PeekPointer (m_value)); |
209 T *v = dynamic_cast<T *> (PeekPointer (m_value)); |
202 return v; |
210 return v; |
203 } |
211 } |
204 |
212 |
205 template <typename T> |
213 template <typename T> |
206 Pointer::operator Ptr<T> () const |
214 PointerValue::operator Ptr<T> () const |
207 { |
215 { |
208 return Get<T> (); |
216 return Get<T> (); |
209 } |
217 } |
210 |
218 |
211 |
219 |