Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | ////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // (C) Copyright Ion Gaztanaga 2012-2012. |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // (See accompanying file LICENSE_1_0.txt or copy at |
| 6 | // http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // See http://www.boost.org/libs/move for documentation. |
| 9 | // |
| 10 | ////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | //! \file |
| 13 | |
| 14 | #ifndef BOOST_MOVE_DETAIL_META_UTILS_HPP |
| 15 | #define BOOST_MOVE_DETAIL_META_UTILS_HPP |
| 16 | |
| 17 | #include <ndnboost/move/detail/config_begin.hpp> |
| 18 | |
| 19 | //Small meta-typetraits to support move |
| 20 | |
| 21 | namespace ndnboost { |
| 22 | namespace move_detail { |
| 23 | |
| 24 | //if_ |
| 25 | template<bool C, typename T1, typename T2> |
| 26 | struct if_c |
| 27 | { |
| 28 | typedef T1 type; |
| 29 | }; |
| 30 | |
| 31 | template<typename T1, typename T2> |
| 32 | struct if_c<false,T1,T2> |
| 33 | { |
| 34 | typedef T2 type; |
| 35 | }; |
| 36 | |
| 37 | template<typename T1, typename T2, typename T3> |
| 38 | struct if_ |
| 39 | { |
| 40 | typedef typename if_c<0 != T1::value, T2, T3>::type type; |
| 41 | }; |
| 42 | |
| 43 | //enable_if_ |
| 44 | template <bool B, class T = void> |
| 45 | struct enable_if_c |
| 46 | { |
| 47 | typedef T type; |
| 48 | }; |
| 49 | |
| 50 | template <class T> |
| 51 | struct enable_if_c<false, T> {}; |
| 52 | |
| 53 | template <class Cond, class T = void> |
| 54 | struct enable_if : public enable_if_c<Cond::value, T> {}; |
| 55 | |
| 56 | template <class Cond, class T = void> |
| 57 | struct disable_if : public enable_if_c<!Cond::value, T> {}; |
| 58 | |
| 59 | //integral_constant |
| 60 | template<class T, T v> |
| 61 | struct integral_constant |
| 62 | { |
| 63 | static const T value = v; |
| 64 | typedef T value_type; |
| 65 | typedef integral_constant<T, v> type; |
| 66 | }; |
| 67 | |
| 68 | //identity |
| 69 | template <class T> |
| 70 | struct identity |
| 71 | { |
| 72 | typedef T type; |
| 73 | }; |
| 74 | |
| 75 | //is_convertible |
| 76 | template <class T, class U> |
| 77 | class is_convertible |
| 78 | { |
| 79 | typedef char true_t; |
| 80 | class false_t { char dummy[2]; }; |
| 81 | static true_t dispatch(U); |
| 82 | static false_t dispatch(...); |
| 83 | static T &trigger(); |
| 84 | public: |
| 85 | enum { value = sizeof(dispatch(trigger())) == sizeof(true_t) }; |
| 86 | }; |
| 87 | |
| 88 | //and_ not_ |
| 89 | template <typename Condition1, typename Condition2, typename Condition3 = integral_constant<bool, true> > |
| 90 | struct and_ |
| 91 | : public integral_constant<bool, Condition1::value && Condition2::value && Condition3::value> |
| 92 | {}; |
| 93 | |
| 94 | template <typename Boolean> |
| 95 | struct not_ |
| 96 | : public integral_constant<bool, !Boolean::value> |
| 97 | {}; |
| 98 | |
| 99 | //is_lvalue_reference |
| 100 | template<class T> |
| 101 | struct is_lvalue_reference |
| 102 | : public integral_constant<bool, false> |
| 103 | {}; |
| 104 | |
| 105 | template<class T> |
| 106 | struct is_lvalue_reference<T&> |
| 107 | : public integral_constant<bool, true> |
| 108 | {}; |
| 109 | |
| 110 | template<class T> |
| 111 | struct is_class_or_union |
| 112 | { |
| 113 | struct twochar { char _[2]; }; |
| 114 | template <class U> |
| 115 | static char is_class_or_union_tester(void(U::*)(void)); |
| 116 | template <class U> |
| 117 | static twochar is_class_or_union_tester(...); |
| 118 | static const bool value = sizeof(is_class_or_union_tester<T>(0)) == sizeof(char); |
| 119 | }; |
| 120 | |
| 121 | struct empty{}; |
| 122 | |
| 123 | //addressof |
| 124 | template<class T> struct addr_impl_ref |
| 125 | { |
| 126 | T & v_; |
| 127 | inline addr_impl_ref( T & v ): v_( v ) {} |
| 128 | inline operator T& () const { return v_; } |
| 129 | |
| 130 | private: |
| 131 | addr_impl_ref & operator=(const addr_impl_ref &); |
| 132 | }; |
| 133 | |
| 134 | template<class T> struct addressof_impl |
| 135 | { |
| 136 | static inline T * f( T & v, long ) |
| 137 | { |
| 138 | return reinterpret_cast<T*>( |
| 139 | &const_cast<char&>(reinterpret_cast<const volatile char &>(v))); |
| 140 | } |
| 141 | |
| 142 | static inline T * f( T * v, int ) |
| 143 | { return v; } |
| 144 | }; |
| 145 | |
| 146 | template<class T> |
| 147 | inline T * addressof( T & v ) |
| 148 | { |
| 149 | return ::ndnboost::move_detail::addressof_impl<T>::f |
| 150 | ( ::ndnboost::move_detail::addr_impl_ref<T>( v ), 0 ); |
| 151 | } |
| 152 | |
| 153 | } //namespace move_detail { |
| 154 | } //namespace ndnboost { |
| 155 | |
| 156 | #include <ndnboost/move/detail/config_end.hpp> |
| 157 | |
| 158 | #endif //#ifndef BOOST_MOVE_DETAIL_META_UTILS_HPP |