A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
25/**
26 * \file
27 * \ingroup attribute_Pointer
28 * ns3::PointerValue attribute value declarations and template implementations.
29 */
30
31namespace ns3
32{
33
34/**
35 * \ingroup attributes
36 * \defgroup attribute_Pointer Pointer Attribute
37 * AttributeValue implementation for Pointer.
38 * Hold objects of type Ptr<T>.
39 */
40
41/**
42 * \ingroup attribute_Pointer
43 * \class ns3::PointerValue "pointer.h"
44 * AttributeValue implementation for Pointer. Hold objects of type Ptr<T>.
45 * \see AttributeValue
46 */
48{
49 public:
51
52 /**
53 * Construct this PointerValue by referencing an explicit Object.
54 *
55 * \param [in] object The object to begin with.
56 */
57 PointerValue(const Ptr<Object>& object);
58
59 /**
60 * Set the value from by reference an Object.
61 *
62 * \param [in] object The object to reference.
63 */
64 void SetObject(Ptr<Object> object);
65
66 /**
67 * Get the Object referenced by the PointerValue.
68 * \returns The Object.
69 */
70 Ptr<Object> GetObject() const;
71
72 /**
73 * Construct this PointerValue by referencing an explicit Object.
74 *
75 * \tparam T \deduced The type of the object.
76 * \param [in] object The object to begin with.
77 */
78 template <typename T>
79 PointerValue(const Ptr<T>& object);
80
81 /**
82 * Cast to an Object of type \c T.
83 * \tparam T \explicit The type to cast to.
84 */
85 template <typename T>
86 operator Ptr<T>() const;
87
88 /**
89 * Set the value.
90 * \param [in] value The value to adopt.
91 */
92 template <typename T>
93 void Set(const Ptr<T>& value);
94
95 /**
96 * \returns The Pointer value.
97 * \tparam T \explicit The type to cast to.
98 */
99 template <typename T>
100 Ptr<T> Get() const;
101
102 /**
103 * Access the Pointer value as type \p T.
104 * \tparam T \explicit The type to cast to.
105 * \param [out] value The Pointer value, as type \p T.
106 * \returns true.
107 */
108 template <typename T>
109 bool GetAccessor(Ptr<T>& value) const;
110
111 Ptr<AttributeValue> Copy() const override;
112 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
113 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
114
115 private:
116 Ptr<Object> m_value; //!< The stored Pointer instance.
117};
118
119/**
120 * \ingroup attribute_Pointer
121 * AttributeChecker implementation for PointerValue.
122 * \see AttributeChecker
123 */
125{
126 public:
127 /**
128 * Get the TypeId of the base type.
129 * \returns The base TypeId.
130 */
131 virtual TypeId GetPointeeTypeId() const = 0;
132};
133
134/**
135 * \ingroup attribute_Pointer
136 * Create a PointerChecker for a type.
137 * \tparam T \explicit The underlying type.
138 * \returns The PointerChecker.
139 */
140template <typename T>
142
143} // namespace ns3
144
145/***************************************************************
146 * Implementation of the templates declared above.
147 ***************************************************************/
148
149namespace ns3
150{
151
152namespace internal
153{
154
155/**
156 * \ingroup attribute_Pointer
157 * PointerChecker implementation.
158 */
159template <typename T>
161{
162 bool Check(const AttributeValue& val) const override
163 {
164 const auto value = dynamic_cast<const PointerValue*>(&val);
165 if (value == nullptr)
166 {
167 return false;
168 }
169 if (!value->GetObject())
170 {
171 // a null pointer is a valid value
172 return true;
173 }
174 T* ptr = dynamic_cast<T*>(PeekPointer(value->GetObject()));
175 return ptr;
176 }
177
178 std::string GetValueTypeName() const override
179 {
180 return "ns3::PointerValue";
181 }
182
183 bool HasUnderlyingTypeInformation() const override
184 {
185 return true;
186 }
187
188 std::string GetUnderlyingTypeInformation() const override
189 {
190 TypeId tid = T::GetTypeId();
191 return "ns3::Ptr< " + tid.GetName() + " >";
192 }
193
195 {
196 return ns3::Create<PointerValue>();
197 }
198
199 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
200 {
201 const auto src = dynamic_cast<const PointerValue*>(&source);
202 auto dst = dynamic_cast<PointerValue*>(&destination);
203 if (src == nullptr || dst == nullptr)
204 {
205 return false;
206 }
207 *dst = *src;
208 return true;
209 }
210
211 TypeId GetPointeeTypeId() const override
212 {
213 return T::GetTypeId();
214 }
215};
216
217} // namespace internal
218
219template <typename T>
221{
222 m_value = object;
223}
224
225template <typename T>
226void
228{
229 m_value = object;
230}
231
232template <typename T>
233Ptr<T>
235{
236 T* v = dynamic_cast<T*>(PeekPointer(m_value));
237 return v;
238}
239
240template <typename T>
241PointerValue::operator Ptr<T>() const
242{
243 return Get<T>();
244}
245
246template <typename T>
247bool
249{
250 Ptr<T> ptr = dynamic_cast<T*>(PeekPointer(m_value));
251 if (!ptr)
252 {
253 return false;
254 }
255 v = ptr;
256 return true;
257}
258
260
261// Documentation of the functions defined by the macro.
262// not documented by print-introspected-doxygen because
263// Pointer has custom functions.
264
265/**
266 * \ingroup attribute_Pointer
267 * \fn ns3::Ptr<const ns3::AttributeAccessor> ns3::MakePointerAccessor (T1 a1)
268 * \copydoc ns3::MakeAccessorHelper(T1)
269 * \see AttributeAccessor
270 */
271/**
272 * \ingroup attribute_Pointer
273 * \fn ns3::Ptr<const ns3::AttributeAccessor> ns3::MakePointerAccessor (T1 a1, T2 a2)
274 * \copydoc ns3::MakeAccessorHelper(T1,T2)
275 * \see AttributeAccessor
276 */
277
278template <typename T>
281{
282 return Create<internal::PointerChecker<T>>();
283}
284
285} // namespace ns3
286
287#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:125
virtual TypeId GetPointeeTypeId() const =0
Get the TypeId of the base type.
AttributeValue implementation for Pointer.
Definition: pointer.h:48
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Definition: pointer.cc:71
Ptr< AttributeValue > Copy() const override
Definition: pointer.cc:64
Ptr< Object > GetObject() const
Get the Object referenced by the PointerValue.
Definition: pointer.cc:57
Ptr< T > Get() const
Definition: pointer.h:234
Ptr< Object > m_value
The stored Pointer instance.
Definition: pointer.h:116
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:227
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:248
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
a unique identifier for an interface.
Definition: type-id.h:59
std::string GetName() const
Get the name.
Definition: type-id.cc:992
PointerChecker implementation.
Definition: pointer.h:161
std::string GetUnderlyingTypeInformation() const override
Definition: pointer.h:188
Ptr< AttributeValue > Create() const override
Definition: pointer.h:194
bool HasUnderlyingTypeInformation() const override
Definition: pointer.h:183
bool Check(const AttributeValue &val) const override
Definition: pointer.h:162
std::string GetValueTypeName() const override
Definition: pointer.h:178
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition: pointer.h:199
TypeId GetPointeeTypeId() const override
Get the TypeId of the base type.
Definition: pointer.h:211
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition: pointer.h:280
#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:454
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.