From 40e3372b800670cb77d6903755291e4809d12c99 Mon Sep 17 00:00:00 2001 From: artemp Date: Mon, 5 May 2014 12:54:17 +0100 Subject: [PATCH] variant : add binary visitation support --- include/llmr/util/variant.hpp | 176 ++++++++++++++++++++++++++++------ 1 file changed, 149 insertions(+), 27 deletions(-) diff --git a/include/llmr/util/variant.hpp b/include/llmr/util/variant.hpp index ff83447ff..b887ae494 100644 --- a/include/llmr/util/variant.hpp +++ b/include/llmr/util/variant.hpp @@ -11,9 +11,9 @@ #include #ifdef NDEBUG - #define VARIANT_INLINE inline __attribute__((always_inline)) +#define VARIANT_INLINE inline __attribute__((always_inline)) #else - #define VARIANT_INLINE __attribute__((noinline)) +#define VARIANT_INLINE __attribute__((noinline)) #endif namespace llmr { @@ -76,7 +76,7 @@ struct variant_helper; template struct variant_helper { - VARIANT_INLINE static void destroy(std::size_t id, void * data) + VARIANT_INLINE static void destroy(const std::size_t id, void * data) { if (id == sizeof...(Types)) { @@ -88,30 +88,30 @@ struct variant_helper } } - VARIANT_INLINE static void move(const std::size_t old_t, void * old_v, void * new_v) + VARIANT_INLINE static void move(const std::size_t old_id, void * old_value, void * new_value) { - if (old_t == sizeof...(Types)) + if (old_id == sizeof...(Types)) { - new (new_v) T(std::move(*reinterpret_cast(old_v))); - //std::memcpy(new_v, old_v, sizeof(T)); + new (new_value) T(std::move(*reinterpret_cast(old_value))); + //std::memcpy(new_value, old_value, sizeof(T)); // ^^ DANGER: this should only be considered for relocatable types e.g built-in types // Also, I don't see any measurable performance benefit just yet } else { - variant_helper::move(old_t, old_v, new_v); + variant_helper::move(old_id, old_value, new_value); } } - VARIANT_INLINE static void copy(const std::size_t old_t, const void * old_v, void * new_v) + VARIANT_INLINE static void copy(const std::size_t old_id, const void * old_value, void * new_value) { - if (old_t == sizeof...(Types)) + if (old_id == sizeof...(Types)) { - new (new_v) T(*reinterpret_cast(old_v)); + new (new_value) T(*reinterpret_cast(old_value)); } else { - variant_helper::copy(old_t, old_v, new_v); + variant_helper::copy(old_id, old_value, new_value); } } }; @@ -132,6 +132,7 @@ template struct dispatcher { typedef typename std::result_of::type result_type; + VARIANT_INLINE static result_type apply(V const& v, F f) { if (v.get_type_id() == sizeof...(Types)) @@ -149,9 +150,115 @@ template struct dispatcher { typedef typename std::result_of::type result_type; + VARIANT_INLINE static result_type apply(V const&, F) { - throw std::runtime_error("dispatch: FAIL"); + throw std::runtime_error("unary dispatch: FAIL"); + } + +}; + + +template +struct binary_dispatcher_rhs; + +template +struct binary_dispatcher_rhs +{ + typedef typename std::result_of::type result_type; + VARIANT_INLINE static result_type apply(V const& lhs, V const& rhs, F f) + { + if (rhs.get_type_id() == sizeof...(Types)) // call binary functor + { + return f(lhs. template get(), rhs. template get()); + } + else + { + return binary_dispatcher_rhs::apply(lhs, rhs, f); + } + } +}; + +template +struct binary_dispatcher_rhs +{ + typedef typename std::result_of::type result_type; + + VARIANT_INLINE static result_type apply(V const&, V const&, F) + { + throw std::runtime_error("binary dispatch: FAIL"); + } +}; + + +template +struct binary_dispatcher_lhs; + +template +struct binary_dispatcher_lhs +{ + typedef typename std::result_of::type binary_result_type; + VARIANT_INLINE static binary_result_type apply(V const& lhs, V const& rhs, F f) + { + if (lhs.get_type_id() == sizeof...(Types)) // call binary functor + { + return f(lhs. template get(), rhs. template get()); + } + else + { + return binary_dispatcher_lhs::apply(lhs, rhs, f); + } + } +}; + +template +struct binary_dispatcher_lhs +{ + typedef typename std::result_of::type result_type; + + VARIANT_INLINE static result_type apply(V const&, V const&, F) + { + throw std::runtime_error("binary dispatch: FAIL"); + } +}; + +template +struct binary_dispatcher; + +template +struct binary_dispatcher +{ + typedef typename std::result_of::type binary_result_type; + + VARIANT_INLINE static binary_result_type apply(V const& v0, V const& v1, F f) + { + if (v0.get_type_id() == sizeof...(Types)) + { + if (v0.get_type_id() == v1.get_type_id()) + { + return f(v0. template get(), v1. template get()); // call binary functor + } + else + { + return binary_dispatcher_rhs::apply(v0, v1, f); + } + } + else if (v1.get_type_id() == sizeof...(Types)) + { + return binary_dispatcher_lhs::apply(v0, v1, f); + } + return binary_dispatcher::apply(v0, v1, f); + } +}; + +template +struct binary_dispatcher +{ + typedef typename std::result_of::type binary_result_type; + + VARIANT_INLINE static binary_result_type apply(V const&, V const&, F) + { + throw std::runtime_error("binary dispatch: FAIL"); } }; @@ -175,7 +282,7 @@ struct less_comp }; template -struct comparer +class comparer { public: explicit comparer(Variant const& lhs) noexcept @@ -194,16 +301,16 @@ private: // operator<< helper template -struct printer +class printer { public: explicit printer(Out & out) : out_(out) {} - printer& operator=(const printer&) = delete; + printer& operator=(printer const &) = delete; // visitor template - void operator()(const T& operand) const + void operator()( T const & operand) const { out_ << operand; } @@ -214,19 +321,19 @@ private: } // detail template -struct variant +class variant { private: static const std::size_t data_size = static_max::value; static const std::size_t data_align = static_max::value; - using data_t = typename std::aligned_storage::type; + using data_type = typename std::aligned_storage::type; - using helper_t = variant_helper; + using helper_type = variant_helper; std::size_t type_id; - data_t data; + data_type data; public: @@ -252,13 +359,13 @@ public: VARIANT_INLINE variant(variant const& old) : type_id(old.type_id) { - helper_t::copy(old.type_id, &old.data, &data); + helper_type::copy(old.type_id, &old.data, &data); } VARIANT_INLINE variant(variant&& old) noexcept : type_id(old.type_id) { - helper_t::move(old.type_id, &old.data, &data); + helper_type::move(old.type_id, &old.data, &data); } friend void swap(variant & first, variant & second) @@ -288,7 +395,7 @@ public: template VARIANT_INLINE void set(Args&&... args) { - helper_t::destroy(type_id, &data); + helper_type::destroy(type_id, &data); new (&data) T(std::forward(args)...); type_id = detail::type_traits::id; } @@ -325,16 +432,24 @@ public: } // visitor + // unary template typename std::result_of::type VARIANT_INLINE static visit(V const& v, F f) { return detail::dispatcher::apply(v, f); } + // binary + template + typename std::result_of::type + VARIANT_INLINE static binary_visit(V const& v0, V const& v1, F f) + { + return detail::binary_dispatcher::apply(v0, v1, f); + } ~variant() noexcept { - helper_t::destroy(type_id, &data); + helper_type::destroy(type_id, &data); } // comparison operators @@ -367,6 +482,15 @@ VARIANT_INLINE static apply_visitor(V const& v, F f) return V::visit(v,f); } +// binary visitor interface +template +typename std::result_of::type +VARIANT_INLINE static apply_visitor(V const& v0, V const& v1, F f) +{ + return V::binary_visit(v0, v1, f); +} + + // operator<< template VARIANT_INLINE std::basic_ostream& @@ -378,7 +502,5 @@ operator<< (std::basic_ostream& out, Variant const& rhs) } } - } - #endif // UTIL_VARIANT_HPP