/* /~` _ _ _|_. _ _ |_ | _ \_,(_)| | | || ||_|(_||_)|(/_ https://github.com/Naios/continuable v3.0.0 Copyright(c) 2015 - 2018 Denis Blank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **/ #ifndef CONTINUABLE_DETAIL_TRAITS_HPP_INCLUDED #define CONTINUABLE_DETAIL_TRAITS_HPP_INCLUDED #include #include #include #include #include #include namespace cti { namespace detail { namespace traits { /// Evaluates to the element at position I. template using at_t = decltype(std::get(std::declval>())); namespace detail { template struct index_of_impl; template struct index_of_impl : std::integral_constant { }; template struct index_of_impl : std::integral_constant::value> {}; } // namespace detail /// Evaluates to the index of T in the given pack template using index_of_t = detail::index_of_impl; /// A tagging type for wrapping other types template struct identity {}; template struct identity : std::common_type {}; template struct is_identity : std::false_type {}; template struct is_identity> : std::true_type {}; template constexpr identity> identity_of(T const& /*type*/) noexcept { return {}; } template constexpr identity identity_of(identity /*type*/) noexcept { return {}; } template using identify = std::conditional_t>::value, T, identity>>; template constexpr auto get(identity) noexcept { return identify>{}; } namespace detail { // Equivalent to C++17's std::void_t which targets a bug in GCC, // that prevents correct SFINAE behavior. // See http://stackoverflow.com/questions/35753920 for details. template struct deduce_to_void : std::common_type {}; } // namespace detail /// C++17 like void_t type template using void_t = typename detail::deduce_to_void::type; namespace detail { template constexpr void static_if_impl(std::true_type, Type&& type, TrueCallback&& trueCallback) { std::forward(trueCallback)(std::forward(type)); } template constexpr void static_if_impl(std::false_type, Type&& /*type*/, TrueCallback&& /*trueCallback*/) { } template constexpr auto static_if_impl(std::true_type, Type&& type, TrueCallback&& trueCallback, FalseCallback&& /*falseCallback*/) { return std::forward(trueCallback)(std::forward(type)); } template constexpr auto static_if_impl(std::false_type, Type&& type, TrueCallback&& /*trueCallback*/, FalseCallback&& falseCallback) { return std::forward(falseCallback)(std::forward(type)); } /// Evaluates to the size of the given tuple like type, // / if the type has no static size it will be one. template struct tuple_like_size : std::integral_constant {}; template struct tuple_like_size::value)>> : std::tuple_size {}; } // namespace detail /// Returns the pack size of the given empty pack constexpr std::size_t pack_size_of(identity<>) noexcept { return 0U; } /// Returns the pack size of the given type template constexpr std::size_t pack_size_of(identity) noexcept { return detail::tuple_like_size::value; } /// Returns the pack size of the given type template constexpr std::size_t pack_size_of(identity) noexcept { return 2U + sizeof...(Args); } /// Returns an index sequence of the given type template constexpr auto sequence_of(identity) noexcept { constexpr auto const size = pack_size_of(identity{}); return std::make_index_sequence(); } /// Invokes the callback only if the given type matches the check template constexpr void static_if(Type&& type, Check&& check, TrueCallback&& trueCallback) { detail::static_if_impl(std::forward(check)(type), std::forward(type), std::forward(trueCallback)); } /// Invokes the callback only if the given type matches the check template constexpr auto static_if(Type&& type, Check&& check, TrueCallback&& trueCallback, FalseCallback&& falseCallback) { return detail::static_if_impl(std::forward(check)(type), std::forward(type), std::forward(trueCallback), std::forward(falseCallback)); } /// Calls the given unpacker with the content of the given sequence template constexpr decltype(auto) unpack(std::integer_sequence, U&& unpacker) { return std::forward(unpacker)(std::integral_constant{}...); } /// Calls the given unpacker with the content of the given sequenceable template constexpr auto unpack(F&& first_sequenceable, U&& unpacker, std::integer_sequence) -> decltype(std::forward(unpacker)( get(std::forward(first_sequenceable))...)) { (void)first_sequenceable; return std::forward(unpacker)( get(std::forward(first_sequenceable))...); } /// Calls the given unpacker with the content of the given sequenceable template constexpr auto unpack(F&& first_sequenceable, S&& second_sequenceable, U&& unpacker, std::integer_sequence, std::integer_sequence) -> decltype(std::forward(unpacker)( get(std::forward(first_sequenceable))..., get(std::forward(second_sequenceable))...)) { (void)first_sequenceable; (void)second_sequenceable; return std::forward(unpacker)( get(std::forward(first_sequenceable))..., get(std::forward(second_sequenceable))...); } /// Calls the given unpacker with the content of the given sequenceable template constexpr auto unpack(F&& first_sequenceable, U&& unpacker) -> decltype(unpack(std::forward(first_sequenceable), std::forward(unpacker), sequence_of(identify{}))) { return unpack(std::forward(first_sequenceable), std::forward(unpacker), sequence_of(identify{})); } /// Calls the given unpacker with the content of the given sequenceables template constexpr auto unpack(F&& first_sequenceable, S&& second_sequenceable, U&& unpacker) -> decltype(unpack(std::forward(first_sequenceable), std::forward(second_sequenceable), std::forward(unpacker), sequence_of(identity_of(first_sequenceable)), sequence_of(identity_of(second_sequenceable)))) { return unpack(std::forward(first_sequenceable), std::forward(second_sequenceable), std::forward(unpacker), sequence_of(identity_of(first_sequenceable)), sequence_of(identity_of(second_sequenceable))); } /// Adds the given type at the back of the left sequenceable template constexpr auto push(Left&& left, Element&& element) { return unpack(std::forward(left), [&](auto&&... args) { return std::make_tuple(std::forward(args)..., std::forward(element)); }); } /// Adds the element to the back of the identity template constexpr auto push(identity, identity) noexcept { return identity{}; } /// Removes the first element from the identity template constexpr auto pop_first(identity) noexcept { return identity{}; } /// Returns the merged sequence template constexpr auto merge(Left&& left) { return std::forward(left); } /// Merges the left sequenceable with the right ones template constexpr auto merge(Left&& left, Right&& right, Rest&&... rest) { // Merge the left with the right sequenceable and // merge the result with the rest. return merge(unpack(std::forward(left), std::forward(right), [&](auto&&... args) { // Maybe use: template class T, // typename... Args> return std::make_tuple( std::forward(args)...); }), std::forward(rest)...); } /// Merges the left identity with the right ones template constexpr auto merge(identity /*left*/, identity /*right*/, Rest&&... rest) { return merge(identity{}, std::forward(rest)...); } namespace detail { template > struct is_invokable_impl : std::common_type {}; template struct is_invokable_impl< T, std::tuple, void_t()(std::declval()...))>> : std::common_type {}; } // namespace detail /// Deduces to a std::true_type if the given type is callable with the arguments /// inside the given tuple. /// The main reason for implementing it with the detection idiom instead of /// hana like detection is that MSVC has issues with capturing raw template /// arguments inside lambda closures. /// /// ```cpp /// traits::is_invokable> /// ``` template using is_invokable_from_tuple = typename detail::is_invokable_impl::type; // Checks whether the given callable object is invocable with the given // arguments. This doesn't take member functions into account! template using is_invocable = is_invokable_from_tuple>; /// Deduces to a std::false_type template using fail = std::integral_constant::value>; #ifdef CONTINUABLE_HAS_CXX17_DISJUNCTION using std::disjunction; #else namespace detail { /// Declares a C++14 polyfill for C++17 std::disjunction. template > struct disjunction_impl : std::common_type {}; template struct disjunction_impl, void_t...>> : std::common_type {}; } // namespace detail template using disjunction = typename detail::disjunction_impl>::type; #endif // CONTINUABLE_HAS_CXX17_DISJUNCTION #ifdef CONTINUABLE_HAS_CXX17_CONJUNCTION using std::conjunction; #else namespace detail { /// Declares a C++14 polyfill for C++17 std::conjunction. template > struct conjunction_impl : std::common_type {}; template struct conjunction_impl, void_t...>> : std::common_type {}; } // namespace detail template using conjunction = typename detail::conjunction_impl>::type; #endif // CONTINUABLE_HAS_CXX17_CONJUNCTION } // namespace traits } // namespace detail } // namespace cti #endif // CONTINUABLE_DETAIL_TRAITS_HPP_INCLUDED