utl/include/utl/meta/integral.h
Christos Houtouridis 2963c008f8 meta: Renaming
2019-10-13 19:52:03 +03:00

143 lines
4.5 KiB
C++

/*!
* \file integralconstant.h
* \brief Template meta-programming integral constant
*
* Copyright (C) 2018-2019 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 evaluate/extract return type of metafunctions
template <typename _Tp>
using eval = typename _Tp::type;
//! integral_
//! 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_ {
using value_type = _Tp;
using type = integral_<_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_<_Tp, _v>::value;
//! @}
//! Wrappers for basic types
//! @{
//! bool_ type: integral constant wrapper for bool
template<bool _v>
using bool_ = integral_<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_<int8_t, _v>;
//! uint8_ type: integral constant wrapper for \c uint8_t
template<uint8_t _v>
using uint8_ = integral_<uint8_t, _v>;
//! int16_ type: integral constant wrapper for \c int16_t
template<int16_t _v>
using int16_ = integral_<int16_t, _v>;
//! uint16_ type: integral constant wrapper for \c uint16_t
template<uint16_t _v>
using uint16_ = integral_<uint16_t, _v>;
//! int32_ type: integral constant wrapper for \c int32_t
template<int32_t _v>
using int32_ = integral_<int32_t, _v>;
//! uint32_ type: integral constant wrapper for \c uint32_t
template<uint32_t _v>
using uint32_ = integral_<uint32_t, _v>;
//! char_ type: integral constant wrapper for \c char
template<char _v>
using char_ = integral_<char, _v>;
//! int_ type: integral constant wrapper for \c int
template<int _v>
using int_ = integral_<int, _v>;
//! long_ type: integral constant wrapper for \c long
template<long _v>
using long_ = integral_<long, _v>;
//! index_ type: integral constant wrapper for \c index_t a.k.a std::size_t
template<index_t _v>
using index_ = integral_<index_t, _v>;
//! size_ type: integral constant wrapper for \c size_t a.k.a std::size_t
template<size_t _v>
using size_ = integral_<size_t, _v>;
//! Computes the size of the type \p _Tp.
//! Complexity \f$ O(1) \f$.
template <typename _Tp>
using sizeof_ = size_<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_<alignof(_Tp)>;
//! @}
//! The last position we can express for indexing
using Npos = size_<index_t(-1)>;
}}
//!@}
#endif /* __utl_meta_integralconstant_h__ */