A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
28 namespace ns3 {
29 
39 {
40 public:
41  typedef std::map<uint32_t, Ptr<Object> >::const_iterator Iterator;
42 
44 
48  Iterator Begin (void) const;
52  Iterator End (void) const;
56  uint32_t GetN (void) const;
61  Ptr<Object> Get (uint32_t i) const;
62 
63  virtual Ptr<AttributeValue> Copy (void) const;
64  virtual std::string SerializeToString (Ptr<const AttributeChecker> checker) const;
65  virtual bool DeserializeFromString (std::string value, Ptr<const AttributeChecker> checker);
66 
67 private:
69  std::map<uint32_t, Ptr<Object> > m_objects;
70 };
71 
72 
73 template <typename T, typename U, typename INDEX>
75 MakeObjectPtrContainerAccessor (Ptr<U> (T::*get)(INDEX) const,
76  INDEX (T::*getN)(void) const);
77 
78 template <typename T, typename U, typename INDEX>
80 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
81  Ptr<U> (T::*get)(INDEX) const);
82 
84 {
85 public:
86  virtual TypeId GetItemTypeId (void) const = 0;
87 };
88 
89 template <typename T>
91 
92 } // namespace ns3
93 
94 namespace ns3 {
95 
96 namespace internal {
97 
98 template <typename T>
100 {
101 public:
102  virtual TypeId GetItemTypeId (void) const {
103  return T::GetTypeId ();
104  }
105  virtual bool Check (const AttributeValue &value) const {
106  return dynamic_cast<const ObjectPtrContainerValue *> (&value) != 0;
107  }
108  virtual std::string GetValueTypeName (void) const {
109  return "ns3::ObjectPtrContainerValue";
110  }
111  virtual bool HasUnderlyingTypeInformation (void) const {
112  return true;
113  }
114  virtual std::string GetUnderlyingTypeInformation (void) const {
115  return "ns3::Ptr< " + T::GetTypeId ().GetName () + " >";
116  }
117  virtual Ptr<AttributeValue> Create (void) const {
118  return ns3::Create<ObjectPtrContainerValue> ();
119  }
120  virtual bool Copy (const AttributeValue &source, AttributeValue &destination) const {
121  const ObjectPtrContainerValue *src = dynamic_cast<const ObjectPtrContainerValue *> (&source);
122  ObjectPtrContainerValue *dst = dynamic_cast<ObjectPtrContainerValue *> (&destination);
123  if (src == 0 || dst == 0)
124  {
125  return false;
126  }
127  *dst = *src;
128  return true;
129  }
130 };
131 
132 } // namespace internal
133 
134 
136 {
137 public:
138  virtual bool Set (ObjectBase * object, const AttributeValue &value) const;
139  virtual bool Get (const ObjectBase * object, AttributeValue &value) const;
140  virtual bool HasGetter (void) const;
141  virtual bool HasSetter (void) const;
142 private:
143  virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const = 0;
144  virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i, uint32_t *index) const = 0;
145 };
146 
147 template <typename T, typename U, typename INDEX>
149 MakeObjectPtrContainerAccessor (Ptr<U> (T::*get)(INDEX) const,
150  INDEX (T::*getN)(void) const)
151 {
152  struct MemberGetters : public ObjectPtrContainerAccessor
153  {
154  virtual bool DoGetN (const ObjectBase *object, uint32_t *n) const {
155  const T *obj = dynamic_cast<const T *> (object);
156  if (obj == 0)
157  {
158  return false;
159  }
160  *n = (obj->*m_getN)();
161  return true;
162  }
163  virtual Ptr<Object> DoGet (const ObjectBase *object, uint32_t i, uint32_t *index) const {
164  const T *obj = static_cast<const T *> (object);
165  *index = i;
166  return (obj->*m_get)(i);
167  }
168  Ptr<U> (T::*m_get)(INDEX) const;
169  INDEX (T::*m_getN)(void) const;
170  } *spec = new MemberGetters ();
171  spec->m_get = get;
172  spec->m_getN = getN;
173  return Ptr<const AttributeAccessor> (spec, false);
174 }
175 
176 template <typename T, typename U, typename INDEX>
177 Ptr<const AttributeAccessor>
178 MakeObjectPtrContainerAccessor (INDEX (T::*getN)(void) const,
179  Ptr<U> (T::*get)(INDEX) const)
180 {
181  return MakeObjectPtrContainerAccessor (get, getN);
182 }
183 
184 template <typename T>
186 {
187  return Create<internal::AnObjectPtrContainerChecker<T> > ();
188 }
189 
190 
191 } // namespace ns3
192 
193 #endif /* OBJECT_PTR_CONTAINER_H */
virtual Ptr< AttributeValue > Copy(void) const
Represent the type of an attribute.
Definition: attribute.h:154
virtual std::string GetUnderlyingTypeInformation(void) const
virtual bool Copy(const AttributeValue &source, AttributeValue &destination) const
Copy the source to the destination.
virtual std::string GetValueTypeName(void) const
virtual bool HasSetter(void) const
virtual Ptr< Object > DoGet(const ObjectBase *object, uint32_t i, uint32_t *index) const =0
Hold a value for an Attribute.
Definition: attribute.h:56
implement the ns-3 type and attribute system
Definition: object-base.h:70
virtual TypeId GetItemTypeId(void) const =0
virtual Ptr< AttributeValue > Create(void) const
std::map< uint32_t, Ptr< Object > >::const_iterator Iterator
Ptr< Object > Get(uint32_t i) const
virtual bool Set(ObjectBase *object, const AttributeValue &value) const
virtual bool HasUnderlyingTypeInformation(void) const
virtual bool HasGetter(void) const
std::map< uint32_t, Ptr< Object > > m_objects
allow setting and getting the value of an attribute.
Definition: attribute.h:102
Ptr< const AttributeAccessor > MakeObjectPtrContainerAccessor(Ptr< U >(T::*get)(INDEX) const, INDEX(T::*getN)(void) const)
virtual bool Check(const AttributeValue &value) const
virtual bool DeserializeFromString(std::string value, Ptr< const AttributeChecker > checker)
virtual bool DoGetN(const ObjectBase *object, uint32_t *n) const =0
contain a set of ns3::Object pointers.
virtual bool Get(const ObjectBase *object, AttributeValue &value) const
virtual std::string SerializeToString(Ptr< const AttributeChecker > checker) const
a unique identifier for an interface.
Definition: type-id.h:49
Ptr< const AttributeChecker > MakeObjectPtrContainerChecker(void)