|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- /*!
- * \file detection.h
- * \brief Detection idiom based on WG21's N4502[1] from Walter E. Brown
- *
- * [1]: www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf
- *
- * 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_detection_h__
- #define __utl_meta_detection_h__
-
- #include <utl/core/impl.h>
- #include <utl/meta/logical.h>
- #include <type_traits>
-
- /*!
- * \ingroup meta
- * \defgroup detection
- * Detection idiom support header.
- */
- //! @{
-
- namespace utl {
- namespace meta {
-
- /*!
- * void_t meta-function that maps a sequence of any types to the type void
- */
- //! @{
- #if defined(UTL_WORKAROUND_CWG_1558)
- template<typename... _Ts>
- struct void_ {
- using type = void;
- };
- //! void_t type alias
- template<typename... _Ts>
- using void_t = eval<void_<_Ts...>>;
- #else
- //! void_ type alias
- template <typename...> using void_ = void;
- //! void_t type alias
- template <typename...> using void_t = void;
- #endif
- //! @}
-
- /*!
- * Not a type to use in detected idiom
- */
- struct nat_ {
- nat_() = delete;
- ~nat_() = delete;
- nat_(nat_ const&) = delete;
- void operator = (nat_ const&) = delete;
- };
-
- //! Detector for detection idiom
- //! @{
- namespace detail {
- template <typename Default,
- typename AlwaysVoid,
- template<typename...> class Op, typename... Args>
- struct detector {
- using detected = false_;
- using type = Default;
- };
-
- template <typename Default,
- template<typename...> class Op, typename... Args>
- struct detector <Default, void_t<Op<Args...>>, Op, Args...> {
- using detected = true_;
- using type = Op<Args...>;
- };
-
- //! helper for detected_or_t
- template <typename Default,
- template<typename...> class Op, typename... Args>
- using detected_or = detector<Default, void, Op, Args...>;
- } // namespace detail
- //! @}
-
- /*!
- * detection interface
- */
- //! @{
-
- /*!
- * Checks if Op<Args...> is a valid expression without evaluating it.
- *
- * \param Op a meta-callback function to pass Args...
- * \param Args... types to pass to Op for checking
- * \return status of the operation [bool_]
- * \arg true_ if Op<Args...> is valid expression
- * \arg false_ if Op<Args...> is not valid
- *
- * \example
- * \code
- * // archetypal alias for a copy assignment operation
- * template< class T >
- * using copy_assign_t = decltype( declval<T&>() = declval<T const &>() );
- *
- * template< class T >
- * using is_copy_assignable = is_detected< T, copy_assign_t >;
- * \endcode
- */
- template <template<typename...> class Op, typename... Args>
- using is_detected = typename detail::detector<nat_, void, Op, Args...>::detected;
-
- //! Detection predicate
- template< template<typename...> class Op, typename... Args>
- constexpr bool is_detected_v = is_detected<Op, Args...>::value;
- //! @}
-
- /*!
- * Detection idiom toolkit
- */
- //! @{
-
- //! evaluates to evaluation of Op<Args...> if it's valid and to nat_ if not
- template <template<typename...> class Op, typename... Args>
- using detected_t = eval <
- detail::detector<nat_, void, Op, Args...>
- >;
-
- //! evaluates to evaluation of Op<Args...> if it's valid and to \p Default if not
- template <typename Default,
- template<typename...> class Op, typename... Args>
- using detected_or_t = eval <
- detail::detected_or<Default, Op, Args...>
- >;
-
- //! evaluates to true_ if evaluation of Op<Args...> is \p Expected and to false_ if not
- template <typename Expected,
- template<typename...> class Op, typename... Args >
- using is_detected_exact = same_<Expected, detected_t<Op, Args...> >;
-
- //! evaluates to true if evaluation of Op<Args...> is \p Expected and to false if not
- template <typename Expected,
- template<typename...> class Op, typename... Args >
- constexpr bool is_detected_exact_v = is_detected_exact< Expected, Op, Args...>::value;
-
- //! evaluates to true_ if evaluation of Op<Args...> is convertible to \p To
- //! and to false_ if not
- template <typename To,
- template<typename...> class Op, typename... Args >
- using is_detected_convertible = std::is_convertible< detected_t<Op, Args...>, To >;
-
- //! evaluates to true if evaluation of Op<Args...> is convertible to \p To
- //! and to false if not
- template <typename To,
- template<typename...> class Op, typename... Args >
- constexpr bool is_detected_convertible_v =
- is_detected_convertible<To, Op, Args...>::value;
- //! @}
-
- }}
- //!@}
-
- #endif /* __utl_meta_detection_h__ */
|