1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

add variant/io

This commit is contained in:
bolero-MURAKAMI 2012-10-24 20:52:09 +09:00
parent 410a8af026
commit 5cb070b3b3
10 changed files with 403 additions and 244 deletions

View file

@ -3,10 +3,8 @@
#include <sprout/config.hpp>
#include <sprout/utility/pair.hpp>
#include <sprout/utility/rel_ops.hpp>
#include <sprout/utility/operation.hpp>
#include <sprout/utility/operation_ext.hpp>
#include <sprout/utility/compare_pointees.hpp>
#include <sprout/utility/noncopyable.hpp>
#include <sprout/utility/enabler_if.hpp>
#include <sprout/utility/pack.hpp>

View file

@ -8,44 +8,124 @@ namespace sprout {
//
// equal_pointees
//
template<class OptionalPointee>
template<typename OptionalPointee>
inline SPROUT_CONSTEXPR bool
equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return (!lhs) != (!rhs) ? false : (!lhs ? true : (*lhs) == (*rhs));
}
//
// not_equal_pointees
//
template<typename OptionalPointee>
inline SPROUT_CONSTEXPR bool
not_equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return !sprout::equal_pointees(lhs, rhs);
}
//
// less_pointees
//
template<class OptionalPointee>
template<typename OptionalPointee>
inline SPROUT_CONSTEXPR bool
less_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return !rhs ? false : (!lhs ? true : (*lhs) < (*rhs));
}
//
// greater_pointees
//
template<typename OptionalPointee>
inline SPROUT_CONSTEXPR bool
greater_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return sprout::less_pointees(rhs, lhs);
}
//
// less_equal_pointees
//
template<typename OptionalPointee>
inline SPROUT_CONSTEXPR bool
less_equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return !sprout::less_pointees(rhs, lhs);
}
//
// greater_equal_pointees
//
template<typename OptionalPointee>
inline SPROUT_CONSTEXPR bool
greater_equal_pointees(OptionalPointee const& lhs, OptionalPointee const& rhs) {
return !sprout::less_pointees(lhs, rhs);
}
//
// equal_pointees_t
//
template<class OptionalPointee>
template<typename OptionalPointee>
struct equal_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
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<typename OptionalPointee>
struct not_equal_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
SPROUT_CONSTEXPR bool
operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const {
return sprout::not_equal_pointees(lhs, rhs);
}
};
//
// less_pointees_t
//
template<class OptionalPointee>
template<typename OptionalPointee>
struct less_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
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<typename OptionalPointee>
struct greater_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
SPROUT_CONSTEXPR bool
operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const {
return sprout::greater_pointees(lhs, rhs);
}
};
//
// less_equal_pointees_t
//
template<typename OptionalPointee>
struct less_equal_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
SPROUT_CONSTEXPR bool
operator()(OptionalPointee const& lhs, OptionalPointee const& rhs) const {
return sprout::less_equal_pointees(lhs, rhs);
}
};
//
// greater_equal_pointees_t
//
template<typename OptionalPointee>
struct greater_equal_pointees_t
: public sprout::binary_function<OptionalPointee, OptionalPointee, bool>
{
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

View file

@ -2,6 +2,7 @@
#define SPROUT_UTILITY_OPERATION_HPP
#include <sprout/config.hpp>
#include <sprout/utility/rel_ops.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/utility/swap.hpp>

View file

@ -2,6 +2,7 @@
#define SPROUT_UTILITY_OPERATION_EXT_HPP
#include <sprout/config.hpp>
#include <sprout/utility/compare_pointees.hpp>
#include <sprout/utility/as_lvalue.hpp>
#include <sprout/utility/as_const.hpp>
#include <sprout/utility/lvalue_forward.hpp>

View file

@ -5,24 +5,33 @@
namespace sprout {
namespace rel_ops {
//
// operator!=
//
template <typename T>
inline SPROUT_CONSTEXPR bool
operator!=(T const& x, T const& y) {
return !(x == y);
}
//
// operator>
//
template <typename T>
inline SPROUT_CONSTEXPR bool
operator>(T const& x, T const& y) {
return y < x;
}
//
// operator<=
//
template <typename T>
inline SPROUT_CONSTEXPR bool
operator<=(T const& x, T const& y) {
return !(y < x);
}
//
// operator>=
//
template <typename T>
inline SPROUT_CONSTEXPR bool
operator>=(T const& x, T const& y) {

View file

@ -2,236 +2,7 @@
#define SPROUT_UTILITY_VALUE_HOLDER_HPP
#include <sprout/config.hpp>
#include <sprout/utility/swap.hpp>
namespace sprout {
namespace detail {
template<typename T>
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<typename T>
struct holder_helper<T const> {
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<typename T>
struct holder_helper<T&> {
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<typename T>
struct holder_helper<T const&> {
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<typename T>
class value_holder {
public:
typedef T type;
private:
typedef sprout::detail::holder_helper<type> 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<typename T>
inline void
swap(sprout::value_holder<T>& lhs, sprout::value_holder<T>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
//
// get
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_holder<T>::reference_const_type
get(sprout::value_holder<T> const& x) {
return x.get();
}
template<typename T>
inline typename sprout::value_holder<T>::reference_type
get(sprout::value_holder<T>& x) {
return x.get();
}
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_holder<T>::pointer_const_type
get(sprout::value_holder<T> const* x) {
return x->get_pointer();
}
template<typename T>
inline typename sprout::value_holder<T>::pointer_type
get(sprout::value_holder<T>* x) {
return x->get_pointer();
}
//
// get_pointer
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_holder<T>::pointer_const_type
get_pointer(sprout::value_holder<T> const& x) {
return x.get_pointer();
}
template<typename T>
inline typename sprout::value_holder<T>::pointer_type
get_pointer(sprout::value_holder<T>& x) {
return x.get_pointer();
}
} // namespace sprout
#include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/value_holder/get.hpp>
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP

View file

@ -0,0 +1,47 @@
#ifndef SPROUT_UTILITY_VALUE_HOLDER_GET_HPP
#define SPROUT_UTILITY_VALUE_HOLDER_GET_HPP
#include <sprout/config.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
namespace sprout {
//
// get
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_holder<T>::reference_const_type
get(sprout::value_holder<T> const& x) {
return x.get();
}
template<typename T>
inline typename sprout::value_holder<T>::reference_type
get(sprout::value_holder<T>& x) {
return x.get();
}
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_holder<T>::pointer_const_type
get(sprout::value_holder<T> const* x) {
return x->get_pointer();
}
template<typename T>
inline typename sprout::value_holder<T>::pointer_type
get(sprout::value_holder<T>* x) {
return x->get_pointer();
}
//
// get_pointer
//
template<typename T>
inline SPROUT_CONSTEXPR typename sprout::value_holder<T>::pointer_const_type
get_pointer(sprout::value_holder<T> const& x) {
return x.get_pointer();
}
template<typename T>
inline typename sprout::value_holder<T>::pointer_type
get_pointer(sprout::value_holder<T>& x) {
return x.get_pointer();
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_GET_HPP

View file

@ -0,0 +1,211 @@
#ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
#define SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
#include <sprout/config.hpp>
#include <sprout/utility/swap.hpp>
namespace sprout {
namespace detail {
template<typename T>
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<typename T>
struct holder_helper<T const> {
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<typename T>
struct holder_helper<T&> {
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<typename T>
struct holder_helper<T const&> {
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<typename T>
class value_holder {
public:
typedef T type;
private:
typedef sprout::detail::holder_helper<type> 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<typename T>
inline void
swap(sprout::value_holder<T>& lhs, sprout::value_holder<T>& rhs)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP

View file

@ -3,6 +3,7 @@
#include <sprout/config.hpp>
#include <sprout/variant/variant.hpp>
#include <sprout/variant/io.hpp>
#include <sprout/variant/tuple.hpp>
#include <sprout/variant/get.hpp>
#include <sprout/variant/static_visitor.hpp>

40
sprout/variant/io.hpp Normal file
View file

@ -0,0 +1,40 @@
#ifndef SPROUT_VARIANT_IO_HPP
#define SPROUT_VARIANT_IO_HPP
#include <sprout/config.hpp>
#include <sprout/variant/variant.hpp>
#include <sprout/variant/static_visitor.hpp>
namespace sprout {
namespace detail {
template<typename OStream>
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<typename T>
void operator()(T const& operand) const {
out_ << operand;
}
};
} // namespace detail
//
// operator<<
//
template<typename Elem, typename Traits, typename... Types>
inline std::basic_ostream<Elem, Traits>&
operator<<(std::basic_ostream<Elem, Traits>& lhs, sprout::variant<Types...> const& rhs) {
sprout::detail::variant_output_visitor<std::basic_ostream<Elem, Traits> > visitor(lhs);
rhs.apply_visitor(visitor);
return lhs;
}
} // namespace sprout
#endif // #ifndef SPROUT_VARIANT_IO_HPP