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
34namespace ns3
35{
36
46{
47 public:
49 typedef std::map<std::size_t, Ptr<Object>>::const_iterator Iterator;
50
53
59 Iterator Begin() const;
65 Iterator End() const;
71 std::size_t GetN() const;
78 Ptr<Object> Get(std::size_t i) const;
79
85 Ptr<AttributeValue> Copy() const override;
94 std::string SerializeToString(Ptr<const AttributeChecker> checker) const override;
102 bool DeserializeFromString(std::string value, Ptr<const AttributeChecker> checker) override;
103
104 private:
108 std::map<std::size_t, Ptr<Object>> m_objects;
109};
110
126template <typename T, typename U, typename INDEX>
128 INDEX (T::*getN)() const);
129
145template <typename T, typename U, typename INDEX>
147 Ptr<U> (T::*get)(INDEX) const);
148
150{
151 public:
156 virtual TypeId GetItemTypeId() const = 0;
157};
158
159template <typename T>
161
162} // namespace ns3
163
164/***************************************************************
165 * The implementation of the above functions.
166 ***************************************************************/
167
168namespace ns3
169{
170
171namespace internal
172{
173
175template <typename T>
177{
178 public:
179 TypeId GetItemTypeId() const override
180 {
181 return T::GetTypeId();
182 }
183
184 bool Check(const AttributeValue& value) const override
185 {
186 return dynamic_cast<const ObjectPtrContainerValue*>(&value) != nullptr;
187 }
188
189 std::string GetValueTypeName() const override
190 {
191 return "ns3::ObjectPtrContainerValue";
192 }
193
194 bool HasUnderlyingTypeInformation() const override
195 {
196 return true;
197 }
198
199 std::string GetUnderlyingTypeInformation() const override
200 {
201 return "ns3::Ptr< " + T::GetTypeId().GetName() + " >";
202 }
203
205 {
206 return ns3::Create<ObjectPtrContainerValue>();
207 }
208
209 bool Copy(const AttributeValue& source, AttributeValue& destination) const override
210 {
211 const auto src = dynamic_cast<const ObjectPtrContainerValue*>(&source);
212 auto dst = dynamic_cast<ObjectPtrContainerValue*>(&destination);
213 if (src == nullptr || dst == nullptr)
214 {
215 return false;
216 }
217 *dst = *src;
218 return true;
219 }
220};
221
222} // namespace internal
223
229{
230 public:
231 bool Set(ObjectBase* object, const AttributeValue& value) const override;
232 bool Get(const ObjectBase* object, AttributeValue& value) const override;
233 bool HasGetter() const override;
234 bool HasSetter() const override;
235
236 private:
244 virtual bool DoGetN(const ObjectBase* object, std::size_t* n) const = 0;
253 virtual Ptr<Object> DoGet(const ObjectBase* object,
254 std::size_t i,
255 std::size_t* index) const = 0;
256};
257
258template <typename T, typename U, typename INDEX>
260MakeObjectPtrContainerAccessor(Ptr<U> (T::*get)(INDEX) const, INDEX (T::*getN)() const)
261{
262 struct MemberGetters : public ObjectPtrContainerAccessor
263 {
264 bool DoGetN(const ObjectBase* object, std::size_t* n) const override
265 {
266 const T* obj = dynamic_cast<const T*>(object);
267 if (obj == nullptr)
268 {
269 return false;
270 }
271 *n = (obj->*m_getN)();
272 return true;
273 }
274
275 Ptr<Object> DoGet(const ObjectBase* object,
276 std::size_t i,
277 std::size_t* index) const override
278 {
279 const T* obj = static_cast<const T*>(object);
280 *index = i;
281 return (obj->*m_get)(i);
282 }
283
284 Ptr<U> (T::*m_get)(INDEX) const;
285 INDEX (T::*m_getN)() const;
286 }* spec = new MemberGetters();
287
288 spec->m_get = get;
289 spec->m_getN = getN;
290 return Ptr<const AttributeAccessor>(spec, false);
291}
292
293template <typename T, typename U, typename INDEX>
294Ptr<const AttributeAccessor>
295MakeObjectPtrContainerAccessor(INDEX (T::*getN)() const, Ptr<U> (T::*get)(INDEX) const)
296{
297 return MakeObjectPtrContainerAccessor(get, getN);
298}
299
300template <typename T>
301Ptr<const AttributeChecker>
303{
304 return Create<internal::ObjectPtrContainerChecker<T>>();
305}
306
307} // namespace ns3
308
309#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.
ObjectPtrContainerValue()
Default constructor.
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
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.