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:
107  std::map<std::size_t, Ptr<Object> > m_objects;
108 };
109 
125 template <typename T, typename U, typename INDEX>
127 MakeObjectPtrContainerAccessor (Ptr<U> (T::*get)(INDEX) const,
128  INDEX (T::*getN)(void) const);
129 
145 template <typename T, typename U, typename INDEX>
147 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
148  Ptr<U> (T::*get)(INDEX) const);
149 
151 {
152 public:
157  virtual TypeId GetItemTypeId (void) const = 0;
158 };
159 
160 template <typename T>
162 
163 } // namespace ns3
164 
165 
166 /***************************************************************
167  * The implementation of the above functions.
168  ***************************************************************/
169 
170 namespace ns3 {
171 
172 namespace internal {
173 
175 template <typename T>
177 {
178 public:
179  virtual TypeId GetItemTypeId (void) const {
180  return T::GetTypeId ();
181  }
182  virtual bool Check (const AttributeValue &value) const {
183  return dynamic_cast<const ObjectPtrContainerValue *> (&value) != 0;
184  }
185  virtual std::string GetValueTypeName (void) const {
186  return "ns3::ObjectPtrContainerValue";
187  }
188  virtual bool HasUnderlyingTypeInformation (void) const {
189  return true;
190  }
191  virtual std::string GetUnderlyingTypeInformation (void) const {
192  return "ns3::Ptr< " + T::GetTypeId ().GetName () + " >";
193  }
194  virtual Ptr<AttributeValue> Create (void) const {
195  return ns3::Create<ObjectPtrContainerValue> ();
196  }
197  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
198  const ObjectPtrContainerValue *src = dynamic_cast<const ObjectPtrContainerValue *> (&source);
199  ObjectPtrContainerValue *dst = dynamic_cast<ObjectPtrContainerValue *> (&destination);
200  if (src == 0 || dst == 0)
201  {
202  return false;
203  }
204  *dst = *src;
205  return true;
206  }
207 };
208 
209 } // namespace internal
210 
211 
217 {
218 public:
219  virtual bool Set (ObjectBase * object, const AttributeValue &value) const;
220  virtual bool Get (const ObjectBase * object, AttributeValue &value) const;
221  virtual bool HasGetter (void) const;
222  virtual bool HasSetter (void) const;
223 private:
231  virtual bool DoGetN(const ObjectBase *object, std::size_t *n) const = 0;
240  virtual Ptr<Object> DoGet(const ObjectBase *object, std::size_t i, std::size_t *index) const = 0;
241 };
242 
243 template <typename T, typename U, typename INDEX>
245 MakeObjectPtrContainerAccessor (Ptr<U> (T::*get)(INDEX) const,
246  INDEX (T::*getN)(void) const)
247 {
248  struct MemberGetters : public ObjectPtrContainerAccessor
249  {
250  virtual bool DoGetN(const ObjectBase *object, std::size_t *n) const {
251  const T *obj = dynamic_cast<const T *> (object);
252  if (obj == 0)
253  {
254  return false;
255  }
256  *n = (obj->*m_getN)();
257  return true;
258  }
259  virtual Ptr<Object> DoGet(const ObjectBase *object, std::size_t i, std::size_t *index) const {
260  const T *obj = static_cast<const T *> (object);
261  *index = i;
262  return (obj->*m_get)(i);
263  }
264  Ptr<U> (T::*m_get)(INDEX) const;
265  INDEX (T::*m_getN)(void) const;
266  } *spec = new MemberGetters ();
267  spec->m_get = get;
268  spec->m_getN = getN;
269  return Ptr<const AttributeAccessor> (spec, false);
270 }
271 
272 template <typename T, typename U, typename INDEX>
273 Ptr<const AttributeAccessor>
274 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
275  Ptr<U> (T::*get)(INDEX) const)
276 {
277  return MakeObjectPtrContainerAccessor (get, getN);
278 }
279 
280 template <typename T>
282 {
283  return Create<internal::ObjectPtrContainerChecker<T> > ();
284 }
285 
286 
287 } // namespace ns3
288 
289 #endif /* OBJECT_PTR_CONTAINER_H */
ObjectPtrContainerChecker implementation class.
ObjectPtrContainerValue()
Default constructor.
Represent the type of an attribute.
Definition: attribute.h:166
ns3::Ptr smart pointer declaration and implementation.
Hold a value for an Attribute.
Definition: attribute.h:68
virtual bool Copy(const AttributeValue &source, AttributeValue &destination) const
Copy the source to the destination.
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
virtual TypeId GetItemTypeId(void) const =0
Get the TypeId of the container class type.
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.
AttributeAccessor implementation for ObjectPtrContainerValue.
Iterator Begin(void) const
Get an iterator to the first Object.
std::map< std::size_t, Ptr< Object > >::const_iterator Iterator
Iterator type for traversing this container.
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
std::map< std::size_t, Ptr< Object > > m_objects
The container implementation.
AttributeChecker implementation for ObjectPtrContainerValue.
Iterator End(void) const
Get an iterator to the past-the-end Object.
allow setting and getting the value of an attribute.
Definition: attribute.h:114
std::size_t GetN(void) const
Get the number of Objects.
Ptr< const AttributeChecker > MakeObjectPtrContainerChecker(void)
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
Serialize each of the Object pointers to a string.
virtual bool Check(const AttributeValue &value) const
virtual bool DoGetN(const ObjectBase *object, std::size_t *n) const =0
Get the number of instances in the container.
virtual bool Get(const ObjectBase *object, AttributeValue &value) const
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual std::string GetUnderlyingTypeInformation(void) const
virtual Ptr< AttributeValue > Create(void) const
ns3::Object class declaration, which is the root of the Object hierarchy and Aggregation.
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
Deserialize from a string.
virtual bool HasUnderlyingTypeInformation(void) 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.
virtual std::string GetValueTypeName(void) const
virtual TypeId GetItemTypeId(void) const
Get the TypeId of the container class type.
virtual bool HasSetter(void) const
Container for a set of ns3::Object pointers.
Ptr< Object > Get(std::size_t i) const
Get a specific Object.
virtual bool Set(ObjectBase *object, const AttributeValue &value) const
a unique identifier for an interface.
Definition: type-id.h:58
virtual bool HasGetter(void) const
virtual Ptr< AttributeValue > Copy(void) const
Get a copy of this container.