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  {
181  return T::GetTypeId ();
182  }
183  virtual bool Check (const AttributeValue &value) const
184  {
185  return dynamic_cast<const ObjectPtrContainerValue *> (&value) != 0;
186  }
187  virtual std::string GetValueTypeName (void) const
188  {
189  return "ns3::ObjectPtrContainerValue";
190  }
191  virtual bool HasUnderlyingTypeInformation (void) const
192  {
193  return true;
194  }
195  virtual std::string GetUnderlyingTypeInformation (void) const
196  {
197  return "ns3::Ptr< " + T::GetTypeId ().GetName () + " >";
198  }
199  virtual Ptr<AttributeValue> Create (void) const
200  {
201  return ns3::Create<ObjectPtrContainerValue> ();
202  }
203  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const
204  {
205  const ObjectPtrContainerValue *src = dynamic_cast<const ObjectPtrContainerValue *> (&source);
206  ObjectPtrContainerValue *dst = dynamic_cast<ObjectPtrContainerValue *> (&destination);
207  if (src == 0 || dst == 0)
208  {
209  return false;
210  }
211  *dst = *src;
212  return true;
213  }
214 };
215 
216 } // namespace internal
217 
218 
224 {
225 public:
226  virtual bool Set (ObjectBase * object, const AttributeValue &value) const;
227  virtual bool Get (const ObjectBase * object, AttributeValue &value) const;
228  virtual bool HasGetter (void) const;
229  virtual bool HasSetter (void) const;
230 
231 private:
239  virtual bool DoGetN (const ObjectBase *object, std::size_t *n) const = 0;
248  virtual Ptr<Object> DoGet (const ObjectBase *object, std::size_t i, std::size_t *index) const = 0;
249 };
250 
251 template <typename T, typename U, typename INDEX>
253 MakeObjectPtrContainerAccessor (Ptr<U> (T::*get)(INDEX) const,
254  INDEX (T::*getN)(void) const)
255 {
256  struct MemberGetters : public ObjectPtrContainerAccessor
257  {
258  virtual bool DoGetN (const ObjectBase *object, std::size_t *n) const
259  {
260  const T *obj = dynamic_cast<const T *> (object);
261  if (obj == 0)
262  {
263  return false;
264  }
265  *n = (obj->*m_getN)();
266  return true;
267  }
268  virtual Ptr<Object> DoGet (const ObjectBase *object, std::size_t i, std::size_t *index) const
269  {
270  const T *obj = static_cast<const T *> (object);
271  *index = i;
272  return (obj->*m_get)(i);
273  }
274  Ptr<U> (T::*m_get)(INDEX) const;
275  INDEX (T::*m_getN)(void) const;
276  } *spec = new MemberGetters ();
277  spec->m_get = get;
278  spec->m_getN = getN;
279  return Ptr<const AttributeAccessor> (spec, false);
280 }
281 
282 template <typename T, typename U, typename INDEX>
283 Ptr<const AttributeAccessor>
284 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
285  Ptr<U> (T::*get)(INDEX) const)
286 {
287  return MakeObjectPtrContainerAccessor (get, getN);
288 }
289 
290 template <typename T>
292 {
293  return Create<internal::ObjectPtrContainerChecker<T> > ();
294 }
295 
296 
297 } // namespace ns3
298 
299 #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.