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.
 
 
 
 

147 lignes
4.7 KiB

  1. /*!
  2. * \file integralconstant.h
  3. * \brief Template meta-programming integral constant
  4. *
  5. * Copyright (C) 2018 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_integralconstant_h__
  21. #define __utl_meta_integralconstant_h__
  22. #include <utl/core/impl.h>
  23. /*!
  24. * \ingroup meta
  25. * \defgroup integral
  26. * integral constant support header
  27. */
  28. //! @{
  29. namespace utl {
  30. namespace meta {
  31. /*!
  32. * Empty type
  33. * utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
  34. * Each time someone applies ::type to it, he gets back nil_. This way we can prevent
  35. * a lot of compilation errors in a wrong meta:: handling.
  36. */
  37. struct nil_ {
  38. using type = nil_;
  39. };
  40. //! Type alias for \p _Tp::type. Used to extract return type of metafunctions
  41. template <typename _Tp>
  42. using type_ = typename _Tp::type;
  43. //! integral_constant
  44. //! An Integral Constant is a holder class for a compile-time value of an integral type.
  45. //! Every Integral Constant is also a null-ary Metafunction, returning itself.
  46. //! An integral constant object is implicitly convertible to the corresponding
  47. //! run-time value of the wrapped integral type
  48. //! @{
  49. template <typename _Tp, _Tp _v>
  50. struct integral_constant {
  51. using value_type = _Tp;
  52. using type = integral_constant<_Tp, _v>;
  53. constexpr operator value_type() const noexcept {
  54. return value;
  55. }
  56. constexpr value_type operator()() const noexcept {
  57. return value;
  58. }
  59. static constexpr _Tp value = _v;
  60. };
  61. template<typename _Tp, _Tp _v>
  62. constexpr _Tp integral_constant<_Tp, _v>::value;
  63. //! @}
  64. //! Wrappers for basic types
  65. //! @{
  66. //! integral constant
  67. template <typename _Tp, _Tp _v>
  68. using integral_c = integral_constant<_Tp, _v>;
  69. //! bool_ type: integral constant wrapper for bool
  70. template<bool _v>
  71. using bool_ = integral_c<bool, _v>;
  72. using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
  73. using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
  74. //! int8_ type: integral constant wrapper for \c int8_t
  75. template<int8_t _v>
  76. using int8_ = integral_c<int8_t, _v>;
  77. //! uint8_ type: integral constant wrapper for \c uint8_t
  78. template<uint8_t _v>
  79. using uint8_ = integral_c<uint8_t, _v>;
  80. //! int16_ type: integral constant wrapper for \c int16_t
  81. template<int16_t _v>
  82. using int16_ = integral_c<int16_t, _v>;
  83. //! uint16_ type: integral constant wrapper for \c uint16_t
  84. template<uint16_t _v>
  85. using uint16_ = integral_c<uint16_t, _v>;
  86. //! int32_ type: integral constant wrapper for \c int32_t
  87. template<int32_t _v>
  88. using int32_ = integral_c<int32_t, _v>;
  89. //! uint32_ type: integral constant wrapper for \c uint32_t
  90. template<uint32_t _v>
  91. using uint32_ = integral_c<uint32_t, _v>;
  92. //! char_ type: integral constant wrapper for \c char
  93. template<char _v>
  94. using char_ = integral_c<char, _v>;
  95. //! int_ type: integral constant wrapper for \c int
  96. template<int _v>
  97. using int_ = integral_c<int, _v>;
  98. //! long_ type: integral constant wrapper for \c long
  99. template<long _v>
  100. using long_ = integral_c<long, _v>;
  101. //! index_t_ type: integral constant wrapper for \c index_t a.k.a std::size_t
  102. template<index_t _v>
  103. using index_t_ = integral_c<index_t, _v>;
  104. //! size_t_ type: integral constant wrapper for \c size_t a.k.a std::size_t
  105. template<size_t _v>
  106. using size_t_ = integral_constant<size_t, _v>;
  107. //! Computes the size of the type \p _Tp.
  108. //! Complexity \f$ O(1) \f$.
  109. template <typename _Tp>
  110. using sizeof_ = size_t_<sizeof(_Tp)>;
  111. //! Computes the alignment required for any instance of the type \p _Tp.
  112. //! Complexity \f$ O(1) \f$.
  113. template <typename _Tp>
  114. using alignof_ = size_t_<alignof(_Tp)>;
  115. //! @}
  116. //! The last position we can express for indexing
  117. using Npos = size_t_<index_t(-1)>;
  118. }}
  119. //!@}
  120. #endif /* __utl_meta_integralconstant_h__ */