utl/include/utl/meta/integral.h

147 lines
4.7 KiB
C++

/*!
* \file integralconstant.h
* \brief Template meta-programming integral constant
*
* Copyright (C) 2018 Christos Choutouridis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __utl_meta_integralconstant_h__
#define __utl_meta_integralconstant_h__
#include <utl/core/impl.h>
/*!
* \ingroup meta
* \defgroup integral
* integral constant support header
*/
//! @{
namespace utl {
namespace meta {
/*!
* Empty type
* utl::meta's nil type is not pure nil. It's a recursive "de-referencable nil.
* Each time someone applies ::type to it, he gets back nil_. This way we can prevent
* a lot of compilation errors in a wrong meta:: handling.
*/
struct nil_ {
using type = nil_;
};
//! Type alias for \p _Tp::type. Used to extract return type of metafunctions
template <typename _Tp>
using type_ = typename _Tp::type;
//! integral_constant
//! An Integral Constant is a holder class for a compile-time value of an integral type.
//! Every Integral Constant is also a null-ary Metafunction, returning itself.
//! An integral constant object is implicitly convertible to the corresponding
//! run-time value of the wrapped integral type
//! @{
template <typename _Tp, _Tp _v>
struct integral_constant {
using value_type = _Tp;
using type = integral_constant<_Tp, _v>;
constexpr operator value_type() const noexcept {
return value;
}
constexpr value_type operator()() const noexcept {
return value;
}
static constexpr _Tp value = _v;
};
template<typename _Tp, _Tp _v>
constexpr _Tp integral_constant<_Tp, _v>::value;
//! @}
//! Wrappers for basic types
//! @{
//! integral constant
template <typename _Tp, _Tp _v>
using integral_c = integral_constant<_Tp, _v>;
//! bool_ type: integral constant wrapper for bool
template<bool _v>
using bool_ = integral_c<bool, _v>;
using true_ = bool_<true>; //!< The type used as a compile-time boolean with true value.
using false_ = bool_<false>; //!< The type used as a compile-time boolean with false value.
//! int8_ type: integral constant wrapper for \c int8_t
template<int8_t _v>
using int8_ = integral_c<int8_t, _v>;
//! uint8_ type: integral constant wrapper for \c uint8_t
template<uint8_t _v>
using uint8_ = integral_c<uint8_t, _v>;
//! int16_ type: integral constant wrapper for \c int16_t
template<int16_t _v>
using int16_ = integral_c<int16_t, _v>;
//! uint16_ type: integral constant wrapper for \c uint16_t
template<uint16_t _v>
using uint16_ = integral_c<uint16_t, _v>;
//! int32_ type: integral constant wrapper for \c int32_t
template<int32_t _v>
using int32_ = integral_c<int32_t, _v>;
//! uint32_ type: integral constant wrapper for \c uint32_t
template<uint32_t _v>
using uint32_ = integral_c<uint32_t, _v>;
//! char_ type: integral constant wrapper for \c char
template<char _v>
using char_ = integral_c<char, _v>;
//! int_ type: integral constant wrapper for \c int
template<int _v>
using int_ = integral_c<int, _v>;
//! long_ type: integral constant wrapper for \c long
template<long _v>
using long_ = integral_c<long, _v>;
//! index_t_ type: integral constant wrapper for \c index_t a.k.a std::size_t
template<index_t _v>
using index_t_ = integral_c<index_t, _v>;
//! size_t_ type: integral constant wrapper for \c size_t a.k.a std::size_t
template<size_t _v>
using size_t_ = integral_constant<size_t, _v>;
//! Computes the size of the type \p _Tp.
//! Complexity \f$ O(1) \f$.
template <typename _Tp>
using sizeof_ = size_t_<sizeof(_Tp)>;
//! Computes the alignment required for any instance of the type \p _Tp.
//! Complexity \f$ O(1) \f$.
template <typename _Tp>
using alignof_ = size_t_<alignof(_Tp)>;
//! @}
//! The last position we can express for indexing
using Npos = size_t_<index_t(-1)>;
}}
//!@}
#endif /* __utl_meta_integralconstant_h__ */