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.
 
 
 
 

119 lines
3.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. //! Empty type
  32. struct nil_ { };
  33. //! Type alias for \p _Tp::type. Used to extract return type of metafunctions
  34. template <typename _Tp>
  35. using type_ = typename _Tp::type;
  36. //! integral_constant
  37. //! An Integral Constant is a holder class for a compile-time value of an integral type.
  38. //! Every Integral Constant is also a nullary Metafunction, returning itself.
  39. //! An integral constant object is implicitly convertible to the corresponding
  40. //! run-time value of the wrapped integral type
  41. //! @{
  42. template <typename _Tp, _Tp _v>
  43. struct integral_constant {
  44. using value_type = _Tp;
  45. using type = integral_constant<_Tp, _v>;
  46. constexpr operator value_type() const noexcept {
  47. return value;
  48. }
  49. constexpr value_type operator()() const noexcept {
  50. return value;
  51. }
  52. static constexpr _Tp value = _v;
  53. };
  54. template<typename _Tp, _Tp _v>
  55. constexpr _Tp integral_constant<_Tp, _v>::value;
  56. //! @}
  57. //! Wrappers for basic types
  58. //! @{
  59. //! integral constant
  60. template <typename _Tp, _Tp _v>
  61. using integral_c = integral_constant<_Tp, _v>;
  62. //! bool_ type: integral constant wrapper for bool
  63. template<bool _v>
  64. using bool_ = integral_c<bool, _v>;
  65. using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
  66. using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
  67. //! char_ type: integral constant wrapper for \c char
  68. template<char _v>
  69. using char_ = integral_c<char, _v>;
  70. //! int_ type: integral constant wrapper for \c int
  71. template<int _v>
  72. using int_ = integral_c<int, _v>;
  73. //! long_ type: integral constant wrapper for \c long
  74. template<long _v>
  75. using long_ = integral_c<long, _v>;
  76. //! index_t_ type: integral constant wrapper for \c index_t a.k.a std::size_t
  77. template<index_t _v>
  78. using index_t_ = integral_c<index_t, _v>;
  79. //! size_t_ type: integral constant wrapper for \c size_t a.k.a std::size_t
  80. template<size_t _v>
  81. using size_t_ = integral_constant<size_t, _v>;
  82. //! Computes the size of the type \p _Tp.
  83. //! Complexity \f$ O(1) \f$.
  84. template <typename _Tp>
  85. using sizeof_ = size_t_<sizeof(_Tp)>;
  86. //! Computes the alignment required for any instance of the type \p _Tp.
  87. //! Complexity \f$ O(1) \f$.
  88. template <typename _Tp>
  89. using alignof_ = size_t_<alignof(_Tp)>;
  90. //! @}
  91. //! The last position we can express for indexing
  92. using Npos = size_t_<index_t(-1)>;
  93. }}
  94. //!@}
  95. #endif /* __utl_meta_integralconstant_h__ */