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
32namespace ns3 {
33
37template <typename T>
39{
40private:
42 typedef char yes;
44 typedef struct
45 {
46 char array[2];
47 } no;
48
53 template <typename C> static yes test (typename C::const_iterator *);
58 template <typename C> static no test (...);
59
60public:
62 static const bool value = sizeof (test<T> (0)) == sizeof (yes);
64 typedef T type;
65};
66
70template <typename T>
72{
77 template <typename C> static char (&f (typename std::enable_if<
78 std::is_same<decltype (static_cast<typename C::const_iterator (C::*) () const> (&C::begin)),
79 typename C::const_iterator (C::*) () const>::value, void>::type *))[1];
80
85 template <typename C> static char (&f (...))[2];
86
91 template <typename C> static char (&g (typename std::enable_if<
92 std::is_same<decltype (static_cast<typename C::const_iterator (C::*) () const> (&C::end)),
93 typename C::const_iterator (C::*) () const>::value, void>::type *))[1];
94
99 template <typename C> static char (&g (...))[2];
100
102 static bool const beg_value = sizeof (f<T> (0)) == 1;
104 static bool const end_value = sizeof (g<T> (0)) == 1;
105};
106
116template<typename T>
117struct is_container : std::integral_constant<bool, has_const_iterator<T>::value && has_begin_end<T>::beg_value && has_begin_end<T>::end_value>
118{ };
119
138template <typename V, typename T, template <typename...> class U, typename ...I,
139 typename = typename std::enable_if< ( is_container< U<I...> >::value ), void>::type >
140inline
142DoMakeAccessorHelperOne (U<I...> T::*memberContainer)
143{
144 /* AttributeAcessor implementation for a class member variable. */
145 class MemberContainer : public AccessorHelper<T,V>
146 {
147 public:
148 /*
149 * Construct from a class data member address.
150 * \param [in] memberContainer The class data member address.
151 */
152 MemberContainer (U<I...> T::*memberContainer)
154 m_memberContainer (memberContainer)
155 {}
156 private:
157 virtual bool DoSet (T *object, const V *v) const {
158 // typename AccessorTrait<U<I>::value_type>::Result tmp;
159 // bool ok = v->GetAccessor (tmp);
160 // if (!ok)
161 // {
162 // return false;
163 // }
164 auto src = v->Get ();
165 (object->*m_memberContainer).clear ();
166 std::copy (src.begin (), src.end (), std::inserter ((object->*m_memberContainer), (object->*m_memberContainer).end ()));
167 return true;
168 }
169 virtual bool DoGet (const T *object, V *v) const {
170 v->Set (object->*m_memberContainer);
171 return true;
172 }
173 virtual bool HasGetter (void) const {
174 return true;
175 }
176 virtual bool HasSetter (void) const {
177 return true;
178 }
179
180 U<I...> T::*m_memberContainer; // Address of the class data member.
181 };
182 return Ptr<const AttributeAccessor> (new MemberContainer (memberContainer), false);
183}
184
185} // namespace ns3
186
187#endif // ATTRIBUTE_CONTAINER_ACCESSOR_HELPER_H
Basic functionality for accessing class attributes via class data members, or get functor/set methods...
virtual bool Get(const ObjectBase *object, AttributeValue &val) const
Get the value of the underlying member into the AttributeValue.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
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.
SFINAE compile time check if type T has begin() and end() methods.
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]
Compiled if type T has an end() method.
static bool const beg_value
True if type T has a begin() method.
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]
Compiled if type T has a begin() method.
static bool const end_value
True if type T has an end() method.
SFINAE compile time check if type T has const iterator.
static const bool value
Value of the test - true if type has a const_iterator.
static yes test(typename C::const_iterator *)
Test function, compiled if type has a const_iterator.
static no test(...)
Test function, compiled if type does not have a const_iterator.
T type
Equivalent name of the type T.
Compile time check if type T is a container.