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 
31 namespace ns3 {
32 
33 
69 template <typename V, typename T1>
70 inline
71 Ptr<const AttributeAccessor>
72 MakeAccessorHelper (T1 a1);
73 
74 
115 template <typename V, typename T1, typename T2>
116 inline
117 Ptr<const AttributeAccessor>
118 MakeAccessorHelper (T1 a1, T2 a2);
119 
120 
121 } // namespace ns3
122 
123 
124 /***************************************************************
125  * Implementation of the templates declared above.
126  ***************************************************************/
127 
128 #include "type-traits.h"
129 
130 namespace ns3 {
131 
132 
140 template <typename T>
142 {
145 };
146 
147 
158 template <typename T, typename U>
160 {
161 public:
164  {}
165 
178  virtual bool Set (ObjectBase * object, const AttributeValue & val) const
179  {
180  const U *value = dynamic_cast<const U *> (&val);
181  if (value == 0)
182  {
183  return false;
184  }
185  T *obj = dynamic_cast<T *> (object);
186  if (obj == 0)
187  {
188  return false;
189  }
190  return DoSet (obj, value);
191  }
192 
205  virtual bool Get (const ObjectBase * object, AttributeValue &val) const
206  {
207  U *value = dynamic_cast<U *> (&val);
208  if (value == 0)
209  {
210  return false;
211  }
212  const T *obj = dynamic_cast<const T *> (object);
213  if (obj == 0)
214  {
215  return false;
216  }
217  return DoGet (obj, value);
218  }
219 
220 private:
229  virtual bool DoSet (T *object, const U *v) const = 0;
238  virtual bool DoGet (const T *object, U *v) const = 0;
239 
240 }; // class AccessorHelper
241 
242 
255 template <typename V, typename T, typename U>
256 inline
258 DoMakeAccessorHelperOne (U T::*memberVariable)
259 {
260  /* AttributeAcessor implementation for a class member variable. */
261  class MemberVariable : public AccessorHelper<T,V>
262  {
263  public:
264  /*
265  * Construct from a class data member address.
266  * \param [in] memberVariable The class data member address.
267  */
268  MemberVariable (U T::*memberVariable)
269  : AccessorHelper<T,V> (),
270  m_memberVariable (memberVariable)
271  {}
272 
273  private:
274  virtual bool DoSet (T *object, const V *v) const
275  {
276  typename AccessorTrait<U>::Result tmp;
277  bool ok = v->GetAccessor (tmp);
278  if (!ok)
279  {
280  return false;
281  }
282  (object->*m_memberVariable) = tmp;
283  return true;
284  }
285  virtual bool DoGet (const T *object, V *v) const
286  {
287  v->Set (object->*m_memberVariable);
288  return true;
289  }
290  virtual bool HasGetter (void) const
291  {
292  return true;
293  }
294  virtual bool HasSetter (void) const
295  {
296  return true;
297  }
298 
299  U T::*m_memberVariable; // Address of the class data member.
300  };
301  return Ptr<const AttributeAccessor> (new MemberVariable (memberVariable), false);
302 }
303 
304 
317 template <typename V, typename T, typename U>
318 inline
319 Ptr<const AttributeAccessor>
320 DoMakeAccessorHelperOne (U (T::*getter)(void) const)
321 {
322  /* AttributeAccessor implementation with a class get functor method. */
323  class MemberMethod : public AccessorHelper<T,V>
324  {
325  public:
326  /*
327  * Construct from a class get functor method.
328  * \param [in] getter The class get functor method pointer.
329  */
330  MemberMethod (U (T::*getter)(void) const)
331  : AccessorHelper<T,V> (),
332  m_getter (getter)
333  {}
334 
335  private:
336  virtual bool DoSet ([[maybe_unused]] T *object, [[maybe_unused]] const V *v) const
337  {
338  return false;
339  }
340  virtual bool DoGet (const T *object, V *v) const
341  {
342  v->Set ((object->*m_getter)());
343  return true;
344  }
345  virtual bool HasGetter (void) const
346  {
347  return true;
348  }
349  virtual bool HasSetter (void) const
350  {
351  return false;
352  }
353  U (T::*m_getter)(void) const; // The class get functor method pointer.
354  };
355  return Ptr<const AttributeAccessor> (new MemberMethod (getter), false);
356 }
357 
358 
372 template <typename V, typename T, typename U>
373 inline
374 Ptr<const AttributeAccessor>
375 DoMakeAccessorHelperOne (void (T::*setter)(U))
376 {
377  /* AttributeAccessor implementation with a class set method returning void. */
378  class MemberMethod : public AccessorHelper<T,V>
379  {
380  public:
381  /*
382  * Construct from a class set method.
383  * \param [in] setter The class set method pointer.
384  */
385  MemberMethod (void (T::*setter)(U))
386  : AccessorHelper<T,V> (),
387  m_setter (setter)
388  {}
389 
390  private:
391  virtual bool DoSet (T *object, const V *v) const
392  {
393  typename AccessorTrait<U>::Result tmp;
394  bool ok = v->GetAccessor (tmp);
395  if (!ok)
396  {
397  return false;
398  }
399  (object->*m_setter)(tmp);
400  return true;
401  }
402  virtual bool DoGet ([[maybe_unused]] const T *object, [[maybe_unused]] V *v) const
403  {
404  return false;
405  }
406  virtual bool HasGetter (void) const
407  {
408  return false;
409  }
410  virtual bool HasSetter (void) const
411  {
412  return true;
413  }
414  void (T::*m_setter)(U); // The class set method pointer, returning void.
415  };
416  return Ptr<const AttributeAccessor> (new MemberMethod (setter), false);
417 }
418 
419 
437 template <typename W, typename T, typename U, typename V>
438 inline
439 Ptr<const AttributeAccessor>
440 DoMakeAccessorHelperTwo (void (T::*setter)(U),
441  V (T::*getter)(void) const)
442 {
443  /*
444  * AttributeAccessor implementation with class get functor and set method,
445  * returning void.
446  */
447  class MemberMethod : public AccessorHelper<T,W>
448  {
449  public:
450  /*
451  * Construct from class get functor and set methods.
452  * \param [in] setter The class set method pointer, returning void.
453  * \param [in] getter The class get functor method pointer.
454  */
455  MemberMethod (void (T::*setter)(U),
456  V (T::*getter)(void) const)
457  : AccessorHelper<T,W> (),
458  m_setter (setter),
459  m_getter (getter)
460  {}
461 
462  private:
463  virtual bool DoSet (T *object, const W *v) const
464  {
465  typename AccessorTrait<U>::Result tmp;
466  bool ok = v->GetAccessor (tmp);
467  if (!ok)
468  {
469  return false;
470  }
471  (object->*m_setter)(tmp);
472  return true;
473  }
474  virtual bool DoGet (const T *object, W *v) const
475  {
476  v->Set ((object->*m_getter)());
477  return true;
478  }
479  virtual bool HasGetter (void) const
480  {
481  return true;
482  }
483  virtual bool HasSetter (void) const
484  {
485  return true;
486  }
487  void (T::*m_setter)(U); // The class set method pointer, returning void.
488  V (T::*m_getter)(void) const; // The class get functor method pointer.
489  };
490  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
491 }
492 
493 
498 template <typename W, typename T, typename U, typename V>
499 inline
500 Ptr<const AttributeAccessor>
501 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
502  void (T::*setter)(U))
503 {
504  return DoMakeAccessorHelperTwo<W> (setter, getter);
505 }
506 
507 
525 template <typename W, typename T, typename U, typename V>
526 inline
527 Ptr<const AttributeAccessor>
528 DoMakeAccessorHelperTwo (bool (T::*setter)(U),
529  V (T::*getter)(void) const)
530 {
531  /*
532  * AttributeAccessor implementation with class get functor and
533  * set method, returning bool.
534  */
535  class MemberMethod : public AccessorHelper<T,W>
536  {
537  public:
538  /*
539  * Construct from class get functor and set method, returning bool.
540  * \param [in] setter The class set method pointer, returning bool.
541  * \param [in] getter The class get functor method pointer.
542  */
543  MemberMethod (bool (T::*setter)(U),
544  V (T::*getter)(void) const)
545  : AccessorHelper<T,W> (),
546  m_setter (setter),
547  m_getter (getter)
548  {}
549 
550  private:
551  virtual bool DoSet (T *object, const W *v) const
552  {
553  typename AccessorTrait<U>::Result tmp;
554  bool ok = v->GetAccessor (tmp);
555  if (!ok)
556  {
557  return false;
558  }
559  ok = (object->*m_setter)(tmp);
560  return ok;
561  }
562  virtual bool DoGet (const T *object, W *v) const
563  {
564  v->Set ((object->*m_getter)());
565  return true;
566  }
567  virtual bool HasGetter (void) const
568  {
569  return true;
570  }
571  virtual bool HasSetter (void) const
572  {
573  return true;
574  }
575  bool (T::*m_setter)(U); // The class set method pointer, returning bool.
576  V (T::*m_getter)(void) const; // The class get functor method pointer.
577  };
578  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
579 }
580 
581 
586 template <typename W, typename T, typename U, typename V>
587 inline
588 Ptr<const AttributeAccessor>
589 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
590  bool (T::*setter)(U))
591 {
592  return DoMakeAccessorHelperTwo<W> (setter, getter);
593 }
594 
595 
596 template <typename V, typename T1>
597 inline
598 Ptr<const AttributeAccessor>
600 {
601  return DoMakeAccessorHelperOne<V> (a1);
602 }
603 
604 template <typename V, typename T1, typename T2>
605 inline
606 Ptr<const AttributeAccessor>
607 MakeAccessorHelper (T1 a1, T2 a2)
608 {
609  return DoMakeAccessorHelperTwo<V> (a1, a2);
610 }
611 
612 } // namespace ns3
613 
614 #endif /* ATTRIBUTE_ACCESSOR_HELPER_H */
ns3::AttributeValue, ns3::AttributeAccessor and ns3::AttributeChecker declarations.
Basic functionality for accessing class attributes via class data members, or get functor/set methods...
virtual bool Set(ObjectBase *object, const AttributeValue &val) const
Set the underlying member to the argument AttributeValue.
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Get the value of the underlying member into the AttributeValue.
virtual bool DoGet(const T *object, U *v) const =0
Getter implementation.
virtual bool DoSet(T *object, const U *v) const =0
Setter implementation.
allow setting and getting the value of an attribute.
Definition: attribute.h:115
Hold a value for an Attribute.
Definition: attribute.h:69
Anchor the ns-3 type and attribute system.
Definition: object-base.h:120
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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...
Ptr< const AttributeAccessor > MakeAccessorHelper(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Ptr< const AttributeAccessor > DoMakeAccessorHelperOne(U T::*memberVariable)
MakeAccessorHelper implementation for a class data member.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
The non-const and non-reference type equivalent to T.
TypeTraits< typename TypeTraits< T >::ReferencedType >::NonConstType Result
The non-const, non reference type.
Inspect a type to deduce its features.
Definition: type-traits.h:40
ns3::TypeTraits introspection declaration and template implementation.