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 */
36class PointerValue : public AttributeValue
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 */
129template <typename T>
131
132} // namespace ns3
133
134/***************************************************************
135 * Implementation of the templates declared above.
136 ***************************************************************/
137
138namespace ns3
139{
140
141namespace internal
142{
143
144/**
145 * \ingroup attribute_Pointer
146 * PointerChecker implementation.
147 */
148template <typename T>
150{
151 bool Check(const AttributeValue& val) const override
152 {
153 const auto value = dynamic_cast<const PointerValue*>(&val);
154 if (value == nullptr)
155 {
156 return false;
157 }
158 if (!value->GetObject())
159 {
160 // a null pointer is a valid value
161 return true;
162 }
163 T* ptr = dynamic_cast<T*>(PeekPointer(value->GetObject()));
164 return ptr;
165 }
166
167 std::string GetValueTypeName() const override
168 {
169 return "ns3::PointerValue";
170 }
171
172 bool HasUnderlyingTypeInformation() const override
173 {
174 return true;
175 }
176
177 std::string GetUnderlyingTypeInformation() const override
178 {
179 TypeId tid = T::GetTypeId();
180 return "ns3::Ptr< " + tid.GetName() + " >";
181 }
182
184 {
186 }
187
188 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
189 {
190 const auto src = dynamic_cast<const PointerValue*>(&source);
191 auto dst = dynamic_cast<PointerValue*>(&destination);
192 if (src == nullptr || dst == nullptr)
193 {
194 return false;
195 }
196 *dst = *src;
197 return true;
198 }
199
200 TypeId GetPointeeTypeId() const override
201 {
202 return T::GetTypeId();
203 }
204};
205
206} // namespace internal
207
208template <typename T>
210{
211 m_value = object;
212}
213
214template <typename T>
215void
217{
218 m_value = object;
219}
220
221template <typename T>
222Ptr<T>
224{
225 T* v = dynamic_cast<T*>(PeekPointer(m_value));
226 return v;
227}
228
229template <typename T>
230PointerValue::operator Ptr<T>() const
231{
232 return Get<T>();
233}
234
235template <typename T>
236bool
238{
239 Ptr<T> ptr = dynamic_cast<T*>(PeekPointer(m_value));
240 if (!ptr)
241 {
242 return false;
243 }
244 v = ptr;
245 return true;
246}
247
249
250// Documentation of the functions defined by the macro.
251// not documented by print-introspected-doxygen because
252// Pointer has custom functions.
253
254/**
255 * \ingroup attribute_Pointer
256 * \fn ns3::Ptr<const ns3::AttributeAccessor> ns3::MakePointerAccessor (T1 a1)
257 * \copydoc ns3::MakeAccessorHelper(T1)
258 * \see AttributeAccessor
259 */
260/**
261 * \ingroup attribute_Pointer
262 * \fn ns3::Ptr<const ns3::AttributeAccessor> ns3::MakePointerAccessor (T1 a1, T2 a2)
263 * \copydoc ns3::MakeAccessorHelper(T1,T2)
264 * \see AttributeAccessor
265 */
266
267template <typename T>
273
274} // namespace ns3
275
276#endif /* NS_POINTER_H */
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Represent the type of an attribute.
Definition attribute.h:157
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.
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:223
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:216
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:237
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
std::string GetName() const
Get the name.
Definition type-id.cc:1061
PointerChecker implementation.
Definition pointer.h:150
std::string GetUnderlyingTypeInformation() const override
Definition pointer.h:177
Ptr< AttributeValue > Create() const override
Definition pointer.h:183
bool HasUnderlyingTypeInformation() const override
Definition pointer.h:172
bool Check(const AttributeValue &val) const override
Definition pointer.h:151
std::string GetValueTypeName() const override
Definition pointer.h:167
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
Definition pointer.h:188
TypeId GetPointeeTypeId() const override
Get the TypeId of the base type.
Definition pointer.h:200
Ptr< AttributeChecker > MakePointerChecker()
Create a PointerChecker for a type.
Definition pointer.h:269
#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:436
Every class exported by the ns3 library is enclosed in the ns3 namespace.
U * PeekPointer(const Ptr< U > &p)
Definition ptr.h:443
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.