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  {}
166 
179  virtual bool Set (ObjectBase * object, const AttributeValue & val) const
180  {
181  const U *value = dynamic_cast<const U *> (&val);
182  if (value == 0)
183  {
184  return false;
185  }
186  T *obj = dynamic_cast<T *> (object);
187  if (obj == 0)
188  {
189  return false;
190  }
191  return DoSet (obj, value);
192  }
193 
206  virtual bool Get (const ObjectBase * object, AttributeValue &val) const
207  {
208  U *value = dynamic_cast<U *> (&val);
209  if (value == 0)
210  {
211  return false;
212  }
213  const T *obj = dynamic_cast<const T *> (object);
214  if (obj == 0)
215  {
216  return false;
217  }
218  return DoGet (obj, value);
219  }
220 
221 private:
230  virtual bool DoSet (T *object, const U *v) const = 0;
239  virtual bool DoGet (const T *object, U *v) const = 0;
240 
241 }; // class AccessorHelper
242 
243 
256 template <typename V, typename T, typename U>
257 inline
258 Ptr<const AttributeAccessor>
259 DoMakeAccessorHelperOne (U T::*memberVariable)
260 {
261  /* AttributeAcessor implementation for a class member variable. */
262  class MemberVariable : public AccessorHelper<T,V>
263  {
264  public:
265  /*
266  * Construct from a class data member address.
267  * \param [in] memberVariable The class data member address.
268  */
269  MemberVariable (U T::*memberVariable)
270  : AccessorHelper<T,V> (),
271  m_memberVariable (memberVariable)
272  {}
273 
274  private:
275  virtual bool DoSet (T *object, const V *v) const
276  {
277  typename AccessorTrait<U>::Result tmp;
278  bool ok = v->GetAccessor (tmp);
279  if (!ok)
280  {
281  return false;
282  }
283  (object->*m_memberVariable) = tmp;
284  return true;
285  }
286  virtual bool DoGet (const T *object, V *v) const
287  {
288  v->Set (object->*m_memberVariable);
289  return true;
290  }
291  virtual bool HasGetter (void) const
292  {
293  return true;
294  }
295  virtual bool HasSetter (void) const
296  {
297  return true;
298  }
299 
300  U T::*m_memberVariable; // Address of the class data member.
301  };
302  return Ptr<const AttributeAccessor> (new MemberVariable (memberVariable), false);
303 }
304 
305 
318 template <typename V, typename T, typename U>
319 inline
320 Ptr<const AttributeAccessor>
321 DoMakeAccessorHelperOne (U (T::*getter)(void) const)
322 {
323  /* AttributeAccessor implementation with a class get functor method. */
324  class MemberMethod : public AccessorHelper<T,V>
325  {
326  public:
327  /*
328  * Construct from a class get functor method.
329  * \param [in] getter The class get functor method pointer.
330  */
331  MemberMethod (U (T::*getter)(void) const)
332  : AccessorHelper<T,V> (),
333  m_getter (getter)
334  {}
335 
336  private:
337  virtual bool DoSet (T *object, const V *v) const
338  {
339  NS_UNUSED (object);
340  NS_UNUSED (v);
341  return false;
342  }
343  virtual bool DoGet (const T *object, V *v) const
344  {
345  v->Set ((object->*m_getter)());
346  return true;
347  }
348  virtual bool HasGetter (void) const
349  {
350  return true;
351  }
352  virtual bool HasSetter (void) const
353  {
354  return false;
355  }
356  U (T::*m_getter)(void) const; // The class get functor method pointer.
357  };
358  return Ptr<const AttributeAccessor> (new MemberMethod (getter), false);
359 }
360 
361 
375 template <typename V, typename T, typename U>
376 inline
377 Ptr<const AttributeAccessor>
378 DoMakeAccessorHelperOne (void (T::*setter)(U))
379 {
380  /* AttributeAccessor implementation with a class set method returning void. */
381  class MemberMethod : public AccessorHelper<T,V>
382  {
383  public:
384  /*
385  * Construct from a class set method.
386  * \param [in] setter The class set method pointer.
387  */
388  MemberMethod (void (T::*setter)(U))
389  : AccessorHelper<T,V> (),
390  m_setter (setter)
391  {}
392 
393  private:
394  virtual bool DoSet (T *object, const V *v) const
395  {
396  typename AccessorTrait<U>::Result tmp;
397  bool ok = v->GetAccessor (tmp);
398  if (!ok)
399  {
400  return false;
401  }
402  (object->*m_setter)(tmp);
403  return true;
404  }
405  virtual bool DoGet (const T *object, V *v) const
406  {
407  NS_UNUSED (object);
408  NS_UNUSED (v);
409  return false;
410  }
411  virtual bool HasGetter (void) const
412  {
413  return false;
414  }
415  virtual bool HasSetter (void) const
416  {
417  return true;
418  }
419  void (T::*m_setter)(U); // The class set method pointer, returning void.
420  };
421  return Ptr<const AttributeAccessor> (new MemberMethod (setter), false);
422 }
423 
424 
442 template <typename W, typename T, typename U, typename V>
443 inline
444 Ptr<const AttributeAccessor>
445 DoMakeAccessorHelperTwo (void (T::*setter)(U),
446  V (T::*getter)(void) const)
447 {
448  /*
449  * AttributeAccessor implementation with class get functor and set method,
450  * returning void.
451  */
452  class MemberMethod : public AccessorHelper<T,W>
453  {
454  public:
455  /*
456  * Construct from class get functor and set methods.
457  * \param [in] setter The class set method pointer, returning void.
458  * \param [in] getter The class get functor method pointer.
459  */
460  MemberMethod (void (T::*setter)(U),
461  V (T::*getter)(void) const)
462  : AccessorHelper<T,W> (),
463  m_setter (setter),
464  m_getter (getter)
465  {}
466 
467  private:
468  virtual bool DoSet (T *object, const W *v) const
469  {
470  typename AccessorTrait<U>::Result tmp;
471  bool ok = v->GetAccessor (tmp);
472  if (!ok)
473  {
474  return false;
475  }
476  (object->*m_setter)(tmp);
477  return true;
478  }
479  virtual bool DoGet (const T *object, W *v) const
480  {
481  v->Set ((object->*m_getter)());
482  return true;
483  }
484  virtual bool HasGetter (void) const
485  {
486  return true;
487  }
488  virtual bool HasSetter (void) const
489  {
490  return true;
491  }
492  void (T::*m_setter)(U); // The class set method pointer, returning void.
493  V (T::*m_getter)(void) const; // The class get functor method pointer.
494  };
495  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
496 }
497 
498 
503 template <typename W, typename T, typename U, typename V>
504 inline
505 Ptr<const AttributeAccessor>
506 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
507  void (T::*setter)(U))
508 {
509  return DoMakeAccessorHelperTwo<W> (setter, getter);
510 }
511 
512 
530 template <typename W, typename T, typename U, typename V>
531 inline
532 Ptr<const AttributeAccessor>
533 DoMakeAccessorHelperTwo (bool (T::*setter)(U),
534  V (T::*getter)(void) const)
535 {
536  /*
537  * AttributeAccessor implementation with class get functor and
538  * set method, returning bool.
539  */
540  class MemberMethod : public AccessorHelper<T,W>
541  {
542  public:
543  /*
544  * Construct from class get functor and set method, returning bool.
545  * \param [in] setter The class set method pointer, returning bool.
546  * \param [in] getter The class get functor method pointer.
547  */
548  MemberMethod (bool (T::*setter)(U),
549  V (T::*getter)(void) const)
550  : AccessorHelper<T,W> (),
551  m_setter (setter),
552  m_getter (getter)
553  {}
554 
555  private:
556  virtual bool DoSet (T *object, const W *v) const
557  {
558  typename AccessorTrait<U>::Result tmp;
559  bool ok = v->GetAccessor (tmp);
560  if (!ok)
561  {
562  return false;
563  }
564  ok = (object->*m_setter)(tmp);
565  return ok;
566  }
567  virtual bool DoGet (const T *object, W *v) const
568  {
569  v->Set ((object->*m_getter)());
570  return true;
571  }
572  virtual bool HasGetter (void) const
573  {
574  return true;
575  }
576  virtual bool HasSetter (void) const
577  {
578  return true;
579  }
580  bool (T::*m_setter)(U); // The class set method pointer, returning bool.
581  V (T::*m_getter)(void) const; // The class get functor method pointer.
582  };
583  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
584 }
585 
586 
591 template <typename W, typename T, typename U, typename V>
592 inline
593 Ptr<const AttributeAccessor>
594 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
595  bool (T::*setter)(U))
596 {
597  return DoMakeAccessorHelperTwo<W> (setter, getter);
598 }
599 
600 
601 template <typename V, typename T1>
602 inline
603 Ptr<const AttributeAccessor>
605 {
606  return DoMakeAccessorHelperOne<V> (a1);
607 }
608 
609 template <typename V, typename T1, typename T2>
610 inline
611 Ptr<const AttributeAccessor>
612 MakeAccessorHelper (T1 a1, T2 a2)
613 {
614  return DoMakeAccessorHelperTwo<V> (a1, a2);
615 }
616 
617 } // namespace ns3
618 
619 #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.