From dc6a08d10af1d45604ada840b713644acbf84b98 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 3 Aug 2017 04:43:38 -0700 Subject: [PATCH] Add variadic min/max --- CHANGELOG.md | 1 + .../boost/beast/core/detail/type_traits.hpp | 74 +++++++++++++++++++ test/beast/core/type_traits.cpp | 23 ++++++ 3 files changed, 98 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fc69f61..338248e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Version 100: * Rename test macros * Reorder define test macro params * vcxproj workaround for include symlinks +* Add variadic min/max WebSocket: diff --git a/include/boost/beast/core/detail/type_traits.hpp b/include/boost/beast/core/detail/type_traits.hpp index ed03ce66..65c27e8f 100644 --- a/include/boost/beast/core/detail/type_traits.hpp +++ b/include/boost/beast/core/detail/type_traits.hpp @@ -180,6 +180,80 @@ struct is_contiguous_container::type>>: std::true_type {}; +template +struct unwidest_unsigned; + +template +struct unwidest_unsigned +{ + using type = U0; +}; + +template +struct unwidest_unsigned +{ + BOOST_STATIC_ASSERT(std::is_unsigned::value); + using type = typename std::conditional< + (sizeof(U0) < sizeof(typename unwidest_unsigned::type)), + U0, typename unwidest_unsigned::type>::type; +}; + +template +struct widest_unsigned; + +template +struct widest_unsigned +{ + using type = U0; +}; + +template +struct widest_unsigned +{ + BOOST_STATIC_ASSERT(std::is_unsigned::value); + using type = typename std::conditional< + (sizeof(U0) > sizeof(typename widest_unsigned::type)), + U0, typename widest_unsigned::type>::type; +}; + +template +inline +constexpr +U +min_all(U u) +{ + BOOST_STATIC_ASSERT(std::is_unsigned::value); + return u; +} + +template +inline +constexpr +typename unwidest_unsigned::type +min_all(U0 u0, U1 u1, UN... un) +{ + return u0 < u1? min_all(u0, un...) : min_all(u1, un...); +} + +template +inline +constexpr +U +max_all(U u) +{ + BOOST_STATIC_ASSERT(std::is_unsigned::value); + return u; +} + +template +inline +constexpr +typename widest_unsigned::type +max_all(U0 u0, U1 u1, UN... un) +{ + return u0 > u1? max_all(u0, un...) : max_all(u1, un...); +} + //------------------------------------------------------------------------------ // diff --git a/test/beast/core/type_traits.cpp b/test/beast/core/type_traits.cpp index ea2684bb..e3be630a 100644 --- a/test/beast/core/type_traits.cpp +++ b/test/beast/core/type_traits.cpp @@ -94,6 +94,29 @@ BOOST_STATIC_ASSERT(std::is_same>::type, F2>::value); BOOST_STATIC_ASSERT(std::is_same>>::type, F1>::value); BOOST_STATIC_ASSERT(std::is_same>>::type, F2>::value); +// +// min_all, max_all +// + +BOOST_STATIC_ASSERT(std::is_same::type>::value); +BOOST_STATIC_ASSERT(std::is_same::type>::value); +BOOST_STATIC_ASSERT(! std::is_same::type>::value); +BOOST_STATIC_ASSERT(std::is_same::type>::value); +BOOST_STATIC_ASSERT(std::is_same::type>::value); +BOOST_STATIC_ASSERT(! std::is_same::type>::value); +BOOST_STATIC_ASSERT(max_all(1u) == 1); +BOOST_STATIC_ASSERT(max_all(1u, 2u) == 2); +BOOST_STATIC_ASSERT(max_all(1u, 2u, static_cast(3)) == 3); +BOOST_STATIC_ASSERT(min_all(1u) == 1); +BOOST_STATIC_ASSERT(min_all(1u, 2u) == 1); +BOOST_STATIC_ASSERT(min_all(1u, 2u, static_cast(3)) == 1); + } // (anonymous) } // detail