From 635ad0feb1e5d9de20f89263a1f0f8b4c56ead86 Mon Sep 17 00:00:00 2001 From: null Date: Fri, 19 Apr 2024 13:58:33 +0300 Subject: [PATCH] Fixed stl namespace conflicts --- include/promise-cpp/add_ons.hpp | 138 +++++++++---- include/promise-cpp/any.hpp | 335 +++++++++++++++++++------------- 2 files changed, 292 insertions(+), 181 deletions(-) diff --git a/include/promise-cpp/add_ons.hpp b/include/promise-cpp/add_ons.hpp index 4a425e6..c89dfb0 100644 --- a/include/promise-cpp/add_ons.hpp +++ b/include/promise-cpp/add_ons.hpp @@ -2,25 +2,32 @@ #ifndef INC_COMPATIBILITY_HPP_ #define INC_COMPATIBILITY_HPP_ +#include #include #include +#include + #ifdef __cpp_rtti #include -namespace promise { +namespace promise +{ using type_index = std::type_index; -template -inline type_index type_id() { +template +inline type_index type_id() +{ return typeid(T); } } // namespace promise #else -namespace promise { +namespace promise +{ using type_index = ptrdiff_t; -template -inline type_index type_id() { +template +inline type_index type_id() +{ static char idHolder; return (type_index)&idHolder; } @@ -30,15 +37,19 @@ inline type_index type_id() { #ifndef INC_STD_ADD_ONS_HPP_ #define INC_STD_ADD_ONS_HPP_ -namespace std { - - #if (defined(_MSVC_LANG) && _MSVC_LANG < 201402L) || (!defined(_MSVC_LANG) && __cplusplus < 201402L) +namespace std_comp +{ + template -struct index_sequence { +struct index_sequence +{ using type = index_sequence; using value_type = size_t; - static constexpr std::size_t size() noexcept { return sizeof...(Ints); } + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } }; template @@ -46,67 +57,108 @@ struct _merge_and_renumber; template struct _merge_and_renumber, index_sequence> - : index_sequence { + : index_sequence +{ }; template -struct make_index_sequence - : _merge_and_renumber::type, - typename make_index_sequence::type> { +struct make_index_sequence : _merge_and_renumber::type, + typename make_index_sequence::type> +{ }; -template<> struct make_index_sequence<0> : index_sequence<> { }; -template<> struct make_index_sequence<1> : index_sequence<0> { }; +template <> +struct make_index_sequence<0> : index_sequence<> +{ +}; +template <> +struct make_index_sequence<1> : index_sequence<0> +{ +}; +} // namespace std_comp +#else +namespace std_comp +{ +/// Alias template index_sequence +template +using index_sequence = std::integer_sequence; + +/// Alias template make_index_sequence +template +using make_index_sequence = std::make_integer_sequence; + +/// Alias template index_sequence_for +template +using index_sequence_for = std::make_index_sequence; +} // namespace std_comp #endif //__cplusplus < 201402L - #if (defined(_MSVC_LANG) && _MSVC_LANG < 201703L) || (!defined(_MSVC_LANG) && __cplusplus < 201703L) +namespace std_comp +{ -template< class... > +template using void_t = void; - +} #endif //__cplusplus < 201703L - #if (defined(_MSVC_LANG) && _MSVC_LANG < 202002L) || (!defined(_MSVC_LANG) && __cplusplus < 202002L) +namespace std_comp +{ -template -struct remove_cvref { +template +struct remove_cvref +{ typedef typename std::remove_cv::type>::type type; }; - +} // namespace std_comp +#else +namespace std_comp +{ +template +using remove_cvref = std::remove_cvref; +} // namespace std_comp #endif //__cplusplus < 202002L -} //namespace std - #endif // #ifndef INC_STD_ADD_ONS_HPP_ -namespace promise { +namespace promise +{ -template -struct tuple_remove_cvref { - using type = typename std::remove_cvref::type; +template +struct tuple_remove_cvref +{ + using type = typename std_comp::remove_cvref::type; }; -template -struct tuple_remove_cvref> { - using type = std::tuple::type...>; +template +struct tuple_remove_cvref> +{ + using type = std::tuple::type...>; }; - - template -struct is_iterable : std::false_type {}; +struct is_iterable : std::false_type +{ +}; // this gets used only when we can call std::begin() and std::end() on that type template -struct is_iterable())), - decltype(std::end(std::declval())) - >> : std::true_type {}; - +struct is_iterable< + T, + std::void_t())), decltype(std::end(std::declval()))>> + : std::true_type +{ +}; -template struct is_std_function : std::false_type {}; -template struct is_std_function> : std::true_type {}; +template +struct is_std_function : std::false_type +{ +}; +template +struct is_std_function> : std::true_type +{ +}; -} //namespace promise +} // namespace promise #endif diff --git a/include/promise-cpp/any.hpp b/include/promise-cpp/any.hpp index 1e559e1..819450e 100644 --- a/include/promise-cpp/any.hpp +++ b/include/promise-cpp/any.hpp @@ -3,7 +3,7 @@ #define INC_PM_ANY_HPP_ /* - * Promise API implemented by cpp as Javascript promise style + * Promise API implemented by cpp as Javascript promise style * * Copyright (c) 2016, xhawk18 * at gmail.com @@ -16,10 +16,10 @@ * 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 @@ -28,15 +28,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include -#include -#include -#include -#include #include "add_ons.hpp" #include "call_traits.hpp" +#include +#include +#include +#include +#include -namespace promise { +namespace promise +{ // Any library // See http://www.boost.org/libs/any for Documentation. @@ -48,298 +49,357 @@ namespace promise { // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95 class any; -template -inline ValueType any_cast(const any &operand); +template +inline ValueType any_cast(const any& operand); -class any { +class any +{ public: // structors any() - : content(0) { + : content(0) + { } - template - any(const ValueType &value) - : content(new holder::type>(value)) { + template + any(const ValueType& value) + : content(new holder::type>(value)) + { } - template + template any(RET value(ARG...)) - : content(new holder(value)) { + : content(new holder(value)) + { } - any(const any &other) - : content(other.content ? other.content->clone() : 0) { + any(const any& other) + : content(other.content ? other.content->clone() : 0) + { } // Move constructor - any(any &&other) - : content(other.content) { + any(any&& other) + : content(other.content) + { other.content = 0; } // Perfect forwarding of ValueType - template - any(ValueType &&value - , typename std::enable_if::value>::type* = nullptr // disable if value has type `any&` - , typename std::enable_if::value>::type* = nullptr) // disable if value has type `const ValueType&&` - : content(new holder::type>(static_cast(value))) { + template + any(ValueType&& value, + typename std::enable_if::value>::type* = + nullptr // disable if value has type `any&` + , + typename std::enable_if::value>::type* = + nullptr) // disable if value has type `const ValueType&&` + : content(new holder::type>( + static_cast(value))) + { } - ~any() { - if (content != nullptr) { - delete(content); + ~any() + { + if (content != nullptr) + { + delete (content); } } - any call(const any &arg) const { + any call(const any& arg) const + { return content ? content->call(arg) : any(); } - template::value>::type *dummy = nullptr> - inline ValueType cast() const { + template ::value>::type* dummy = nullptr> + inline ValueType cast() const + { return any_cast(*this); } - template::value>::type *dummy = nullptr> - inline ValueType cast() const { + template ::value>::type* dummy = nullptr> + inline ValueType cast() const + { if (this->empty()) return nullptr; else return any_cast(*this); } - - public: // modifiers - - any & swap(any & rhs) { + any& swap(any& rhs) + { std::swap(content, rhs.content); return *this; } - template - any & operator=(const ValueType & rhs) { + template + any& operator=(const ValueType& rhs) + { any(rhs).swap(*this); return *this; } - any & operator=(const any & rhs) { + any& operator=(const any& rhs) + { any(rhs).swap(*this); return *this; } public: // queries - bool empty() const { + bool empty() const + { return !content; } - - void clear() { + + void clear() + { any().swap(*this); } - type_index type() const { + type_index type() const + { return content ? content->type() : type_id(); } public: // types (public so any_cast can be non-friend) - class placeholder { + class placeholder + { public: // structors - virtual ~placeholder() { + virtual ~placeholder() + { } public: // queries virtual type_index type() const = 0; - virtual placeholder *clone() const = 0; - virtual any call(const any &arg) const = 0; + virtual placeholder* clone() const = 0; + virtual any call(const any& arg) const = 0; }; - template - class holder : public placeholder { + template + class holder : public placeholder + { public: // structors - holder(const ValueType & value) - : held(value) { + holder(const ValueType& value) + : held(value) + { } public: // queries - virtual type_index type() const { + virtual type_index type() const + { return type_id(); } - virtual placeholder * clone() const { + virtual placeholder* clone() const + { return new holder(held); } - virtual any call(const any &arg) const { + virtual any call(const any& arg) const + { return any_call(held, arg); } + public: // representation ValueType held; + private: // intentionally left unimplemented - holder & operator=(const holder &); + holder& operator=(const holder&); }; public: // representation (public so any_cast can be non-friend) - placeholder * content; + placeholder* content; }; -class bad_any_cast : public std::bad_cast { +class bad_any_cast : public std::bad_cast +{ public: type_index from_; type_index to_; - bad_any_cast(const type_index &from, const type_index &to) + bad_any_cast(const type_index& from, const type_index& to) : from_(from) - , to_(to) { - //fprintf(stderr, "bad_any_cast: from = %s, to = %s\n", from.name(), to.name()); + , to_(to) + { + // fprintf(stderr, "bad_any_cast: from = %s, to = %s\n", from.name(), to.name()); } - virtual const char * what() const throw() { + virtual const char* what() const throw() + { return "bad_any_cast"; } }; -template -ValueType * any_cast(any *operand) { +template +ValueType* any_cast(any* operand) +{ typedef typename any::template holder holder_t; - return operand && - operand->type() == type_id() - ? &static_cast(operand->content)->held - : 0; + return operand && operand->type() == type_id() + ? &static_cast(operand->content)->held + : 0; } -template -inline const ValueType * any_cast(const any *operand) { - return any_cast(const_cast(operand)); +template +inline const ValueType* any_cast(const any* operand) +{ + return any_cast(const_cast(operand)); } -template -ValueType any_cast(any & operand) { - typedef typename std::remove_cvref::type nonref; +template +ValueType any_cast(any& operand) +{ + typedef typename std_comp::remove_cvref::type nonref; - nonref *result = any_cast(&operand); + nonref* result = any_cast(&operand); if (!result) throw bad_any_cast(operand.type(), type_id()); return *result; } -template -inline ValueType any_cast(const any &operand) { - typedef typename std::remove_cvref::type nonref; - return any_cast(const_cast(operand)); +template +inline ValueType any_cast(const any& operand) +{ + typedef typename std_comp::remove_cvref::type nonref; + return any_cast(const_cast(operand)); } - - -template +template struct any_call_t; -template -struct any_call_t, FUNC> { +template +struct any_call_t, FUNC> +{ - static inline RET call(const typename FUNC::fun_type &func, const any &arg) { - return call(func, arg, std::make_index_sequence()); + static inline RET call(const typename FUNC::fun_type& func, const any& arg) + { + return call(func, arg, std_comp::make_index_sequence()); } - template - static inline RET call(const typename FUNC::fun_type &func, const any &arg, const std::index_sequence &) { + template + static inline RET call(const typename FUNC::fun_type& func, + const any& arg, + const std_comp::index_sequence&) + { using nocvr_argument_type = std::tuple; using any_arguemnt_type = std::vector; - const any_arguemnt_type &args = (arg.type() != type_id() - ? any_arguemnt_type{ arg } - : any_cast(arg)); - if(args.size() < sizeof...(NOCVR_ARGS)) + const any_arguemnt_type& args = + (arg.type() != type_id() ? any_arguemnt_type{arg} + : any_cast(arg)); + if (args.size() < sizeof...(NOCVR_ARGS)) throw bad_any_cast(arg.type(), type_id()); - return func(any_cast::type &>(args[I])...); + return func( + any_cast::type&>(args[I])...); } }; -template -struct any_call_t, FUNC> { +template +struct any_call_t, FUNC> +{ - static inline RET call(const typename FUNC::fun_type &func, const any &arg) { + static inline RET call(const typename FUNC::fun_type& func, const any& arg) + { using nocvr_argument_type = std::tuple; using any_arguemnt_type = std::vector; - if (arg.type() == type_id()) { - try { + if (arg.type() == type_id()) + { + try + { std::rethrow_exception(any_cast(arg)); } - catch (const NOCVR_ARG &ex_arg) { - return func(const_cast(ex_arg)); + catch (const NOCVR_ARG& ex_arg) + { + return func(const_cast(ex_arg)); } } - if (type_id() == type_id()) { - return func(any_cast(arg)); + if (type_id() == type_id()) + { + return func(any_cast(arg)); } - const any_arguemnt_type &args = (arg.type() != type_id() - ? any_arguemnt_type{ arg } - : any_cast(arg)); - if(args.size() < 1) + const any_arguemnt_type& args = + (arg.type() != type_id() ? any_arguemnt_type{arg} + : any_cast(arg)); + if (args.size() < 1) throw bad_any_cast(arg.type(), type_id()); - //printf("[%s] [%s]\n", args.front().type().name(), type_id().name()); - return func(any_cast(args.front())); + // printf("[%s] [%s]\n", args.front().type().name(), type_id().name()); + return func(any_cast(args.front())); } }; - -template -struct any_call_t, FUNC> { - static inline RET call(const typename FUNC::fun_type &func, const any &arg) { +template +struct any_call_t, FUNC> +{ + static inline RET call(const typename FUNC::fun_type& func, const any& arg) + { using any_arguemnt_type = std::vector; if (arg.type() != type_id()) - return (func(const_cast(arg))); + return (func(const_cast(arg))); - any_arguemnt_type &args = any_cast(arg); - if (args.size() == 0) { + any_arguemnt_type& args = any_cast(arg); + if (args.size() == 0) + { any empty; return (func(empty)); } - else if(args.size() == 1) + else if (args.size() == 1) return (func(args.front())); else - return (func(const_cast(arg))); + return (func(const_cast(arg))); } }; -template -struct any_call_with_ret_t { - static inline any call(const typename FUNC::fun_type &func, const any &arg) { +template +struct any_call_with_ret_t +{ + static inline any call(const typename FUNC::fun_type& func, const any& arg) + { return any_call_t::call(func, arg); } }; -template -struct any_call_with_ret_t { - static inline any call(const typename FUNC::fun_type &func, const any &arg) { +template +struct any_call_with_ret_t +{ + static inline any call(const typename FUNC::fun_type& func, const any& arg) + { any_call_t::call(func, arg); return any(); } }; -template -inline any any_call(const FUNC &func, const any &arg) { +template +inline any any_call(const FUNC& func, const any& arg) +{ using func_t = call_traits; using nocvr_argument_type = typename tuple_remove_cvref::type; - const auto &stdFunc = func_t::to_std_function(func); + const auto& stdFunc = func_t::to_std_function(func); if (!stdFunc) return any(); - if (arg.type() == type_id()) { - try { + if (arg.type() == type_id()) + { + try + { std::rethrow_exception(any_cast(arg)); } - catch (const any &ex_arg) { - return any_call_with_ret_t::result_type, nocvr_argument_type, func_t>::call(stdFunc, ex_arg); + catch (const any& ex_arg) + { + return any_call_with_ret_t::result_type, + nocvr_argument_type, + func_t>::call(stdFunc, ex_arg); } - catch (...) { + catch (...) + { } } - return any_call_with_ret_t::result_type, nocvr_argument_type, func_t>::call(stdFunc, arg); + return any_call_with_ret_t::result_type, + nocvr_argument_type, + func_t>::call(stdFunc, arg); } using pm_any = any; @@ -350,6 +410,5 @@ using pm_any = any; // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) - -} +} // namespace promise #endif