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