Micro template library A library for building device drivers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

228 lignes
6.4 KiB

  1. /*!
  2. * \file operations.h
  3. * \brief Integral constant operations and logical operations
  4. *
  5. * Copyright (C) 2018-2019 Christos Choutouridis
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef __utl_meta_operations_h__
  21. #define __utl_meta_operations_h__
  22. #include <utl/core/impl.h>
  23. #include <utl/meta/selection.h>
  24. /*!
  25. * \ingroup meta
  26. * \defgroup logic
  27. * logic operators and type relations support
  28. */
  29. //! @{
  30. namespace utl {
  31. namespace meta{
  32. /*!
  33. * Logical relation for types
  34. */
  35. //! @{
  36. //! Negate the *bool* constant parameter
  37. template <bool B>
  38. using not_c = bool_<!B>;
  39. //! not
  40. template<typename _Tp>
  41. using not_ = not_c<_Tp::type::value>;
  42. //! OR implementation
  43. //! @{
  44. namespace detail {
  45. template<typename...> struct _or_;
  46. template<>
  47. struct _or_<> : false_ { };
  48. template<typename _T1>
  49. struct _or_<_T1> : _T1 { };
  50. template<typename _T1, typename _T2>
  51. struct _or_ <_T1, _T2>
  52. : if_<_T1, _T1, _T2> { };
  53. template<typename _T1, typename _T2, typename _T3, typename... _Tn>
  54. struct _or_<_T1, _T2, _T3, _Tn...>
  55. : if_<_T1, _T1, _or_<_T2, _T3, _Tn...>> { };
  56. }
  57. template <typename... _Ts>
  58. using or_ = eval<detail::_or_<_Ts...>>;
  59. //! @}
  60. //! AND implementation
  61. //! @{
  62. namespace detail {
  63. template<typename...> struct _and_;
  64. template<>
  65. struct _and_<>
  66. : true_ { };
  67. template<typename _T1>
  68. struct _and_ <_T1>
  69. : _T1 { };
  70. template<typename _T1, typename _T2>
  71. struct _and_<_T1, _T2>
  72. : if_<_T1, _T2, _T1> { };
  73. template<typename _T1, typename _T2, typename _T3, typename... _Tn>
  74. struct _and_<_T1, _T2, _T3, _Tn...>
  75. : if_<_T1, _and_<_T2, _T3, _Tn...>, _T1> { };
  76. }
  77. template <typename... _Ts>
  78. using and_ = eval<detail::_and_<_Ts...>>;
  79. //! @}
  80. //! same
  81. //! @{
  82. template<typename _T1, typename _T2>
  83. struct same_ : false_ { };
  84. template<typename _Tp>
  85. struct same_ <_Tp, _Tp> : true_ { };
  86. //! @}
  87. //! not same
  88. //! @{
  89. template<typename _T1, typename _T2>
  90. using not_same_ = not_<eval<same_<_T1, _T2>>>;
  91. //! @}
  92. //! @}
  93. }}
  94. //! @}
  95. /*!
  96. * \ingroup meta
  97. * \defgroup integral operators
  98. * Type arithmetic and operators
  99. */
  100. //! @{
  101. namespace utl {
  102. namespace meta {
  103. /*!
  104. * Math operations
  105. * requires IntegralConstant(_Tp)
  106. */
  107. //! @{
  108. //! Negation
  109. template <typename _Tp>
  110. using negate = integral_<decltype(-_Tp()), -_Tp()>;
  111. //! Addition
  112. template <typename _Tp1, typename _Tp2>
  113. using add = integral_<
  114. decltype(_Tp1() + _Tp2()),
  115. _Tp1() + _Tp2()
  116. >;
  117. //! Multiplication
  118. template <typename _Tp1, typename _Tp2>
  119. using mult = integral_<
  120. decltype(_Tp2() * _Tp2()),
  121. _Tp1() * _Tp2()
  122. >;
  123. //! Division
  124. template <typename _Tp1, typename _Tp2>
  125. using divide = integral_<
  126. decltype(_Tp2() / _Tp2()),
  127. _Tp1() / _Tp2()
  128. >;
  129. //! Modulo
  130. template <typename _Tp1, typename _Tp2>
  131. using modulo = integral_<
  132. decltype(_Tp1() % _Tp2()),
  133. _Tp1() % _Tp2()
  134. >;
  135. //! Substruction
  136. template <typename _Tp1, typename _Tp2>
  137. using sub = add<_Tp1, negate<_Tp2>>;
  138. //! Increase
  139. template <typename _Tp>
  140. using inc = add<_Tp, int_<1>>;
  141. //! decrease
  142. template <typename _Tp>
  143. using dec = add<_Tp, int_<-1>>;
  144. //! @}
  145. /*!
  146. * Comparison operations
  147. * requires IntegralConstant(_Tp)
  148. */
  149. //! @{
  150. //! \return a true-valued Integral Constant if _Tp1 and _Tp2 are equal.
  151. template <typename _Tp1, typename _Tp2> using comp_eq = bool_<_Tp1() == _Tp2()>;
  152. //! \return a true-valued Integral Constant if _Tp1 is less than _Tp2.
  153. template <typename _Tp1, typename _Tp2> using comp_lt = bool_<(_Tp1() < _Tp2())>;
  154. //! Not equal
  155. template <typename _Tp1, typename _Tp2> using comp_ne = not_<comp_eq<_Tp1, _Tp2>>;
  156. //! Greater than
  157. template <typename _Tp1, typename _Tp2> using comp_gt = comp_lt <_Tp2, _Tp1>;
  158. //! Less or equal
  159. template <typename _Tp1, typename _Tp2> using comp_le = not_<comp_lt<_Tp2, _Tp1>>;
  160. //! Greater or equal
  161. template <typename _Tp1, typename _Tp2> using comp_ge = not_<comp_lt<_Tp1, _Tp2>>;
  162. //! @}
  163. /*!
  164. * Bitwise operations
  165. * requires IntegralConstant(_Tp)
  166. */
  167. //! @{
  168. //! \return bitwise not (~) operation of its argument.
  169. template <typename _T> using bitnot_ = integral_<typename _T::value_type, (typename _T::value_type)(~_T())>;
  170. //! \return bitwise and (&) operation of its arguments
  171. template <typename _Tp1, typename _Tp2>
  172. using bitand_ = integral_<decltype(_Tp1() & _Tp2()), _Tp1() & _Tp2()>;
  173. //! \return bitwise or (|) operation of its arguments.
  174. template <typename _Tp1, typename _Tp2>
  175. using bitor_ = integral_<decltype(_Tp1() | _Tp2()), _Tp1() | _Tp2()>;
  176. //! \return bitwise xor (^) operation of its arguments.
  177. template <typename _Tp1, typename _Tp2>
  178. using bitxor_ = integral_<decltype(_Tp1() ^ _Tp2()), _Tp1() ^ _Tp2()>;
  179. //! \return the result of bitwise shift left (<<) operation on _Tp.
  180. template <typename _Tp, typename shift>
  181. using shift_left = integral_<typename _Tp::value_type, (typename _Tp::value_type)(_Tp() << shift())>;
  182. //! \return the result of bitwise shift right (>>) operation on _Tp.
  183. template <typename _Tp, typename shift>
  184. using shift_right = integral_<typename _Tp::value_type, (typename _Tp::value_type)(_Tp() >> shift())>;
  185. //! @}
  186. }}
  187. //!@}
  188. #endif /* __utl_meta_operations_h__ */