A Discrete-Event Network Simulator
API
object-ptr-container.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 INRIA, Mathieu Lacage
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Authors: Mathieu Lacage <mathieu.lacage@gmail.com>
19  */
20 #ifndef OBJECT_PTR_CONTAINER_H
21 #define OBJECT_PTR_CONTAINER_H
22 
23 #include <map>
24 #include "object.h"
25 #include "ptr.h"
26 #include "attribute.h"
27 
35 namespace ns3 {
36 
46 {
47 public:
49  typedef std::map<std::size_t, Ptr<Object> >::const_iterator Iterator;
50 
53 
59  Iterator Begin (void) const;
65  Iterator End (void) const;
71  std::size_t GetN (void) const;
78  Ptr<Object> Get (std::size_t i) const;
79 
85  virtual Ptr<AttributeValue> Copy (void) const;
94  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
102  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
103 
104 private:
108  std::map<std::size_t, Ptr<Object> > m_objects;
109 };
110 
126 template <typename T, typename U, typename INDEX>
128 MakeObjectPtrContainerAccessor (Ptr<U> (T::*get)(INDEX) const,
129  INDEX (T::*getN)(void) const);
130 
146 template <typename T, typename U, typename INDEX>
148 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
149  Ptr<U> (T::*get)(INDEX) const);
150 
152 {
153 public:
158  virtual TypeId GetItemTypeId (void) const = 0;
159 };
160 
161 template <typename T>
163 
164 } // namespace ns3
165 
166 
167 /***************************************************************
168  * The implementation of the above functions.
169  ***************************************************************/
170 
171 namespace ns3 {
172 
173 namespace internal {
174 
176 template <typename T>
178 {
179 public:
180  virtual TypeId GetItemTypeId (void) const
181  {
182  return T::GetTypeId ();
183  }
184  virtual bool Check (const AttributeValue &value) const
185  {
186  return dynamic_cast<const ObjectPtrContainerValue *> (&value) != 0;
187  }
188  virtual std::string GetValueTypeName (void) const
189  {
190  return "ns3::ObjectPtrContainerValue";
191  }
192  virtual bool HasUnderlyingTypeInformation (void) const
193  {
194  return true;
195  }
196  virtual std::string GetUnderlyingTypeInformation (void) const
197  {
198  return "ns3::Ptr< " + T::GetTypeId ().GetName () + " >";
199  }
200  virtual Ptr<AttributeValue> Create (void) const
201  {
202  return ns3::Create<ObjectPtrContainerValue> ();
203  }
204  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const
205  {
206  const ObjectPtrContainerValue *src = dynamic_cast<const ObjectPtrContainerValue *> (&source);
207  ObjectPtrContainerValue *dst = dynamic_cast<ObjectPtrContainerValue *> (&destination);
208  if (src == 0 || dst == 0)
209  {
210  return false;
211  }
212  *dst = *src;
213  return true;
214  }
215 };
216 
217 } // namespace internal
218 
219 
225 {
226 public:
227  virtual bool Set (ObjectBase * object, const AttributeValue &value) const;
228  virtual bool Get (const ObjectBase * object, AttributeValue &value) const;
229  virtual bool HasGetter (void) const;
230  virtual bool HasSetter (void) const;
231 
232 private:
240  virtual bool DoGetN (const ObjectBase *object, std::size_t *n) const = 0;
249  virtual Ptr<Object> DoGet (const ObjectBase *object, std::size_t i, std::size_t *index) const = 0;
250 };
251 
252 template <typename T, typename U, typename INDEX>
255  INDEX (T::*getN)(void) const)
256 {
257  struct MemberGetters : public ObjectPtrContainerAccessor
258  {
259  virtual bool DoGetN (const ObjectBase *object, std::size_t *n) const
260  {
261  const T *obj = dynamic_cast<const T *> (object);
262  if (obj == 0)
263  {
264  return false;
265  }
266  *n = (obj->*m_getN)();
267  return true;
268  }
269  virtual Ptr<Object> DoGet (const ObjectBase *object, std::size_t i, std::size_t *index) const
270  {
271  const T *obj = static_cast<const T *> (object);
272  *index = i;
273  return (obj->*m_get)(i);
274  }
275  Ptr<U> (T::*m_get)(INDEX) const;
276  INDEX (T::*m_getN)(void) const;
277  } *spec = new MemberGetters ();
278  spec->m_get = get;
279  spec->m_getN = getN;
280  return Ptr<const AttributeAccessor> (spec, false);
281 }
282 
283 template <typename T, typename U, typename INDEX>
284 Ptr<const AttributeAccessor>
285 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
286  Ptr<U> (T::*get)(INDEX) const)
287 {
288  return MakeObjectPtrContainerAccessor (get, getN);
289 }
290 
291 template <typename T>
293 {
294  return Create<internal::ObjectPtrContainerChecker<T> > ();
295 }
296 
297 
298 } // namespace ns3
299 
300 #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:115
Represent the type of an attribute.
Definition: attribute.h:167
Hold a value for an Attribute.
Definition: attribute.h:69
Anchor the ns-3 type and attribute system.
Definition: object-base.h:120
AttributeAccessor implementation for ObjectPtrContainerValue.
virtual bool Set(ObjectBase *object, const AttributeValue &value) const
virtual bool HasSetter(void) const
virtual bool DoGetN(const ObjectBase *object, std::size_t *n) const =0
Get the number of instances in the container.
virtual bool HasGetter(void) const
virtual bool Get(const ObjectBase *object, AttributeValue &value) const
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.
AttributeChecker implementation for ObjectPtrContainerValue.
virtual TypeId GetItemTypeId(void) const =0
Get the TypeId of the container class type.
Container for a set of ns3::Object pointers.
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Serialize each of the Object pointers to a string.
virtual Ptr< AttributeValue > Copy(void) const
Get a copy of this container.
std::map< std::size_t, Ptr< Object > > m_objects
The container implementation.
std::size_t GetN(void) const
Get the number of Objects.
Iterator Begin(void) const
Get an iterator to the first Object.
ObjectPtrContainerValue()
Default constructor.
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
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.
Iterator End(void) const
Get an iterator to the past-the-end Object.
a unique identifier for an interface.
Definition: type-id.h:59
ObjectPtrContainerChecker implementation class.
virtual TypeId GetItemTypeId(void) const
Get the TypeId of the container class type.
virtual bool Check(const AttributeValue &value) const
virtual std::string GetUnderlyingTypeInformation(void) const
virtual std::string GetValueTypeName(void) const
virtual bool Copy(const AttributeValue &source, AttributeValue &destination) const
Copy the source to the destination.
virtual Ptr< AttributeValue > Create(void) const
virtual bool HasUnderlyingTypeInformation(void) const
Ptr< const AttributeAccessor > MakeObjectPtrContainerAccessor(Ptr< U >(T::*get)(INDEX) const, INDEX(T::*getN)(void) const)
Create an AttributeAccessor using a container class indexed get method.
Ptr< const AttributeChecker > MakeObjectPtrContainerChecker(void)
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.h:4023
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.