diff options
author | Grigoriy Chudnov <g.chudnov@gmail.com> | 2017-01-07 00:10:24 +0300 |
---|---|---|
committer | Kirk Shoop <kirk.shoop@microsoft.com> | 2017-01-06 14:44:03 -0800 |
commit | 36b3d8c855002fbfb4e1b17258bab4a0702d6e54 (patch) | |
tree | 28530e1e5fcb023b2599d5b8a7af320731e495f2 /Rx/v2/src/rxcpp/operators | |
parent | b343f852570a1808c083cc8f6901e6febb46d892 (diff) | |
download | RxCpp-36b3d8c855002fbfb4e1b17258bab4a0702d6e54.tar.gz |
decouple take_last from observable
Diffstat (limited to 'Rx/v2/src/rxcpp/operators')
-rw-r--r-- | Rx/v2/src/rxcpp/operators/rx-take_last.hpp | 73 |
1 files changed, 54 insertions, 19 deletions
diff --git a/Rx/v2/src/rxcpp/operators/rx-take_last.hpp b/Rx/v2/src/rxcpp/operators/rx-take_last.hpp index 7d1297d..d59b355 100644 --- a/Rx/v2/src/rxcpp/operators/rx-take_last.hpp +++ b/Rx/v2/src/rxcpp/operators/rx-take_last.hpp @@ -2,6 +2,21 @@ #pragma once +/*! \file rx-take_last.hpp + + \brief Emit only the final t items emitted by the source Observable. + + \tparam Count the type of the items counter. + + \param t the number of last items to take. + + \return An observable that emits only the last t items emitted by the source Observable, or all of the items from the source observable if that observable emits fewer than t items. + + \sample + \snippet take_last.cpp take_last sample + \snippet output.txt take_last sample +*/ + #if !defined(RXCPP_OPERATORS_RX_TAKE_LAST_HPP) #define RXCPP_OPERATORS_RX_TAKE_LAST_HPP @@ -13,6 +28,16 @@ namespace operators { namespace detail { +template<class... AN> +struct take_last_invalid_arguments {}; + +template<class... AN> +struct take_last_invalid : public rxo::operator_base<take_last_invalid_arguments<AN...>> { + using type = observable<take_last_invalid_arguments<AN...>, take_last_invalid<AN...>>; +}; +template<class... AN> +using take_last_invalid_t = typename take_last_invalid<AN...>::type; + template<class T, class Observable, class Count> struct take_last : public operator_base<T> { @@ -90,31 +115,41 @@ struct take_last : public operator_base<T> } }; -template<class T> -class take_last_factory -{ - typedef rxu::decay_t<T> count_type; - count_type count; -public: - take_last_factory(count_type t) : count(std::move(t)) {} - template<class Observable> - auto operator()(Observable&& source) - -> observable<rxu::value_type_t<rxu::decay_t<Observable>>, take_last<rxu::value_type_t<rxu::decay_t<Observable>>, Observable, count_type>> { - return observable<rxu::value_type_t<rxu::decay_t<Observable>>, take_last<rxu::value_type_t<rxu::decay_t<Observable>>, Observable, count_type>>( - take_last<rxu::value_type_t<rxu::decay_t<Observable>>, Observable, count_type>(std::forward<Observable>(source), count)); - } -}; - } -template<class T> -auto take_last(T&& t) - -> detail::take_last_factory<T> { - return detail::take_last_factory<T>(std::forward<T>(t)); +/*! @copydoc rx-take_last.hpp +*/ +template<class... AN> +auto take_last(AN&&... an) + -> operator_factory<take_last_tag, AN...> { + return operator_factory<take_last_tag, AN...>(std::make_tuple(std::forward<AN>(an)...)); } } +template<> +struct member_overload<take_last_tag> +{ + template<class Observable, + class Count, + class Enabled = rxu::enable_if_all_true_type_t< + is_observable<Observable>>, + class SourceValue = rxu::value_type_t<Observable>, + class TakeLast = rxo::detail::take_last<SourceValue, rxu::decay_t<Observable>, rxu::decay_t<Count>>, + class Value = rxu::value_type_t<TakeLast>, + class Result = observable<Value, TakeLast>> + static Result member(Observable&& o, Count&& c) { + return Result(TakeLast(std::forward<Observable>(o), std::forward<Count>(c))); + } + + template<class... AN> + static operators::detail::take_last_invalid_t<AN...> member(AN...) { + std::terminate(); + return {}; + static_assert(sizeof...(AN) == 10000, "take_last takes (Count)"); + } +}; + } #endif |