A Discrete-Event Network Simulator
API
pointer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008 INRIA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 */
19#ifndef NS_POINTER_H
20#define NS_POINTER_H
21
22#include "attribute.h"
23#include "object.h"
24
31namespace ns3
32{
33
34// Additional docs for class PointerValue:
37{
38 public:
40
47
53 void SetObject(Ptr<Object> object);
54
59 Ptr<Object> GetObject() const;
60
67 template <typename T>
68 PointerValue(const Ptr<T>& object);
69
74 template <typename T>
75 operator Ptr<T>() const;
76
77 // Documentation generated by print-introspected-doxygen.cc
78 template <typename T>
79 void Set(const Ptr<T>& value);
80
82 template <typename T>
83 Ptr<T> Get() const;
84
85 template <typename T>
86 bool GetAccessor(Ptr<T>& value) const;
87
88 Ptr<AttributeValue> Copy() const override;
89 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
90 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
91
92 private:
94};
95
97{
98 public:
103 virtual TypeId GetPointeeTypeId() const = 0;
104};
105
111template <typename T>
113
114} // namespace ns3
115
116/***************************************************************
117 * Implementation of the templates declared above.
118 ***************************************************************/
119
120namespace ns3
121{
122
123namespace internal
124{
125
127template <typename T>
129{
130 bool Check(const AttributeValue& val) const override
131 {
132 const PointerValue* value = dynamic_cast<const PointerValue*>(&val);
133 if (value == nullptr)
134 {
135 return false;
136 }
137 if (!value->GetObject())
138 {
139 return true;
140 }
141 T* ptr = dynamic_cast<T*>(PeekPointer(value->GetObject()));
142 if (ptr == nullptr)
143 {
144 return false;
145 }
146 return true;
147 }
148
149 std::string GetValueTypeName() const override
150 {
151 return "ns3::PointerValue";
152 }
153
154 bool HasUnderlyingTypeInformation() const override
155 {
156 return true;
157 }
158
159 std::string GetUnderlyingTypeInformation() const override
160 {
161 TypeId tid = T::GetTypeId();
162 return "ns3::Ptr< " + tid.GetName() + " >";
163 }
164
166 {
167 return ns3::Create<PointerValue>();
168 }
169
170 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
171 {
172 const PointerValue* src = dynamic_cast<const PointerValue*>(&source);
173 PointerValue* dst = dynamic_cast<PointerValue*>(&destination);
174 if (src == nullptr || dst == nullptr)
175 {
176 return false;
177 }
178 *dst = *src;
179 return true;
180 }
181
182 TypeId GetPointeeTypeId() const override
183 {
184 return T::GetTypeId();
185 }
186};
187
188} // namespace internal
189
190template <typename T>
192{
193 m_value = object;
194}
195
196template <typename T>
197void
199{
200 m_value = object;
201}
202
203template <typename T>
204Ptr<T>
206{
207 T* v = dynamic_cast<T*>(PeekPointer(m_value));
208 return v;
209}
210
211template <typename T>
212PointerValue::operator Ptr<T>() const
213{
214 return Get<T>();
215}
216
217template <typename T>
218bool
220{
221 Ptr<T> ptr = dynamic_cast<T*>(PeekPointer(m_value));
222 if (!ptr)
223 {
224 return false;
225 }
226 v = ptr;
227 return true;
228}
229
231
232template <typename T>
235{
236 return Create<internal::PointerChecker<T>>();
237}
238
239} // namespace ns3
240
241#endif /* NS_POINTER_H */
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Represent the type of an attribute.
Definition: attribute.h:168
Hold a value for an Attribute.
Definition: attribute.h:70
AttributeChecker implementation for PointerValue.
Definition: pointer.h:97
virtual TypeId GetPointeeTypeId() const =0
Get the TypeId of the base type.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: pointer.cc:71
Ptr< Object > m_value
The stored Pointer instance.
Definition: pointer.h:93
Ptr< AttributeValue > Copy() const override
Definition: pointer.cc:64
Ptr< Object > GetObject() const
Get the Object referenced by the PointerValue.
Definition: pointer.cc:57
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Definition: pointer.cc:80
void Set(const Ptr< T > &value)
Set the value.
Definition: pointer.h:198
Ptr< T > Get() const
Definition: pointer.h:205
void SetObject(Ptr< Object > object)
Set the value from by reference an Object.
Definition: pointer.cc:50
bool GetAccessor(Ptr< T > &value) const
Access the Pointer value as type T.
Definition: pointer.h:219
a unique identifier for an interface.
Definition: type-id.h:60
std::string GetName() const
Get the name.
Definition: type-id.cc:995
PointerChecker implementation.
Definition: pointer.h:129
std::string GetUnderlyingTypeInformation() const override
Definition: pointer.h:159
Ptr< AttributeValue > Create() const override
Definition: pointer.h:165
bool HasUnderlyingTypeInformation() const override
Definition: pointer.h:154
bool Check(const AttributeValue &val) const override
Definition: pointer.h:130
std::string GetValueTypeName() const override
Definition: pointer.h:149
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition: pointer.h:170
TypeId GetPointeeTypeId() const override
Get the TypeId of the base type.
Definition: pointer.h:182
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition: pointer.h:234
#define ATTRIBUTE_ACCESSOR_DEFINE(type)
Define the attribute accessor functions MakeTypeAccessor for class type.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
value
Definition: second.py:41
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.