// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. #pragma once #if !defined(RXCPP_RX_UTIL_HPP) #define RXCPP_RX_UTIL_HPP #include "rx-includes.hpp" #if !defined(RXCPP_ON_IOS) && !defined(RXCPP_ON_ANDROID) && !defined(RXCPP_THREAD_LOCAL) #if defined(_MSC_VER) #define RXCPP_THREAD_LOCAL __declspec(thread) #else #define RXCPP_THREAD_LOCAL __thread #endif #endif #if !defined(RXCPP_DELETE) #if defined(_MSC_VER) #define RXCPP_DELETE __pragma(warning(disable: 4822)) =delete #else #define RXCPP_DELETE =delete #endif #endif #define RXCPP_CONCAT(Prefix, Suffix) Prefix ## Suffix #define RXCPP_CONCAT_EVALUATE(Prefix, Suffix) RXCPP_CONCAT(Prefix, Suffix) #define RXCPP_MAKE_IDENTIFIER(Prefix) RXCPP_CONCAT_EVALUATE(Prefix, __LINE__) // Provide replacements for try/catch keywords, using which is a compilation error // when exceptions are disabled with -fno-exceptions. #if RXCPP_USE_EXCEPTIONS #define RXCPP_TRY try #define RXCPP_CATCH(...) catch(__VA_ARGS__) // See also rxu::throw_exception for 'throw' keyword replacement. #else #define RXCPP_TRY if ((true)) #define RXCPP_CATCH(...) if ((false)) // See also rxu::throw_exception, which will std::terminate without exceptions. #endif namespace rxcpp { namespace util { template using value_type_t = typename std::decay::type::value_type; template using decay_t = typename std::decay::type; template using result_of_t = typename std::result_of::type; template std::vector to_vector(const T (&arr) [size]) { return std::vector(std::begin(arr), std::end(arr)); } template std::vector to_vector(std::initializer_list il) { return std::vector(il); } template typename std::enable_if::value && std::is_pod::value, std::vector>::type to_vector(T0 t0, TN... tn) { return to_vector({t0, tn...}); } // lifted from https://github.com/ericniebler/range-v3/blob/630fc70baa07cbfd222f329e44a3122ab64ce364/include/range/v3/range_fwd.hpp // removed constexpr & noexcept to support older VC compilers template /*constexpr*/ T const &as_const(T & t) /*noexcept*/ { return t; } template void as_const(T const &&) = delete; template struct values {}; template struct values_from; template struct values_from { typedef values type; }; template struct values_from { typedef typename values_from::type type; }; template struct all_true; template struct all_true { static const bool value = B; }; template struct all_true { static const bool value = B && all_true::value; }; template using enable_if_all_true_t = typename std::enable_if::value>::type; template struct all_true_type; template struct all_true_type { static const bool value = B::value; }; template struct all_true_type { static const bool value = B::value && all_true_type::value; }; template using enable_if_all_true_type_t = typename std::enable_if::value>::type; struct all_values_true { template bool operator()(ValueN... vn) const; template bool operator()(Value0 v0) const { return v0; } template bool operator()(Value0 v0, ValueN... vn) const { return v0 && all_values_true()(vn...); } }; struct any_value_true { template bool operator()(ValueN... vn) const; template bool operator()(Value0 v0) const { return v0; } template bool operator()(Value0 v0, ValueN... vn) const { return v0 || any_value_true()(vn...); } }; template struct types {}; // // based on Walter Brown's void_t proposal // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3911.pdf // struct types_checked {}; namespace detail { template struct types_checked_from {typedef types_checked type;}; } template struct types_checked_from {typedef typename detail::types_checked_from::type type;}; template using types_checked_t = typename types_checked_from::type; template struct expand_value_types { struct type; }; template struct expand_value_types, types_checked_t::type::value_type...>> { using type = types::type::value_type...>; }; template using value_types_t = typename expand_value_types>::type; template struct value_type_from : public std::false_type {typedef types_checked type;}; template struct value_type_from>::type> : public std::true_type {typedef value_type_t type;}; namespace detail { template auto apply(std::tuple p, values, F&& f) -> decltype(f(std::forward(std::get(p))...)) { return f(std::forward(std::get(p))...); } template auto apply_to_each(std::tuple& p, values, F_inner& f_inner, F_outer& f_outer) -> decltype(f_outer(std::move(f_inner(std::get(p)))...)) { return f_outer(std::move(f_inner(std::get(p)))...); } template auto apply_to_each(std::tuple& p, values, const F_inner& f_inner, const F_outer& f_outer) -> decltype(f_outer(std::move(f_inner(std::get(p)))...)) { return f_outer(std::move(f_inner(std::get(p)))...); } } template auto apply(std::tuple p, F&& f) -> decltype(detail::apply(std::move(p), typename values_from::type(), std::forward(f))) { return detail::apply(std::move(p), typename values_from::type(), std::forward(f)); } template auto apply_to_each(std::tuple& p, F_inner& f_inner, F_outer& f_outer) -> decltype(detail::apply_to_each(p, typename values_from::type(), f_inner, f_outer)) { return detail::apply_to_each(p, typename values_from::type(), f_inner, f_outer); } template auto apply_to_each(std::tuple& p, const F_inner& f_inner, const F_outer& f_outer) -> decltype(detail::apply_to_each(p, typename values_from::type(), f_inner, f_outer)) { return detail::apply_to_each(p, typename values_from::type(), f_inner, f_outer); } namespace detail { template struct apply_to { F to; explicit apply_to(F f) : to(std::move(f)) { } template auto operator()(std::tuple p) -> decltype(rxcpp::util::apply(std::move(p), to)) { return rxcpp::util::apply(std::move(p), to); } template auto operator()(std::tuple p) const -> decltype(rxcpp::util::apply(std::move(p), to)) { return rxcpp::util::apply(std::move(p), to); } }; } template auto apply_to(F f) -> detail::apply_to { return detail::apply_to(std::move(f)); } namespace detail { struct pack { template auto operator()(ParamN... pn) -> decltype(std::make_tuple(std::move(pn)...)) { return std::make_tuple(std::move(pn)...); } template auto operator()(ParamN... pn) const -> decltype(std::make_tuple(std::move(pn)...)) { return std::make_tuple(std::move(pn)...); } }; } inline auto pack() -> detail::pack { return detail::pack(); } namespace detail { template struct take_at { template auto operator()(ParamN... pn) -> typename std::tuple_element...>>::type { return std::get(std::make_tuple(std::move(pn)...)); } template auto operator()(ParamN... pn) const -> typename std::tuple_element...>>::type { return std::get(std::make_tuple(std::move(pn)...)); } }; } template inline auto take_at() -> detail::take_at { return detail::take_at(); } template struct resolve_type; template class Deferred, class... AN> struct defer_trait { template struct tag_valid {static const bool valid = true; static const bool value = R;}; struct tag_not_valid {static const bool valid = false; static const bool value = false;}; typedef Deferred::type...> resolved_type; template static auto check(int) -> tag_valid; template static tag_not_valid check(...); typedef decltype(check(0)) tag_type; static const bool valid = tag_type::valid; static const bool value = tag_type::value; static const bool not_value = valid && !value; }; template class Deferred, class... AN> struct defer_type { template struct tag_valid {typedef R type; static const bool value = true;}; struct tag_not_valid {typedef void type; static const bool value = false;}; typedef Deferred::type...> resolved_type; template static auto check(int) -> tag_valid; template static tag_not_valid check(...); typedef decltype(check(0)) tag_type; typedef typename tag_type::type type; static const bool value = tag_type::value; }; template class Deferred, class... AN> struct defer_value_type { template struct tag_valid {typedef R type; static const bool value = true;}; struct tag_not_valid {typedef void type; static const bool value = false;}; typedef Deferred::type...> resolved_type; template static auto check(int) -> tag_valid>; template static tag_not_valid check(...); typedef decltype(check(0)) tag_type; typedef typename tag_type::type type; static const bool value = tag_type::value; }; template class Deferred, class... AN> struct defer_seed_type { template struct tag_valid {typedef R type; static const bool value = true;}; struct tag_not_valid {typedef void type; static const bool value = false;}; typedef Deferred::type...> resolved_type; template static auto check(int) -> tag_valid; template static tag_not_valid check(...); typedef decltype(check(0)) tag_type; typedef typename tag_type::type type; static const bool value = tag_type::value; }; template struct resolve_type { typedef D type; }; template class Deferred, class... AN> struct resolve_type> { typedef typename defer_type::type type; }; template class Deferred, class... AN> struct resolve_type> { typedef typename defer_value_type::type type; }; template class Deferred, class... AN> struct resolve_type> { typedef typename defer_seed_type::type type; }; struct plus { template auto operator()(LHS&& lhs, RHS&& rhs) const -> decltype(std::forward(lhs) + std::forward(rhs)) { return std::forward(lhs) + std::forward(rhs); } }; struct count { template int operator()(int cnt, T&&) const { return cnt + 1; } }; struct less { template auto operator()(LHS&& lhs, RHS&& rhs) const -> decltype(std::forward(lhs) < std::forward(rhs)) { return std::forward(lhs) < std::forward(rhs); } }; template struct ret { template auto operator()(LHS&& ) const -> decltype(T()) { return T(); } }; template struct equal_to { bool operator()(const T& lhs, const T& rhs) const { return lhs == rhs; } }; template<> struct equal_to { template auto operator()(LHS&& lhs, RHS&& rhs) const -> decltype(std::forward(lhs) == std::forward(rhs)) { return std::forward(lhs) == std::forward(rhs); } }; namespace detail { template struct print_function { OStream& os; Delimit delimit; print_function(OStream& os, Delimit d) : os(os), delimit(std::move(d)) {} template void operator()(const TN&... tn) const { bool inserts[] = {(os << tn, true)...}; inserts[0] = *reinterpret_cast(inserts); // silence warning delimit(); } template void operator()(const std::tuple& tpl) const { rxcpp::util::apply(tpl, *this); } }; template struct endline { OStream& os; endline(OStream& os) : os(os) {} void operator()() const { os << std::endl; } private: endline& operator=(const endline&) RXCPP_DELETE; }; template struct insert_value { OStream& os; ValueType value; insert_value(OStream& os, ValueType v) : os(os), value(std::move(v)) {} void operator()() const { os << value; } private: insert_value& operator=(const insert_value&) RXCPP_DELETE; }; template struct insert_function { OStream& os; Function call; insert_function(OStream& os, Function f) : os(os), call(std::move(f)) {} void operator()() const { call(os); } private: insert_function& operator=(const insert_function&) RXCPP_DELETE; }; template auto print_followed_with(OStream& os, Delimit d) -> detail::print_function { return detail::print_function(os, std::move(d)); } } template auto endline(OStream& os) -> detail::endline { return detail::endline(os); } template auto println(OStream& os) -> decltype(detail::print_followed_with(os, endline(os))) { return detail::print_followed_with(os, endline(os)); } template auto print_followed_with(OStream& os, Delimit d) -> decltype(detail::print_followed_with(os, detail::insert_function(os, std::move(d)))) { return detail::print_followed_with(os, detail::insert_function(os, std::move(d))); } template auto print_followed_by(OStream& os, DelimitValue dv) -> decltype(detail::print_followed_with(os, detail::insert_value(os, std::move(dv)))) { return detail::print_followed_with(os, detail::insert_value(os, std::move(dv))); } inline std::string what(std::exception_ptr ep) { #if RXCPP_USE_EXCEPTIONS try {std::rethrow_exception(ep);} catch (const std::exception& ex) { return ex.what(); } catch (...) { return std::string(""); } #endif (void)ep; return std::string(""); } namespace detail { template class maybe { bool is_set; typename std::aligned_storage::value>::type storage; public: maybe() : is_set(false) { } maybe(T value) : is_set(false) { new (reinterpret_cast(&storage)) T(value); is_set = true; } maybe(const maybe& other) : is_set(false) { if (other.is_set) { new (reinterpret_cast(&storage)) T(other.get()); is_set = true; } } maybe(maybe&& other) : is_set(false) { if (other.is_set) { new (reinterpret_cast(&storage)) T(std::move(other.get())); is_set = true; other.reset(); } } ~maybe() { reset(); } typedef T value_type; typedef T* iterator; typedef const T* const_iterator; bool empty() const { return !is_set; } std::size_t size() const { return is_set ? 1 : 0; } iterator begin() { return reinterpret_cast(&storage); } const_iterator begin() const { return reinterpret_cast(&storage); } iterator end() { return reinterpret_cast(&storage) + size(); } const_iterator end() const { return reinterpret_cast(&storage) + size(); } T* operator->() { if (!is_set) std::terminate(); return reinterpret_cast(&storage); } const T* operator->() const { if (!is_set) std::terminate(); return reinterpret_cast(&storage); } T& operator*() { if (!is_set) std::terminate(); return *reinterpret_cast(&storage); } const T& operator*() const { if (!is_set) std::terminate(); return *reinterpret_cast(&storage); } T& get() { if (!is_set) std::terminate(); return *reinterpret_cast(&storage); } const T& get() const { if (!is_set) std::terminate(); return *reinterpret_cast(&storage); } void reset() { if (is_set) { is_set = false; reinterpret_cast(&storage)->~T(); //std::fill_n(reinterpret_cast(&storage), sizeof(T), 0); } } template void reset(U&& value) { reset(); new (reinterpret_cast(&storage)) T(std::forward(value)); is_set = true; } maybe& operator=(const T& other) { reset(other); return *this; } maybe& operator=(const maybe& other) { if (!other.empty()) { reset(other.get()); } else { reset(); } return *this; } }; } using detail::maybe; namespace detail { struct surely { template auto operator()(T... t) -> decltype(std::make_tuple(t.get()...)) { return std::make_tuple(t.get()...); } template auto operator()(T... t) const -> decltype(std::make_tuple(t.get()...)) { return std::make_tuple(t.get()...); } }; } template inline auto surely(const std::tuple& tpl) -> decltype(apply(tpl, detail::surely())) { return apply(tpl, detail::surely()); } namespace detail { template class unwinder { public: ~unwinder() { if (!!function) { RXCPP_TRY { (*function)(); } RXCPP_CATCH(...) { std::terminate(); } } } explicit unwinder(Function* functionArg) : function(functionArg) { } void dismiss() { function = nullptr; } private: unwinder(); unwinder(const unwinder&); unwinder& operator=(const unwinder&); Function* function; }; } #if !defined(RXCPP_THREAD_LOCAL) template class thread_local_storage { private: pthread_key_t key; public: thread_local_storage() { pthread_key_create(&key, NULL); } ~thread_local_storage() { pthread_key_delete(key); } thread_local_storage& operator =(T* p) { pthread_setspecific(key, p); return *this; } bool operator !() { return pthread_getspecific(key) == NULL; } T* operator ->() { return static_cast(pthread_getspecific(key)); } T* get() { return static_cast(pthread_getspecific(key)); } }; #endif template struct is_string : std::false_type { }; template struct is_string::type> : std::is_base_of< std::basic_string< typename T::value_type, typename T::traits_type, typename T::allocator_type>, T> { }; namespace detail { template struct is_duration : std::false_type {}; template struct is_duration> : std::is_convertible*> {}; } template > struct is_duration : detail::is_duration {}; // C++17 negation namespace detail { template struct not_value : std::conditional::type { }; } template struct negation : detail::not_value {}; } #if !RXCPP_USE_EXCEPTIONS namespace util { namespace detail { struct error_base { virtual const char* what() = 0; virtual ~error_base() {} }; // Use the "Type Erasure" idiom to wrap an std::exception-like // value into an error pointer. // // Supported types: // exception, bad_exception, bad_alloc. template struct error_specific : public error_base { error_specific(const E& e) : data(e) {} error_specific(E&& e) : data(std::move(e)) {} virtual ~error_specific() {} virtual const char* what() { return data.what(); } E data; }; } } #endif namespace util { #if RXCPP_USE_EXCEPTIONS using error_ptr = std::exception_ptr; #else // Note: std::exception_ptr cannot be used directly when exceptions are disabled. // Any attempt to 'throw' or to call into any of the std functions accepting // an std::exception_ptr will either fail to compile or result in an abort at runtime. using error_ptr = std::shared_ptr; inline std::string what(error_ptr ep) { return std::string(ep->what()); } #endif // TODO: Do we really need an identity make? // (It was causing some compilation errors deep inside templates). inline error_ptr make_error_ptr(error_ptr e) { return e; } // Replace std::make_exception_ptr (which would immediately terminate // when exceptions are disabled). template error_ptr make_error_ptr(E&& e) { #if RXCPP_USE_EXCEPTIONS return std::make_exception_ptr(std::forward(e)); #else using e_type = rxcpp::util::decay_t; using pointed_to_type = rxcpp::util::detail::error_specific; auto sp = std::make_shared(std::forward(e)); return std::static_pointer_cast(sp); #endif } // Replace std::rethrow_exception to be compatible with our error_ptr typedef. RXCPP_NORETURN inline void rethrow_exception(error_ptr e) { #if RXCPP_USE_EXCEPTIONS std::rethrow_exception(e); #else // error_ptr != std::exception_ptr so we can't use std::rethrow_exception // // However even if we could, calling std::rethrow_exception just terminates if exceptions are disabled. // // Therefore this function should only be called when we are completely giving up and have no idea // how to handle the error. (void)e; std::terminate(); #endif } // A replacement for the "throw" keyword which is illegal when // exceptions are disabled with -fno-exceptions. template RXCPP_NORETURN inline void throw_exception(E&& e) { #if RXCPP_USE_EXCEPTIONS throw std::forward(e); #else // "throw" keyword is unsupported when exceptions are disabled. // Immediately terminate instead. (void)e; std::terminate(); #endif } // TODO: Do we really need this? rxu::rethrow_exception(rxu::current_exception()) // would have the same semantics in either case. RXCPP_NORETURN inline void rethrow_current_exception() { #if RXCPP_USE_EXCEPTIONS std::rethrow_exception(std::current_exception()); #else std::terminate(); #endif } // If called during exception handling, return the currently caught exception. // Otherwise return null. inline error_ptr current_exception() { #if RXCPP_USE_EXCEPTIONS return std::current_exception(); #else // When exceptions are disabled, we can never be inside of a catch block. // Return null similar to std::current_exception returning null outside of catch. return nullptr; #endif } } namespace rxu=util; // // due to an noisy static_assert issue in more than one std lib impl, // rxcpp maintains a whitelist filter for the types that are allowed // to be hashed. this allows is_hashable to work. // // NOTE: this should eventually be removed! // template struct filtered_hash; #if RXCPP_HASH_ENUM template struct filtered_hash::value>::type> : std::hash { }; #elif RXCPP_HASH_ENUM_UNDERLYING template struct filtered_hash::value>::type> : std::hash::type> { }; #endif template struct filtered_hash::value>::type> : std::hash { }; template struct filtered_hash::value>::type> : std::hash { }; template struct filtered_hash::value>::type> : std::hash { }; template struct filtered_hash>::value>::type> { using argument_type = T; using result_type = std::size_t; result_type operator()(argument_type const & dur) const { return std::hash{}(dur.count()); } }; template struct filtered_hash>::value>::type> { using argument_type = T; using result_type = std::size_t; result_type operator()(argument_type const & tp) const { return std::hash{}(tp.time_since_epoch().count()); } }; template struct is_hashable : std::false_type {}; template struct is_hashable::result_type, typename filtered_hash::argument_type, typename std::result_of(T)>::type>::type> : std::true_type {}; } #define RXCPP_UNWIND(Name, Function) \ RXCPP_UNWIND_EXPLICIT(uwfunc_ ## Name, Name, Function) #define RXCPP_UNWIND_AUTO(Function) \ RXCPP_UNWIND_EXPLICIT(RXCPP_MAKE_IDENTIFIER(uwfunc_), RXCPP_MAKE_IDENTIFIER(unwind_), Function) #define RXCPP_UNWIND_EXPLICIT(FunctionName, UnwinderName, Function) \ auto FunctionName = (Function); \ rxcpp::util::detail::unwinder UnwinderName(std::addressof(FunctionName)) #endif