1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
5 #if !defined(RXCPP_OPERATORS_RX_REPEAT_HPP)
6 #define RXCPP_OPERATORS_RX_REPEAT_HPP
8 #include "../rx-includes.hpp"
16 template<class T, class Observable, class Count>
17 struct repeat : public operator_base<T>
19 typedef typename std::decay<Observable>::type source_type;
20 typedef typename std::decay<Count>::type count_type;
23 values(source_type s, count_type t)
24 : source(std::move(s))
25 , remaining(std::move(t))
26 , repeat_infinitely(t == 0)
31 bool repeat_infinitely;
35 repeat(source_type s, count_type t)
36 : initial(std::move(s), std::move(t))
40 template<class Subscriber>
41 void on_subscribe(const Subscriber& s) const {
43 typedef Subscriber output_type;
45 : public std::enable_shared_from_this<state_type>
48 state_type(const values& i, const output_type& oarg)
50 , source_lifetime(composite_subscription::empty())
54 composite_subscription source_lifetime;
58 auto state = this->shared_from_this();
60 state->source_lifetime = composite_subscription();
61 state->out.add(state->source_lifetime);
63 state->source.subscribe(
65 state->source_lifetime,
68 state->out.on_next(t);
71 [state](std::exception_ptr e) {
72 state->out.on_error(e);
76 if (state->repeat_infinitely || (--state->remaining > 0)) {
77 state->do_subscribe();
79 state->out.on_completed();
86 // take a copy of the values for each subscription
87 auto state = std::shared_ptr<state_type>(new state_type(initial, s));
89 // start the first iteration
90 state->do_subscribe();
97 typedef typename std::decay<T>::type count_type;
100 repeat_factory(count_type t) : count(std::move(t)) {}
102 template<class Observable>
103 auto operator()(Observable&& source)
104 -> observable<typename std::decay<Observable>::type::value_type, repeat<typename std::decay<Observable>::type::value_type, Observable, count_type>> {
105 return observable<typename std::decay<Observable>::type::value_type, repeat<typename std::decay<Observable>::type::value_type, Observable, count_type>>(
106 repeat<typename std::decay<Observable>::type::value_type, Observable, count_type>(std::forward<Observable>(source), count));
114 -> detail::repeat_factory<T> {
115 return detail::repeat_factory<T>(std::forward<T>(t));