A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
object-ptr-container.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 INRIA, Mathieu Lacage
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mathieu Lacage <mathieu.lacage@gmail.com>
7 */
8#ifndef OBJECT_PTR_CONTAINER_H
9#define OBJECT_PTR_CONTAINER_H
10
11#include "attribute.h"
12#include "object.h"
13#include "ptr.h"
14
15#include <map>
16
17/**
18 * @file
19 * @ingroup attribute_ObjectPtrContainer
20 * ns3::ObjectPtrContainerValue attribute value declarations and template implementations.
21 */
22
23/**
24 * @ingroup attributes
25 * @defgroup attribute_ObjectPtrContainer ObjectPtrContainer Attribute
26 * AttributeValue implementation for ObjectPtrContainer
27 */
28namespace ns3
29{
30
31/**
32 * @ingroup attribute_ObjectPtrContainer
33 *
34 * @brief Container for a set of ns3::Object pointers.
35 *
36 * This class it used to get attribute access to an array of
37 * ns3::Object pointers.
38 *
39 * @see AttributeValue
40 *
41 * Call graph was not generated because of its size.
42 * @hidecallergraph
43 * @hidecallgraph
44 */
46{
47 public:
48 /** Iterator type for traversing this container. */
49 typedef std::map<std::size_t, Ptr<Object>>::const_iterator Iterator;
50
51 /** Default constructor. */
53
54 /**
55 * Get an iterator to the first Object.
56 *
57 * @returns An iterator to the first Object.
58 */
59 Iterator Begin() const;
60 /**
61 * Get an iterator to the _past-the-end_ Object.
62 *
63 * @returns An iterator to the _past-the-end_ Object.
64 */
65 Iterator End() const;
66 /**
67 * Get the number of Objects.
68 *
69 * @returns The number of objects.
70 */
71 std::size_t GetN() const;
72 /**
73 * Get a specific Object.
74 *
75 * @param [in] i The index of the requested object.
76 * @returns The requested object
77 */
78 Ptr<Object> Get(std::size_t i) const;
79
80 /**
81 * Get a copy of this container.
82 *
83 * @returns A copy of this container.
84 */
85 Ptr<AttributeValue> Copy() const override;
86 /**
87 * Serialize each of the Object pointers to a string.
88 *
89 * Note this serializes the Ptr values, not the Objects themselves.
90 *
91 * @param [in] checker The checker to use (currently not used.)
92 * @returns The string form of the Objects.
93 */
94 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
95 /**
96 * Deserialize from a string. (Not implemented; raises a fatal error.)
97 *
98 * @param [in] value The serialized string form.
99 * @param [in] checker The checker to use.
100 * @returns \c true.
101 */
102 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
103
104 private:
105 /** ObjectPtrContainerAccessor::Get() needs access. */
107 /** The container implementation. */
108 std::map<std::size_t, Ptr<Object>> m_objects;
109};
110
111/**
112 * @ingroup attribute_ObjectPtrContainer
113 * Create an AttributeAccessor using a container class indexed get method.
114 *
115 * The two versions of this function differ only in argument order.
116 *
117 * @tparam T \deduced The container class type.
118 * @tparam U \deduced The type of object the get method returns.
119 * @tparam INDEX \deduced The type of the index variable.
120 * @param [in] get The class method to get a specific instance
121 * from the container.
122 * @param [in] getN The class method to return the number of objects
123 * in the container.
124 * @return The AttributeAccessor.
125 */
126template <typename T, typename U, typename INDEX>
128 INDEX (T::*getN)() const);
129
130/**
131 * @ingroup attribute_ObjectPtrContainer
132 * Create an AttributeAccessor using a container class indexed get method.
133 *
134 * The two versions of this function differ only in argument order.
135 *
136 * @tparam T \deduced The container class type.
137 * @tparam U \deduced The type of object the get method returns.
138 * @tparam INDEX \deduced The type of the index variable.
139 * @param [in] get The class method to get a specific instance
140 * from the container.
141 * @param [in] getN The class method to return the number of objects
142 * in the container.
143 * @return The AttributeAccessor.
144 */
145template <typename T, typename U, typename INDEX>
147 Ptr<U> (T::*get)(INDEX) const);
148
149/**
150 * @ingroup attribute_ObjectPtrContainer
151 *
152 * AttributeChecker implementation for ObjectPtrContainerValue.
153 * @see AttributeChecker
154 */
156{
157 public:
158 /**
159 * Get the TypeId of the container class type.
160 * @returns The class TypeId.
161 */
162 virtual TypeId GetItemTypeId() const = 0;
163};
164
165/**
166 * @ingroup attribute_ObjectPtrContainer
167 * @returns The AttributeChecker.
168 * @see AttributeChecker
169 */
170template <typename T>
172
173} // namespace ns3
174
175//
176// The implementation of the above functions.
177//
178
179namespace ns3
180{
181
182namespace internal
183{
184/**
185 * ObjectPtrContainerChecker implementation class.
186 */
187template <typename T>
189{
190 public:
191 TypeId GetItemTypeId() const override
192 {
193 return T::GetTypeId();
194 }
195
196 bool Check(const AttributeValue& value) const override
197 {
198 return dynamic_cast<const ObjectPtrContainerValue*>(&value) != nullptr;
199 }
200
201 std::string GetValueTypeName() const override
202 {
203 return "ns3::ObjectPtrContainerValue";
204 }
205
206 bool HasUnderlyingTypeInformation() const override
207 {
208 return true;
209 }
210
211 std::string GetUnderlyingTypeInformation() const override
212 {
213 return "ns3::Ptr< " + T::GetTypeId().GetName() + " >";
214 }
215
217 {
219 }
220
221 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
222 {
223 const auto src = dynamic_cast<const ObjectPtrContainerValue*>(&source);
224 auto dst = dynamic_cast<ObjectPtrContainerValue*>(&destination);
225 if (src == nullptr || dst == nullptr)
226 {
227 return false;
228 }
229 *dst = *src;
230 return true;
231 }
232};
233
234} // namespace internal
235
236/**
237 * @ingroup attribute_ObjectPtrContainer
238 * AttributeAccessor implementation for ObjectPtrContainerValue.
239 */
241{
242 public:
243 bool Set(ObjectBase* object, const AttributeValue& value) const override;
244 bool Get(const ObjectBase* object, AttributeValue& value) const override;
245 bool HasGetter() const override;
246 bool HasSetter() const override;
247
248 private:
249 /**
250 * Get the number of instances in the container.
251 *
252 * @param [in] object The container object.
253 * @param [out] n The number of instances in the container.
254 * @returns true if the value could be obtained successfully.
255 */
256 virtual bool DoGetN(const ObjectBase* object, std::size_t* n) const = 0;
257 /**
258 * Get an instance from the container, identified by index.
259 *
260 * @param [in] object The container object.
261 * @param [in] i The desired instance index.
262 * @param [out] index The index retrieved.
263 * @returns The index requested.
264 */
265 virtual Ptr<Object> DoGet(const ObjectBase* object,
266 std::size_t i,
267 std::size_t* index) const = 0;
268};
269
270template <typename T, typename U, typename INDEX>
272MakeObjectPtrContainerAccessor(Ptr<U> (T::*get)(INDEX) const, INDEX (T::*getN)() const)
273{
274 struct MemberGetters : public ObjectPtrContainerAccessor
275 {
276 bool DoGetN(const ObjectBase* object, std::size_t* n) const override
277 {
278 const T* obj = dynamic_cast<const T*>(object);
279 if (obj == nullptr)
280 {
281 return false;
282 }
283 *n = (obj->*m_getN)();
284 return true;
285 }
286
287 Ptr<Object> DoGet(const ObjectBase* object,
288 std::size_t i,
289 std::size_t* index) const override
290 {
291 const T* obj = static_cast<const T*>(object);
292 *index = i;
293 return (obj->*m_get)(i);
294 }
295
296 Ptr<U> (T::*m_get)(INDEX) const;
297 INDEX (T::*m_getN)() const;
298 }* spec = new MemberGetters();
299
300 spec->m_get = get;
301 spec->m_getN = getN;
302 return Ptr<const AttributeAccessor>(spec, false);
303}
304
305template <typename T, typename U, typename INDEX>
306Ptr<const AttributeAccessor>
307MakeObjectPtrContainerAccessor(INDEX (T::*getN)() const, Ptr<U> (T::*get)(INDEX) const)
308{
309 return MakeObjectPtrContainerAccessor(get, getN);
310}
311
312template <typename T>
313Ptr<const AttributeChecker>
318
319} // namespace ns3
320
321#endif /* OBJECT_PTR_CONTAINER_H */
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
allow setting and getting the value of an attribute.
Definition attribute.h:105
Represent the type of an attribute.
Definition attribute.h:157
Hold a value for an Attribute.
Definition attribute.h:59
Anchor the ns-3 type and attribute system.
AttributeAccessor implementation for ObjectPtrContainerValue.
virtual Ptr< Object > DoGet(const ObjectBase *object, std::size_t i, std::size_t *index) const =0
Get an instance from the container, identified by index.
virtual bool DoGetN(const ObjectBase *object, std::size_t *n) const =0
Get the number of instances in the container.
bool Set(ObjectBase *object, const AttributeValue &value) const override
bool Get(const ObjectBase *object, AttributeValue &value) const override
AttributeChecker implementation for ObjectPtrContainerValue.
virtual TypeId GetItemTypeId() const =0
Get the TypeId of the container class type.
Container for a set of ns3::Object pointers.
std::map< std::size_t, Ptr< Object > > m_objects
The container implementation.
std::size_t GetN() const
Get the number of Objects.
Iterator End() const
Get an iterator to the past-the-end Object.
Iterator Begin() const
Get an iterator to the first Object.
bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker) override
Deserialize from a string.
std::map< std::size_t, Ptr< Object > >::const_iterator Iterator
Iterator type for traversing this container.
Ptr< Object > Get(std::size_t i) const
Get a specific Object.
std::string SerializeToString(Ptr< const AttributeChecker > checker) const override
Serialize each of the Object pointers to a string.
Ptr< AttributeValue > Copy() const override
Get a copy of this container.
ObjectPtrContainerValue()
Default constructor.
Smart pointer class similar to boost::intrusive_ptr.
a unique identifier for an interface.
Definition type-id.h:48
ObjectPtrContainerChecker implementation class.
TypeId GetItemTypeId() const override
Get the TypeId of the container class type.
bool Copy(const AttributeValue &source, AttributeValue &destination) const override
Copy the source to the destination.
bool Check(const AttributeValue &value) const override
std::string GetValueTypeName() const override
std::string GetUnderlyingTypeInformation() const override
Ptr< AttributeValue > Create() const override
Ptr< const AttributeAccessor > MakeObjectPtrContainerAccessor(Ptr< U >(T::*get)(INDEX) const, INDEX(T::*getN)() const)
Create an AttributeAccessor using a container class indexed get method.
Ptr< const AttributeChecker > MakeObjectPtrContainerChecker()
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.
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
ns3::Ptr smart pointer declaration and implementation.