Micro template library A library for building device drivers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

143 lines
4.5 KiB

  1. /*!
  2. * \file integralconstant.h
  3. * \brief Template meta-programming integral constant
  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_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 evaluate/extract return type of metafunctions
  41. template <typename _Tp>
  42. using eval = typename _Tp::type;
  43. //! integral_
  44. //! 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_ {
  51. using value_type = _Tp;
  52. using type = integral_<_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_<_Tp, _v>::value;
  63. //! @}
  64. //! Wrappers for basic types
  65. //! @{
  66. //! bool_ type: integral constant wrapper for bool
  67. template<bool _v>
  68. using bool_ = integral_<bool, _v>;
  69. using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
  70. using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
  71. //! int8_ type: integral constant wrapper for \c int8_t
  72. template<int8_t _v>
  73. using int8_ = integral_<int8_t, _v>;
  74. //! uint8_ type: integral constant wrapper for \c uint8_t
  75. template<uint8_t _v>
  76. using uint8_ = integral_<uint8_t, _v>;
  77. //! int16_ type: integral constant wrapper for \c int16_t
  78. template<int16_t _v>
  79. using int16_ = integral_<int16_t, _v>;
  80. //! uint16_ type: integral constant wrapper for \c uint16_t
  81. template<uint16_t _v>
  82. using uint16_ = integral_<uint16_t, _v>;
  83. //! int32_ type: integral constant wrapper for \c int32_t
  84. template<int32_t _v>
  85. using int32_ = integral_<int32_t, _v>;
  86. //! uint32_ type: integral constant wrapper for \c uint32_t
  87. template<uint32_t _v>
  88. using uint32_ = integral_<uint32_t, _v>;
  89. //! char_ type: integral constant wrapper for \c char
  90. template<char _v>
  91. using char_ = integral_<char, _v>;
  92. //! int_ type: integral constant wrapper for \c int
  93. template<int _v>
  94. using int_ = integral_<int, _v>;
  95. //! long_ type: integral constant wrapper for \c long
  96. template<long _v>
  97. using long_ = integral_<long, _v>;
  98. //! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
  99. template<index_t _v>
  100. using index_ = integral_<index_t, _v>;
  101. //! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
  102. template<size_t _v>
  103. using size_ = integral_<size_t, _v>;
  104. //! Computes the size of the type \p _Tp.
  105. //! Complexity \f$ O(1) \f$.
  106. template <typename _Tp>
  107. using sizeof_ = size_<sizeof(_Tp)>;
  108. //! Computes the alignment required for any instance of the type \p _Tp.
  109. //! Complexity \f$ O(1) \f$.
  110. template <typename _Tp>
  111. using alignof_ = size_<alignof(_Tp)>;
  112. //! @}
  113. //! The last position we can express for indexing
  114. using Npos = size_<index_t(-1)>;
  115. }}
  116. //!@}
  117. #endif /* __utl_meta_integralconstant_h__ */