mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
support polymorphic visitor (variant)
This commit is contained in:
parent
9ee3e03ddd
commit
f01382e3e5
8 changed files with 204 additions and 24 deletions
|
@ -16,5 +16,6 @@
|
|||
#include <sprout/string/type_traits.hpp>
|
||||
#include <sprout/string/alias.hpp>
|
||||
#include <sprout/string/shrink.hpp>
|
||||
#include <sprout/string/stretch.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_HPP
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <sprout/index_tuple.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
#include <sprout/type_traits/enabler_if.hpp>
|
||||
#include <sprout/math/floor.hpp>
|
||||
#include <sprout/detail/char_conversion.hpp>
|
||||
#include <sprout/detail/math/int.hpp>
|
||||
#include <sprout/detail/math/float.hpp>
|
||||
|
@ -32,14 +33,15 @@ namespace sprout {
|
|||
namespace detail {
|
||||
template<typename Elem, typename FloatType, sprout::index_t... Indexes>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||
float_to_string(FloatType val, bool negative, int digits, sprout::index_tuple<Indexes...>) {
|
||||
float_to_string_impl(FloatType val, bool negative, int digits, int v, sprout::index_tuple<Indexes...>) {
|
||||
return negative
|
||||
? sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>{
|
||||
{
|
||||
static_cast<Elem>('-'),
|
||||
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - 1 - Indexes))
|
||||
: Indexes == digits ? static_cast<Elem>('.')
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - Indexes))
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length
|
||||
? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at(v, digits + sprout::detail::decimal_places_length - Indexes))
|
||||
: Elem()
|
||||
)...
|
||||
},
|
||||
|
@ -49,7 +51,8 @@ namespace sprout {
|
|||
{
|
||||
(Indexes < digits ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - 1 - Indexes))
|
||||
: Indexes == digits ? static_cast<Elem>('.')
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length ? sprout::detail::int_to_char<Elem>(sprout::detail::float_digit_at(val, digits - Indexes))
|
||||
: Indexes < digits + 1 + sprout::detail::decimal_places_length
|
||||
? sprout::detail::int_to_char<Elem>(sprout::detail::int_digit_at(v, digits + sprout::detail::decimal_places_length - Indexes))
|
||||
: Elem()
|
||||
)...
|
||||
},
|
||||
|
@ -57,6 +60,14 @@ namespace sprout {
|
|||
}
|
||||
;
|
||||
}
|
||||
template<typename Elem, typename FloatType>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<Elem, sprout::printed_float_digits<FloatType>::value>
|
||||
float_to_string(FloatType val, bool negative, int digits) {
|
||||
return sprout::detail::float_to_string_impl<Elem>(
|
||||
val, negative, digits, static_cast<int>((val - sprout::floor(val)) * sprout::detail::int_pow<int>(sprout::detail::decimal_places_length)),
|
||||
sprout::index_range<0, sprout::printed_float_digits<FloatType>::value - 1>::make()
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
|
@ -71,8 +82,7 @@ namespace sprout {
|
|||
return sprout::detail::float_to_string<Elem>(
|
||||
sprout::detail::float_round_at(val < 0 ? -val : val, sprout::detail::decimal_places_length),
|
||||
val < 0,
|
||||
sprout::detail::float_digits(val),
|
||||
sprout::index_range<0, sprout::printed_float_digits<FloatType>::value - 1>::make()
|
||||
sprout::detail::float_digits(val)
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,22 @@ namespace sprout {
|
|||
shrink(sprout::basic_string<T, N, Traits> const& s) {
|
||||
return sprout::shrink_string<T, N, Traits>(s);
|
||||
}
|
||||
template<std::size_t To, typename T, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, To, Traits>
|
||||
shrink(sprout::basic_string<T, N, Traits> const& s) {
|
||||
return sprout::shrink(s);
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
inline SPROUT_CONSTEXPR sprout::shrink_string<T, N>
|
||||
shrink(T const(& arr)[N]) {
|
||||
return sprout::shrink(sprout::to_string(arr));
|
||||
}
|
||||
template<std::size_t To, typename T, std::size_t N>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, To>
|
||||
shrink(T const(& arr)[N]) {
|
||||
return sprout::shrink<To>(sprout::to_string(arr));
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_SHRINK_HPP
|
||||
|
|
25
sprout/string/stretch.hpp
Normal file
25
sprout/string/stretch.hpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef SPROUT_STRING_STRETCH_HPP
|
||||
#define SPROUT_STRING_STRETCH_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/string/string.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// stretch
|
||||
//
|
||||
template<std::size_t To, typename T, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, To, Traits>
|
||||
stretch(sprout::basic_string<T, N, Traits> const& s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
template<std::size_t To, typename T, std::size_t N>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, To>
|
||||
stretch(T const(& arr)[N]) {
|
||||
return sprout::stretch<To>(sprout::to_string(arr));
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_STRETCH_HPP
|
|
@ -861,9 +861,6 @@ namespace sprout {
|
|||
};
|
||||
} // namespace detail
|
||||
|
||||
//
|
||||
// to_string
|
||||
//
|
||||
namespace detail {
|
||||
template<typename T, std::size_t N, sprout::index_t... Indexes>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, N - 1>
|
||||
|
@ -880,6 +877,14 @@ namespace sprout {
|
|||
return to_string_impl_1(arr, sprout::char_traits<T>::length(arr), sprout::index_tuple<Indexes...>());
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// to_string
|
||||
//
|
||||
template<typename T, std::size_t N, typename Traits>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, N, Traits>
|
||||
to_string(sprout::basic_string<T, N, Traits> const& s) {
|
||||
return s;
|
||||
}
|
||||
template<typename T, std::size_t N>
|
||||
inline SPROUT_CONSTEXPR sprout::basic_string<T, N - 1>
|
||||
to_string(T const(& arr)[N]) {
|
||||
|
|
|
@ -61,4 +61,32 @@
|
|||
#define SPROUT_HAS_XXX_VALUE_DEF_LAZY(VALUE) \
|
||||
SPROUT_HAS_XXX_VALUE_DEF(SPROUT_PP_CAT(has_, VALUE), VALUE)
|
||||
|
||||
//
|
||||
// SPROUT_HAS_XXX_TEMPLATE_DEF
|
||||
// SPROUT_HAS_XXX_TEMPLATE_DEF_LAZY
|
||||
//
|
||||
#if defined(_MSC_VER)
|
||||
#define SPROUT_HAS_XXX_TEMPLATE_DEF(NAME, TEMPLATE) \
|
||||
template<typename T, template<typename...> class = T::template TEMPLATE> \
|
||||
std::true_type SPROUT_PP_CAT(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TEMPLATE), NAME), __LINE__)(int); \
|
||||
template<typename T> \
|
||||
std::false_type SPROUT_PP_CAT(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TEMPLATE), NAME), __LINE__)(long); \
|
||||
template<typename T, typename Base_ = decltype(SPROUT_PP_CAT(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TEMPLATE), NAME), __LINE__)<T>(0))> \
|
||||
struct NAME \
|
||||
: public Base_ \
|
||||
{}
|
||||
#else
|
||||
#define SPROUT_HAS_XXX_TEMPLATE_DEF(NAME, TEMPLATE) \
|
||||
template<typename T, template<typename...> class = T::template TEMPLATE> \
|
||||
std::true_type SPROUT_PP_CAT(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TEMPLATE), NAME), __LINE__)(int); \
|
||||
template<typename T> \
|
||||
std::false_type SPROUT_PP_CAT(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TEMPLATE), NAME), __LINE__)(long); \
|
||||
template<typename T> \
|
||||
struct NAME \
|
||||
: public decltype(SPROUT_PP_CAT(SPROUT_PP_CAT(SPROUT_PP_CAT(sprout_has_xxx_impl_check_type_, TEMPLATE), NAME), __LINE__)<T>(0)) \
|
||||
{}
|
||||
#endif
|
||||
#define SPROUT_HAS_XXX_TEMPLATE_DEF_LAZY(TEMPLATE) \
|
||||
SPROUT_HAS_XXX_TEMPLATE_DEF(SPROUT_PP_CAT(has_, TEMPLATE), TEMPLATE)
|
||||
|
||||
#endif // #ifndef SPROUT_TYPE_TRAITS_HAS_XXX_HPP
|
||||
|
|
|
@ -1,12 +1,52 @@
|
|||
#ifndef SPROUT_VARIANT_APPLY_VISITOR_HPP
|
||||
#define SPROUT_VARIANT_APPLY_VISITOR_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/type_traits/has_xxx.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// has_visitor_result
|
||||
//
|
||||
SPROUT_HAS_XXX_TEMPLATE_DEF_LAZY(visitor_result);
|
||||
|
||||
namespace detail {
|
||||
template<typename Visitor, typename Visitable, typename = void>
|
||||
struct visitor_result_impl;
|
||||
template<typename Visitor, typename Visitable>
|
||||
struct visitor_result_impl<
|
||||
Visitor, Visitable,
|
||||
typename std::enable_if<sprout::has_visitor_result<Visitable>::value>::type
|
||||
>
|
||||
: public Visitable::template visitor_result<Visitor, Visitable>
|
||||
{};
|
||||
template<typename Visitor, typename Visitable>
|
||||
struct visitor_result_impl<
|
||||
Visitor, Visitable,
|
||||
typename std::enable_if<!sprout::has_visitor_result<Visitable>::value>::type
|
||||
> {
|
||||
public:
|
||||
typedef typename Visitor::result_type type;
|
||||
};
|
||||
} // namespace detail
|
||||
//
|
||||
// visitor_result
|
||||
//
|
||||
template<typename Visitor, typename Visitable>
|
||||
inline SPROUT_CONSTEXPR typename std::decay<Visitor>::type::result_type
|
||||
struct visitor_result
|
||||
: public sprout::detail::visitor_result_impl<Visitor, Visitable>
|
||||
{};
|
||||
|
||||
//
|
||||
// apply_visitor
|
||||
//
|
||||
template<typename Visitor, typename Visitable>
|
||||
inline SPROUT_CONSTEXPR typename sprout::visitor_result<
|
||||
typename std::remove_reference<Visitor>::type,
|
||||
typename std::remove_reference<Visitable>::type
|
||||
>::type
|
||||
apply_visitor(Visitor&& visitor, Visitable&& visitable) {
|
||||
return sprout::forward<Visitable>(visitable).apply_visitor(sprout::forward<Visitor>(visitor));
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@
|
|||
#include <sprout/tuple/functions.hpp>
|
||||
#include <sprout/type/type_tuple.hpp>
|
||||
#include <sprout/type/algorithm/find_index.hpp>
|
||||
#include <sprout/functional/type_traits/has_type.hpp>
|
||||
#include <sprout/functional/type_traits/weak_result_type.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename... Types>
|
||||
|
@ -72,6 +74,52 @@ namespace sprout {
|
|||
typedef typename impl_type::uncvref_tuple_type uncvref_tuple_type;
|
||||
public:
|
||||
typedef typename impl_type::tuple_type tuple_type;
|
||||
private:
|
||||
template<typename Visitor, typename Tuple, typename IndexTuple>
|
||||
struct visitor_result_impl_1;
|
||||
template<typename Visitor, typename Tuple, sprout::index_t... Indexes>
|
||||
struct visitor_result_impl_1<Visitor, Tuple, sprout::index_tuple<Indexes...> > {
|
||||
public:
|
||||
typedef typename std::decay<
|
||||
typename std::common_type<
|
||||
decltype((std::declval<Visitor>())(sprout::tuples::get<Indexes>(std::declval<Tuple>())))...
|
||||
>::type
|
||||
>::type type;
|
||||
};
|
||||
template<typename Visitor, typename Tuple, typename = void>
|
||||
struct visitor_result_impl;
|
||||
template<typename Visitor, typename Tuple>
|
||||
struct visitor_result_impl<
|
||||
Visitor, Tuple,
|
||||
typename std::enable_if<!sprout::has_result_type<sprout::weak_result_type<Visitor> >::value>::type
|
||||
>
|
||||
: public visitor_result_impl_1<Visitor, Tuple, typename sprout::index_range<0, sprout::tuples::tuple_size<Tuple>::value>::type>
|
||||
{};
|
||||
template<typename Visitor, typename Tuple>
|
||||
struct visitor_result_impl<
|
||||
Visitor, Tuple,
|
||||
typename std::enable_if<sprout::has_result_type<sprout::weak_result_type<Visitor> >::value>::type
|
||||
> {
|
||||
public:
|
||||
typedef typename sprout::weak_result_type<Visitor>::result_type type;
|
||||
};
|
||||
public:
|
||||
template<typename Visitor, typename Visitable>
|
||||
struct visitor_result
|
||||
: public visitor_result_impl<Visitor, typename Visitable::tuple_type>
|
||||
{};
|
||||
template<typename Visitor, typename Visitable>
|
||||
struct visitor_result<Visitor, Visitable const>
|
||||
: public visitor_result_impl<Visitor, typename Visitable::tuple_type const>
|
||||
{};
|
||||
template<typename Visitor, typename Visitable>
|
||||
struct visitor_result<Visitor, Visitable volatile>
|
||||
: public visitor_result_impl<Visitor, typename Visitable::tuple_type volatile>
|
||||
{};
|
||||
template<typename Visitor, typename Visitable>
|
||||
struct visitor_result<Visitor, Visitable const volatile>
|
||||
: public visitor_result_impl<Visitor, typename Visitable::tuple_type const volatile>
|
||||
{};
|
||||
private:
|
||||
template<int I>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<
|
||||
|
@ -124,21 +172,24 @@ namespace sprout {
|
|||
: output<I + 1>(os, t, which)
|
||||
;
|
||||
}
|
||||
template<int I, typename Tuple, typename Visitor>
|
||||
template<typename Result, int I, typename Tuple, typename Visitor>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<
|
||||
I == sizeof...(Types),
|
||||
typename std::decay<Visitor>::type::result_type
|
||||
>::type visit(Tuple&& t, Visitor&& v, int which) {
|
||||
return typename std::decay<Visitor>::type::result_type();
|
||||
}
|
||||
template<int I, typename Tuple, typename Visitor>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<
|
||||
I != sizeof...(Types),
|
||||
typename std::decay<Visitor>::type::result_type
|
||||
I == sizeof...(Types) - 1,
|
||||
Result
|
||||
>::type visit(Tuple&& t, Visitor&& v, int which) {
|
||||
return I == which
|
||||
? sprout::forward<Visitor>(v)(sprout::tuples::get<I>(sprout::forward<Tuple>(t)))
|
||||
: visit<I + 1>(sprout::forward<Tuple>(t), sprout::forward<Visitor>(v), which)
|
||||
: throw std::domain_error("variant<>: bad visit")
|
||||
;
|
||||
}
|
||||
template<typename Result, int I, typename Tuple, typename Visitor>
|
||||
static SPROUT_CONSTEXPR typename std::enable_if<
|
||||
I != sizeof...(Types) - 1,
|
||||
Result
|
||||
>::type visit(Tuple&& t, Visitor&& v, int which) {
|
||||
return I == which
|
||||
? sprout::forward<Visitor>(v)(sprout::tuples::get<I>(sprout::forward<Tuple>(t)))
|
||||
: visit<Result, I + 1>(sprout::forward<Tuple>(t), sprout::forward<Visitor>(v), which)
|
||||
;
|
||||
}
|
||||
private:
|
||||
|
@ -249,12 +300,16 @@ namespace sprout {
|
|||
}
|
||||
// visitation support
|
||||
template<typename Visitor>
|
||||
SPROUT_CONSTEXPR typename std::decay<Visitor>::type::result_type apply_visitor(Visitor&& visitor) const {
|
||||
return visit<0>(tuple_, sprout::forward<Visitor>(visitor), which_);
|
||||
SPROUT_CONSTEXPR typename visitor_result<typename std::remove_reference<Visitor>::type, variant const>::type
|
||||
apply_visitor(Visitor&& visitor) const {
|
||||
typedef typename visitor_result<typename std::remove_reference<Visitor>::type, variant const>::type result_type;
|
||||
return visit<result_type, 0>(tuple_, sprout::forward<Visitor>(visitor), which_);
|
||||
}
|
||||
template<typename Visitor>
|
||||
typename std::decay<Visitor>::type::result_type apply_visitor(Visitor&& visitor) {
|
||||
return visit<0>(tuple_, sprout::forward<Visitor>(visitor), which_);
|
||||
typename visitor_result<typename std::remove_reference<Visitor>::type, variant>::type
|
||||
apply_visitor(Visitor&& visitor) {
|
||||
typedef typename visitor_result<typename std::remove_reference<Visitor>::type, variant>::type result_type;
|
||||
return visit<result_type, 0>(tuple_, sprout::forward<Visitor>(visitor), which_);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue