aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHåvard Pettersen <havardpe@oath.com>2020-06-13 16:23:42 +0000
committerHåvard Pettersen <havardpe@oath.com>2020-06-13 16:23:42 +0000
commite7c5b55b45c0dca76ab593f979c7fba471ff4c30 (patch)
tree342004e6d5c9a1e9b8ec81be3b3ddbd883a896ac /vespalib
parentc9c54cbc1f20ed5f54bc63035fe7881604c795ff (diff)
make type type member detection a generic trait and test it
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/traits/traits_test.cpp10
-rw-r--r--vespalib/src/vespa/vespalib/util/traits.h6
-rw-r--r--vespalib/src/vespa/vespalib/util/typify.h24
3 files changed, 27 insertions, 13 deletions
diff --git a/vespalib/src/tests/traits/traits_test.cpp b/vespalib/src/tests/traits/traits_test.cpp
index 0a29721df1d..7751327df75 100644
--- a/vespalib/src/tests/traits/traits_test.cpp
+++ b/vespalib/src/tests/traits/traits_test.cpp
@@ -42,4 +42,14 @@ TEST("require that can_skip_destruction works") {
EXPECT_EQUAL(can_skip_destruction<Child2>::value, true);
}
+struct NoType {};
+struct TypeType { using type = NoType; };
+struct NoTypeType { static constexpr int type = 3; };
+
+TEST("require that type type member can be detected") {
+ EXPECT_FALSE(has_type_type_v<NoType>);
+ EXPECT_TRUE(has_type_type_v<TypeType>);
+ EXPECT_FALSE(has_type_type_v<NoTypeType>);
+}
+
TEST_MAIN() { TEST_RUN_ALL(); }
diff --git a/vespalib/src/vespa/vespalib/util/traits.h b/vespalib/src/vespa/vespalib/util/traits.h
index eb0385abc72..7f8945954a8 100644
--- a/vespalib/src/vespa/vespalib/util/traits.h
+++ b/vespalib/src/vespa/vespalib/util/traits.h
@@ -39,4 +39,10 @@ struct can_skip_destruction : std::is_trivially_destructible<T> {};
//-----------------------------------------------------------------------------
+template <typename, typename = std::void_t<>> struct has_type_type : std::false_type {};
+template <typename T> struct has_type_type<T, std::void_t<typename T::type>> : std::true_type {};
+template <typename T> constexpr bool has_type_type_v = has_type_type<T>::value;
+
+//-----------------------------------------------------------------------------
+
} // namespace vespalib
diff --git a/vespalib/src/vespa/vespalib/util/typify.h b/vespalib/src/vespa/vespalib/util/typify.h
index afedb4219a5..0d84d1756a5 100644
--- a/vespalib/src/vespa/vespalib/util/typify.h
+++ b/vespalib/src/vespa/vespalib/util/typify.h
@@ -2,6 +2,7 @@
#pragma once
+#include "traits.h"
#include <stddef.h>
#include <utility>
@@ -32,8 +33,8 @@ template <typename T, T VALUE> struct TypifyResultValue {
/**
* Typification result for values resolving into simple templates
* (templated on one type). Using this exact template is not required,
- * but is supplied as convenience and example. The resolved template
- * should be called 'templ' for consistency across typifiers.
+ * but is supplied for convenience and as example. The resolved
+ * template should be called 'templ' for consistency across typifiers.
**/
template <template<typename> typename TT> struct TypifyResultSimpleTemplate {
template <typename T> using templ = TT<T>;
@@ -71,22 +72,19 @@ template <size_t N, typename Typifier, typename Target, typename ...Rs> struct T
static_assert(sizeof...(Rs) == N);
return Target::template invoke<Rs...>();
}
- template <class, class = std::void_t<>> struct has_type : std::false_type {};
- template <class T> struct has_type<T, std::void_t<typename T::type>> : std::true_type {};
template <typename T, typename ...Args> static decltype(auto) select(T &&value, Args &&...args) {
if constexpr (N == sizeof...(Rs)) {
return Target::template invoke<Rs...>(std::forward<T>(value), std::forward<Args>(args)...);
} else {
return Typifier::resolve(value, [&](auto t)->decltype(auto)
- {
- using X = decltype(t);
- constexpr bool x_has_type = has_type<X>::value;
- if constexpr (x_has_type) {
- return TypifyInvokeImpl<N, Typifier, Target, Rs..., typename X::type>::select(std::forward<Args>(args)...);
- } else {
- return TypifyInvokeImpl<N, Typifier, Target, Rs..., X>::select(std::forward<Args>(args)...);
- }
- });
+ {
+ using X = decltype(t);
+ if constexpr (has_type_type_v<X>) {
+ return TypifyInvokeImpl<N, Typifier, Target, Rs..., typename X::type>::select(std::forward<Args>(args)...);
+ } else {
+ return TypifyInvokeImpl<N, Typifier, Target, Rs..., X>::select(std::forward<Args>(args)...);
+ }
+ });
}
}
};