From 5cb070b3b373565cf60ad4c0336c91b6613fd2e4 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Wed, 24 Oct 2012 20:52:09 +0900 Subject: [PATCH] add variant/io --- sprout/utility.hpp | 2 - sprout/utility/compare_pointees.hpp | 96 +++++++- sprout/utility/operation.hpp | 1 + sprout/utility/operation_ext.hpp | 1 + sprout/utility/rel_ops.hpp | 15 +- sprout/utility/value_holder.hpp | 233 +------------------ sprout/utility/value_holder/get.hpp | 47 ++++ sprout/utility/value_holder/value_holder.hpp | 211 +++++++++++++++++ sprout/variant.hpp | 1 + sprout/variant/io.hpp | 40 ++++ 10 files changed, 403 insertions(+), 244 deletions(-) create mode 100644 sprout/utility/value_holder/get.hpp create mode 100644 sprout/utility/value_holder/value_holder.hpp create mode 100644 sprout/variant/io.hpp diff --git a/sprout/utility.hpp b/sprout/utility.hpp index 56e6feda..f9f4beae 100644 --- a/sprout/utility.hpp +++ b/sprout/utility.hpp @@ -3,10 +3,8 @@ #include #include -#include #include #include -#include #include #include #include diff --git a/sprout/utility/compare_pointees.hpp b/sprout/utility/compare_pointees.hpp index 6c88783e..33ae7a1c 100644 --- a/sprout/utility/compare_pointees.hpp +++ b/sprout/utility/compare_pointees.hpp @@ -8,44 +8,124 @@ namespace sprout { // // equal_pointees // - template + template inline SPROUT_CONSTEXPR bool equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) { return (!lhs) != (!rhs) ? false : (!lhs ? true : (*lhs) == (*rhs)); } // + // not_equal_pointees + // + template + inline SPROUT_CONSTEXPR bool + not_equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) { + return !sprout::equal_pointees(lhs, rhs); + } + // // less_pointees // - template + template inline SPROUT_CONSTEXPR bool less_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) { return !rhs ? false : (!lhs ? true : (*lhs) < (*rhs)); } + // + // greater_pointees + // + template + inline SPROUT_CONSTEXPR bool + greater_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) { + return sprout::less_pointees(rhs, lhs); + } + // + // less_equal_pointees + // + template + inline SPROUT_CONSTEXPR bool + less_equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) { + return !sprout::less_pointees(rhs, lhs); + } + // + // greater_equal_pointees + // + template + inline SPROUT_CONSTEXPR bool + greater_equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) { + return !sprout::less_pointees(lhs, rhs); + } // // equal_pointees_t // - template + template struct equal_pointees_t : public sprout::binary_function { SPROUT_CONSTEXPR bool operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const { - return sprout::equal_pointees(lhs, rhs) ; + return sprout::equal_pointees(lhs, rhs); } - } ; + }; + // + // not_equal_pointees_t + // + template + struct not_equal_pointees_t + : public sprout::binary_function + { + SPROUT_CONSTEXPR bool + operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const { + return sprout::not_equal_pointees(lhs, rhs); + } + }; // // less_pointees_t // - template + template struct less_pointees_t : public sprout::binary_function { SPROUT_CONSTEXPR bool operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const { - return sprout::less_pointees(lhs, rhs) ; + return sprout::less_pointees(lhs, rhs); } - } ; + }; + // + // greater_pointees_t + // + template + struct greater_pointees_t + : public sprout::binary_function + { + SPROUT_CONSTEXPR bool + operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const { + return sprout::greater_pointees(lhs, rhs); + } + }; + // + // less_equal_pointees_t + // + template + struct less_equal_pointees_t + : public sprout::binary_function + { + SPROUT_CONSTEXPR bool + operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const { + return sprout::less_equal_pointees(lhs, rhs); + } + }; + // + // greater_equal_pointees_t + // + template + struct greater_equal_pointees_t + : public sprout::binary_function + { + SPROUT_CONSTEXPR bool + operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const { + return sprout::greater_equal_pointees(lhs, rhs); + } + }; } // namespace sprout #endif // #ifndef SPROUT_UTILITY_COMPARE_POINTEES_HPP diff --git a/sprout/utility/operation.hpp b/sprout/utility/operation.hpp index 5c2dd996..11f00bd0 100644 --- a/sprout/utility/operation.hpp +++ b/sprout/utility/operation.hpp @@ -2,6 +2,7 @@ #define SPROUT_UTILITY_OPERATION_HPP #include +#include #include #include #include diff --git a/sprout/utility/operation_ext.hpp b/sprout/utility/operation_ext.hpp index 924d4edf..a79ca805 100644 --- a/sprout/utility/operation_ext.hpp +++ b/sprout/utility/operation_ext.hpp @@ -2,6 +2,7 @@ #define SPROUT_UTILITY_OPERATION_EXT_HPP #include +#include #include #include #include diff --git a/sprout/utility/rel_ops.hpp b/sprout/utility/rel_ops.hpp index a74456b8..74fff4f8 100644 --- a/sprout/utility/rel_ops.hpp +++ b/sprout/utility/rel_ops.hpp @@ -5,24 +5,33 @@ namespace sprout { namespace rel_ops { + // + // operator!= + // template inline SPROUT_CONSTEXPR bool operator!=(T const& x, T const& y) { return !(x == y); } - + // + // operator> + // template inline SPROUT_CONSTEXPR bool operator>(T const& x, T const& y) { return y < x; } - + // + // operator<= + // template inline SPROUT_CONSTEXPR bool operator<=(T const& x, T const& y) { return !(y < x); } - + // + // operator>= + // template inline SPROUT_CONSTEXPR bool operator>=(T const& x, T const& y) { diff --git a/sprout/utility/value_holder.hpp b/sprout/utility/value_holder.hpp index 822df021..9105852b 100644 --- a/sprout/utility/value_holder.hpp +++ b/sprout/utility/value_holder.hpp @@ -2,236 +2,7 @@ #define SPROUT_UTILITY_VALUE_HOLDER_HPP #include -#include - -namespace sprout { - namespace detail { - template - struct holder_helper { - public: - typedef T value_type; - typedef T& reference; - typedef T const& const_reference; - typedef T const& mutable_or_const_reference; - typedef T* pointer; - typedef T const* const_pointer; - typedef T const* mutable_or_const_pointer; - typedef T const& param_type; - typedef T holder_type; - public: - static SPROUT_CONSTEXPR holder_type const& hold(param_type p) { - return p; - } - static SPROUT_CONSTEXPR reference ref(holder_type& r) { - return r; - } - static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) { - return r; - } - static SPROUT_CONSTEXPR pointer ptr(holder_type& r) { - return &r; - } - static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) { - return &r; - } - }; - template - struct holder_helper { - public: - typedef T value_type; - typedef T const& reference; - typedef T const& const_reference; - typedef T const& mutable_or_const_reference; - typedef T const* pointer; - typedef T const* const_pointer; - typedef T const* mutable_or_const_pointer; - typedef T const& param_type; - typedef T holder_type; - public: - static SPROUT_CONSTEXPR holder_type const& hold(param_type p) { - return &p; - } - static SPROUT_CONSTEXPR reference ref(holder_type& r) { - return *r; - } - static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) { - return *r; - } - static SPROUT_CONSTEXPR pointer ptr(holder_type& r) { - return &r; - } - static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) { - return &r; - } - }; - template - struct holder_helper { - public: - typedef T value_type; - typedef T& reference; - typedef T const& const_reference; - typedef T& mutable_or_const_reference; - typedef T* pointer; - typedef T const* const_pointer; - typedef T* mutable_or_const_pointer; - typedef T& param_type; - typedef T* holder_type; - public: - static SPROUT_CONSTEXPR holder_type hold(param_type p) { - return &p; - } - static SPROUT_CONSTEXPR reference ref(holder_type r) { - return *r; - } - static SPROUT_CONSTEXPR pointer ptr(holder_type r) { - return r; - } - }; - template - struct holder_helper { - public: - typedef T value_type; - typedef T const& reference; - typedef T const& const_reference; - typedef T const& mutable_or_const_reference; - typedef T const* pointer; - typedef T const* const_pointer; - typedef T const* mutable_or_const_pointer; - typedef T const& param_type; - typedef T const* holder_type; - public: - static SPROUT_CONSTEXPR holder_type hold(param_type p) { - return &p; - } - static SPROUT_CONSTEXPR reference ref(holder_type r) { - return *r; - } - static SPROUT_CONSTEXPR pointer ptr(holder_type r) { - return r; - } - }; - } // namespace detail - // - // value_holder - // - template - class value_holder { - public: - typedef T type; - private: - typedef sprout::detail::holder_helper helper_type; - typedef typename helper_type::holder_type holder_type; - public: - typedef typename helper_type::value_type value_type; - typedef typename helper_type::reference reference; - typedef typename helper_type::const_reference const_reference; - typedef typename helper_type::mutable_or_const_reference mutable_or_const_reference; - typedef typename helper_type::pointer pointer; - typedef typename helper_type::const_pointer const_pointer; - typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer; - typedef typename helper_type::param_type param_type; - typedef reference reference_type; - typedef mutable_or_const_reference reference_const_type; - typedef pointer pointer_type; - typedef mutable_or_const_pointer pointer_const_type; - typedef param_type argument_type; - private: - holder_type holder_; - public: - value_holder() = default; - value_holder(value_holder const&) = default; - explicit SPROUT_CONSTEXPR value_holder(param_type p) - : holder_(helper_type::hold(p)) - {} - - void swap(value_holder& other) - SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(holder_, other.holder_))) - { - sprout::swap(holder_, other.holder_); - } - - operator reference() { - return helper_type::ref(holder_); - } - SPROUT_CONSTEXPR operator mutable_or_const_reference() const { - return helper_type::ref(holder_); - } - pointer operator->() { - return helper_type::ptr(holder_); - } - SPROUT_CONSTEXPR mutable_or_const_pointer operator->() const { - return helper_type::ptr(holder_); - } - - reference get() { - return helper_type::ref(holder_); - } - SPROUT_CONSTEXPR mutable_or_const_reference get() const { - return helper_type::ref(holder_); - } - - pointer get_pointer() { - return helper_type::ptr(holder_); - } - SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer() const { - return helper_type::ptr(holder_); - } - pointer get_ptr() { - return get_pointer(); - } - SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const { - return get_pointer(); - } - }; - - // - // swap - // - template - inline void - swap(sprout::value_holder& lhs, sprout::value_holder& rhs) - SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) - { - lhs.swap(rhs); - } - - // - // get - // - template - inline SPROUT_CONSTEXPR typename sprout::value_holder::reference_const_type - get(sprout::value_holder const& x) { - return x.get(); - } - template - inline typename sprout::value_holder::reference_type - get(sprout::value_holder& x) { - return x.get(); - } - template - inline SPROUT_CONSTEXPR typename sprout::value_holder::pointer_const_type - get(sprout::value_holder const* x) { - return x->get_pointer(); - } - template - inline typename sprout::value_holder::pointer_type - get(sprout::value_holder* x) { - return x->get_pointer(); - } - - // - // get_pointer - // - template - inline SPROUT_CONSTEXPR typename sprout::value_holder::pointer_const_type - get_pointer(sprout::value_holder const& x) { - return x.get_pointer(); - } - template - inline typename sprout::value_holder::pointer_type - get_pointer(sprout::value_holder& x) { - return x.get_pointer(); - } -} // namespace sprout +#include +#include #endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP diff --git a/sprout/utility/value_holder/get.hpp b/sprout/utility/value_holder/get.hpp new file mode 100644 index 00000000..1b70b481 --- /dev/null +++ b/sprout/utility/value_holder/get.hpp @@ -0,0 +1,47 @@ +#ifndef SPROUT_UTILITY_VALUE_HOLDER_GET_HPP +#define SPROUT_UTILITY_VALUE_HOLDER_GET_HPP + +#include +#include + +namespace sprout { + // + // get + // + template + inline SPROUT_CONSTEXPR typename sprout::value_holder::reference_const_type + get(sprout::value_holder const& x) { + return x.get(); + } + template + inline typename sprout::value_holder::reference_type + get(sprout::value_holder& x) { + return x.get(); + } + template + inline SPROUT_CONSTEXPR typename sprout::value_holder::pointer_const_type + get(sprout::value_holder const* x) { + return x->get_pointer(); + } + template + inline typename sprout::value_holder::pointer_type + get(sprout::value_holder* x) { + return x->get_pointer(); + } + + // + // get_pointer + // + template + inline SPROUT_CONSTEXPR typename sprout::value_holder::pointer_const_type + get_pointer(sprout::value_holder const& x) { + return x.get_pointer(); + } + template + inline typename sprout::value_holder::pointer_type + get_pointer(sprout::value_holder& x) { + return x.get_pointer(); + } +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_GET_HPP diff --git a/sprout/utility/value_holder/value_holder.hpp b/sprout/utility/value_holder/value_holder.hpp new file mode 100644 index 00000000..d9b9cdd0 --- /dev/null +++ b/sprout/utility/value_holder/value_holder.hpp @@ -0,0 +1,211 @@ +#ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP +#define SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP + +#include +#include + +namespace sprout { + namespace detail { + template + struct holder_helper { + public: + typedef T value_type; + typedef T& reference; + typedef T const& const_reference; + typedef T const& mutable_or_const_reference; + typedef T* pointer; + typedef T const* const_pointer; + typedef T const* mutable_or_const_pointer; + typedef T const& param_type; + typedef T holder_type; + public: + static SPROUT_CONSTEXPR holder_type const& hold(param_type p) { + return p; + } + static SPROUT_CONSTEXPR reference ref(holder_type& r) { + return r; + } + static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) { + return r; + } + static SPROUT_CONSTEXPR pointer ptr(holder_type& r) { + return &r; + } + static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) { + return &r; + } + }; + template + struct holder_helper { + public: + typedef T value_type; + typedef T const& reference; + typedef T const& const_reference; + typedef T const& mutable_or_const_reference; + typedef T const* pointer; + typedef T const* const_pointer; + typedef T const* mutable_or_const_pointer; + typedef T const& param_type; + typedef T holder_type; + public: + static SPROUT_CONSTEXPR holder_type const& hold(param_type p) { + return &p; + } + static SPROUT_CONSTEXPR reference ref(holder_type& r) { + return *r; + } + static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) { + return *r; + } + static SPROUT_CONSTEXPR pointer ptr(holder_type& r) { + return &r; + } + static SPROUT_CONSTEXPR const_pointer ptr(holder_type const& r) { + return &r; + } + }; + template + struct holder_helper { + public: + typedef T value_type; + typedef T& reference; + typedef T const& const_reference; + typedef T& mutable_or_const_reference; + typedef T* pointer; + typedef T const* const_pointer; + typedef T* mutable_or_const_pointer; + typedef T& param_type; + typedef T* holder_type; + public: + static SPROUT_CONSTEXPR holder_type hold(param_type p) { + return &p; + } + static SPROUT_CONSTEXPR reference ref(holder_type r) { + return *r; + } + static SPROUT_CONSTEXPR pointer ptr(holder_type r) { + return r; + } + }; + template + struct holder_helper { + public: + typedef T value_type; + typedef T const& reference; + typedef T const& const_reference; + typedef T const& mutable_or_const_reference; + typedef T const* pointer; + typedef T const* const_pointer; + typedef T const* mutable_or_const_pointer; + typedef T const& param_type; + typedef T const* holder_type; + public: + static SPROUT_CONSTEXPR holder_type hold(param_type p) { + return &p; + } + static SPROUT_CONSTEXPR reference ref(holder_type r) { + return *r; + } + static SPROUT_CONSTEXPR pointer ptr(holder_type r) { + return r; + } + }; + } // namespace detail + // + // value_holder + // + template + class value_holder { + public: + typedef T type; + private: + typedef sprout::detail::holder_helper helper_type; + typedef typename helper_type::holder_type holder_type; + public: + typedef typename helper_type::value_type value_type; + typedef typename helper_type::reference reference; + typedef typename helper_type::const_reference const_reference; + typedef typename helper_type::mutable_or_const_reference mutable_or_const_reference; + typedef typename helper_type::pointer pointer; + typedef typename helper_type::const_pointer const_pointer; + typedef typename helper_type::mutable_or_const_pointer mutable_or_const_pointer; + typedef typename helper_type::param_type param_type; + typedef reference reference_type; + typedef mutable_or_const_reference reference_const_type; + typedef pointer pointer_type; + typedef mutable_or_const_pointer pointer_const_type; + typedef param_type argument_type; + private: + holder_type holder_; + public: + value_holder() = default; + value_holder(value_holder const&) = default; + explicit SPROUT_CONSTEXPR value_holder(param_type p) + : holder_(helper_type::hold(p)) + {} + + value_holder& operator=(value_holder const&) = default; + value_holder& operator=(param_type p) { + value_holder temp(p); + temp.swap(p); + return *this; + } + + void swap(value_holder& other) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout::swap(holder_, other.holder_))) + { + sprout::swap(holder_, other.holder_); + } + + operator reference() { + return helper_type::ref(holder_); + } + SPROUT_CONSTEXPR operator mutable_or_const_reference() const { + return helper_type::ref(holder_); + } + reference operator*() { + return helper_type::ref(holder_); + } + SPROUT_CONSTEXPR mutable_or_const_reference operator*() const { + return helper_type::ref(holder_); + } + reference get() { + return helper_type::ref(holder_); + } + SPROUT_CONSTEXPR mutable_or_const_reference get() const { + return helper_type::ref(holder_); + } + + pointer operator->() { + return helper_type::ptr(holder_); + } + SPROUT_CONSTEXPR mutable_or_const_pointer operator->() const { + return helper_type::ptr(holder_); + } + pointer get_pointer() { + return helper_type::ptr(holder_); + } + SPROUT_CONSTEXPR mutable_or_const_pointer get_pointer() const { + return helper_type::ptr(holder_); + } + pointer get_ptr() { + return get_pointer(); + } + SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const { + return get_pointer(); + } + }; + + // + // swap + // + template + inline void + swap(sprout::value_holder& lhs, sprout::value_holder& rhs) + SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) + { + lhs.swap(rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP diff --git a/sprout/variant.hpp b/sprout/variant.hpp index 1640face..d95fb9b7 100644 --- a/sprout/variant.hpp +++ b/sprout/variant.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/sprout/variant/io.hpp b/sprout/variant/io.hpp new file mode 100644 index 00000000..76c3f10c --- /dev/null +++ b/sprout/variant/io.hpp @@ -0,0 +1,40 @@ +#ifndef SPROUT_VARIANT_IO_HPP +#define SPROUT_VARIANT_IO_HPP + +#include +#include +#include + +namespace sprout { + namespace detail { + template + class variant_output_visitor + : public sprout::static_visitor<> + { + public: + typedef OStream ostream_type; + private: + ostream_type& out_; + public: + explicit variant_output_visitor(ostream_type& out) + : out_(out) + {} + template + void operator()(T const& operand) const { + out_ << operand; + } + }; + } // namespace detail + // + // operator<< + // + template + inline std::basic_ostream& + operator<<(std::basic_ostream& lhs, sprout::variant const& rhs) { + sprout::detail::variant_output_visitor > visitor(lhs); + rhs.apply_visitor(visitor); + return lhs; + } +} // namespace sprout + +#endif // #ifndef SPROUT_VARIANT_IO_HPP