mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2024-11-12 21:09:01 +00:00
add value_holder comparison and i/o
This commit is contained in:
parent
4dd6a25354
commit
44f16918b7
7 changed files with 258 additions and 12 deletions
|
@ -1,5 +1,5 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2017 Bolero MURAKAMI
|
||||
Copyright (c) 2011-2016 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
|
@ -14,10 +14,10 @@
|
|||
#include <sprout/optional/io.hpp>
|
||||
#include <sprout/optional/nullopt.hpp>
|
||||
#include <sprout/optional/in_place.hpp>
|
||||
#include <sprout/optional/exceptions.hpp>
|
||||
#include <sprout/optional/make_optional.hpp>
|
||||
#include <sprout/optional/get.hpp>
|
||||
#include <sprout/optional/hash.hpp>
|
||||
#include <sprout/optional/exceptions.hpp>
|
||||
#include <sprout/optional/container.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_OPTIONAL_HPP
|
||||
|
|
|
@ -23,5 +23,6 @@
|
|||
#include <sprout/utility/use_default.hpp>
|
||||
#include <sprout/utility/loop.hpp>
|
||||
#include <sprout/utility/comparison.hpp>
|
||||
#include <sprout/utility/fold.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_UTILITY_HPP
|
||||
|
|
98
sprout/utility/fold.hpp
Normal file
98
sprout/utility/fold.hpp
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2017 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_UTILITY_FOLD_HPP
|
||||
#define SPROUT_UTILITY_FOLD_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/utility/move.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename BinaryOperation>
|
||||
class folder_forwarder {
|
||||
public:
|
||||
typedef BinaryOperation function_type;
|
||||
private:
|
||||
function_type binary_op_;
|
||||
public:
|
||||
explicit SPROUT_CONSTEXPR folder_forwarder(function_type const& binary_op)
|
||||
: binary_op_(binary_op)
|
||||
{}
|
||||
SPROUT_CONSTEXPR function_type const& func() const {
|
||||
return binary_op_;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename BinaryOperation, typename T>
|
||||
class folder {
|
||||
public:
|
||||
typedef BinaryOperation function_type;
|
||||
typedef T value_type;
|
||||
private:
|
||||
function_type binary_op_;
|
||||
value_type value_;
|
||||
public:
|
||||
explicit SPROUT_CONSTEXPR folder(function_type const& binary_op, value_type const& value)
|
||||
: binary_op_(binary_op), value_(value)
|
||||
{}
|
||||
template<typename U>
|
||||
SPROUT_CXX14_CONSTEXPR folder& left(U&& val) {
|
||||
return (value_ = binary_op_(value_, SPROUT_FORWARD(U, val))), *this;
|
||||
}
|
||||
template<typename U>
|
||||
SPROUT_CXX14_CONSTEXPR folder& right(U&& val) {
|
||||
return (value_ = binary_op_(SPROUT_FORWARD(U, val), value_)), *this;
|
||||
}
|
||||
SPROUT_CONSTEXPR function_type const& func() const {
|
||||
return binary_op_;
|
||||
}
|
||||
SPROUT_CONSTEXPR value_type value() const {
|
||||
return value_;
|
||||
}
|
||||
SPROUT_CONSTEXPR value_type get() const {
|
||||
return value_;
|
||||
}
|
||||
SPROUT_CONSTEXPR operator value_type() const {
|
||||
return value_;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename BinaryOperation, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::folder<BinaryOperation, T>
|
||||
operator,(sprout::folder_forwarder<BinaryOperation> const& lhs, T&& rhs) {
|
||||
return sprout::folder<BinaryOperation, T>(lhs.func(), SPROUT_FORWARD(T, rhs));
|
||||
}
|
||||
template<typename BinaryOperation, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::folder<BinaryOperation, T>
|
||||
operator,(T&& lhs, sprout::folder_forwarder<BinaryOperation> const& rhs) {
|
||||
return sprout::folder<BinaryOperation, T>(rhs.func(), SPROUT_FORWARD(T, lhs));
|
||||
}
|
||||
template<typename BinaryOperation, typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR sprout::folder<BinaryOperation, T>&&
|
||||
operator,(sprout::folder<BinaryOperation, T>&& lhs, U&& rhs) {
|
||||
return sprout::move(lhs.left(SPROUT_FORWARD(U, rhs)));
|
||||
}
|
||||
template<typename BinaryOperation, typename T, typename U>
|
||||
inline SPROUT_CONSTEXPR sprout::folder<BinaryOperation, T>&&
|
||||
operator,(U&& lhs, sprout::folder<BinaryOperation, T>&& rhs) {
|
||||
return sprout::move(rhs.right(SPROUT_FORWARD(U, lhs)));
|
||||
}
|
||||
|
||||
template<typename BinaryOperation>
|
||||
inline SPROUT_CONSTEXPR sprout::folder_forwarder<BinaryOperation>
|
||||
fold_by(BinaryOperation const& binary_op) {
|
||||
return sprout::folder_forwarder<BinaryOperation>(binary_op);
|
||||
}
|
||||
template<typename BinaryOperation, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::folder<BinaryOperation, T>
|
||||
fold_by(BinaryOperation const& binary_op, T&& init) {
|
||||
return sprout::folder<BinaryOperation, T>(binary_op, SPROUT_FORWARD(T, init));
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_UTILITY_FOLD_HPP
|
|
@ -1,5 +1,5 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2017 Bolero MURAKAMI
|
||||
Copyright (c) 2011-2016 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
|
@ -10,7 +10,11 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/value_holder/value_holder.hpp>
|
||||
#include <sprout/utility/value_holder/comparison.hpp>
|
||||
#include <sprout/utility/value_holder/io.hpp>
|
||||
#include <sprout/utility/value_holder/get.hpp>
|
||||
#include <sprout/utility/value_holder/hash.hpp>
|
||||
#include <sprout/optional/in_place.hpp>
|
||||
#include <sprout/optional/exceptions.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_HPP
|
||||
|
|
56
sprout/utility/value_holder/comparison.hpp
Normal file
56
sprout/utility/value_holder/comparison.hpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2016 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_UTILITY_VALUE_HOLDER_COMPARISON_HPP
|
||||
#define SPROUT_UTILITY_VALUE_HOLDER_COMPARISON_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/compare_pointees.hpp>
|
||||
#include <sprout/utility/value_holder/value_holder.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// operator==
|
||||
// operator!=
|
||||
// operator<
|
||||
// operator>
|
||||
// operator<=
|
||||
// operator>=
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::value_holder<T> const& lhs, sprout::value_holder<T> const& rhs) {
|
||||
return sprout::equal_pointees(lhs, rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::value_holder<T> const& lhs, sprout::value_holder<T> const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<(sprout::value_holder<T> const& lhs, sprout::value_holder<T> const& rhs) {
|
||||
return sprout::less_pointees(lhs, rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator>(sprout::value_holder<T> const& lhs, sprout::value_holder<T> const& rhs) {
|
||||
return rhs < lhs;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator<=(sprout::value_holder<T> const& lhs, sprout::value_holder<T> const& rhs) {
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator>=(sprout::value_holder<T> const& lhs, sprout::value_holder<T> const& rhs) {
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_COMPARISON_HPP
|
74
sprout/utility/value_holder/io.hpp
Normal file
74
sprout/utility/value_holder/io.hpp
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2016 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_UTILITY_VALUE_HOLDER_IO_HPP
|
||||
#define SPROUT_UTILITY_VALUE_HOLDER_IO_HPP
|
||||
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/value_holder/value_holder.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// operator>>
|
||||
//
|
||||
template<typename Elem, typename Traits, typename T>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>&
|
||||
operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::value_holder<T>& rhs) {
|
||||
if (lhs.good()) {
|
||||
int d = lhs.get();
|
||||
if (d == ' ') {
|
||||
T x;
|
||||
lhs >> x;
|
||||
rhs = x;
|
||||
} else {
|
||||
lhs.setstate(std::ios::failbit);
|
||||
}
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
template<typename Elem, typename Traits, typename T>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>&
|
||||
operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::value_holder<T&>& rhs) {
|
||||
if (lhs.good()) {
|
||||
int d = lhs.get();
|
||||
if (d == ' ') {
|
||||
T x;
|
||||
lhs >> x;
|
||||
rhs = x;
|
||||
} else {
|
||||
if (d == '-') {
|
||||
d = lhs.get();
|
||||
if (d == '-') {
|
||||
rhs = sprout::value_holder<T&>();
|
||||
return lhs;
|
||||
}
|
||||
}
|
||||
lhs.setstate(std::ios::failbit);
|
||||
}
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
//
|
||||
// operator<<
|
||||
//
|
||||
template<typename Elem, typename Traits, typename T>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
|
||||
operator<<(std::basic_ostream<Elem, Traits>& lhs, sprout::value_holder<T> const& rhs) {
|
||||
if (lhs.good()) {
|
||||
if (!rhs) {
|
||||
lhs << "--";
|
||||
} else {
|
||||
lhs << ' ' << *rhs;
|
||||
}
|
||||
}
|
||||
return lhs;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_UTILITY_VALUE_HOLDER_IO_HPP
|
|
@ -1,5 +1,5 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2017 Bolero MURAKAMI
|
||||
Copyright (c) 2011-2016 Bolero MURAKAMI
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
|
@ -11,12 +11,14 @@
|
|||
#include <type_traits>
|
||||
#include <initializer_list>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/workaround/std/cstddef.hpp>
|
||||
#include <sprout/memory/addressof.hpp>
|
||||
#include <sprout/utility/swap.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/utility/move.hpp>
|
||||
#include <sprout/type_traits/is_constructible.hpp>
|
||||
#include <sprout/optional/in_place.hpp>
|
||||
#include <sprout/optional/exceptions.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
|
@ -44,10 +46,10 @@ namespace sprout {
|
|||
static SPROUT_CONSTEXPR holder_type&& hold(movable_param_type p) SPROUT_NOEXCEPT {
|
||||
return sprout::move(p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type& r) {
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type& r) SPROUT_NOEXCEPT {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) {
|
||||
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) SPROUT_NOEXCEPT {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type& r) SPROUT_NOEXCEPT {
|
||||
|
@ -81,11 +83,11 @@ namespace sprout {
|
|||
static SPROUT_CONSTEXPR holder_type const&& hold(movable_param_type p) SPROUT_NOEXCEPT {
|
||||
return sprout::move(p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type& r) {
|
||||
return *r;
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type& r) SPROUT_NOEXCEPT {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) {
|
||||
return *r;
|
||||
static SPROUT_CONSTEXPR const_reference ref(holder_type const& r) SPROUT_NOEXCEPT {
|
||||
return r;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type& r) SPROUT_NOEXCEPT {
|
||||
return sprout::addressof(r);
|
||||
|
@ -116,7 +118,9 @@ namespace sprout {
|
|||
return sprout::addressof(p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type r) {
|
||||
return *r;
|
||||
return r ? *r
|
||||
: (throw sprout::bad_optional_access("value_holder<>: bad optional access"), *r)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type r) SPROUT_NOEXCEPT {
|
||||
return r;
|
||||
|
@ -144,7 +148,9 @@ namespace sprout {
|
|||
return sprout::addressof(p);
|
||||
}
|
||||
static SPROUT_CONSTEXPR reference ref(holder_type r) {
|
||||
return *r;
|
||||
return r ? *r
|
||||
: (throw sprout::bad_optional_access("value_holder<>: bad optional access"), *r)
|
||||
;
|
||||
}
|
||||
static SPROUT_CONSTEXPR pointer ptr(holder_type r) SPROUT_NOEXCEPT {
|
||||
return r;
|
||||
|
@ -306,6 +312,13 @@ namespace sprout {
|
|||
SPROUT_CONSTEXPR mutable_or_const_pointer get_ptr() const SPROUT_NOEXCEPT {
|
||||
return get_pointer();
|
||||
}
|
||||
|
||||
SPROUT_EXPLICIT_CONVERSION SPROUT_CONSTEXPR operator bool() const SPROUT_NOEXCEPT {
|
||||
return is_initialized();
|
||||
}
|
||||
SPROUT_CONSTEXPR bool operator!() const SPROUT_NOEXCEPT {
|
||||
return !is_initialized();
|
||||
}
|
||||
SPROUT_CONSTEXPR bool is_initialized() const SPROUT_NOEXCEPT {
|
||||
return !!get_pointer();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue