From f097f401cbd32df604eee0107715ac35f9231d61 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Mon, 8 May 2017 16:26:07 -0700 Subject: [PATCH] Consolidate get_lowest_layer in type_traits.hpp fix #347 --- CHANGELOG.md | 1 + extras/beast/test/fail_stream.hpp | 2 +- include/beast/core/buffered_read_stream.hpp | 2 +- .../beast/core/detail/get_lowest_layer.hpp | 33 --------- include/beast/core/detail/type_traits.hpp | 33 +++++++++ include/beast/websocket/stream.hpp | 2 +- test/Jamfile | 1 - test/core/CMakeLists.txt | 1 - test/core/get_lowest_layer.cpp | 74 ------------------- test/core/type_traits.cpp | 69 ++++++++++++++++- 10 files changed, 105 insertions(+), 113 deletions(-) delete mode 100644 test/core/get_lowest_layer.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 1757fe2d..21c200a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 40 * Add to_static_string +* Consolidate get_lowest_layer in type_traits.hpp -------------------------------------------------------------------------------- diff --git a/extras/beast/test/fail_stream.hpp b/extras/beast/test/fail_stream.hpp index f6f8127f..fedd0033 100644 --- a/extras/beast/test/fail_stream.hpp +++ b/extras/beast/test/fail_stream.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/beast/core/buffered_read_stream.hpp b/include/beast/core/buffered_read_stream.hpp index 31a41ba3..f264a037 100644 --- a/include/beast/core/buffered_read_stream.hpp +++ b/include/beast/core/buffered_read_stream.hpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/include/beast/core/detail/get_lowest_layer.hpp b/include/beast/core/detail/get_lowest_layer.hpp index 4b199ce6..7dacfb17 100644 --- a/include/beast/core/detail/get_lowest_layer.hpp +++ b/include/beast/core/detail/get_lowest_layer.hpp @@ -13,39 +13,6 @@ namespace beast { namespace detail { -template -class has_lowest_layer -{ - template - static std::true_type check(int); - template - static std::false_type check(...); - using type = decltype(check(0)); -public: - static bool constexpr value = type::value; -}; - -template -struct maybe_get_lowest_layer -{ - using type = T; -}; - -template -struct maybe_get_lowest_layer -{ - using type = typename T::lowest_layer_type; -}; - -// If T has a nested type lowest_layer_type, -// returns that, else returns T. -template -struct get_lowest_layer -{ - using type = typename maybe_get_lowest_layer::value>::type; -}; } // detail } // beast diff --git a/include/beast/core/detail/type_traits.hpp b/include/beast/core/detail/type_traits.hpp index b667aa31..f5b50c81 100644 --- a/include/beast/core/detail/type_traits.hpp +++ b/include/beast/core/detail/type_traits.hpp @@ -127,6 +127,39 @@ struct is_invocable }; /** @} */ +template +class has_lowest_layer +{ + template + static std::true_type check(int); + template + static std::false_type check(...); + using type = decltype(check(0)); +public: + static bool constexpr value = type::value; +}; + +template +struct maybe_get_lowest_layer +{ + using type = T; +}; + +template +struct maybe_get_lowest_layer +{ + using type = typename T::lowest_layer_type; +}; + +// Returns T::lowest_layer_type if it exists, else T +template +struct get_lowest_layer +{ + using type = typename maybe_get_lowest_layer::value>::type; +}; + } // detail } // beast diff --git a/include/beast/websocket/stream.hpp b/include/beast/websocket/stream.hpp index da200594..f00f7838 100644 --- a/include/beast/websocket/stream.hpp +++ b/include/beast/websocket/stream.hpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/Jamfile b/test/Jamfile index bfd83dcd..5250cd20 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -40,7 +40,6 @@ unit-test core-tests : core/string_view.cpp core/base64.cpp core/empty_base_optimization.cpp - core/get_lowest_layer.cpp core/sha1.cpp core/type_traits.cpp ; diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 87c7858a..78a2b769 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -33,7 +33,6 @@ add_executable (core-tests string_view.cpp base64.cpp empty_base_optimization.cpp - get_lowest_layer.cpp sha1.cpp type_traits.cpp ) diff --git a/test/core/get_lowest_layer.cpp b/test/core/get_lowest_layer.cpp deleted file mode 100644 index 315d7e02..00000000 --- a/test/core/get_lowest_layer.cpp +++ /dev/null @@ -1,74 +0,0 @@ -// -// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -// Test that header file is self-contained. -#include - -#include - -namespace beast { -namespace detail { - -struct F1 -{ -}; - -struct F2 -{ -}; - -template -struct F3 -{ - using next_layer_type = - typename std::remove_reference::type; - - using lowest_layer_type = typename - get_lowest_layer::type; -}; - -template -struct F4 -{ - using next_layer_type = - typename std::remove_reference::type; - - using lowest_layer_type = typename - get_lowest_layer::type; -}; - -static_assert(! has_lowest_layer::value, ""); -static_assert(! has_lowest_layer::value, ""); -static_assert(has_lowest_layer>::value, ""); -static_assert(has_lowest_layer>>::value, ""); - -static_assert(std::is_same< - get_lowest_layer::type, F1>::value, ""); - -static_assert(std::is_same< - get_lowest_layer::type, F2>::value, ""); - -static_assert(std::is_same< - get_lowest_layer>::type, F1>::value, ""); - -static_assert(std::is_same< - get_lowest_layer>::type, F2>::value, ""); - -static_assert(std::is_same< - get_lowest_layer>::type, F1>::value, ""); - -static_assert(std::is_same< - get_lowest_layer>::type, F2>::value, ""); - -static_assert(std::is_same< - get_lowest_layer>>::type, F1>::value, ""); - -static_assert(std::is_same< - get_lowest_layer>>::type, F2>::value, ""); - -} // detail -} // beast diff --git a/test/core/type_traits.cpp b/test/core/type_traits.cpp index 0f57b040..2c0054ed 100644 --- a/test/core/type_traits.cpp +++ b/test/core/type_traits.cpp @@ -10,8 +10,13 @@ namespace beast { namespace detail { + namespace { +// +// is_invocable +// + struct is_invocable_udt1 { void operator()(int) const; @@ -51,6 +56,68 @@ static_assert(! is_invocable< is_invocable_udt3 const, int(int)>::value, ""); #endif -} +// +// get_lowest_layer +// + +struct F1 +{ +}; + +struct F2 +{ +}; + +template +struct F3 +{ + using next_layer_type = + typename std::remove_reference::type; + + using lowest_layer_type = typename + get_lowest_layer::type; +}; + +template +struct F4 +{ + using next_layer_type = + typename std::remove_reference::type; + + using lowest_layer_type = typename + get_lowest_layer::type; +}; + +static_assert(! has_lowest_layer::value, ""); +static_assert(! has_lowest_layer::value, ""); +static_assert(has_lowest_layer>::value, ""); +static_assert(has_lowest_layer>>::value, ""); + +static_assert(std::is_same< + get_lowest_layer::type, F1>::value, ""); + +static_assert(std::is_same< + get_lowest_layer::type, F2>::value, ""); + +static_assert(std::is_same< + get_lowest_layer>::type, F1>::value, ""); + +static_assert(std::is_same< + get_lowest_layer>::type, F2>::value, ""); + +static_assert(std::is_same< + get_lowest_layer>::type, F1>::value, ""); + +static_assert(std::is_same< + get_lowest_layer>::type, F2>::value, ""); + +static_assert(std::is_same< + get_lowest_layer>>::type, F1>::value, ""); + +static_assert(std::is_same< + get_lowest_layer>>::type, F2>::value, ""); + +} // (anonymous) + } // detail } // beast