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 
177  virtual bool Set (ObjectBase * object, const AttributeValue & val) const {
178  const U *value = dynamic_cast<const U *> (&val);
179  if (value == 0)
180  {
181  return false;
182  }
183  T *obj = dynamic_cast<T *> (object);
184  if (obj == 0)
185  {
186  return false;
187  }
188  return DoSet (obj, value);
189  }
190 
203  virtual bool Get (const ObjectBase * object, AttributeValue &val) const {
204  U *value = dynamic_cast<U *> (&val);
205  if (value == 0)
206  {
207  return false;
208  }
209  const T *obj = dynamic_cast<const T *> (object);
210  if (obj == 0)
211  {
212  return false;
213  }
214  return DoGet (obj, value);
215  }
216 
217 private:
226  virtual bool DoSet (T *object, const U *v) const = 0;
235  virtual bool DoGet (const T *object, U *v) const = 0;
236 
237 }; // class AccessorHelper
238 
239 
252 template <typename V, typename T, typename U>
253 inline
254 Ptr<const AttributeAccessor>
255 DoMakeAccessorHelperOne (U T::*memberVariable)
256 {
257  /* AttributeAcessor implementation for a class member variable. */
258  class MemberVariable : public AccessorHelper<T,V>
259  {
260 public:
261  /*
262  * Construct from a class data member address.
263  * \param [in] memberVariable The class data member address.
264  */
265  MemberVariable (U T::*memberVariable)
266  : AccessorHelper<T,V> (),
267  m_memberVariable (memberVariable)
268  {}
269 private:
270  virtual bool DoSet (T *object, const V *v) const {
271  typename AccessorTrait<U>::Result tmp;
272  bool ok = v->GetAccessor (tmp);
273  if (!ok)
274  {
275  return false;
276  }
277  (object->*m_memberVariable) = tmp;
278  return true;
279  }
280  virtual bool DoGet (const T *object, V *v) const {
281  v->Set (object->*m_memberVariable);
282  return true;
283  }
284  virtual bool HasGetter (void) const {
285  return true;
286  }
287  virtual bool HasSetter (void) const {
288  return true;
289  }
290 
291  U T::*m_memberVariable; // Address of the class data member.
292  };
293  return Ptr<const AttributeAccessor> (new MemberVariable (memberVariable), false);
294 }
295 
296 
309 template <typename V, typename T, typename U>
310 inline
311 Ptr<const AttributeAccessor>
312 DoMakeAccessorHelperOne (U (T::*getter)(void) const)
313 {
314  /* AttributeAccessor implementation with a class get functor method. */
315  class MemberMethod : public AccessorHelper<T,V>
316  {
317 public:
318  /*
319  * Construct from a class get functor method.
320  * \param [in] getter The class get functor method pointer.
321  */
322  MemberMethod (U (T::*getter)(void) const)
323  : AccessorHelper<T,V> (),
324  m_getter (getter)
325  {}
326 private:
327  virtual bool DoSet (T *object, const V *v) const {
328  return false;
329  }
330  virtual bool DoGet (const T *object, V *v) const {
331  v->Set ((object->*m_getter)());
332  return true;
333  }
334  virtual bool HasGetter (void) const {
335  return true;
336  }
337  virtual bool HasSetter (void) const {
338  return false;
339  }
340  U (T::*m_getter)(void) const; // The class get functor method pointer.
341  };
342  return Ptr<const AttributeAccessor> (new MemberMethod (getter), false);
343 }
344 
345 
359 template <typename V, typename T, typename U>
360 inline
361 Ptr<const AttributeAccessor>
362 DoMakeAccessorHelperOne (void (T::*setter)(U))
363 {
364  /* AttributeAccessor implemenation with a class set method returning void. */
365  class MemberMethod : public AccessorHelper<T,V>
366  {
367 public:
368  /*
369  * Construct from a class set method.
370  * \param [in] setter The class set method pointer.
371  */
372  MemberMethod (void (T::*setter)(U))
373  : AccessorHelper<T,V> (),
374  m_setter (setter)
375  {}
376 private:
377  virtual bool DoSet (T *object, const V *v) const {
378  typename AccessorTrait<U>::Result tmp;
379  bool ok = v->GetAccessor (tmp);
380  if (!ok)
381  {
382  return false;
383  }
384  (object->*m_setter)(tmp);
385  return true;
386  }
387  virtual bool DoGet (const T *object, V *v) const {
388  return false;
389  }
390  virtual bool HasGetter (void) const {
391  return false;
392  }
393  virtual bool HasSetter (void) const {
394  return true;
395  }
396  void (T::*m_setter)(U); // The class set method pointer, returning void.
397  };
398  return Ptr<const AttributeAccessor> (new MemberMethod (setter), false);
399 }
400 
401 
419 template <typename W, typename T, typename U, typename V>
420 inline
421 Ptr<const AttributeAccessor>
422 DoMakeAccessorHelperTwo (void (T::*setter)(U),
423  V (T::*getter)(void) const)
424 {
425  /*
426  * AttributeAccessor implemenation with class get functor and set method,
427  * returning void.
428  */
429  class MemberMethod : public AccessorHelper<T,W>
430  {
431 public:
432  /*
433  * Construct from class get functor and set methods.
434  * \param [in] setter The class set method pointer, returning void.
435  * \param [in] getter The class get functor method pointer.
436  */
437  MemberMethod (void (T::*setter)(U),
438  V (T::*getter)(void) const)
439  : AccessorHelper<T,W> (),
440  m_setter (setter),
441  m_getter (getter)
442  {}
443 private:
444  virtual bool DoSet (T *object, const W *v) const {
445  typename AccessorTrait<U>::Result tmp;
446  bool ok = v->GetAccessor (tmp);
447  if (!ok)
448  {
449  return false;
450  }
451  (object->*m_setter)(tmp);
452  return true;
453  }
454  virtual bool DoGet (const T *object, W *v) const {
455  v->Set ((object->*m_getter)());
456  return true;
457  }
458  virtual bool HasGetter (void) const {
459  return true;
460  }
461  virtual bool HasSetter (void) const {
462  return true;
463  }
464  void (T::*m_setter)(U); // The class set method pointer, returning void.
465  V (T::*m_getter)(void) const; // The class get functor method pointer.
466  };
467  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
468 }
469 
470 
475 template <typename W, typename T, typename U, typename V>
476 inline
477 Ptr<const AttributeAccessor>
478 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
479  void (T::*setter)(U))
480 {
481  return DoMakeAccessorHelperTwo<W> (setter, getter);
482 }
483 
484 
502 template <typename W, typename T, typename U, typename V>
503 inline
504 Ptr<const AttributeAccessor>
505 DoMakeAccessorHelperTwo (bool (T::*setter)(U),
506  V (T::*getter)(void) const)
507 {
508  /*
509  * AttributeAccessor implemenation with class get functor and
510  * set method, returning bool.
511  */
512  class MemberMethod : public AccessorHelper<T,W>
513  {
514 public:
515  /*
516  * Construct from class get functor and set method, returning bool.
517  * \param [in] setter The class set method pointer, returning bool.
518  * \param [in] getter The class get functor method pointer.
519  */
520  MemberMethod (bool (T::*setter)(U),
521  V (T::*getter)(void) const)
522  : AccessorHelper<T,W> (),
523  m_setter (setter),
524  m_getter (getter)
525  {}
526 private:
527  virtual bool DoSet (T *object, const W *v) const {
528  typename AccessorTrait<U>::Result tmp;
529  bool ok = v->GetAccessor (tmp);
530  if (!ok)
531  {
532  return false;
533  }
534  ok = (object->*m_setter)(tmp);
535  return ok;
536  }
537  virtual bool DoGet (const T *object, W *v) const {
538  v->Set ((object->*m_getter)());
539  return true;
540  }
541  virtual bool HasGetter (void) const {
542  return true;
543  }
544  virtual bool HasSetter (void) const {
545  return true;
546  }
547  bool (T::*m_setter)(U); // The class set method pointer, returning bool.
548  V (T::*m_getter)(void) const; // The class get functor method pointer.
549  };
550  return Ptr<const AttributeAccessor> (new MemberMethod (setter, getter), false);
551 }
552 
553 
558 template <typename W, typename T, typename U, typename V>
559 inline
560 Ptr<const AttributeAccessor>
561 DoMakeAccessorHelperTwo (V (T::*getter)(void) const,
562  bool (T::*setter)(U))
563 {
564  return DoMakeAccessorHelperTwo<W> (setter, getter);
565 }
566 
567 
568 template <typename V, typename T1>
569 inline
570 Ptr<const AttributeAccessor>
572 {
573  return DoMakeAccessorHelperOne<V> (a1);
574 }
575 
576 template <typename V, typename T1, typename T2>
577 inline
578 Ptr<const AttributeAccessor>
579 MakeAccessorHelper (T1 a1, T2 a2)
580 {
581  return DoMakeAccessorHelperTwo<V> (a1, a2);
582 }
583 
584 } // namespace ns3
585 
586 #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...
Anchor the ns-3 type and attribute system.
Definition: object-base.h:68
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Get the value of the underlying member into the AttributeValue.
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.
virtual bool Set(ObjectBase *object, const AttributeValue &val) const
Set the underlying member to the argument AttributeValue.
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.
Inspect a type to deduce its features.
Definition: type-traits.h:37
Ptr< const AttributeAccessor > DoMakeAccessorHelperOne(U T::*memberVariable)
MakeAccessorHelper implementation for a class data member.
TypeTraits introspection template.
virtual bool DoGet(const T *object, U *v) const =0
Getter implementation.