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