A Discrete-Event Network Simulator
API
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 #include "unused.h"
25 
32 namespace ns3 {
33 
34 
70 template <typename V, typename T1>
71 inline
72 Ptr<const AttributeAccessor>
73 MakeAccessorHelper (T1 a1);
74 
75 
116 template <typename V, typename T1, typename T2>
117 inline
118 Ptr<const AttributeAccessor>
119 MakeAccessorHelper (T1 a1, T2 a2);
120 
121 
122 } // namespace ns3
123 
124 
125 /***************************************************************
126  * Implementation of the templates declared above.
127  ***************************************************************/
128 
129 #include "type-traits.h"
130 
131 namespace ns3 {
132 
133 
141 template <typename T>
143 {
146 };
147 
148 
159 template <typename T, typename U>
161 {
162 public:
165 
178  virtual bool Set (ObjectBase * object, const AttributeValue & val) const {
179  const U *value = dynamic_cast<const U *> (&val);
180  if (value == 0)
181  {
182  return false;
183  }
184  T *obj = dynamic_cast<T *> (object);
185  if (obj == 0)
186  {
187  return false;
188  }
189  return DoSet (obj, value);
190  }
191 
204  virtual bool Get (const ObjectBase * object, AttributeValue &val) const {
205  U *value = dynamic_cast<U *> (&val);
206  if (value == 0)
207  {
208  return false;
209  }
210  const T *obj = dynamic_cast<const T *> (object);
211  if (obj == 0)
212  {
213  return false;
214  }
215  return DoGet (obj, value);
216  }
217 
218 private:
227  virtual bool DoSet (T *object, const U *v) const = 0;
236  virtual bool DoGet (const T *object, U *v) const = 0;
237 
238 }; // class AccessorHelper
239 
240 
253 template <typename V, typename T, typename U>
254 inline
255 Ptr<const AttributeAccessor>
256 DoMakeAccessorHelperOne (U T::*memberVariable)
257 {
258  /* AttributeAcessor implementation for a class member variable. */
259  class MemberVariable : public AccessorHelper<T,V>
260  {
261 public:
262  /*
263  * Construct from a class data member address.
264  * \param [in] memberVariable The class data member address.
265  */
266  MemberVariable (U T::*memberVariable)
267  : AccessorHelper<T,V> (),
268  m_memberVariable (memberVariable)
269  {}
270 private:
271  virtual bool DoSet (T *object, const V *v) const {
272  typename AccessorTrait<U>::Result tmp;
273  bool ok = v->GetAccessor (tmp);
274  if (!ok)
275  {
276  return false;
277  }
278  (object->*m_memberVariable) = tmp;
279  return true;
280  }
281  virtual bool DoGet (const T *object, V *v) const {
282  v->Set (object->*m_memberVariable);
283  return true;
284  }
285  virtual bool HasGetter (void) const {
286  return true;
287  }
288  virtual bool HasSetter (void) const {
289  return true;
290  }
291 
292  U T::*m_memberVariable; // Address of the class data member.
293  };
294  return Ptr<const AttributeAccessor> (new MemberVariable (memberVariable), false);
295 }
296 
297 
310 template <typename V, typename T, typename U>
311 inline
312 Ptr<const AttributeAccessor>
313 DoMakeAccessorHelperOne (U (T::*getter)(void) const)
314 {
315  /* AttributeAccessor implementation with a class get functor method. */
316  class MemberMethod : public AccessorHelper<T,V>
317  {
318 public:
319  /*
320  * Construct from a class get functor method.
321  * \param [in] getter The class get functor method pointer.
322  */
323  MemberMethod (U (T::*getter)(void) const)
324  : AccessorHelper<T,V> (),
325  m_getter (getter)
326  {}
327 private:
328  virtual bool DoSet (T *object, const V *v) const {
329  NS_UNUSED (object);
330  NS_UNUSED (v);
331  return false;
332  }
333  virtual bool DoGet (const T *object, V *v) const {
334  v->Set ((object->*m_getter)());
335  return true;
336  }
337  virtual bool HasGetter (void) const {
338  return true;
339  }
340  virtual bool HasSetter (void) const {
341  return false;
342  }
343  U (T::*m_getter)(void) const; // The class get functor method pointer.
344  };
345  return Ptr<const AttributeAccessor> (new MemberMethod (getter), false);
346 }
347 
348 
362 template <typename V, typename T, typename U>
363 inline
364 Ptr<const AttributeAccessor>
365 DoMakeAccessorHelperOne (void (T::*setter)(U))
366 {
367  /* AttributeAccessor implementation with a class set method returning void. */
368  class MemberMethod : public AccessorHelper<T,V>
369  {
370 public:
371  /*
372  * Construct from a class set method.
373  * \param [in] setter The class set method pointer.
374  */
375  MemberMethod (void (T::*setter)(U))
376  : AccessorHelper<T,V> (),
377  m_setter (setter)
378  {}
379 private:
380  virtual bool DoSet (T *object, const V *v) const {
381  typename AccessorTrait<U>::Result tmp;
382  bool ok = v->GetAccessor (tmp);
383  if (!ok)
384  {
385  return false;
386  }
387  (object->*m_setter)(tmp);
388  return true;
389  }
390  virtual bool DoGet (const T *object, V *v) const {
391  NS_UNUSED(object);
392  NS_UNUSED(v);
393  return false;
394  }
395  virtual bool HasGetter (void) const {
396  return false;
397  }
398  virtual bool HasSetter (void) const {
399  return true;
400  }
401  void (T::*m_setter)(U); // The class set method pointer, returning void.
402  };
403  return Ptr<const AttributeAccessor> (new MemberMethod (setter), false);
404 }
405 
406 
424 template <typename W, typename T, typename U, typename V>
425 inline
426 Ptr<const AttributeAccessor>
427 DoMakeAccessorHelperTwo (void (T::*setter)(U),
428  V (T::*getter)(void) const)
429 {
430  /*
431  * AttributeAccessor implementation with class get functor and set method,
432  * returning void.
433  */
434  class MemberMethod : public AccessorHelper<T,W>
435  {
436 public:
437  /*
438  * Construct from class get functor and set methods.
439  * \param [in] setter The class set method pointer, returning void.
440  * \param [in] getter The class get functor method pointer.
441  */
442  MemberMethod (void (T::*setter)(U),
443  V (T::*getter)(void) const)
444  : AccessorHelper<T,W> (),
445  m_setter (setter),
446  m_getter (getter)
447  {}
448 private:
449  virtual bool DoSet (T *object, const W *v) const {
450  typename AccessorTrait<U>::Result tmp;
451  bool ok = v->GetAccessor (tmp);
452  if (!ok)
453  {
454  return false;
455  }
456  (object->*m_setter)(tmp);
457  return true;
458  }
459  virtual bool DoGet (const T *object, W *v) const {
460  v->Set ((object->*m_getter)());
461  return true;
462  }
463  virtual bool HasGetter (void) const {
464  return true;
465  }
466  virtual bool HasSetter (void) const {
467  return true;
468  }
469  void (T::*m_setter)(U); // The class set method pointer, returning void.
470  V (T::*m_getter)(void) const; // The class get functor method pointer.
471  };
472  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
473 }
474 
475 
480 template <typename W, typename T, typename U, typename V>
481 inline
482 Ptr<const AttributeAccessor>
483 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
484  void (T::*setter)(U))
485 {
486  return DoMakeAccessorHelperTwo<W> (setter, getter);
487 }
488 
489 
507 template <typename W, typename T, typename U, typename V>
508 inline
509 Ptr<const AttributeAccessor>
510 DoMakeAccessorHelperTwo (bool (T::*setter)(U),
511  V (T::*getter)(void) const)
512 {
513  /*
514  * AttributeAccessor implementation with class get functor and
515  * set method, returning bool.
516  */
517  class MemberMethod : public AccessorHelper<T,W>
518  {
519 public:
520  /*
521  * Construct from class get functor and set method, returning bool.
522  * \param [in] setter The class set method pointer, returning bool.
523  * \param [in] getter The class get functor method pointer.
524  */
525  MemberMethod (bool (T::*setter)(U),
526  V (T::*getter)(void) const)
527  : AccessorHelper<T,W> (),
528  m_setter (setter),
529  m_getter (getter)
530  {}
531 private:
532  virtual bool DoSet (T *object, const W *v) const {
533  typename AccessorTrait<U>::Result tmp;
534  bool ok = v->GetAccessor (tmp);
535  if (!ok)
536  {
537  return false;
538  }
539  ok = (object->*m_setter)(tmp);
540  return ok;
541  }
542  virtual bool DoGet (const T *object, W *v) const {
543  v->Set ((object->*m_getter)());
544  return true;
545  }
546  virtual bool HasGetter (void) const {
547  return true;
548  }
549  virtual bool HasSetter (void) const {
550  return true;
551  }
552  bool (T::*m_setter)(U); // The class set method pointer, returning bool.
553  V (T::*m_getter)(void) const; // The class get functor method pointer.
554  };
555  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
556 }
557 
558 
563 template <typename W, typename T, typename U, typename V>
564 inline
565 Ptr<const AttributeAccessor>
566 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
567  bool (T::*setter)(U))
568 {
569  return DoMakeAccessorHelperTwo<W> (setter, getter);
570 }
571 
572 
573 template <typename V, typename T1>
574 inline
575 Ptr<const AttributeAccessor>
577 {
578  return DoMakeAccessorHelperOne<V> (a1);
579 }
580 
581 template <typename V, typename T1, typename T2>
582 inline
583 Ptr<const AttributeAccessor>
584 MakeAccessorHelper (T1 a1, T2 a2)
585 {
586  return DoMakeAccessorHelperTwo<V> (a1, a2);
587 }
588 
589 } // namespace ns3
590 
591 #endif /* ATTRIBUTE_ACCESSOR_HELPER_H */
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Hold a value for an Attribute.
Definition: attribute.h:68
Ptr< const AttributeAccessor > MakeAccessorHelper(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
#define NS_UNUSED(x)
Mark a local variable as unused.
Definition: unused.h:36
Anchor the ns-3 type and attribute system.
Definition: object-base.h:119
Ptr< const AttributeAccessor > DoMakeAccessorHelperTwo(void(T::*setter)(U), V(T::*getter)(void) const)
MakeAccessorHelper implementation with a class get functor method and a class set method returning vo...
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
virtual bool DoSet(T *object, const U *v) const =0
Setter implementation.
allow setting and getting the value of an attribute.
Definition: attribute.h:114
TypeTraits< typename TypeTraits< T >::ReferencedType >::NonConstType Result
The non-const, non reference type.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Inspect a type to deduce its features.
Definition: type-traits.h:39
Basic functionality for accessing class attributes via class data members, or get functor/set methods...
The non-const and non-reference type equivalent to T.
Ptr< const AttributeAccessor > DoMakeAccessorHelperOne(U T::*memberVariable)
MakeAccessorHelper implementation for a class data member.
ns3::TypeTraits introspection declaration and template implementation.
virtual bool Set(ObjectBase *object, const AttributeValue &val) const
Set the underlying member to the argument AttributeValue.
virtual bool DoGet(const T *object, U *v) const =0
Getter implementation.
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Get the value of the underlying member into the AttributeValue.
NS_UNUSED and NS_UNUSED_GLOBAL macro definitions.