add weed parser: char_(f, l)

This commit is contained in:
bolero-MURAKAMI 2013-05-21 19:56:41 +09:00
parent e6420f2bbd
commit ab096d442c
5 changed files with 167 additions and 21 deletions

View file

@ -6,6 +6,7 @@
#include <sprout/optional/comparison.hpp>
#include <sprout/optional/io.hpp>
#include <sprout/optional/nullopt.hpp>
#include <sprout/optional/in_place.hpp>
#include <sprout/optional/make_optional.hpp>
#include <sprout/optional/get.hpp>
#include <sprout/optional/hash.hpp>

View file

@ -0,0 +1,17 @@
#ifndef SPROUT_OPTIONAL_IN_PLACE_HPP
#define SPROUT_OPTIONAL_IN_PLACE_HPP
#include <sprout/config.hpp>
namespace sprout {
//
// in_place_t
// in_place
//
struct in_place_t {};
namespace {
SPROUT_STATIC_CONSTEXPR in_place_t in_place{};
} // anonymous-namespace
} // namespace sprout
#endif // #ifndef SPROUT_OPTIONAL_IN_PLACE_HPP

View file

@ -1,13 +1,18 @@
#ifndef SPROUT_OPTIONAL_OPTIONAL_HPP
#define SPROUT_OPTIONAL_OPTIONAL_HPP
#include <type_traits>
#include <initializer_list>
#include <sprout/config.hpp>
#include <sprout/utility/value_holder/value_holder.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/utility/as_const.hpp>
#include <sprout/type_traits/is_convert_constructible.hpp>
#include <sprout/none.hpp>
#include <sprout/optional/nullopt.hpp>
#include <sprout/optional/in_place.hpp>
#include <sprout/optional/exceptions.hpp>
#include <sprout/assert.hpp>
@ -52,9 +57,16 @@ namespace sprout {
: init(v.init)
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
// constexpr support
// SPROUT_CONSTEXPR optional(optional&& v)
// SPROUT_NOEXCEPT_EXPR(std::is_nothrow_move_constructible<T>::value)
// : init(v.init)
// , val(v.is_initialized() ? holder_type(sprout::move(*v)) : holder_type())
// {}
SPROUT_CONSTEXPR optional(optional&& v)
SPROUT_NOEXCEPT_EXPR(std::is_nothrow_copy_constructible<T>::value)
: init(v.init)
, val(v.is_initialized() ? holder_type(sprout::move(*v)) : holder_type())
, val(v.is_initialized() ? holder_type(*sprout::as_const(v)) : holder_type())
{}
SPROUT_CONSTEXPR optional(T const& v)
: init(true)
@ -64,6 +76,22 @@ namespace sprout {
: init(true)
, val(sprout::move(v))
{}
template<
typename... Args,
typename = typename std::enable_if<std::is_constructible<T, Args&&...>::value>::type
>
explicit SPROUT_CONSTEXPR optional(sprout::in_place_t, Args&&... args)
: init(true)
, val(sprout::in_place, sprout::forward<Args>(args)...)
{}
template<
typename U, typename... Args,
typename = typename std::enable_if<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>::type
>
explicit SPROUT_CONSTEXPR optional(sprout::in_place_t, std::initializer_list<U> il, Args&&... args)
: init(true)
, val(sprout::in_place, il, sprout::forward<Args>(args)...)
{}
SPROUT_CONSTEXPR optional(bool cond, T const& v)
: init(cond)
, val(cond ? holder_type(v) : holder_type())
@ -77,6 +105,17 @@ namespace sprout {
: init(v.is_initialized())
, val(v.is_initialized() ? holder_type(*v) : holder_type())
{}
// constexpr support
// template<typename U>
// explicit SPROUT_CONSTEXPR optional(optional<U>&& v)
// : init(v.is_initialized())
// , val(v.is_initialized() ? holder_type(sprout::move(*v)) : holder_type())
// {}
template<typename U>
explicit SPROUT_CONSTEXPR optional(optional<U>&& v)
: init(v.is_initialized())
, val(v.is_initialized() ? holder_type(*sprout::as_const(v)) : holder_type())
{}
// 20.6.4.3, assignment
optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
assign(v);
@ -86,16 +125,19 @@ namespace sprout {
assign(v);
return *this;
}
optional& operator=(optional&& v) {
optional& operator=(optional&& v)
SPROUT_NOEXCEPT_EXPR(std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)
{
assign(sprout::forward<optional>(v));
return *this;
}
optional& operator=(T const& v) {
assign(v);
return *this;
}
optional& operator=(T&& v) {
assign(sprout::forward<T>(v));
template<
typename U,
// typename = typename std::enable_if<std::is_constructible<T, U>::value && std::is_assignable<U, T>::value>::type
typename = typename std::enable_if<std::is_constructible<T, U&&>::value>::type
>
optional& operator=(U&& v) {
assign(sprout::forward<U>(v));
return *this;
}
template<typename U>
@ -103,6 +145,11 @@ namespace sprout {
assign(v);
return *this;
}
template<typename U>
optional& operator=(optional<U>&& v) {
assign(sprout::forward<optional<U> >(v));
return *this;
}
void assign(sprout::nullopt_t) SPROUT_NOEXCEPT {
destroy();
@ -111,16 +158,19 @@ namespace sprout {
optional temp(v);
temp.swap(*this);
}
void assign(optional&& v) {
void assign(optional&& v)
SPROUT_NOEXCEPT_EXPR(std::is_move_constructible<T>::value && std::is_move_assignable<T>::value)
{
optional temp(sprout::forward<optional>(v));
temp.swap(*this);
}
void assign(T const& v) {
optional temp(v);
temp.swap(*this);
}
void assign(T&& v) {
optional temp(sprout::forward<T>(v));
template<
typename U,
// typename = typename std::enable_if<std::is_constructible<T, U>::value && std::is_assignable<U, T>::value>::type
typename = typename std::enable_if<std::is_constructible<T, U&&>::value>::type
>
void assign(U&& v) {
optional temp(sprout::forward<U>(v));
temp.swap(*this);
}
template<typename U>
@ -128,6 +178,11 @@ namespace sprout {
optional temp(v);
temp.swap(*this);
}
template<typename U>
void assign(optional<U>&& v) {
optional temp(sprout::forward<optional<U> >(v));
temp.swap(*this);
}
void reset() SPROUT_NOEXCEPT {
destroy();
@ -135,11 +190,13 @@ namespace sprout {
void reset(sprout::nullopt_t v) SPROUT_NOEXCEPT {
assign(v);
}
void reset(T const& v) {
assign(v);
}
void reset(T&& v) {
assign(sprout::forward<T>(v));
template<
typename U,
// typename = typename std::enable_if<std::is_constructible<T, U>::value && std::is_assignable<U, T>::value>::type
typename = typename std::enable_if<std::is_constructible<T, U&&>::value>::type
>
void reset(U&& v) {
assign(sprout::forward<U>(v));
}
// 20.6.4.4, swap
void swap(optional& other)

View file

@ -1,9 +1,13 @@
#ifndef SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
#define SPROUT_UTILITY_VALUE_HOLDER_VALUE_HOLDER_HPP
#include <type_traits>
#include <initializer_list>
#include <sprout/config.hpp>
#include <sprout/utility/swap.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/utility/move.hpp>
#include <sprout/optional/in_place.hpp>
namespace sprout {
namespace detail {
@ -162,6 +166,20 @@ namespace sprout {
explicit SPROUT_CONSTEXPR value_holder(movable_argument_type p)
: holder_(helper_type::hold(sprout::move(p)))
{}
template<
typename... Args,
typename = typename std::enable_if<std::is_constructible<T, Args&&...>::value>::type
>
explicit SPROUT_CONSTEXPR value_holder(sprout::in_place_t, Args&&... args)
: holder_(sprout::forward<Args>(args)...)
{}
template<
typename U, typename... Args,
typename = typename std::enable_if<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>::type
>
explicit SPROUT_CONSTEXPR value_holder(sprout::in_place_t, std::initializer_list<U> il, Args&&... args)
: holder_(il, sprout::forward<Args>(args)...)
{}
value_holder& operator=(value_holder const&) = default;
value_holder& operator=(value_holder&&) = default;

View file

@ -111,6 +111,47 @@ namespace sprout {
}
};
//
// char_range_p
//
template<typename T>
struct char_range_p
: public sprout::weed::parser_base
{
public:
template<typename Context, typename Iterator>
struct attribute
: public sprout::identity<typename std::iterator_traits<Iterator>::value_type>
{};
template<typename Context, typename Iterator>
struct result
: public sprout::identity<sprout::weed::parser_result<Iterator, typename attribute<Context, Iterator>::type> >
{};
private:
T f_;
T l_;
public:
char_range_p() = default;
SPROUT_CONSTEXPR char_range_p(T const& f, T const& l)
: f_(f), l_(l)
{}
template<typename Context, typename Iterator>
SPROUT_CONSTEXPR typename result<Context, Iterator>::type operator()(
Iterator first,
Iterator last,
Context const&
) const
{
typedef typename result<Context, Iterator>::type result_type;
typedef typename attribute<Context, Iterator>::type attribute_type;
typedef typename std::iterator_traits<Iterator>::value_type elem_type;
return first != last && *first >= elem_type(f_) && *first <= elem_type(l_)
? result_type(true, sprout::next(first), *first)
: result_type(false, first, attribute_type())
;
}
};
//
// any_char_p
//
@ -142,9 +183,21 @@ namespace sprout {
;
}
template<typename T>
SPROUT_CONSTEXPR sprout::weed::char_p<T> operator()(T const& t) const {
SPROUT_CONSTEXPR typename std::enable_if<
sprout::weed::traits::is_char_type<T>::value,
sprout::weed::char_p<T>
>::type
operator()(T const& t) const {
return sprout::weed::char_p<T>(t);
}
template<typename T>
SPROUT_CONSTEXPR typename std::enable_if<
sprout::weed::traits::is_char_type<T>::value,
sprout::weed::char_range_p<T>
>::type
operator()(T const& f, T const& l) const {
return sprout::weed::char_range_p<T>(f, l);
}
};
//
// char_