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