utl/include/utl/meta/selection.h
2019-01-29 16:01:02 +02:00

93 lines
2.4 KiB
C++

/*!
* \file selection.h
* \brief Template meta-programming type selections.
*
* 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_selection_h__
#define __utl_meta_selection_h__
#include <utl/core/impl.h>
#include <utl/meta/integral.h>
#include <utl/meta/sfinae.h>
/*!
* \ingroup meta
* \defgroup type selection
* Type selection support header
*/
//! @{
namespace utl {
namespace meta{
/*!
* Type selection
*/
//! @{
//! if_, if_c
//! @{
namespace detail {
template <bool If, typename...>
struct if_c_ {
using type = nil_; //!< avoid ill formed result
};
template<typename Then>
struct if_c_<true, Then> {
using type = Then;
};
template<typename Then, typename Else>
struct if_c_<true, Then, Else> {
using type = Then;
};
template<typename Then, typename Else>
struct if_c_<false, Then, Else> {
using type = Else;
};
}
//! Select one type or another depending on a compile-time Boolean.
template <bool B, typename... Args>
using if_c = type_<detail::if_c_<B, Args...>>;
//! Select one type or another depending on a compile-time Boolean type
template <typename If, typename... Args>
using if_ = if_c<If::type::value, Args...>;
//! @}
/*!
* Named type selectors
*/
//! @{
//! Select the first type of a type sequence
template <typename T1, typename ...>
struct first_of {
using type = T1;
};
//! Select the second type of a type sequence
template <typename T1, typename T2, typename ...>
struct second_of {
using type = T2;
};
//! @}
}}
//! @}
#endif /* __utl_meta_selection_h__ */