A Discrete-Event Network Simulator
API
attribute-container-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) 2018 Caliola Engineering, LLC.
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  * Author: Jared Dulmage <jared.dulmage@caliola.com>
19  */
20 
21 #ifndef ATTRIBUTE_CONTAINER_ACCESSOR_HELPER_H
22 #define ATTRIBUTE_CONTAINER_ACCESSOR_HELPER_H
23 
24 #include "attribute-container.h"
25 
26 #include <ns3/attribute-helper.h>
27 
28 
29 #include <type_traits>
30 #include <list>
31 
32 namespace ns3 {
33 
37 template<typename T>
39 {
40 private:
41  typedef char yes;
42  typedef struct { char array[2]; } no;
43 
44  template<typename C> static yes test(typename C::const_iterator*);
45  template<typename C> static no test(...);
46 public:
47  static const bool value = sizeof(test<T>(0)) == sizeof(yes);
48  typedef T type;
49 };
50 
54 template <typename T>
56 {
57  template<typename C> static char (&f(typename std::enable_if<
58  std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::begin)),
59  typename C::const_iterator(C::*)() const>::value, void>::type*))[1];
60 
61  template<typename C> static char (&f(...))[2];
62 
63  template<typename C> static char (&g(typename std::enable_if<
64  std::is_same<decltype(static_cast<typename C::const_iterator (C::*)() const>(&C::end)),
65  typename C::const_iterator(C::*)() const>::value, void>::type*))[1];
66 
67  template<typename C> static char (&g(...))[2];
68 
69  static bool const beg_value = sizeof(f<T>(0)) == 1;
70  static bool const end_value = sizeof(g<T>(0)) == 1;
71 };
72 
82 template<typename T>
83 struct is_container : std::integral_constant<bool, has_const_iterator<T>::value && has_begin_end<T>::beg_value && has_begin_end<T>::end_value>
84 { };
85 
104 template <typename V, typename T, template <typename...> class U, typename ...I,
105  typename = typename std::enable_if< ( is_container< U<I...> >::value ), void>::type >
106 inline
108 DoMakeAccessorHelperOne (U<I...> T::*memberContainer)
109 {
110  /* AttributeAcessor implementation for a class member variable. */
111  class MemberContainer : public AccessorHelper<T,V>
112  {
113  public:
114  /*
115  * Construct from a class data member address.
116  * \param [in] memberContainer The class data member address.
117  */
118  MemberContainer (U<I...> T::*memberContainer)
119  : AccessorHelper<T,V> (),
120  m_memberContainer (memberContainer)
121  {}
122  private:
123  virtual bool DoSet (T *object, const V *v) const {
124  // typename AccessorTrait<U<I>::value_type>::Result tmp;
125  // bool ok = v->GetAccessor (tmp);
126  // if (!ok)
127  // {
128  // return false;
129  // }
130  auto src = v->Get ();
131  (object->*m_memberContainer).clear ();
132  std::copy (src.begin (), src.end (), std::inserter ((object->*m_memberContainer), (object->*m_memberContainer).end ()));
133  return true;
134  }
135  virtual bool DoGet (const T *object, V *v) const {
136  v->Set (object->*m_memberContainer);
137  return true;
138  }
139  virtual bool HasGetter (void) const {
140  return true;
141  }
142  virtual bool HasSetter (void) const {
143  return true;
144  }
145 
146  U<I...> T::*m_memberContainer; // Address of the class data member.
147  };
148  return Ptr<const AttributeAccessor> (new MemberContainer (memberContainer), false);
149 }
150 
151 } // namespace ns3
152 
153 #endif // ATTRIBUTE_CONTAINER_ACCESSOR_HELPER_H
Compile time check if type T has const iterator.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
static yes test(typename C::const_iterator *)
static char(& g(typename std::enable_if< std::is_same< decltype(static_cast< typename C::const_iterator(C::*)() const >(&C::end)), typename C::const_iterator(C::*)() const >::value, void >::type *))[1]
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Basic functionality for accessing class attributes via class data members, or get functor/set methods...
Compile time check if type T is a container.
static char(& f(typename std::enable_if< std::is_same< decltype(static_cast< typename C::const_iterator(C::*)() const >(&C::begin)), typename C::const_iterator(C::*)() const >::value, void >::type *))[1]
Ptr< const AttributeAccessor > DoMakeAccessorHelperOne(U T::*memberVariable)
MakeAccessorHelper implementation for a class data member.
Compile time check if type T has begin() and end() methods.
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Get the value of the underlying member into the AttributeValue.