From 36f0494e8f680e63dec5d44b5f3c256ea426fb87 Mon Sep 17 00:00:00 2001 From: Christos Houtouridis Date: Tue, 23 Oct 2018 13:55:25 +0300 Subject: [PATCH] meta: Small changes and detect idiom --- include/utl/meta/bool.h | 2 +- include/utl/meta/detect.h | 70 +++++++++++++++++++++++++++++++++++++ include/utl/meta/if.h | 2 +- include/utl/meta/integral.h | 2 +- include/utl/meta/logical.h | 2 +- include/utl/meta/sfinae.h | 2 +- include/utl/meta/void.h | 2 +- 7 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 include/utl/meta/detect.h diff --git a/include/utl/meta/bool.h b/include/utl/meta/bool.h index 1767115..5e91e00 100644 --- a/include/utl/meta/bool.h +++ b/include/utl/meta/bool.h @@ -20,7 +20,7 @@ #ifndef __utl_meta_bool_h__ #define __utl_meta_bool_h__ -#include +#include #include /*! diff --git a/include/utl/meta/detect.h b/include/utl/meta/detect.h new file mode 100644 index 0000000..49964a4 --- /dev/null +++ b/include/utl/meta/detect.h @@ -0,0 +1,70 @@ +/*! + * \file utl/meta/detect.h + * \brief Template meta-programming void helpers + * + * 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 . + */ +#ifndef __utl_meta_detect_h__ +#define __utl_meta_detect_h__ + +#include +#include + +/*! + * \ingroup meta + * \defgroup detect + * Implementation of the detection idiom + */ +//! @{ + +namespace utl { + + //! detection idiom (negative case). + template class _Op, + typename... _Args> + struct detector_ { + using value_ = false_; + using type = _Def; + }; + + //! detection idiom (positive case). + template class _Op, + typename... _Args> + struct detector_ <_Def, void_t<_Op<_Args...>>, _Op, _Args...> { + using value_ = true_; + using type = _Op<_Args...>; + }; + + // Detect whether _Op<_Args...> is a valid type, use _Def if not. + template class _Op, + typename... _Args> + using detected_or_ = detector_ <_Def, void, _Op, _Args...>; + + // template alias + template class _Op, + typename... _Args> + using detected_or_t = typename detected_or_<_Def, _Op, _Args...>::type; + + + +} +//! @} +#endif /* __utl_meta_detect_h__ */ diff --git a/include/utl/meta/if.h b/include/utl/meta/if.h index 11c0b89..66e5034 100644 --- a/include/utl/meta/if.h +++ b/include/utl/meta/if.h @@ -20,7 +20,7 @@ #ifndef __utl_meta_if_h__ #define __utl_meta_if_h__ -#include +#include /*! diff --git a/include/utl/meta/integral.h b/include/utl/meta/integral.h index 061b55f..11a2993 100644 --- a/include/utl/meta/integral.h +++ b/include/utl/meta/integral.h @@ -20,7 +20,7 @@ #ifndef __utl_meta_integral_h__ #define __utl_meta_integral_h__ -#include +#include /*! * \ingroup meta diff --git a/include/utl/meta/logical.h b/include/utl/meta/logical.h index 9960c39..6b8ce35 100644 --- a/include/utl/meta/logical.h +++ b/include/utl/meta/logical.h @@ -20,7 +20,7 @@ #ifndef __utl_meta_logical_h__ #define __utl_meta_logical_h__ -#include +#include #include #include diff --git a/include/utl/meta/sfinae.h b/include/utl/meta/sfinae.h index 9dc127e..3f9f0ac 100644 --- a/include/utl/meta/sfinae.h +++ b/include/utl/meta/sfinae.h @@ -20,7 +20,7 @@ #ifndef __utl_meta_use_h__ #define __utl_meta_use_h__ -#include +#include #include diff --git a/include/utl/meta/void.h b/include/utl/meta/void.h index b5df113..2211b47 100644 --- a/include/utl/meta/void.h +++ b/include/utl/meta/void.h @@ -20,7 +20,7 @@ #ifndef __utl_meta_void_h__ #define __utl_meta_void_h__ -#include +#include #include /*! \ingroup meta