A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
attribute-accessor-helper.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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@sophia.inria.fr>
19  */
20 #ifndef ATTRIBUTE_ACCESSOR_HELPER_H
21 #define ATTRIBUTE_ACCESSOR_HELPER_H
22 
23 #include "attribute.h"
24 
25 namespace ns3 {
26 
30 template <typename V, typename T1>
31 inline
32 Ptr<const AttributeAccessor>
33 MakeAccessorHelper (T1 a1);
34 
38 template <typename V, typename T1, typename T2>
39 inline
40 Ptr<const AttributeAccessor>
41 MakeAccessorHelper (T1 a1, T2 a2);
42 
43 } // namespace ns3
44 
45 /***************************************************************
46  * The implementation of the above functions.
47  ***************************************************************/
48 
49 #include "type-traits.h"
50 
51 namespace ns3 {
52 
53 template <typename T>
55 {
57 };
58 
59 template <typename T, typename U>
61 {
62 public:
64 
65  virtual bool Set (ObjectBase * object, const AttributeValue & val) const {
66  const U *value = dynamic_cast<const U *> (&val);
67  if (value == 0)
68  {
69  return false;
70  }
71  T *obj = dynamic_cast<T *> (object);
72  if (obj == 0)
73  {
74  return false;
75  }
76  return DoSet (obj, value);
77  }
78 
79  virtual bool Get (const ObjectBase * object, AttributeValue &val) const {
80  U *value = dynamic_cast<U *> (&val);
81  if (value == 0)
82  {
83  return false;
84  }
85  const T *obj = dynamic_cast<const T *> (object);
86  if (obj == 0)
87  {
88  return false;
89  }
90  return DoGet (obj, value);
91  }
92 
93 private:
94  virtual bool DoSet (T *object, const U *v) const = 0;
95  virtual bool DoGet (const T *object, U *v) const = 0;
96 };
97 
98 template <typename V, typename T, typename U>
99 inline
100 Ptr<const AttributeAccessor>
101 DoMakeAccessorHelperOne (U T::*memberVariable)
102 {
103  class MemberVariable : public AccessorHelper<T,V>
104  {
105 public:
106  MemberVariable (U T::*memberVariable)
107  : AccessorHelper<T,V> (),
108  m_memberVariable (memberVariable)
109  {}
110 private:
111  virtual bool DoSet (T *object, const V *v) const {
112  typename AccessorTrait<U>::Result tmp;
113  bool ok = v->GetAccessor (tmp);
114  if (!ok)
115  {
116  return false;
117  }
118  (object->*m_memberVariable) = tmp;
119  return true;
120  }
121  virtual bool DoGet (const T *object, V *v) const {
122  v->Set (object->*m_memberVariable);
123  return true;
124  }
125  virtual bool HasGetter (void) const {
126  return true;
127  }
128  virtual bool HasSetter (void) const {
129  return true;
130  }
131 
132  U T::*m_memberVariable;
133  };
134  return Ptr<const AttributeAccessor> (new MemberVariable (memberVariable), false);
135 }
136 
137 template <typename V, typename T, typename U>
138 inline
139 Ptr<const AttributeAccessor>
140 DoMakeAccessorHelperOne (U (T::*getter)(void) const)
141 {
142  class MemberMethod : public AccessorHelper<T,V>
143  {
144 public:
145  MemberMethod (U (T::*getter)(void) const)
146  : AccessorHelper<T,V> (),
147  m_getter (getter)
148  {}
149 private:
150  virtual bool DoSet (T *object, const V *v) const {
151  return false;
152  }
153  virtual bool DoGet (const T *object, V *v) const {
154  v->Set ((object->*m_getter)());
155  return true;
156  }
157  virtual bool HasGetter (void) const {
158  return true;
159  }
160  virtual bool HasSetter (void) const {
161  return false;
162  }
163  U (T::*m_getter)(void) const;
164  };
165  return Ptr<const AttributeAccessor> (new MemberMethod (getter), false);
166 }
167 
168 
169 template <typename V, typename T, typename U>
170 inline
171 Ptr<const AttributeAccessor>
172 DoMakeAccessorHelperOne (void (T::*setter)(U))
173 {
174  class MemberMethod : public AccessorHelper<T,V>
175  {
176 public:
177  MemberMethod (void (T::*setter)(U))
178  : AccessorHelper<T,V> (),
179  m_setter (setter)
180  {}
181 private:
182  virtual bool DoSet (T *object, const V *v) const {
183  typename AccessorTrait<U>::Result tmp;
184  bool ok = v->GetAccessor (tmp);
185  if (!ok)
186  {
187  return false;
188  }
189  (object->*m_setter)(tmp);
190  return true;
191  }
192  virtual bool DoGet (const T *object, V *v) const {
193  return false;
194  }
195  virtual bool HasGetter (void) const {
196  return false;
197  }
198  virtual bool HasSetter (void) const {
199  return true;
200  }
201  void (T::*m_setter)(U);
202  };
203  return Ptr<const AttributeAccessor> (new MemberMethod (setter), false);
204 }
205 
206 template <typename W, typename T, typename U, typename V>
207 inline
208 Ptr<const AttributeAccessor>
209 DoMakeAccessorHelperTwo (void (T::*setter)(U),
210  V (T::*getter)(void) const)
211 {
212  class MemberMethod : public AccessorHelper<T,W>
213  {
214 public:
215  MemberMethod (void (T::*setter)(U),
216  V (T::*getter)(void) const)
217  : AccessorHelper<T,W> (),
218  m_setter (setter),
219  m_getter (getter)
220  {}
221 private:
222  virtual bool DoSet (T *object, const W *v) const {
223  typename AccessorTrait<U>::Result tmp;
224  bool ok = v->GetAccessor (tmp);
225  if (!ok)
226  {
227  return false;
228  }
229  (object->*m_setter)(tmp);
230  return true;
231  }
232  virtual bool DoGet (const T *object, W *v) const {
233  v->Set ((object->*m_getter)());
234  return true;
235  }
236  virtual bool HasGetter (void) const {
237  return true;
238  }
239  virtual bool HasSetter (void) const {
240  return true;
241  }
242  void (T::*m_setter)(U);
243  V (T::*m_getter)(void) const;
244  };
245  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
246 }
247 
248 template <typename W, typename T, typename U, typename V>
249 inline
250 Ptr<const AttributeAccessor>
251 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
252  void (T::*setter)(U))
253 {
254  return DoMakeAccessorHelperTwo<W> (setter, getter);
255 }
256 
257 template <typename W, typename T, typename U, typename V>
258 inline
259 Ptr<const AttributeAccessor>
260 DoMakeAccessorHelperTwo (bool (T::*setter)(U),
261  V (T::*getter)(void) const)
262 {
263  class MemberMethod : public AccessorHelper<T,W>
264  {
265 public:
266  MemberMethod (bool (T::*setter)(U),
267  V (T::*getter)(void) const)
268  : AccessorHelper<T,W> (),
269  m_setter (setter),
270  m_getter (getter)
271  {}
272 private:
273  virtual bool DoSet (T *object, const W *v) const {
274  typename AccessorTrait<U>::Result tmp;
275  bool ok = v->GetAccessor (tmp);
276  if (!ok)
277  {
278  return false;
279  }
280  ok = (object->*m_setter)(tmp);
281  return ok;
282  }
283  virtual bool DoGet (const T *object, W *v) const {
284  v->Set ((object->*m_getter)());
285  return true;
286  }
287  virtual bool HasGetter (void) const {
288  return true;
289  }
290  virtual bool HasSetter (void) const {
291  return true;
292  }
293  bool (T::*m_setter)(U);
294  V (T::*m_getter)(void) const;
295  };
296  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
297 }
298 
299 template <typename W, typename T, typename U, typename V>
300 inline
301 Ptr<const AttributeAccessor>
302 DoMakeAccessorHelperTwo (bool (T::*getter)(void) const,
303  void (T::*setter)(U))
304 {
305  return DoMakeAccessorHelperTwo<W> (setter, getter);
306 }
307 
308 template <typename V, typename T1>
309 inline
310 Ptr<const AttributeAccessor>
312 {
313  return DoMakeAccessorHelperOne<V> (a1);
314 }
315 
316 template <typename V, typename T1, typename T2>
317 inline
318 Ptr<const AttributeAccessor>
319 MakeAccessorHelper (T1 a1, T2 a2)
320 {
321  return DoMakeAccessorHelperTwo<V> (a1, a2);
322 }
323 
324 } // namespace ns3
325 
326 #endif /* ATTRIBUTE_ACCESSOR_HELPER_H */
Ptr< const AttributeAccessor > DoMakeAccessorHelperOne(U T::*memberVariable)
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
Hold a value for an Attribute.
Definition: attribute.h:56
Ptr< const AttributeAccessor > DoMakeAccessorHelperTwo(void(T::*setter)(U), V(T::*getter)(void) const)
implement the ns-3 type and attribute system
Definition: object-base.h:61
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Ptr< const AttributeAccessor > MakeAccessorHelper(T1 a1)
virtual bool DoSet(T *object, const U *v) const =0
allow setting and getting the value of an attribute.
Definition: attribute.h:102
TypeTraits< typename TypeTraits< T >::ReferencedType >::NonConstType Result
virtual bool Set(ObjectBase *object, const AttributeValue &val) const
Type trait reference values.
Definition: type-traits.h:6
virtual bool DoGet(const T *object, U *v) const =0