diff --git a/src/rxcpp/RxCpp-4.1.1.zip b/src/rxcpp/RxCpp-4.1.1.zip new file mode 100644 index 000000000..c0b6c922a Binary files /dev/null and b/src/rxcpp/RxCpp-4.1.1.zip differ diff --git a/src/rxcpp/build_rxcpp.ps1 b/src/rxcpp/build_rxcpp.ps1 index 33fa2bf47..9c0179dd0 100644 --- a/src/rxcpp/build_rxcpp.ps1 +++ b/src/rxcpp/build_rxcpp.ps1 @@ -1,4 +1,4 @@ -$rxcpp = "rxcpp-4.0.0" +$rxcpp = "RxCpp-4.1.1" $root = Resolve-Path "../.." $work = Join-Path $root "work" $src = Join-Path $root src diff --git a/windows/include/rxcpp/operators/rx-all.hpp b/windows/include/rxcpp/operators/rx-all.hpp index 65bec4660..a0f6a3ea4 100644 --- a/windows/include/rxcpp/operators/rx-all.hpp +++ b/windows/include/rxcpp/operators/rx-all.hpp @@ -83,7 +83,7 @@ struct all dest.on_completed(); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-amb.hpp b/windows/include/rxcpp/operators/rx-amb.hpp index 595dc4dd6..56bfbe9aa 100644 --- a/windows/include/rxcpp/operators/rx-amb.hpp +++ b/windows/include/rxcpp/operators/rx-amb.hpp @@ -183,7 +183,7 @@ struct amb } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, //on_completed @@ -196,7 +196,7 @@ struct amb selectedSource.subscribe(std::move(selectedSinkInner)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-any.hpp b/windows/include/rxcpp/operators/rx-any.hpp index 44883751e..19fca6e56 100644 --- a/windows/include/rxcpp/operators/rx-any.hpp +++ b/windows/include/rxcpp/operators/rx-any.hpp @@ -90,7 +90,7 @@ struct any dest.on_completed(); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-buffer_count.hpp b/windows/include/rxcpp/operators/rx-buffer_count.hpp index a4dea3b2d..79eb30e4f 100644 --- a/windows/include/rxcpp/operators/rx-buffer_count.hpp +++ b/windows/include/rxcpp/operators/rx-buffer_count.hpp @@ -96,7 +96,7 @@ struct buffer_count chunks.pop_front(); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-buffer_time.hpp b/windows/include/rxcpp/operators/rx-buffer_time.hpp index 7e54eb8e4..aa94f7bf1 100644 --- a/windows/include/rxcpp/operators/rx-buffer_time.hpp +++ b/windows/include/rxcpp/operators/rx-buffer_time.hpp @@ -197,7 +197,7 @@ struct buffer_with_time } localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ localState->dest.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-buffer_time_count.hpp b/windows/include/rxcpp/operators/rx-buffer_time_count.hpp index 346a6ef21..6d4b9a4c3 100644 --- a/windows/include/rxcpp/operators/rx-buffer_time_count.hpp +++ b/windows/include/rxcpp/operators/rx-buffer_time_count.hpp @@ -179,7 +179,7 @@ struct buffer_with_time_or_count } localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ localState->dest.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-combine_latest.hpp b/windows/include/rxcpp/operators/rx-combine_latest.hpp index 0f94fde78..06ff3d1e6 100644 --- a/windows/include/rxcpp/operators/rx-combine_latest.hpp +++ b/windows/include/rxcpp/operators/rx-combine_latest.hpp @@ -173,7 +173,7 @@ struct combine_latest : public operator_baseout.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-concat.hpp b/windows/include/rxcpp/operators/rx-concat.hpp index 57b42fa5e..457b9cfdb 100644 --- a/windows/include/rxcpp/operators/rx-concat.hpp +++ b/windows/include/rxcpp/operators/rx-concat.hpp @@ -143,7 +143,7 @@ struct concat state->out.on_next(ct); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, //on_completed @@ -207,7 +207,7 @@ struct concat } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-concat_map.hpp b/windows/include/rxcpp/operators/rx-concat_map.hpp index edcdb2683..546c1eb9d 100644 --- a/windows/include/rxcpp/operators/rx-concat_map.hpp +++ b/windows/include/rxcpp/operators/rx-concat_map.hpp @@ -183,7 +183,7 @@ struct concat_map state->out.on_next(std::move(selectedResult)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, //on_completed @@ -246,7 +246,7 @@ struct concat_map } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-debounce.hpp b/windows/include/rxcpp/operators/rx-debounce.hpp index 5f56ff9d4..6fbedd7a3 100644 --- a/windows/include/rxcpp/operators/rx-debounce.hpp +++ b/windows/include/rxcpp/operators/rx-debounce.hpp @@ -158,7 +158,7 @@ struct debounce localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&) { localState->dest.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-delay.hpp b/windows/include/rxcpp/operators/rx-delay.hpp index 19bbed567..5986f79db 100644 --- a/windows/include/rxcpp/operators/rx-delay.hpp +++ b/windows/include/rxcpp/operators/rx-delay.hpp @@ -131,7 +131,7 @@ struct delay localState->worker.schedule(localState->worker.now() + localState->period, selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ localState->dest.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-distinct.hpp b/windows/include/rxcpp/operators/rx-distinct.hpp index c3ca08628..c90ebdbfe 100644 --- a/windows/include/rxcpp/operators/rx-distinct.hpp +++ b/windows/include/rxcpp/operators/rx-distinct.hpp @@ -61,7 +61,7 @@ struct distinct dest.on_next(v); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-distinct_until_changed.hpp b/windows/include/rxcpp/operators/rx-distinct_until_changed.hpp index 7d8ef5a97..370218566 100644 --- a/windows/include/rxcpp/operators/rx-distinct_until_changed.hpp +++ b/windows/include/rxcpp/operators/rx-distinct_until_changed.hpp @@ -74,7 +74,7 @@ struct distinct_until_changed dest.on_next(v); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-element_at.hpp b/windows/include/rxcpp/operators/rx-element_at.hpp index 5cbe6dc41..1d773ac92 100644 --- a/windows/include/rxcpp/operators/rx-element_at.hpp +++ b/windows/include/rxcpp/operators/rx-element_at.hpp @@ -77,12 +77,12 @@ struct element_at { dest.on_completed(); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { if(current <= this->index) { - dest.on_error(std::make_exception_ptr(std::range_error("index is out of bounds"))); + dest.on_error(rxu::make_error_ptr(std::range_error("index is out of bounds"))); } } diff --git a/windows/include/rxcpp/operators/rx-filter.hpp b/windows/include/rxcpp/operators/rx-filter.hpp index ef6e133d9..3a622bb01 100644 --- a/windows/include/rxcpp/operators/rx-filter.hpp +++ b/windows/include/rxcpp/operators/rx-filter.hpp @@ -58,25 +58,28 @@ struct filter typedef rxu::decay_t dest_type; typedef observer observer_type; dest_type dest; - test_type test; + mutable test_type test; filter_observer(dest_type d, test_type t) : dest(std::move(d)) , test(std::move(t)) { } - void on_next(source_value_type v) const { + + template + void on_next(Value&& v) const { auto filtered = on_exception([&](){ - return !this->test(v);}, + return !this->test(rxu::as_const(v)); + }, dest); if (filtered.empty()) { return; } if (!filtered.get()) { - dest.on_next(v); + dest.on_next(std::forward(v)); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-finally.hpp b/windows/include/rxcpp/operators/rx-finally.hpp index 4e4416cf5..d6f548722 100644 --- a/windows/include/rxcpp/operators/rx-finally.hpp +++ b/windows/include/rxcpp/operators/rx-finally.hpp @@ -70,7 +70,7 @@ struct finally void on_next(source_value_type v) const { dest.on_next(v); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-flat_map.hpp b/windows/include/rxcpp/operators/rx-flat_map.hpp index 5b68b37b4..eb761987d 100644 --- a/windows/include/rxcpp/operators/rx-flat_map.hpp +++ b/windows/include/rxcpp/operators/rx-flat_map.hpp @@ -198,7 +198,7 @@ struct flat_map state->out.on_next(std::move(selectedResult)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, //on_completed @@ -213,7 +213,7 @@ struct flat_map selectedSource.subscribe(std::move(selectedSinkInner)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-group_by.hpp b/windows/include/rxcpp/operators/rx-group_by.hpp index 4eadbab95..d1c4ea416 100644 --- a/windows/include/rxcpp/operators/rx-group_by.hpp +++ b/windows/include/rxcpp/operators/rx-group_by.hpp @@ -9,6 +9,7 @@ \tparam KeySelector the type of the key extracting function \tparam MarbleSelector the type of the element extracting function \tparam BinaryPredicate the type of the key comparing function + \tparam DurationSelector the type of the duration observable function \param ks a function that extracts the key for each item (optional) \param ms a function that extracts the return element for each item (optional) @@ -63,7 +64,7 @@ struct is_group_by_selector_for { static const bool value = !std::is_same::value; }; -template +template struct group_by_traits { typedef T source_value_type; @@ -71,6 +72,7 @@ struct group_by_traits typedef rxu::decay_t key_selector_type; typedef rxu::decay_t marble_selector_type; typedef rxu::decay_t predicate_type; + typedef rxu::decay_t duration_selector_type; static_assert(is_group_by_selector_for::value, "group_by KeySelector must be a function with the signature key_type(source_value_type)"); @@ -87,14 +89,15 @@ struct group_by_traits typedef grouped_observable grouped_observable_type; }; -template +template struct group_by { - typedef group_by_traits traits_type; + typedef group_by_traits traits_type; typedef typename traits_type::key_selector_type key_selector_type; typedef typename traits_type::marble_selector_type marble_selector_type; typedef typename traits_type::marble_type marble_type; typedef typename traits_type::predicate_type predicate_type; + typedef typename traits_type::duration_selector_type duration_selector_type; typedef typename traits_type::subject_type subject_type; typedef typename traits_type::key_type key_type; @@ -130,21 +133,23 @@ struct group_by struct group_by_values { - group_by_values(key_selector_type ks, marble_selector_type ms, predicate_type p) + group_by_values(key_selector_type ks, marble_selector_type ms, predicate_type p, duration_selector_type ds) : keySelector(std::move(ks)) , marbleSelector(std::move(ms)) , predicate(std::move(p)) + , durationSelector(std::move(ds)) { } mutable key_selector_type keySelector; mutable marble_selector_type marbleSelector; mutable predicate_type predicate; + mutable duration_selector_type durationSelector; }; group_by_values initial; - group_by(key_selector_type ks, marble_selector_type ms, predicate_type p) - : initial(std::move(ks), std::move(ms), std::move(p)) + group_by(key_selector_type ks, marble_selector_type ms, predicate_type p, duration_selector_type ds) + : initial(std::move(ks), std::move(ms), std::move(p), std::move(ds)) { } @@ -195,7 +200,7 @@ struct group_by auto selectedKey = on_exception( [&](){ return this->keySelector(v);}, - [this](std::exception_ptr e){on_error(e);}); + [this](rxu::error_ptr e){on_error(e);}); if (selectedKey.empty()) { return; } @@ -206,18 +211,46 @@ struct group_by } auto sub = subject_type(); g = state->groups.insert(std::make_pair(selectedKey.get(), sub.get_subscriber())).first; - dest.on_next(make_dynamic_grouped_observable(group_by_observable(state, sub, selectedKey.get()))); + auto obs = make_dynamic_grouped_observable(group_by_observable(state, sub, selectedKey.get())); + auto durationObs = on_exception( + [&](){ + return this->durationSelector(obs);}, + [this](rxu::error_ptr e){on_error(e);}); + if (durationObs.empty()) { + return; + } + + dest.on_next(obs); + composite_subscription duration_sub; + auto ssub = state->source_lifetime.add(duration_sub); + + auto expire_state = state; + auto expire_dest = g->second; + auto expire = [=]() { + auto g = expire_state->groups.find(selectedKey.get()); + if (g != expire_state->groups.end()) { + expire_state->groups.erase(g); + expire_dest.on_completed(); + } + expire_state->source_lifetime.remove(ssub); + }; + auto robs = durationObs.get().take(1); + duration_sub.add(robs.subscribe( + [](const typename decltype(robs)::value_type &){}, + [=](rxu::error_ptr) {expire();}, + [=](){expire();} + )); } auto selectedMarble = on_exception( [&](){ return this->marbleSelector(v);}, - [this](std::exception_ptr e){on_error(e);}); + [this](rxu::error_ptr e){on_error(e);}); if (selectedMarble.empty()) { return; } g->second.on_next(std::move(selectedMarble.get())); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { for(auto& g : state->groups) { g.second.on_error(e); } @@ -243,33 +276,36 @@ struct group_by } }; -template +template class group_by_factory { typedef rxu::decay_t key_selector_type; typedef rxu::decay_t marble_selector_type; typedef rxu::decay_t predicate_type; + typedef rxu::decay_t duration_selector_type; key_selector_type keySelector; marble_selector_type marbleSelector; predicate_type predicate; + duration_selector_type durationSelector; public: - group_by_factory(key_selector_type ks, marble_selector_type ms, predicate_type p) + group_by_factory(key_selector_type ks, marble_selector_type ms, predicate_type p, duration_selector_type ds) : keySelector(std::move(ks)) , marbleSelector(std::move(ms)) , predicate(std::move(p)) + , durationSelector(std::move(ds)) { } template struct group_by_factory_traits { typedef rxu::value_type_t> value_type; - typedef detail::group_by_traits traits_type; - typedef detail::group_by group_by_type; + typedef detail::group_by_traits traits_type; + typedef detail::group_by group_by_type; }; template auto operator()(Observable&& source) - -> decltype(source.template lift::traits_type::grouped_observable_type>(typename group_by_factory_traits::group_by_type(std::move(keySelector), std::move(marbleSelector), std::move(predicate)))) { - return source.template lift::traits_type::grouped_observable_type>(typename group_by_factory_traits::group_by_type(std::move(keySelector), std::move(marbleSelector), std::move(predicate))); + -> decltype(source.template lift::traits_type::grouped_observable_type>(typename group_by_factory_traits::group_by_type(std::move(keySelector), std::move(marbleSelector), std::move(predicate), std::move(durationSelector)))) { + return source.template lift::traits_type::grouped_observable_type>(typename group_by_factory_traits::group_by_type(std::move(keySelector), std::move(marbleSelector), std::move(predicate), std::move(durationSelector))); } }; @@ -288,61 +324,75 @@ auto group_by(AN&&... an) template<> struct member_overload { - template, - class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate>, - class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t>, + class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate, DurationSelector>, + class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t, rxu::decay_t>, class Value = typename Traits::grouped_observable_type> - static auto member(Observable&& o, KeySelector&& ks, MarbleSelector&& ms, BinaryPredicate&& p) - -> decltype(o.template lift(GroupBy(std::forward(ks), std::forward(ms), std::forward(p)))) { - return o.template lift(GroupBy(std::forward(ks), std::forward(ms), std::forward(p))); + static auto member(Observable&& o, KeySelector&& ks, MarbleSelector&& ms, BinaryPredicate&& p, DurationSelector&& ds) + -> decltype(o.template lift(GroupBy(std::forward(ks), std::forward(ms), std::forward(p), std::forward(ds)))) { + return o.template lift(GroupBy(std::forward(ks), std::forward(ms), std::forward(p), std::forward(ds))); } - template>>, class SourceValue = rxu::value_type_t, - class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate>, - class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t>, + class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate, DurationSelector>, + class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t, rxu::decay_t>, + class Value = typename Traits::grouped_observable_type> + static auto member(Observable&& o, KeySelector&& ks, MarbleSelector&& ms, BinaryPredicate&& p) + -> decltype(o.template lift(GroupBy(std::forward(ks), std::forward(ms), std::forward(p), rxu::ret>>()))) { + return o.template lift(GroupBy(std::forward(ks), std::forward(ms), std::forward(p), rxu::ret>>())); + } + + template>>, + class SourceValue = rxu::value_type_t, + class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate, DurationSelector>, + class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t, rxu::decay_t>, class Value = typename Traits::grouped_observable_type> static auto member(Observable&& o, KeySelector&& ks, MarbleSelector&& ms) - -> decltype(o.template lift(GroupBy(std::forward(ks), std::forward(ms), rxu::less()))) { - return o.template lift(GroupBy(std::forward(ks), std::forward(ms), rxu::less())); + -> decltype(o.template lift(GroupBy(std::forward(ks), std::forward(ms), rxu::less(), rxu::ret>>()))) { + return o.template lift(GroupBy(std::forward(ks), std::forward(ms), rxu::less(), rxu::ret>>())); } template, class BinaryPredicate=rxu::less, + class DurationSelector=rxu::ret>>, class SourceValue = rxu::value_type_t, - class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate>, - class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t>, + class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate, DurationSelector>, + class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t, rxu::decay_t>, class Value = typename Traits::grouped_observable_type> static auto member(Observable&& o, KeySelector&& ks) - -> decltype(o.template lift(GroupBy(std::forward(ks), rxu::detail::take_at<0>(), rxu::less()))) { - return o.template lift(GroupBy(std::forward(ks), rxu::detail::take_at<0>(), rxu::less())); + -> decltype(o.template lift(GroupBy(std::forward(ks), rxu::detail::take_at<0>(), rxu::less(), rxu::ret>>()))) { + return o.template lift(GroupBy(std::forward(ks), rxu::detail::take_at<0>(), rxu::less(), rxu::ret>>())); } template, class MarbleSelector=rxu::detail::take_at<0>, class BinaryPredicate=rxu::less, + class DurationSelector=rxu::ret>>, class Enabled = rxu::enable_if_all_true_type_t< all_observables>, class SourceValue = rxu::value_type_t, - class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate>, - class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t>, + class Traits = rxo::detail::group_by_traits, KeySelector, MarbleSelector, BinaryPredicate, DurationSelector>, + class GroupBy = rxo::detail::group_by, rxu::decay_t, rxu::decay_t, rxu::decay_t, rxu::decay_t>, class Value = typename Traits::grouped_observable_type> static auto member(Observable&& o) - -> decltype(o.template lift(GroupBy(rxu::detail::take_at<0>(), rxu::detail::take_at<0>(), rxu::less()))) { - return o.template lift(GroupBy(rxu::detail::take_at<0>(), rxu::detail::take_at<0>(), rxu::less())); + -> decltype(o.template lift(GroupBy(rxu::detail::take_at<0>(), rxu::detail::take_at<0>(), rxu::less(), rxu::ret>>()))) { + return o.template lift(GroupBy(rxu::detail::take_at<0>(), rxu::detail::take_at<0>(), rxu::less(), rxu::ret>>())); } template static operators::detail::group_by_invalid_t member(const AN&...) { std::terminate(); return {}; - static_assert(sizeof...(AN) == 10000, "group_by takes (optional KeySelector, optional MarbleSelector, optional BinaryKeyPredicate), KeySelector takes (Observable::value_type) -> KeyValue, MarbleSelector takes (Observable::value_type) -> MarbleValue, BinaryKeyPredicate takes (KeyValue, KeyValue) -> bool"); - } + static_assert(sizeof...(AN) == 10000, "group_by takes (optional KeySelector, optional MarbleSelector, optional BinaryKeyPredicate, optional DurationSelector), KeySelector takes (Observable::value_type) -> KeyValue, MarbleSelector takes (Observable::value_type) -> MarbleValue, BinaryKeyPredicate takes (KeyValue, KeyValue) -> bool, DurationSelector takes (Observable::value_type) -> Observable"); + } }; diff --git a/windows/include/rxcpp/operators/rx-ignore_elements.hpp b/windows/include/rxcpp/operators/rx-ignore_elements.hpp index 82fbe9582..00b10a64e 100644 --- a/windows/include/rxcpp/operators/rx-ignore_elements.hpp +++ b/windows/include/rxcpp/operators/rx-ignore_elements.hpp @@ -56,7 +56,7 @@ struct ignore_elements { // no-op; ignore element } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } diff --git a/windows/include/rxcpp/operators/rx-map.hpp b/windows/include/rxcpp/operators/rx-map.hpp index a670d88b5..7570376c6 100644 --- a/windows/include/rxcpp/operators/rx-map.hpp +++ b/windows/include/rxcpp/operators/rx-map.hpp @@ -77,7 +77,7 @@ struct map } dest.on_next(std::move(selected.get())); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-merge.hpp b/windows/include/rxcpp/operators/rx-merge.hpp index ad8f7d020..cc1ea7783 100644 --- a/windows/include/rxcpp/operators/rx-merge.hpp +++ b/windows/include/rxcpp/operators/rx-merge.hpp @@ -173,7 +173,7 @@ struct merge state->out.on_next(std::move(ct)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, //on_completed @@ -188,7 +188,7 @@ struct merge selectedSource.subscribe(std::move(selectedSinkInner)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-merge_delay_error.hpp b/windows/include/rxcpp/operators/rx-merge_delay_error.hpp new file mode 100644 index 000000000..f1e72eb3f --- /dev/null +++ b/windows/include/rxcpp/operators/rx-merge_delay_error.hpp @@ -0,0 +1,304 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +#pragma once + +/*! \file rx-merge_delay_error.hpp + + \brief For each given observable subscribe. + For each item emitted from all of the given observables, deliver from the new observable that is returned. + The first error to occure is hold off until all of the given non-error-emitting observables have finished their emission. + + There are 2 variants of the operator: + - The source observable emits nested observables, nested observables are merged. + - The source observable and the arguments v0...vn are used to provide the observables to merge. + + \tparam Coordination the type of the scheduler (optional). + \tparam Value0 ... (optional). + \tparam ValueN types of source observables (optional). + + \param cn the scheduler to synchronize sources from different contexts (optional). + \param v0 ... (optional). + \param vn source observables (optional). + + \return Observable that emits items that are the result of flattening the observables emitted by the source observable. + + If scheduler is omitted, identity_current_thread is used. + + \sample + \snippet merge_delay_error.cpp threaded implicit merge sample + \snippet output.txt threaded implicit merge sample + + \sample + \snippet merge_delay_error.cpp implicit merge sample + \snippet output.txt implicit merge sample + + \sample + \snippet merge_delay_error.cpp merge sample + \snippet output.txt merge sample + + \sample + \snippet merge_delay_error.cpp threaded merge sample + \snippet output.txt threaded merge sample +*/ + +#if !defined(RXCPP_OPERATORS_RX_MERGE_DELAY_ERROR_HPP) +#define RXCPP_OPERATORS_RX_MERGE_DELAY_ERROR_HPP + +#include "rx-merge.hpp" + +#include "../rx-composite_exception.hpp" + +namespace rxcpp { + +namespace operators { + +namespace detail { + +template +struct merge_delay_error + : public operator_base>> +{ + //static_assert(is_observable::value, "merge requires an observable"); + //static_assert(is_observable::value, "merge requires an observable that contains observables"); + + typedef merge_delay_error this_type; + + typedef rxu::decay_t source_value_type; + typedef rxu::decay_t source_type; + + typedef typename source_type::source_operator_type source_operator_type; + typedef typename source_value_type::value_type value_type; + + typedef rxu::decay_t coordination_type; + typedef typename coordination_type::coordinator_type coordinator_type; + + struct values + { + values(source_operator_type o, coordination_type sf) + : source_operator(std::move(o)) + , coordination(std::move(sf)) + { + } + source_operator_type source_operator; + coordination_type coordination; + }; + values initial; + + merge_delay_error(const source_type& o, coordination_type sf) + : initial(o.source_operator, std::move(sf)) + { + } + + template + void on_subscribe(Subscriber scbr) const { + static_assert(is_subscriber::value, "subscribe must be passed a subscriber"); + + typedef Subscriber output_type; + + struct merge_state_type + : public std::enable_shared_from_this + , public values + { + merge_state_type(values i, coordinator_type coor, output_type oarg) + : values(i) + , source(i.source_operator) + , pendingCompletions(0) + , coordinator(std::move(coor)) + , out(std::move(oarg)) + { + } + observable source; + // on_completed on the output must wait until all the + // subscriptions have received on_completed + int pendingCompletions; + composite_exception exception; + coordinator_type coordinator; + output_type out; + }; + + auto coordinator = initial.coordination.create_coordinator(scbr.get_subscription()); + + // take a copy of the values for each subscription + auto state = std::make_shared(initial, std::move(coordinator), std::move(scbr)); + + composite_subscription outercs; + + // when the out observer is unsubscribed all the + // inner subscriptions are unsubscribed as well + state->out.add(outercs); + + auto source = on_exception( + [&](){return state->coordinator.in(state->source);}, + state->out); + if (source.empty()) { + return; + } + + ++state->pendingCompletions; + // this subscribe does not share the observer subscription + // so that when it is unsubscribed the observer can be called + // until the inner subscriptions have finished + auto sink = make_subscriber( + state->out, + outercs, + // on_next + [state](source_value_type st) { + + composite_subscription innercs; + + // when the out observer is unsubscribed all the + // inner subscriptions are unsubscribed as well + auto innercstoken = state->out.add(innercs); + + innercs.add(make_subscription([state, innercstoken](){ + state->out.remove(innercstoken); + })); + + auto selectedSource = state->coordinator.in(st); + + ++state->pendingCompletions; + // this subscribe does not share the source subscription + // so that when it is unsubscribed the source will continue + auto sinkInner = make_subscriber( + state->out, + innercs, + // on_next + [state, st](value_type ct) { + state->out.on_next(std::move(ct)); + }, + // on_error + [state](rxu::error_ptr e) { + if(--state->pendingCompletions == 0) { + state->out.on_error( + rxu::make_error_ptr(std::move(state->exception.add(e)))); + } else { + state->exception.add(e); + } + }, + //on_completed + [state](){ + if (--state->pendingCompletions == 0) { + if(!state->exception.empty()) { + state->out.on_error( + rxu::make_error_ptr(std::move(state->exception))); + } else { + state->out.on_completed(); + } + } + } + ); + + auto selectedSinkInner = state->coordinator.out(sinkInner); + selectedSource.subscribe(std::move(selectedSinkInner)); + }, + // on_error + [state](rxu::error_ptr e) { + if(--state->pendingCompletions == 0) { + state->out.on_error( + rxu::make_error_ptr(std::move(state->exception.add(e)))); + } else { + state->exception.add(e); + } + }, + // on_completed + [state]() { + if (--state->pendingCompletions == 0) { + if(!state->exception.empty()) { + state->out.on_error( + rxu::make_error_ptr(std::move(state->exception))); + } else { + state->out.on_completed(); + } + } + } + ); + auto selectedSink = on_exception( + [&](){return state->coordinator.out(sink);}, + state->out); + if (selectedSink.empty()) { + return; + } + source->subscribe(std::move(selectedSink.get())); + } +}; + +} + +/*! @copydoc rx-merge-delay-error.hpp +*/ +template +auto merge_delay_error(AN&&... an) + -> operator_factory { + return operator_factory(std::make_tuple(std::forward(an)...)); +} + +} + +template<> +struct member_overload +{ + template>, + class SourceValue = rxu::value_type_t, + class Merge = rxo::detail::merge_delay_error, identity_one_worker>, + class Value = rxu::value_type_t, + class Result = observable + > + static Result member(Observable&& o) { + return Result(Merge(std::forward(o), identity_current_thread())); + } + + template, + is_coordination>, + class SourceValue = rxu::value_type_t, + class Merge = rxo::detail::merge_delay_error, rxu::decay_t>, + class Value = rxu::value_type_t, + class Result = observable + > + static Result member(Observable&& o, Coordination&& cn) { + return Result(Merge(std::forward(o), std::forward(cn))); + } + + template>, + class EmittedValue = rxu::value_type_t, + class SourceValue = observable, + class ObservableObservable = observable, + class Merge = typename rxu::defer_type::type, + class Value = rxu::value_type_t, + class Result = observable + > + static Result member(Observable&& o, Value0&& v0, ValueN&&... vn) { + return Result(Merge(rxs::from(o.as_dynamic(), v0.as_dynamic(), vn.as_dynamic()...), identity_current_thread())); + } + + template, + is_coordination>, + class EmittedValue = rxu::value_type_t, + class SourceValue = observable, + class ObservableObservable = observable, + class Merge = typename rxu::defer_type>::type, + class Value = rxu::value_type_t, + class Result = observable + > + static Result member(Observable&& o, Coordination&& cn, Value0&& v0, ValueN&&... vn) { + return Result(Merge(rxs::from(o.as_dynamic(), v0.as_dynamic(), vn.as_dynamic()...), std::forward(cn))); + } + + template + static operators::detail::merge_invalid_t member(AN...) { + std::terminate(); + return {}; + static_assert(sizeof...(AN) == 10000, "merge_delay_error takes (optional Coordination, optional Value0, optional ValueN...)"); + } +}; + +} + +#endif diff --git a/windows/include/rxcpp/operators/rx-observe_on.hpp b/windows/include/rxcpp/operators/rx-observe_on.hpp index 99de4c323..b50b773c0 100644 --- a/windows/include/rxcpp/operators/rx-observe_on.hpp +++ b/windows/include/rxcpp/operators/rx-observe_on.hpp @@ -127,7 +127,7 @@ struct observe_on auto drain = [keepAlive, this](const rxsc::schedulable& self){ using std::swap; - try { + RXCPP_TRY { for (;;) { if (drain_queue.empty() || !destination.is_subscribed()) { std::unique_lock guard(lock); @@ -151,8 +151,9 @@ struct observe_on self(); if (lifetime.is_subscribed()) break; } - } catch(...) { - destination.on_error(std::current_exception()); + } + RXCPP_CATCH(...) { + destination.on_error(rxu::current_exception()); std::unique_lock guard(lock); finish(guard, mode::Errored); } @@ -188,7 +189,7 @@ struct observe_on state->fill_queue.push_back(notification_type::on_next(std::move(v))); state->ensure_processing(guard); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { std::unique_lock guard(state->lock); if (state->current == mode::Errored || state->current == mode::Disposed) { return; } state->fill_queue.push_back(notification_type::on_error(e)); @@ -316,8 +317,7 @@ public: }; inline observe_on_one_worker observe_on_run_loop(const rxsc::run_loop& rl) { - static observe_on_one_worker r(rxsc::make_run_loop(rl)); - return r; + return observe_on_one_worker(rxsc::make_run_loop(rl)); } inline observe_on_one_worker observe_on_event_loop() { diff --git a/windows/include/rxcpp/operators/rx-on_error_resume_next.hpp b/windows/include/rxcpp/operators/rx-on_error_resume_next.hpp index 4ebee385c..bc9beba54 100644 --- a/windows/include/rxcpp/operators/rx-on_error_resume_next.hpp +++ b/windows/include/rxcpp/operators/rx-on_error_resume_next.hpp @@ -6,9 +6,9 @@ \brief If an error occurs, take the result from the Selector and subscribe to that instead. - \tparam Selector the actual type of a function of the form `observable(std::exception_ptr)` + \tparam Selector the actual type of a function of the form `observable(rxu::error_ptr)` - \param s the function of the form `observable(std::exception_ptr)` + \param s the function of the form `observable(rxu::error_ptr)` \return Observable that emits the items from the source observable and switches to a new observable on error. @@ -44,7 +44,7 @@ struct on_error_resume_next { typedef rxu::decay_t value_type; typedef rxu::decay_t select_type; - typedef decltype((*(select_type*)nullptr)(std::exception_ptr())) fallback_type; + typedef decltype((*(select_type*)nullptr)(rxu::error_ptr())) fallback_type; select_type selector; on_error_resume_next(select_type s) @@ -58,7 +58,7 @@ struct on_error_resume_next typedef on_error_resume_next_observer this_type; typedef rxu::decay_t value_type; typedef rxu::decay_t select_type; - typedef decltype((*(select_type*)nullptr)(std::exception_ptr())) fallback_type; + typedef decltype((*(select_type*)nullptr)(rxu::error_ptr())) fallback_type; typedef rxu::decay_t dest_type; typedef observer observer_type; dest_type dest; @@ -75,7 +75,7 @@ struct on_error_resume_next void on_next(value_type v) const { dest.on_next(std::move(v)); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto selected = on_exception( [&](){ return this->selector(std::move(e));}, diff --git a/windows/include/rxcpp/operators/rx-pairwise.hpp b/windows/include/rxcpp/operators/rx-pairwise.hpp index 584a6bf52..411cf2753 100644 --- a/windows/include/rxcpp/operators/rx-pairwise.hpp +++ b/windows/include/rxcpp/operators/rx-pairwise.hpp @@ -67,7 +67,7 @@ struct pairwise dest.on_next(std::make_tuple(remembered.get(), v)); remembered.reset(v); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-publish.hpp b/windows/include/rxcpp/operators/rx-publish.hpp index dc3819102..bc686fcfe 100644 --- a/windows/include/rxcpp/operators/rx-publish.hpp +++ b/windows/include/rxcpp/operators/rx-publish.hpp @@ -21,6 +21,18 @@ \sample \snippet publish.cpp publish behavior sample \snippet output.txt publish behavior sample + + \sample + \snippet publish.cpp publish diamond samethread sample + \snippet output.txt publish diamond samethread sample + + \sample + \snippet publish.cpp publish diamond bgthread sample + \snippet output.txt publish diamond bgthread sample + + \sample + \snippet ref_count.cpp ref_count other diamond sample + \snippet output.txt ref_count other diamond sample */ #if !defined(RXCPP_OPERATORS_RX_PUBLISH_HPP) diff --git a/windows/include/rxcpp/operators/rx-reduce.hpp b/windows/include/rxcpp/operators/rx-reduce.hpp index 7fc9d08f9..e5dc819a5 100644 --- a/windows/include/rxcpp/operators/rx-reduce.hpp +++ b/windows/include/rxcpp/operators/rx-reduce.hpp @@ -166,7 +166,7 @@ struct reduce : public operator_basecurrent = std::move(next); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed @@ -244,7 +244,7 @@ struct average { } return avg; } - throw rxcpp::empty_error("average() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("average() requires a stream with at least one value")); } }; @@ -264,7 +264,7 @@ struct sum { } T operator()(seed_type a) const { if (a.empty()) - throw rxcpp::empty_error("sum() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("sum() requires a stream with at least one value")); return *a; } }; @@ -283,7 +283,7 @@ struct max { } T operator()(seed_type a) { if (a.empty()) - throw rxcpp::empty_error("max() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("max() requires a stream with at least one value")); return *a; } }; @@ -302,7 +302,7 @@ struct min { } T operator()(seed_type a) { if (a.empty()) - throw rxcpp::empty_error("min() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("min() requires a stream with at least one value")); return *a; } }; @@ -320,7 +320,7 @@ struct first { } T operator()(seed_type a) { if (a.empty()) { - throw rxcpp::empty_error("first() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("first() requires a stream with at least one value")); } return *a; } @@ -339,7 +339,7 @@ struct last { } T operator()(seed_type a) { if (a.empty()) { - throw rxcpp::empty_error("last() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("last() requires a stream with at least one value")); } return *a; } diff --git a/windows/include/rxcpp/operators/rx-ref_count.hpp b/windows/include/rxcpp/operators/rx-ref_count.hpp index 55dde05f0..1ce9a40dc 100644 --- a/windows/include/rxcpp/operators/rx-ref_count.hpp +++ b/windows/include/rxcpp/operators/rx-ref_count.hpp @@ -4,10 +4,26 @@ /*! \file rx-ref_count.hpp - \brief takes a connectable_observable source and uses a ref_count of the subscribers to control the connection to the published source. - The first subscription will cause a call to connect() and the last unsubscribe will unsubscribe the connection. + \brief Make some \c connectable_observable behave like an ordinary \c observable. + Uses a reference count of the subscribers to control the connection to the published observable. - \return An observable that emitting the items from its source. + The first subscription will cause a call to \c connect(), and the last \c unsubscribe will unsubscribe the connection. + + There are 2 variants of the operator: + \li \c ref_count(): calls \c connect on the \c source \c connectable_observable. + \li \c ref_count(other): calls \c connect on the \c other \c connectable_observable. + + \tparam ConnectableObservable the type of the \c other \c connectable_observable (optional) + \param other \c connectable_observable to call \c connect on (optional) + + If \c other is omitted, then \c source is used instead (which must be a \c connectable_observable). + Otherwise, \c source can be a regular \c observable. + + \return An \c observable that emits the items from its \c source. + + \sample + \snippet ref_count.cpp ref_count other diamond sample + \snippet output.txt ref_count other diamond sample */ #if !defined(RXCPP_OPERATORS_RX_REF_COUNT_HPP) @@ -30,29 +46,101 @@ struct ref_count_invalid : public rxo::operator_base using ref_count_invalid_t = typename ref_count_invalid::type; - -template + +// ref_count(other) takes a regular observable source, not a connectable_observable. +// use template specialization to avoid instantiating 'subscribe' for two different types +// which would cause a compilation error. +template +struct ref_count_state_base { + ref_count_state_base(connectable_type other, observable_type source) + : connectable(std::move(other)) + , subscribable(std::move(source)) {} + + connectable_type connectable; // connects to this. subscribes to this if subscribable empty. + observable_type subscribable; // subscribes to this if non-empty. + + template + void subscribe(Subscriber&& o) { + subscribable.subscribe(std::forward(o)); + } +}; + +// Note: explicit specializations have to be at namespace scope prior to C++17. +template +struct ref_count_state_base { + explicit ref_count_state_base(connectable_type c) + : connectable(std::move(c)) {} + + connectable_type connectable; // connects to this. subscribes to this if subscribable empty. + + template + void subscribe(Subscriber&& o) { + connectable.subscribe(std::forward(o)); + } +}; + +template // note: type order flipped versus the operator. struct ref_count : public operator_base { - typedef rxu::decay_t source_type; + typedef rxu::decay_t observable_type; + typedef rxu::decay_t connectable_type; - struct ref_count_state : public std::enable_shared_from_this + // ref_count() == false + // ref_count(other) == true + using has_observable_t = rxu::negation>; + // removed constexpr to support older VC compilers + static /*constexpr */ const bool has_observable_v = has_observable_t::value; + + struct ref_count_state : public std::enable_shared_from_this, + public ref_count_state_base { - explicit ref_count_state(source_type o) - : source(std::move(o)) + template >> + explicit ref_count_state(connectable_type source) + : ref_count_state_base(std::move(source)) + , subscribers(0) + { + } + + template + ref_count_state(connectable_type other, + typename std::enable_if::type source) + : ref_count_state_base(std::move(other), + std::move(source)) , subscribers(0) { } - source_type source; std::mutex lock; long subscribers; composite_subscription connection; }; std::shared_ptr state; - explicit ref_count(source_type o) - : state(std::make_shared(std::move(o))) + // connectable_observable source = ...; + // source.ref_count(); + // + // calls connect on source after the subscribe on source. + template >> + explicit ref_count(connectable_type source) + : state(std::make_shared(std::move(source))) + { + } + + // connectable_observable other = ...; + // observable source = ...; + // source.ref_count(other); + // + // calls connect on 'other' after the subscribe on 'source'. + template + ref_count(connectable_type other, + typename std::enable_if::type source) + : state(std::make_shared(std::move(other), std::move(source))) { } @@ -70,9 +158,9 @@ struct ref_count : public operator_base keepAlive->connection = composite_subscription(); } }); - keepAlive->source.subscribe(std::forward(o)); + keepAlive->subscribe(std::forward(o)); if (needConnect) { - keepAlive->source.connect(keepAlive->connection); + keepAlive->connectable.connect(keepAlive->connection); } } }; @@ -104,11 +192,28 @@ struct member_overload return Result(RefCount(std::forward(o))); } + template, + is_connectable_observable>, + class SourceValue = rxu::value_type_t, + class RefCount = rxo::detail::ref_count, + rxu::decay_t>, + class Value = rxu::value_type_t, + class Result = observable + > + static Result member(Observable&& o, ConnectableObservable&& other) { + return Result(RefCount(std::forward(other), + std::forward(o))); + } + template static operators::detail::ref_count_invalid_t member(AN...) { std::terminate(); return {}; - static_assert(sizeof...(AN) == 10000, "ref_count takes no arguments"); + static_assert(sizeof...(AN) == 10000, "ref_count takes (optional ConnectableObservable)"); } }; diff --git a/windows/include/rxcpp/operators/rx-repeat.hpp b/windows/include/rxcpp/operators/rx-repeat.hpp index 97ba19766..3b9ac89bc 100644 --- a/windows/include/rxcpp/operators/rx-repeat.hpp +++ b/windows/include/rxcpp/operators/rx-repeat.hpp @@ -47,7 +47,7 @@ using repeat_invalid_t = typename repeat_invalid::type; namespace repeat { struct event_handlers { template - static inline void on_error(State& state, std::exception_ptr& e) { + static inline void on_error(State& state, rxu::error_ptr& e) { state->out.on_error(e); } diff --git a/windows/include/rxcpp/operators/rx-retry-repeat-common.hpp b/windows/include/rxcpp/operators/rx-retry-repeat-common.hpp index 373d9b3f5..30a71fed5 100644 --- a/windows/include/rxcpp/operators/rx-retry-repeat-common.hpp +++ b/windows/include/rxcpp/operators/rx-retry-repeat-common.hpp @@ -42,7 +42,7 @@ namespace rxcpp { state->out.on_next(t); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { EventHandlers::on_error(state, e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-retry.hpp b/windows/include/rxcpp/operators/rx-retry.hpp index 3cee7d35d..63e5c27bb 100644 --- a/windows/include/rxcpp/operators/rx-retry.hpp +++ b/windows/include/rxcpp/operators/rx-retry.hpp @@ -43,7 +43,7 @@ using retry_invalid_t = typename retry_invalid::type; namespace retry { struct event_handlers { template - static inline void on_error(State& state, std::exception_ptr& e) { + static inline void on_error(State& state, rxu::error_ptr& e) { state->update(); // Use specialized predicate for finite/infinte case if (state->completed_predicate()) { diff --git a/windows/include/rxcpp/operators/rx-sample_time.hpp b/windows/include/rxcpp/operators/rx-sample_time.hpp index fe1caa277..f50cbe40b 100644 --- a/windows/include/rxcpp/operators/rx-sample_time.hpp +++ b/windows/include/rxcpp/operators/rx-sample_time.hpp @@ -150,7 +150,7 @@ struct sample_with_time localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&) { localState->dest.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-scan.hpp b/windows/include/rxcpp/operators/rx-scan.hpp index b03be3081..73bcd872d 100644 --- a/windows/include/rxcpp/operators/rx-scan.hpp +++ b/windows/include/rxcpp/operators/rx-scan.hpp @@ -90,7 +90,7 @@ struct scan : public operator_base> state->out.on_next(state->result); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-sequence_equal.hpp b/windows/include/rxcpp/operators/rx-sequence_equal.hpp index 06253b435..3350e44fe 100644 --- a/windows/include/rxcpp/operators/rx-sequence_equal.hpp +++ b/windows/include/rxcpp/operators/rx-sequence_equal.hpp @@ -161,7 +161,7 @@ struct sequence_equal : public operator_base check_equal(); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed @@ -189,7 +189,7 @@ struct sequence_equal : public operator_base check_equal(); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-skip.hpp b/windows/include/rxcpp/operators/rx-skip.hpp index c7f6c1166..b77c4da96 100644 --- a/windows/include/rxcpp/operators/rx-skip.hpp +++ b/windows/include/rxcpp/operators/rx-skip.hpp @@ -109,7 +109,7 @@ struct skip : public operator_base } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->mode_value = mode::errored; state->out.on_error(e); }, diff --git a/windows/include/rxcpp/operators/rx-skip_last.hpp b/windows/include/rxcpp/operators/rx-skip_last.hpp index 6641bd8b6..95947151c 100644 --- a/windows/include/rxcpp/operators/rx-skip_last.hpp +++ b/windows/include/rxcpp/operators/rx-skip_last.hpp @@ -103,7 +103,7 @@ struct skip_last : public operator_base } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-skip_until.hpp b/windows/include/rxcpp/operators/rx-skip_until.hpp index 02a24245f..4df6671da 100644 --- a/windows/include/rxcpp/operators/rx-skip_until.hpp +++ b/windows/include/rxcpp/operators/rx-skip_until.hpp @@ -139,7 +139,7 @@ struct skip_until : public operator_base state->trigger_lifetime.unsubscribe(); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { if (state->mode_value != mode::skipping) { return; } @@ -174,7 +174,7 @@ struct skip_until : public operator_base state->out.on_next(t); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { if (state->mode_value > mode::triggered) { return; } diff --git a/windows/include/rxcpp/operators/rx-skip_while.hpp b/windows/include/rxcpp/operators/rx-skip_while.hpp new file mode 100644 index 000000000..643d86758 --- /dev/null +++ b/windows/include/rxcpp/operators/rx-skip_while.hpp @@ -0,0 +1,131 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +#pragma once + +/*! \file rx-skip_while.hpp + + \brief Discards the first items fulfilling the predicate from this observable emit them from the new observable that is returned. + + \tparam Predicate the type of the predicate + + \param t the predicate + + \return An observable that discards the first items until condition emitted by the source Observable is not fulfilling the predicate, or all of the items from the source observable if the predicate never returns false + + \sample + \snippet skip_while.cpp skip_while sample + \snippet output.txt skip_while sample +*/ + +#if !defined(RXCPP_OPERATORS_RX_SKIP_WHILE_HPP) +#define RXCPP_OPERATORS_RX_SKIP_WHILE_HPP + +#include "../rx-includes.hpp" + +namespace rxcpp { + +namespace operators { + +namespace detail { + +template +struct skip_while_invalid_arguments {}; + +template +struct skip_while_invalid : public rxo::operator_base> { + using type = observable, skip_while_invalid>; +}; +template +using skip_while_invalid_t = typename skip_while_invalid::type; + +template +struct skip_while +{ + typedef rxu::decay_t source_value_type; + typedef rxu::decay_t test_type; + test_type test; + + + skip_while(test_type t) + : test(std::move(t)) + { + } + + template + struct skip_while_observer + { + typedef skip_while_observer this_type; + typedef source_value_type value_type; + typedef rxu::decay_t dest_type; + typedef observer observer_type; + dest_type dest; + test_type test; + bool pass; + + skip_while_observer(dest_type d, test_type t) + : dest(std::move(d)) + , test(std::move(t)), + pass(false) + { + } + void on_next(source_value_type v) { + if(pass || !test(v)) + { + pass = true; + dest.on_next(v); + } + } + void on_error(rxu::error_ptr e) const { + dest.on_error(e); + } + void on_completed() const { + dest.on_completed(); + } + + static subscriber make(dest_type d, test_type t) { + return make_subscriber(d, this_type(d, std::move(t))); + } + }; + + template + auto operator()(Subscriber dest) const + -> decltype(skip_while_observer::make(std::move(dest), test)) { + return skip_while_observer::make(std::move(dest), test); + } +}; + +} + +/*! @copydoc rx-skip_while.hpp +*/ +template +auto skip_while(AN&&... an) + -> operator_factory { + return operator_factory(std::make_tuple(std::forward(an)...)); + } + +} + +template<> +struct member_overload +{ + template, + class TakeWhile = rxo::detail::skip_while>> + static auto member(Observable&& o, Predicate&& p) + -> decltype(o.template lift(TakeWhile(std::forward(p)))) { + return o.template lift(TakeWhile(std::forward(p))); + } + + template + static operators::detail::skip_while_invalid_t member(const AN&...) { + std::terminate(); + return {}; + static_assert(sizeof...(AN) == 10000, "skip_while takes (Predicate)"); + } +}; + + +} + +#endif diff --git a/windows/include/rxcpp/operators/rx-switch_if_empty.hpp b/windows/include/rxcpp/operators/rx-switch_if_empty.hpp index b9bab5247..8d3e57e08 100644 --- a/windows/include/rxcpp/operators/rx-switch_if_empty.hpp +++ b/windows/include/rxcpp/operators/rx-switch_if_empty.hpp @@ -76,7 +76,7 @@ struct switch_if_empty is_empty = false; dest.on_next(std::move(v)); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(std::move(e)); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-switch_on_next.hpp b/windows/include/rxcpp/operators/rx-switch_on_next.hpp index b18963a0d..457212101 100644 --- a/windows/include/rxcpp/operators/rx-switch_on_next.hpp +++ b/windows/include/rxcpp/operators/rx-switch_on_next.hpp @@ -155,7 +155,7 @@ struct switch_on_next state->out.on_next(std::move(ct)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, //on_completed @@ -171,7 +171,7 @@ struct switch_on_next selectedSource.subscribe(std::move(selectedSinkInner)); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-take.hpp b/windows/include/rxcpp/operators/rx-take.hpp index 054c136fe..1e4da4d29 100644 --- a/windows/include/rxcpp/operators/rx-take.hpp +++ b/windows/include/rxcpp/operators/rx-take.hpp @@ -112,7 +112,7 @@ struct take : public operator_base } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->mode_value = mode::errored; state->out.on_error(e); }, diff --git a/windows/include/rxcpp/operators/rx-take_last.hpp b/windows/include/rxcpp/operators/rx-take_last.hpp index d59b35545..12e28b61b 100644 --- a/windows/include/rxcpp/operators/rx-take_last.hpp +++ b/windows/include/rxcpp/operators/rx-take_last.hpp @@ -100,7 +100,7 @@ struct take_last : public operator_base } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { state->out.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/operators/rx-take_until.hpp b/windows/include/rxcpp/operators/rx-take_until.hpp index 29aaf8804..3fd9b7164 100644 --- a/windows/include/rxcpp/operators/rx-take_until.hpp +++ b/windows/include/rxcpp/operators/rx-take_until.hpp @@ -147,7 +147,7 @@ struct take_until : public operator_base state->out.on_completed(); }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { if (state->mode_value != mode::taking) {return;} state->mode_value = mode::errored; state->out.on_error(e); @@ -179,7 +179,7 @@ struct take_until : public operator_base } }, // on_error - [state](std::exception_ptr e) { + [state](rxu::error_ptr e) { if (state->mode_value > mode::clear) {return;} state->mode_value = mode::errored; state->out.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-take_while.hpp b/windows/include/rxcpp/operators/rx-take_while.hpp index de1d57d76..85630e7aa 100644 --- a/windows/include/rxcpp/operators/rx-take_while.hpp +++ b/windows/include/rxcpp/operators/rx-take_while.hpp @@ -73,7 +73,7 @@ struct take_while dest.on_completed(); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-tap.hpp b/windows/include/rxcpp/operators/rx-tap.hpp index 51bfb2ff5..550163a5e 100644 --- a/windows/include/rxcpp/operators/rx-tap.hpp +++ b/windows/include/rxcpp/operators/rx-tap.hpp @@ -92,7 +92,7 @@ struct tap out.on_next(v); dest.on_next(v); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { out.on_error(e); dest.on_error(e); } diff --git a/windows/include/rxcpp/operators/rx-time_interval.hpp b/windows/include/rxcpp/operators/rx-time_interval.hpp index 181c06664..1a4c9a584 100644 --- a/windows/include/rxcpp/operators/rx-time_interval.hpp +++ b/windows/include/rxcpp/operators/rx-time_interval.hpp @@ -84,7 +84,7 @@ struct time_interval dest.on_next(now - last); last = now; } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-timeout.hpp b/windows/include/rxcpp/operators/rx-timeout.hpp index 6750e34e4..d100fa4e3 100644 --- a/windows/include/rxcpp/operators/rx-timeout.hpp +++ b/windows/include/rxcpp/operators/rx-timeout.hpp @@ -126,6 +126,20 @@ struct timeout localState->cs.add([=](){ localState->worker.schedule(selectedDisposer.get()); }); + + auto work = [v, localState](const rxsc::schedulable&) { + auto new_id = ++localState->index; + auto produce_time = localState->worker.now() + localState->period; + + localState->worker.schedule(produce_time, produce_timeout(new_id, localState)); + }; + auto selectedWork = on_exception( + [&](){return localState->coordinator.act(work);}, + localState->dest); + if (selectedWork.empty()) { + return; + } + localState->worker.schedule(selectedWork.get()); } static std::function produce_timeout(std::size_t id, state_type state) { @@ -133,7 +147,7 @@ struct timeout if(id != state->index) return; - state->dest.on_error(std::make_exception_ptr(rxcpp::timeout_error("timeout has occurred"))); + state->dest.on_error(rxu::make_error_ptr(rxcpp::timeout_error("timeout has occurred"))); }; auto selectedProduce = on_exception( @@ -164,7 +178,7 @@ struct timeout localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&) { localState->dest.on_error(e); diff --git a/windows/include/rxcpp/operators/rx-timestamp.hpp b/windows/include/rxcpp/operators/rx-timestamp.hpp index d1a11bfc0..923cf5dcb 100644 --- a/windows/include/rxcpp/operators/rx-timestamp.hpp +++ b/windows/include/rxcpp/operators/rx-timestamp.hpp @@ -78,7 +78,7 @@ struct timestamp void on_next(source_value_type v) const { dest.on_next(std::make_pair(v, coord.now())); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { dest.on_error(e); } void on_completed() const { diff --git a/windows/include/rxcpp/operators/rx-window.hpp b/windows/include/rxcpp/operators/rx-window.hpp index e5d2c6f9c..e033a849c 100644 --- a/windows/include/rxcpp/operators/rx-window.hpp +++ b/windows/include/rxcpp/operators/rx-window.hpp @@ -103,7 +103,7 @@ struct window } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { for (auto s : subj) { s.get_subscriber().on_error(e); } diff --git a/windows/include/rxcpp/operators/rx-window_time.hpp b/windows/include/rxcpp/operators/rx-window_time.hpp index 17d4dd2ef..57f75721a 100644 --- a/windows/include/rxcpp/operators/rx-window_time.hpp +++ b/windows/include/rxcpp/operators/rx-window_time.hpp @@ -195,7 +195,7 @@ struct window_with_time localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ for (auto s : localState->subj) { diff --git a/windows/include/rxcpp/operators/rx-window_time_count.hpp b/windows/include/rxcpp/operators/rx-window_time_count.hpp index 39c64ad4d..93757379e 100644 --- a/windows/include/rxcpp/operators/rx-window_time_count.hpp +++ b/windows/include/rxcpp/operators/rx-window_time_count.hpp @@ -183,7 +183,7 @@ struct window_with_time_or_count localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ localState->subj.get_subscriber().on_error(e); diff --git a/windows/include/rxcpp/operators/rx-window_toggle.hpp b/windows/include/rxcpp/operators/rx-window_toggle.hpp index 1460feba3..b9f119ac3 100644 --- a/windows/include/rxcpp/operators/rx-window_toggle.hpp +++ b/windows/include/rxcpp/operators/rx-window_toggle.hpp @@ -120,6 +120,8 @@ struct window_toggle localState->dest.remove(innerscope); }); + localState->dest.add(localState->cs); + auto source = on_exception( [&](){return localState->coordinator.in(localState->openings);}, localState->dest); @@ -174,7 +176,7 @@ struct window_toggle innercs.unsubscribe(); }, // on_error - [localState](std::exception_ptr e) { + [localState](rxu::error_ptr e) { localState->dest.on_error(e); }, // on_completed @@ -184,7 +186,7 @@ struct window_toggle source.subscribe(std::move(selectedSink)); }, // on_error - [localState](std::exception_ptr e) { + [localState](rxu::error_ptr e) { localState->dest.on_error(e); }, // on_completed @@ -216,7 +218,7 @@ struct window_toggle localState->worker.schedule(selectedWork.get()); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { auto localState = state; auto work = [e, localState](const rxsc::schedulable&){ for (auto s : localState->subj) { @@ -252,7 +254,7 @@ struct window_toggle static subscriber make(dest_type d, window_toggle_values v) { auto cs = composite_subscription(); - auto coordinator = v.coordination.create_coordinator(); + auto coordinator = v.coordination.create_coordinator(d.get_subscription()); return make_subscriber(cs, observer_type(this_type(cs, std::move(d), std::move(v), std::move(coordinator)))); } diff --git a/windows/include/rxcpp/operators/rx-with_latest_from.hpp b/windows/include/rxcpp/operators/rx-with_latest_from.hpp index bce0596d4..616e5d821 100644 --- a/windows/include/rxcpp/operators/rx-with_latest_from.hpp +++ b/windows/include/rxcpp/operators/rx-with_latest_from.hpp @@ -173,7 +173,7 @@ struct with_latest_from : public operator_baseout.on_error(e); }, // on_completed @@ -193,7 +193,7 @@ struct with_latest_from : public operator_base void subscribe_all(std::shared_ptr state, rxu::values) const { - bool subscribed[] = {(subscribe_one(state), true)...}; + bool subscribed[] = {(subscribe_one<(sizeof...(IndexN)) - 1 - IndexN>(state), true)...}; subscribed[0] = (*subscribed); // silence warning } diff --git a/windows/include/rxcpp/operators/rx-zip.hpp b/windows/include/rxcpp/operators/rx-zip.hpp index 90efe076f..b8169fd2a 100644 --- a/windows/include/rxcpp/operators/rx-zip.hpp +++ b/windows/include/rxcpp/operators/rx-zip.hpp @@ -203,7 +203,7 @@ struct zip : public operator_baseout.on_error(e); }, // on_completed diff --git a/windows/include/rxcpp/rx-composite_exception.hpp b/windows/include/rxcpp/rx-composite_exception.hpp new file mode 100644 index 000000000..cddd03ed1 --- /dev/null +++ b/windows/include/rxcpp/rx-composite_exception.hpp @@ -0,0 +1,34 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. + +#pragma once + +#if !defined(RXCPP_SOURCES_RX_COMPOSITE_EXCEPTION_HPP) +#define RXCPP_SOURCES_RX_COMPOSITE_EXCEPTION_HPP + +#include "rx-includes.hpp" + +namespace rxcpp { + +struct composite_exception : std::exception { + + typedef std::vector exception_values; + + virtual const char *what() const RXCPP_NOEXCEPT override { + return "rxcpp composite exception"; + } + + virtual bool empty() const { + return exceptions.empty(); + } + + virtual composite_exception add(rxu::error_ptr exception_ptr) { + exceptions.push_back(exception_ptr); + return *this; + } + + exception_values exceptions; +}; + +} + +#endif diff --git a/windows/include/rxcpp/rx-coordination.hpp b/windows/include/rxcpp/rx-coordination.hpp index d2769e909..34e1223f8 100644 --- a/windows/include/rxcpp/rx-coordination.hpp +++ b/windows/include/rxcpp/rx-coordination.hpp @@ -227,7 +227,7 @@ class serialize_one_worker : public coordination_base std::unique_lock guard(*lock); dest.on_next(v); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { std::unique_lock guard(*lock); dest.on_error(e); } diff --git a/windows/include/rxcpp/rx-includes.hpp b/windows/include/rxcpp/rx-includes.hpp index 3527f01ee..55b1e0be1 100644 --- a/windows/include/rxcpp/rx-includes.hpp +++ b/windows/include/rxcpp/rx-includes.hpp @@ -23,6 +23,12 @@ #define RXCPP_USE_RTTI 1 #endif +#if _HAS_EXCEPTIONS +#define RXCPP_USE_EXCEPTIONS 1 +#endif + +#define RXCPP_NORETURN __declspec(noreturn) + #elif defined(__clang__) #if __has_feature(cxx_rvalue_references) @@ -34,6 +40,15 @@ #if __has_feature(cxx_variadic_templates) #define RXCPP_USE_VARIADIC_TEMPLATES 1 #endif +#if __has_feature(cxx_exceptions) +#define RXCPP_USE_EXCEPTIONS 1 +#endif + +#if __has_feature(cxx_attributes) +#define RXCPP_NORETURN [[noreturn]] +#else +#define RXCPP_NORETURN __attribute__ ((noreturn)) +#endif #elif defined(__GNUG__) @@ -53,6 +68,12 @@ #define RXCPP_USE_RTTI 1 #endif +#if defined(__EXCEPTIONS) +#define RXCPP_USE_EXCEPTIONS 1 +#endif + +#define RXCPP_NORETURN __attribute__ ((noreturn)) + #endif // @@ -95,6 +116,11 @@ #define RXCPP_USE_RTTI RXCPP_FORCE_USE_RTTI #endif +#if defined(RXCPP_FORCE_USE_EXCEPTIONS) +#undef RXCPP_USE_EXCEPTIONS +#define RXCPP_USE_EXCEPTIONS RXCPP_FORCE_USE_EXCEPTIONS +#endif + #if defined(RXCPP_FORCE_USE_WINRT) #undef RXCPP_USE_WINRT #define RXCPP_USE_WINRT RXCPP_FORCE_USE_WINRT @@ -125,6 +151,12 @@ #define _VARIADIC_MAX 10 #endif +#if defined(_MSC_VER) && (_MSC_VER <= 1800) +#define RXCPP_NOEXCEPT +#else +#define RXCPP_NOEXCEPT noexcept +#endif + #pragma push_macro("min") #pragma push_macro("max") #undef min @@ -134,6 +166,8 @@ #include +#include + #include #include @@ -203,6 +237,7 @@ #include "operators/rx-ignore_elements.hpp" #include "operators/rx-map.hpp" #include "operators/rx-merge.hpp" +#include "operators/rx-merge_delay_error.hpp" #include "operators/rx-observe_on.hpp" #include "operators/rx-on_error_resume_next.hpp" #include "operators/rx-pairwise.hpp" @@ -214,6 +249,7 @@ #include "operators/rx-scan.hpp" #include "operators/rx-sequence_equal.hpp" #include "operators/rx-skip.hpp" +#include "operators/rx-skip_while.hpp" #include "operators/rx-skip_last.hpp" #include "operators/rx-skip_until.hpp" #include "operators/rx-start_with.hpp" diff --git a/windows/include/rxcpp/rx-notification.hpp b/windows/include/rxcpp/rx-notification.hpp index c071e6734..20e0c6943 100644 --- a/windows/include/rxcpp/rx-notification.hpp +++ b/windows/include/rxcpp/rx-notification.hpp @@ -106,7 +106,7 @@ auto equals(const T& lhs, const T& rhs, int) template bool equals(const T&, const T&, ...) { - throw std::runtime_error("value does not support equality tests"); + rxu::throw_exception(std::runtime_error("value does not support equality tests")); return false; } @@ -146,26 +146,20 @@ private: }; struct on_error_notification : public base { - on_error_notification(std::exception_ptr ep) : ep(ep) { + on_error_notification(rxu::error_ptr ep) : ep(ep) { } on_error_notification(const on_error_notification& o) : ep(o.ep) {} on_error_notification(const on_error_notification&& o) : ep(std::move(o.ep)) {} on_error_notification& operator=(on_error_notification o) { ep = std::move(o.ep); return *this; } virtual void out(std::ostream& os) const { os << "on_error("; - try { - std::rethrow_exception(ep); - } catch (const std::exception& e) { - os << e.what(); - } catch (...) { - os << ""; - } + os << rxu::what(ep); os << ")"; } virtual bool equals(const typename base::type& other) const { bool result = false; // not trying to compare exceptions - other->accept(make_subscriber(make_observer_dynamic([](T){}, [&result](std::exception_ptr){ + other->accept(make_subscriber(make_observer_dynamic([](T){}, [&result](rxu::error_ptr){ result = true; }))); return result; @@ -173,7 +167,7 @@ private: virtual void accept(const typename base::observer_type& o) const { o.on_error(ep); } - const std::exception_ptr ep; + const rxu::error_ptr ep; }; struct on_completed_notification : public base { @@ -199,20 +193,14 @@ private: template static type make_on_error(exception_tag&&, Exception&& e) { - std::exception_ptr ep; - try { - throw std::forward(e); - } - catch (...) { - ep = std::current_exception(); - } + rxu::error_ptr ep = rxu::make_error_ptr(std::forward(e)); return std::make_shared(ep); } struct exception_ptr_tag {}; static - type make_on_error(exception_ptr_tag&&, std::exception_ptr ep) { + type make_on_error(exception_ptr_tag&&, rxu::error_ptr ep) { return std::make_shared(ep); } @@ -229,7 +217,7 @@ public: template static type on_error(Exception&& e) { return make_on_error(typename std::conditional< - std::is_same, std::exception_ptr>::value, + std::is_same, rxu::error_ptr>::value, exception_ptr_tag, exception_tag>::type(), std::forward(e)); } @@ -280,8 +268,6 @@ std::ostream& operator<< (std::ostream& out, const recorded& r) { } namespace rxn=notifications; -} - inline std::ostream& operator<< (std::ostream& out, const std::vector& vs) { return rxcpp::notifications::detail::ostreamvector(out, vs); } @@ -290,4 +276,6 @@ inline std::ostream& operator<< (std::ostream& out, const std::vector + +namespace rxcpp { + +template +class dynamic_observable; + +template< + class T = void, + class SourceObservable = typename std::conditional::value, + void, dynamic_observable>::type> +class observable; + +template +observable make_observable_dynamic(Source&&); + +} + +#endif diff --git a/windows/include/rxcpp/rx-observable.hpp b/windows/include/rxcpp/rx-observable.hpp index ef96d36d7..232dac142 100644 --- a/windows/include/rxcpp/rx-observable.hpp +++ b/windows/include/rxcpp/rx-observable.hpp @@ -173,27 +173,8 @@ class blocking_observable -> void { std::mutex lock; std::condition_variable wake; - std::exception_ptr error; - - struct tracking - { - ~tracking() - { - if (!disposed || !wakened) std::terminate(); - } - tracking() - { - disposed = false; - wakened = false; - false_wakes = 0; - true_wakes = 0; - } - std::atomic_bool disposed; - std::atomic_bool wakened; - std::atomic_int false_wakes; - std::atomic_int true_wakes; - }; - auto track = std::make_shared(); + bool disposed = false; + rxu::error_ptr error; auto dest = make_subscriber(std::forward(an)...); @@ -201,7 +182,7 @@ class blocking_observable auto scbr = make_subscriber( dest, [&](T t){dest.on_next(t);}, - [&](std::exception_ptr e){ + [&](rxu::error_ptr e){ if (do_rethrow) { error = e; } else { @@ -213,33 +194,21 @@ class blocking_observable auto cs = scbr.get_subscription(); cs.add( - [&, track](){ - // OSX geting invalid x86 op if notify_one is after the disposed = true - // presumably because the condition_variable may already have been awakened - // and is now sitting in a while loop on disposed + [&](){ + std::unique_lock guard(lock); + disposed = true; wake.notify_one(); - track->disposed = true; }); - std::unique_lock guard(lock); source.subscribe(std::move(scbr)); + std::unique_lock guard(lock); wake.wait(guard, - [&, track](){ - // this is really not good. - // false wakeups were never followed by true wakeups so.. - - // anyways this gets triggered before disposed is set now so wait. - while (!track->disposed) { - ++track->false_wakes; - } - ++track->true_wakes; - return true; + [&](){ + return disposed; }); - track->wakened = true; - if (!track->disposed || !track->wakened) std::terminate(); - if (error) {std::rethrow_exception(error);} + if (error) {rxu::rethrow_exception(error);} } public: @@ -319,7 +288,7 @@ public: cs, [&](T v){result.reset(v); cs.unsubscribe();}); if (result.empty()) - throw rxcpp::empty_error("first() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("first() requires a stream with at least one value")); return result.get(); static_assert(sizeof...(AN) == 0, "first() was passed too many arguments."); } @@ -345,7 +314,7 @@ public: subscribe_with_rethrow( [&](T v){result.reset(v);}); if (result.empty()) - throw rxcpp::empty_error("last() requires a stream with at least one value"); + rxu::throw_exception(rxcpp::empty_error("last() requires a stream with at least one value")); return result.get(); static_assert(sizeof...(AN) == 0, "last() was passed too many arguments."); } @@ -462,14 +431,13 @@ struct safe_subscriber safe_subscriber(SourceOperator& so, Subscriber& o) : so(std::addressof(so)), o(std::addressof(o)) {} void subscribe() { - try { + RXCPP_TRY { so->on_subscribe(*o); - } - catch(...) { + } RXCPP_CATCH(...) { if (!o->is_subscribed()) { - throw; + rxu::rethrow_current_exception(); } - o->on_error(std::current_exception()); + o->on_error(rxu::make_error_ptr(rxu::current_exception())); o->unsubscribe(); } } @@ -606,6 +574,17 @@ public: static_assert(sizeof...(AN) == 0, "as_dynamic() was passed too many arguments."); } + /*! @copydoc rx-ref_count.hpp + */ + template + auto ref_count(AN... an) const // ref_count(ConnectableObservable&&) + /// \cond SHOW_SERVICE_MEMBERS + -> decltype(observable_member(ref_count_tag{}, *(this_type*)nullptr, std::forward(an)...)) + /// \endcond + { + return observable_member(ref_count_tag{}, *this, std::forward(an)...); + } + /*! @copydoc rxcpp::operators::as_blocking */ template @@ -1023,6 +1002,17 @@ public: return observable_member(merge_tag{}, *this, std::forward(an)...); } + /*! @copydoc rx-merge_delay_error.hpp + */ + template + auto merge_delay_error(AN... an) const + /// \cond SHOW_SERVICE_MEMBERS + -> decltype(observable_member(merge_delay_error_tag{}, *(this_type*)nullptr, std::forward(an)...)) + /// \endcond + { + return observable_member(merge_delay_error_tag{}, *this, std::forward(an)...); + } + /*! @copydoc rx-amb.hpp */ template @@ -1350,6 +1340,17 @@ public: return observable_member(skip_tag{}, *this, std::forward(an)...); } + /*! @copydoc rx-skip.hpp + */ + template + auto skip_while(AN... an) const + /// \cond SHOW_SERVICE_MEMBERS + -> decltype(observable_member(skip_while_tag{}, *(this_type*)nullptr, std::forward(an)...)) + /// \endcond + { + return observable_member(skip_while_tag{}, *this, std::forward(an)...); + } + /*! @copydoc rx-skip_last.hpp */ template diff --git a/windows/include/rxcpp/rx-observer.hpp b/windows/include/rxcpp/rx-observer.hpp index 39f427ed6..92321f117 100644 --- a/windows/include/rxcpp/rx-observer.hpp +++ b/windows/include/rxcpp/rx-observer.hpp @@ -25,14 +25,14 @@ struct OnNextEmpty }; struct OnErrorEmpty { - void operator()(std::exception_ptr) const { + void operator()(rxu::error_ptr) const { // error implicitly ignored, abort std::terminate(); } }; struct OnErrorIgnore { - void operator()(std::exception_ptr) const { + void operator()(rxu::error_ptr) const { } }; struct OnCompletedEmpty @@ -76,7 +76,7 @@ struct OnErrorForward OnErrorForward() : onerror() {} explicit OnErrorForward(onerror_t oe) : onerror(std::move(oe)) {} onerror_t onerror; - void operator()(state_t& s, std::exception_ptr ep) const { + void operator()(state_t& s, rxu::error_ptr ep) const { onerror(s, ep); } }; @@ -85,7 +85,7 @@ struct OnErrorForward { using state_t = rxu::decay_t; OnErrorForward() {} - void operator()(state_t& s, std::exception_ptr ep) const { + void operator()(state_t& s, rxu::error_ptr ep) const { s.on_error(ep); } }; @@ -129,7 +129,7 @@ struct is_on_error { struct not_void {}; template - static auto check(int) -> decltype((*(CF*)nullptr)(*(std::exception_ptr*)nullptr)); + static auto check(int) -> decltype((*(CF*)nullptr)(*(rxu::error_ptr*)nullptr)); template static not_void check(...); @@ -141,7 +141,7 @@ struct is_on_error_for { struct not_void {}; template - static auto check(int) -> decltype((*(CF*)nullptr)(*(State*)nullptr, *(std::exception_ptr*)nullptr)); + static auto check(int) -> decltype((*(CF*)nullptr)(*(State*)nullptr, *(rxu::error_ptr*)nullptr)); template static not_void check(...); @@ -169,7 +169,7 @@ struct is_on_completed \tparam T - the type of value in the stream \tparam State - the type of the stored state \tparam OnNext - the type of a function that matches `void(State&, T)`. Called 0 or more times. If `void` State::on_next will be called. - \tparam OnError - the type of a function that matches `void(State&, std::exception_ptr)`. Called 0 or 1 times, no further calls will be made. If `void` State::on_error will be called. + \tparam OnError - the type of a function that matches `void(State&, rxu::error_ptr)`. Called 0 or 1 times, no further calls will be made. If `void` State::on_error will be called. \tparam OnCompleted - the type of a function that matches `void(State&)`. Called 0 or 1 times, no further calls will be made. If `void` State::on_completed will be called. \ingroup group-core @@ -244,7 +244,7 @@ public: void on_next(T&& t) const { onnext(state, std::move(t)); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { onerror(state, e); } void on_completed() const { @@ -260,7 +260,7 @@ public: \tparam T - the type of value in the stream \tparam OnNext - the type of a function that matches `void(T)`. Called 0 or more times. If `void` OnNextEmpty is used. - \tparam OnError - the type of a function that matches `void(std::exception_ptr)`. Called 0 or 1 times, no further calls will be made. If `void` OnErrorEmpty is used. + \tparam OnError - the type of a function that matches `void(rxu::error_ptr)`. Called 0 or 1 times, no further calls will be made. If `void` OnErrorEmpty is used. \tparam OnCompleted - the type of a function that matches `void()`. Called 0 or 1 times, no further calls will be made. If `void` OnCompletedEmpty is used. \ingroup group-core @@ -291,7 +291,7 @@ private: public: static_assert(detail::is_on_next_of::value, "Function supplied for on_next must be a function with the signature void(T);"); - static_assert(detail::is_on_error::value, "Function supplied for on_error must be a function with the signature void(std::exception_ptr);"); + static_assert(detail::is_on_error::value, "Function supplied for on_error must be a function with the signature void(rxu::error_ptr);"); static_assert(detail::is_on_completed::value, "Function supplied for on_completed must be a function with the signature void();"); observer() @@ -332,7 +332,7 @@ public: void on_next(T&& t) const { onnext(std::move(t)); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { onerror(e); } void on_completed() const { @@ -352,7 +352,7 @@ struct virtual_observer : public std::enable_shared_from_this virtual void on_next(T&& t) const { destination.on_next(std::move(t)); } - virtual void on_error(std::exception_ptr e) const { + virtual void on_error(rxu::error_ptr e) const { destination.on_error(e); } virtual void on_completed() const { @@ -439,7 +439,7 @@ public: destination->on_next(std::forward(v)); } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { if (destination) { destination->on_error(e); } @@ -495,6 +495,7 @@ auto make_observer(OnNext on) template auto make_observer(OnError oe) -> typename std::enable_if< + !detail::is_on_next_of::value && detail::is_on_error::value, observer, OnError>>::type { return observer, OnError>( @@ -639,10 +640,10 @@ template auto on_exception(const F& f, const OnError& c) -> typename std::enable_if::value, typename detail::maybe_from_result::type>::type { typename detail::maybe_from_result::type r; - try { + RXCPP_TRY { r.reset(f()); - } catch (...) { - c(std::current_exception()); + } RXCPP_CATCH(...) { + c(rxu::current_exception()); } return r; } @@ -651,10 +652,10 @@ template auto on_exception(const F& f, const Subscriber& s) -> typename std::enable_if::value, typename detail::maybe_from_result::type>::type { typename detail::maybe_from_result::type r; - try { + RXCPP_TRY { r.reset(f()); - } catch (...) { - s.on_error(std::current_exception()); + } RXCPP_CATCH(...) { + s.on_error(rxu::current_exception()); } return r; } diff --git a/windows/include/rxcpp/rx-operators.hpp b/windows/include/rxcpp/rx-operators.hpp index 0fa5d5884..aa7d7f4ae 100644 --- a/windows/include/rxcpp/rx-operators.hpp +++ b/windows/include/rxcpp/rx-operators.hpp @@ -258,6 +258,12 @@ struct merge_tag { static_assert(Included::value, "missing include: please #include "); }; }; +struct merge_delay_error_tag { + template + struct include_header{ + static_assert(Included::value, "missing include: please #include "); + }; +}; struct multicast_tag { template @@ -371,6 +377,13 @@ struct skip_tag { }; }; +struct skip_while_tag { + template + struct include_header{ + static_assert(Included::value, "missing include: please #include "); + }; +}; + struct skip_last_tag { template struct include_header{ @@ -445,7 +458,7 @@ struct take_until_tag { struct tap_tag { template struct include_header{ - static_assert(Included::value, "missing include: please #include "); + static_assert(Included::value, "missing include: please #include "); }; }; diff --git a/windows/include/rxcpp/rx-predef.hpp b/windows/include/rxcpp/rx-predef.hpp index f83362088..908a38017 100644 --- a/windows/include/rxcpp/rx-predef.hpp +++ b/windows/include/rxcpp/rx-predef.hpp @@ -6,6 +6,7 @@ #define RXCPP_RX_PREDEF_HPP #include "rx-includes.hpp" +#include "rx-observable-fwd.hpp" namespace rxcpp { @@ -136,18 +137,6 @@ public: static const bool value = std::is_convertible>(0)), tag_dynamic_observable*>::value; }; -template -class dynamic_observable; - -template< - class T = void, - class SourceObservable = typename std::conditional::value, - void, dynamic_observable>::type> -class observable; - -template -observable make_observable_dynamic(Source&&); - template class SO, class... AN> struct defer_observable; diff --git a/windows/include/rxcpp/rx-scheduler.hpp b/windows/include/rxcpp/rx-scheduler.hpp index 0f239be73..b6fd83136 100644 --- a/windows/include/rxcpp/rx-scheduler.hpp +++ b/windows/include/rxcpp/rx-scheduler.hpp @@ -62,12 +62,12 @@ public: /// the action uses recurse to coordinate the scheduler and the function. class recurse { - bool& isallowed; + std::atomic& isallowed; mutable bool isrequested; recursed requestor; recurse operator=(const recurse&); public: - explicit recurse(bool& a) + explicit recurse(std::atomic& a) : isallowed(a) , isrequested(true) , requestor(isrequested) @@ -94,7 +94,7 @@ public: /// recursion is used by the scheduler to signal to each action whether tail recursion is allowed. class recursion { - mutable bool isallowed; + mutable std::atomic isallowed; recurse recursor; recursion operator=(const recursion&); public: @@ -458,12 +458,19 @@ class schedulable : public schedulable_base public: ~exit_recursed_scope_type() { + if (that != nullptr) { that->requestor = nullptr; + } } exit_recursed_scope_type(const recursed_scope_type* that) : that(that) { } + exit_recursed_scope_type(exit_recursed_scope_type && other) RXCPP_NOEXCEPT + : that(other.that) + { + other.that = nullptr; + } }; public: recursed_scope_type() diff --git a/windows/include/rxcpp/rx-subscriber.hpp b/windows/include/rxcpp/rx-subscriber.hpp index abdb7b6fd..3d6c51501 100644 --- a/windows/include/rxcpp/rx-subscriber.hpp +++ b/windows/include/rxcpp/rx-subscriber.hpp @@ -50,11 +50,11 @@ class subscriber : public subscriber_base template void operator()(U u) { trace_activity().on_next_enter(*that, u); - try { + RXCPP_TRY { that->destination.on_next(std::move(u)); do_unsubscribe = false; - } catch(...) { - auto ex = std::current_exception(); + } RXCPP_CATCH(...) { + auto ex = rxu::current_exception(); trace_activity().on_error_enter(*that, ex); that->destination.on_error(std::move(ex)); trace_activity().on_error_return(*that); @@ -75,7 +75,7 @@ class subscriber : public subscriber_base : that(that) { } - inline void operator()(std::exception_ptr ex) { + inline void operator()(rxu::error_ptr ex) { trace_activity().on_error_enter(*that, ex); that->destination.on_error(std::move(ex)); } @@ -180,7 +180,7 @@ public: nextdetacher protect(this); protect(std::forward(v)); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { if (!is_subscribed()) { return; } diff --git a/windows/include/rxcpp/rx-subscription.hpp b/windows/include/rxcpp/rx-subscription.hpp index 9c0046905..2d6bcb604 100644 --- a/windows/include/rxcpp/rx-subscription.hpp +++ b/windows/include/rxcpp/rx-subscription.hpp @@ -117,6 +117,14 @@ private: std::terminate(); } } + + explicit subscription(std::shared_ptr s) + : state(std::move(s)) + { + if (!state) { + std::terminate(); + } + } public: subscription() @@ -178,9 +186,23 @@ public: weak_state_type get_weak() { return state; } + + // Atomically promote weak subscription to strong. + // Calls std::terminate if w has already expired. static subscription lock(weak_state_type w) { return subscription(w); } + + // Atomically try to promote weak subscription to strong. + // Returns an empty maybe<> if w has already expired. + static rxu::maybe maybe_lock(weak_state_type w) { + auto strong_subscription = w.lock(); + if (!strong_subscription) { + return rxu::detail::maybe{}; + } else { + return rxu::detail::maybe{subscription{std::move(strong_subscription)}}; + } + } }; inline bool operator<(const subscription& lhs, const subscription& rhs) { @@ -223,8 +245,14 @@ private: typedef subscription::weak_state_type weak_subscription; struct composite_subscription_state : public std::enable_shared_from_this { + // invariant: cannot access this data without the lock held. std::set subscriptions; + // double checked locking: + // issubscribed must be loaded again after each lock acquisition. + // invariant: + // never call subscription::unsubscribe with lock held. std::mutex lock; + // invariant: transitions from 'true' to 'false' exactly once, at any time. std::atomic issubscribed; ~composite_subscription_state() @@ -242,29 +270,78 @@ private: { } + // Atomically add 's' to the set of subscriptions. + // + // If unsubscribe() has already occurred, this immediately + // calls s.unsubscribe(). + // + // cs.unsubscribe() [must] happens-before s.unsubscribe() + // + // Due to the un-atomic nature of calling 's.unsubscribe()', + // it is possible to observe the unintuitive + // add(s)=>s.unsubscribe() prior + // to any of the unsubscribe()=>sN.unsubscribe(). inline weak_subscription add(subscription s) { - if (!issubscribed) { + if (!issubscribed) { // load.acq [seq_cst] s.unsubscribe(); } else if (s.is_subscribed()) { std::unique_lock guard(lock); - subscriptions.insert(s); + if (!issubscribed) { // load.acq [seq_cst] + // unsubscribe was called concurrently. + guard.unlock(); + // invariant: do not call unsubscribe with lock held. + s.unsubscribe(); + } else { + subscriptions.insert(s); + } } return s.get_weak(); } + // Atomically remove 'w' from the set of subscriptions. + // + // This does nothing if 'w' was already previously removed, + // or refers to an expired value. inline void remove(weak_subscription w) { - if (issubscribed && !w.expired()) { - auto s = subscription::lock(w); + if (issubscribed) { // load.acq [seq_cst] + rxu::maybe maybe_subscription = subscription::maybe_lock(w); + + if (maybe_subscription.empty()) { + // Do nothing if the subscription has already expired. + return; + } + std::unique_lock guard(lock); - subscriptions.erase(std::move(s)); + // invariant: subscriptions must be accessed under the lock. + + if (issubscribed) { // load.acq [seq_cst] + subscription& s = maybe_subscription.get(); + subscriptions.erase(std::move(s)); + } // else unsubscribe() was called concurrently; this becomes a no-op. } } + // Atomically clear all subscriptions that were observably added + // (and not subsequently observably removed). + // + // Un-atomically call unsubscribe on those subscriptions. + // + // forall subscriptions in {add(s1),add(s2),...} + // - {remove(s3), remove(s4), ...}: + // cs.unsubscribe() || cs.clear() happens before s.unsubscribe() + // + // cs.unsubscribe() observed-before cs.clear ==> do nothing. inline void clear() { - if (issubscribed) { + if (issubscribed) { // load.acq [seq_cst] std::unique_lock guard(lock); + if (!issubscribed) { // load.acq [seq_cst] + // unsubscribe was called concurrently. + return; + } + std::set v(std::move(subscriptions)); + // invariant: do not call unsubscribe with lock held. guard.unlock(); std::for_each(v.begin(), v.end(), [](const subscription& s) { @@ -272,11 +349,29 @@ private: } } + // Atomically clear all subscriptions that were observably added + // (and not subsequently observably removed). + // + // Un-atomically call unsubscribe on those subscriptions. + // + // Switches to an 'unsubscribed' state, all subsequent + // adds are immediately unsubscribed. + // + // cs.unsubscribe() [must] happens-before + // cs.add(s) ==> s.unsubscribe() + // + // forall subscriptions in {add(s1),add(s2),...} + // - {remove(s3), remove(s4), ...}: + // cs.unsubscribe() || cs.clear() happens before s.unsubscribe() inline void unsubscribe() { - if (issubscribed.exchange(false)) { + if (issubscribed.exchange(false)) { // cas.acq_rel [seq_cst] std::unique_lock guard(lock); + // is_subscribed can only transition to 'false' once, + // does not need an extra atomic access here. + std::set v(std::move(subscriptions)); + // invariant: do not call unsubscribe with lock held. guard.unlock(); std::for_each(v.begin(), v.end(), [](const subscription& s) { @@ -379,7 +474,7 @@ public: composite_subscription() : inner_type() - , subscription(*static_cast(this)) + , subscription(*static_cast(this)) { } diff --git a/windows/include/rxcpp/rx-trace.hpp b/windows/include/rxcpp/rx-trace.hpp index a14895392..bf0abaf0a 100644 --- a/windows/include/rxcpp/rx-trace.hpp +++ b/windows/include/rxcpp/rx-trace.hpp @@ -93,8 +93,8 @@ struct trace_noop template inline void on_next_return(const Subscriber&) {} - template - inline void on_error_enter(const Subscriber&, const std::exception_ptr&) {} + template + inline void on_error_enter(const Subscriber&, const ErrorPtr&) {} template inline void on_error_return(const Subscriber&) {} diff --git a/windows/include/rxcpp/rx-util.hpp b/windows/include/rxcpp/rx-util.hpp index 77e0d3fcb..d76fa76a1 100644 --- a/windows/include/rxcpp/rx-util.hpp +++ b/windows/include/rxcpp/rx-util.hpp @@ -28,13 +28,36 @@ #define RXCPP_MAKE_IDENTIFIER(Prefix) RXCPP_CONCAT_EVALUATE(Prefix, __LINE__) +// Provide replacements for try/catch keywords, using which is a compilation error +// when exceptions are disabled with -fno-exceptions. +#if RXCPP_USE_EXCEPTIONS +#define RXCPP_TRY try +#define RXCPP_CATCH(...) catch(__VA_ARGS__) +// See also rxu::throw_exception for 'throw' keyword replacement. +#else +#define RXCPP_TRY if ((true)) +#define RXCPP_CATCH(...) if ((false)) +// See also rxu::throw_exception, which will std::terminate without exceptions. +#endif + namespace rxcpp { namespace util { template using value_type_t = typename std::decay::type::value_type; template using decay_t = typename std::decay::type; -template using result_of_t = typename std::result_of::type; +#ifdef __cpp_lib_is_invocable +template struct result_of; + +template +struct result_of +{ + using type = std::invoke_result_t; +}; +#else +template using result_of = std::result_of; +#endif +template using result_of_t = typename result_of::type; template std::vector to_vector(const T (&arr) [size]) { @@ -51,10 +74,20 @@ typename std::enable_if::value && std::is_pod::value, std return to_vector({t0, tn...}); } +// lifted from https://github.com/ericniebler/range-v3/blob/630fc70baa07cbfd222f329e44a3122ab64ce364/include/range/v3/range_fwd.hpp +// removed constexpr & noexcept to support older VC compilers +template +/*constexpr*/ T const &as_const(T & t) /*noexcept*/ +{ + return t; +} +template +void as_const(T const &&) = delete; + template struct values {}; -template +template struct values_from; template @@ -63,7 +96,7 @@ struct values_from typedef values type; }; -template +template struct values_from { typedef typename values_from::type type; @@ -157,7 +190,7 @@ using types_checked_t = typename types_checked_from::type; template struct expand_value_types { struct type; }; template -struct expand_value_types, types_checked_t::type::value_type...>> +struct expand_value_types, types_checked_t::type::value_type...>> { using type = types::type::value_type...>; }; @@ -406,6 +439,15 @@ struct less { return std::forward(lhs) < std::forward(rhs); } }; +template +struct ret +{ + template + auto operator()(LHS&& ) const + -> decltype(T()) + { return T(); } +}; + template struct equal_to { @@ -511,13 +553,18 @@ auto print_followed_by(OStream& os, DelimitValue dv) } inline std::string what(std::exception_ptr ep) { +#if RXCPP_USE_EXCEPTIONS try {std::rethrow_exception(ep);} catch (const std::exception& ex) { return ex.what(); + } catch (...) { + return std::string(""); } - return std::string(); +#endif + (void)ep; + return std::string(""); } - + namespace detail { template @@ -680,10 +727,10 @@ public: { if (!!function) { - try { + RXCPP_TRY { (*function)(); - } catch (...) { - std::unexpected(); + } RXCPP_CATCH(...) { + std::terminate(); } } } @@ -754,7 +801,7 @@ struct is_string : std::false_type { }; template -struct is_string struct negation : detail::not_value {}; +} + +#if !RXCPP_USE_EXCEPTIONS +namespace util { + +namespace detail { + +struct error_base { + virtual const char* what() = 0; + virtual ~error_base() {} +}; + +// Use the "Type Erasure" idiom to wrap an std::exception-like +// value into an error pointer. +// +// Supported types: +// exception, bad_exception, bad_alloc. +template +struct error_specific : public error_base { + error_specific(const E& e) : data(e) {} + error_specific(E&& e) : data(std::move(e)) {} + + virtual ~error_specific() {} + + virtual const char* what() { + return data.what(); + } + + E data; +}; + +} + +} +#endif + +namespace util { + +#if RXCPP_USE_EXCEPTIONS +using error_ptr = std::exception_ptr; +#else +// Note: std::exception_ptr cannot be used directly when exceptions are disabled. +// Any attempt to 'throw' or to call into any of the std functions accepting +// an std::exception_ptr will either fail to compile or result in an abort at runtime. +using error_ptr = std::shared_ptr; + +inline std::string what(error_ptr ep) { + return std::string(ep->what()); +} +#endif + +// TODO: Do we really need an identity make? +// (It was causing some compilation errors deep inside templates). +inline error_ptr make_error_ptr(error_ptr e) { + return e; +} + +// Replace std::make_exception_ptr (which would immediately terminate +// when exceptions are disabled). +template +error_ptr make_error_ptr(E&& e) { +#if RXCPP_USE_EXCEPTIONS + return std::make_exception_ptr(std::forward(e)); +#else + using e_type = rxcpp::util::decay_t; + using pointed_to_type = rxcpp::util::detail::error_specific; + auto sp = std::make_shared(std::forward(e)); + return std::static_pointer_cast(sp); +#endif +} + +// Replace std::rethrow_exception to be compatible with our error_ptr typedef. +RXCPP_NORETURN inline void rethrow_exception(error_ptr e) { +#if RXCPP_USE_EXCEPTIONS + std::rethrow_exception(e); +#else + // error_ptr != std::exception_ptr so we can't use std::rethrow_exception + // + // However even if we could, calling std::rethrow_exception just terminates if exceptions are disabled. + // + // Therefore this function should only be called when we are completely giving up and have no idea + // how to handle the error. + (void)e; + std::terminate(); +#endif +} + +// A replacement for the "throw" keyword which is illegal when +// exceptions are disabled with -fno-exceptions. +template +RXCPP_NORETURN inline void throw_exception(E&& e) { +#if RXCPP_USE_EXCEPTIONS + throw std::forward(e); +#else + // "throw" keyword is unsupported when exceptions are disabled. + // Immediately terminate instead. + (void)e; + std::terminate(); +#endif +} + +// TODO: Do we really need this? rxu::rethrow_exception(rxu::current_exception()) +// would have the same semantics in either case. +RXCPP_NORETURN inline void rethrow_current_exception() { +#if RXCPP_USE_EXCEPTIONS + std::rethrow_exception(std::current_exception()); +#else + std::terminate(); +#endif +} + +// If called during exception handling, return the currently caught exception. +// Otherwise return null. +inline error_ptr current_exception() { +#if RXCPP_USE_EXCEPTIONS + return std::current_exception(); +#else + // When exceptions are disabled, we can never be inside of a catch block. + // Return null similar to std::current_exception returning null outside of catch. + return nullptr; +#endif +} + } namespace rxu=util; // -// due to an noisy static_assert issue in more than one std lib impl, -// rxcpp maintains a whitelist filter for the types that are allowed +// due to an noisy static_assert issue in more than one std lib impl, +// rxcpp maintains a whitelist filter for the types that are allowed // to be hashed. this allows is_hashable to work. // // NOTE: this should eventually be removed! // -template +template struct filtered_hash; #if RXCPP_HASH_ENUM -template +template struct filtered_hash::value>::type> : std::hash { }; #elif RXCPP_HASH_ENUM_UNDERLYING -template +template struct filtered_hash::value>::type> : std::hash::type> { }; #endif -template +template struct filtered_hash::value>::type> : std::hash { }; -template +template struct filtered_hash::value>::type> : std::hash { }; -template +template struct filtered_hash::value>::type> : std::hash { }; template @@ -850,11 +1020,11 @@ struct is_hashable : std::false_type {}; template -struct is_hashable::result_type, - typename filtered_hash::argument_type, - typename std::result_of(T)>::type>::type> + typename filtered_hash::result_type, + typename filtered_hash::argument_type, + typename rxu::result_of(T)>::type>::type> : std::true_type {}; } diff --git a/windows/include/rxcpp/schedulers/rx-eventloop.hpp b/windows/include/rxcpp/schedulers/rx-eventloop.hpp index da45e8d94..4f837670b 100644 --- a/windows/include/rxcpp/schedulers/rx-eventloop.hpp +++ b/windows/include/rxcpp/schedulers/rx-eventloop.hpp @@ -30,15 +30,21 @@ private: composite_subscription lifetime; worker controller; + std::shared_ptr alive; public: virtual ~loop_worker() { } - loop_worker(composite_subscription cs, worker w) + loop_worker(composite_subscription cs, worker w, std::shared_ptr alive) : lifetime(cs) , controller(w) + , alive(alive) { + auto token = controller.add(cs); + cs.add([token, w](){ + w.remove(token); + }); } virtual clock_type::time_point now() const { @@ -57,6 +63,7 @@ private: mutable thread_factory factory; scheduler newthread; mutable std::atomic count; + composite_subscription loops_lifetime; std::vector loops; public: @@ -69,7 +76,7 @@ public: { auto remaining = std::max(std::thread::hardware_concurrency(), unsigned(4)); while (remaining--) { - loops.push_back(newthread.create_worker()); + loops.push_back(newthread.create_worker(loops_lifetime)); } } explicit event_loop(thread_factory tf) @@ -79,11 +86,12 @@ public: { auto remaining = std::max(std::thread::hardware_concurrency(), unsigned(4)); while (remaining--) { - loops.push_back(newthread.create_worker()); + loops.push_back(newthread.create_worker(loops_lifetime)); } } virtual ~event_loop() { + loops_lifetime.unsubscribe(); } virtual clock_type::time_point now() const { @@ -91,7 +99,7 @@ public: } virtual worker create_worker(composite_subscription cs) const { - return worker(cs, std::make_shared(cs, loops[++count % loops.size()])); + return worker(cs, std::make_shared(cs, loops[++count % loops.size()], this->shared_from_this())); } }; diff --git a/windows/include/rxcpp/schedulers/rx-newthread.hpp b/windows/include/rxcpp/schedulers/rx-newthread.hpp index c68399634..1500b25d2 100644 --- a/windows/include/rxcpp/schedulers/rx-newthread.hpp +++ b/windows/include/rxcpp/schedulers/rx-newthread.hpp @@ -37,16 +37,6 @@ private: virtual ~new_worker_state() { - std::unique_lock guard(lock); - if (worker.joinable() && worker.get_id() != std::this_thread::get_id()) { - lifetime.unsubscribe(); - guard.unlock(); - worker.join(); - } - else { - lifetime.unsubscribe(); - worker.detach(); - } } explicit new_worker_state(composite_subscription cs) @@ -82,8 +72,17 @@ private: state->lifetime.add([keepAlive](){ std::unique_lock guard(keepAlive->lock); auto expired = std::move(keepAlive->q); + keepAlive->q = new_worker_state::queue_item_time{}; if (!keepAlive->q.empty()) std::terminate(); keepAlive->wake.notify_one(); + + if (keepAlive->worker.joinable() && keepAlive->worker.get_id() != std::this_thread::get_id()) { + guard.unlock(); + keepAlive->worker.join(); + } + else { + keepAlive->worker.detach(); + } }); state->worker = tf([keepAlive](){ @@ -110,8 +109,9 @@ private: keepAlive->q.pop(); continue; } - if (clock_type::now() < peek.when) { - keepAlive->wake.wait_until(guard, peek.when); + auto when = peek.when; + if (clock_type::now() < when) { + keepAlive->wake.wait_until(guard, when); continue; } auto what = peek.what; diff --git a/windows/include/rxcpp/schedulers/rx-runloop.hpp b/windows/include/rxcpp/schedulers/rx-runloop.hpp index 7e522f685..2e99a5c47 100644 --- a/windows/include/rxcpp/schedulers/rx-runloop.hpp +++ b/windows/include/rxcpp/schedulers/rx-runloop.hpp @@ -164,10 +164,12 @@ public: } bool empty() const { + std::unique_lock guard(state->lock); return state->q.empty(); } const_reference_item_type peek() const { + std::unique_lock guard(state->lock); return state->q.top(); } diff --git a/windows/include/rxcpp/schedulers/rx-test.hpp b/windows/include/rxcpp/schedulers/rx-test.hpp index 86d426aaa..aaa073d45 100644 --- a/windows/include/rxcpp/schedulers/rx-test.hpp +++ b/windows/include/rxcpp/schedulers/rx-test.hpp @@ -200,7 +200,7 @@ subscriber> test_type::test_type_worker::make_subsc recorded_type(ts->sc->clock(), notification_type::on_next(value))); }, // on_error - [ts](std::exception_ptr e) + [ts](rxu::error_ptr e) { ts->m.push_back( recorded_type(ts->sc->clock(), notification_type::on_error(e))); @@ -340,8 +340,9 @@ public: template rxt::testable_observable test_type::make_hot_observable(std::vector>>> messages) const { - return rxt::testable_observable( - std::make_shared>(state, create_worker(composite_subscription()), std::move(messages))); + auto worker = create_worker(composite_subscription()); + auto shared = std::make_shared>(state, worker, std::move(messages)); + return rxt::testable_observable(shared); } template diff --git a/windows/include/rxcpp/sources/rx-error.hpp b/windows/include/rxcpp/sources/rx-error.hpp index 461c08103..f245d02bd 100644 --- a/windows/include/rxcpp/sources/rx-error.hpp +++ b/windows/include/rxcpp/sources/rx-error.hpp @@ -46,17 +46,17 @@ struct error : public source_base struct error_initial_type { - error_initial_type(std::exception_ptr e, coordination_type cn) + error_initial_type(rxu::error_ptr e, coordination_type cn) : exception(e) , coordination(std::move(cn)) { } - std::exception_ptr exception; + rxu::error_ptr exception; coordination_type coordination; }; error_initial_type initial; - error(std::exception_ptr e, coordination_type cn) + error(rxu::error_ptr e, coordination_type cn) : initial(e, std::move(cn)) { } @@ -93,7 +93,7 @@ struct throw_ptr_tag{}; struct throw_instance_tag{}; template -auto make_error(throw_ptr_tag&&, std::exception_ptr exception, Coordination cn) +auto make_error(throw_ptr_tag&&, rxu::error_ptr exception, Coordination cn) -> observable> { return observable>(error(std::move(exception), std::move(cn))); } @@ -101,26 +101,29 @@ auto make_error(throw_ptr_tag&&, std::exception_ptr exception, Coordination cn) template auto make_error(throw_instance_tag&&, E e, Coordination cn) -> observable> { - std::exception_ptr exception; - try {throw e;} catch(...) {exception = std::current_exception();} - return observable>(error(std::move(exception), std::move(cn))); + rxu::error_ptr ep = rxu::make_error_ptr(e); + return observable>(error(std::move(ep), std::move(cn))); } } +} + +namespace sources { + /*! @copydoc rx-error.hpp */ template auto error(E e) - -> decltype(detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), identity_immediate())) { - return detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), identity_immediate()); + -> decltype(detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), identity_immediate())) { + return detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), identity_immediate()); } /*! @copydoc rx-error.hpp */ template auto error(E e, Coordination cn) - -> decltype(detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), std::move(cn))) { - return detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), std::move(cn)); + -> decltype(detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), std::move(cn))) { + return detail::make_error(typename std::conditional>::value, detail::throw_ptr_tag, detail::throw_instance_tag>::type(), std::move(e), std::move(cn)); } } diff --git a/windows/include/rxcpp/sources/rx-iterate.hpp b/windows/include/rxcpp/sources/rx-iterate.hpp index cb526b3cc..07f50bf6c 100644 --- a/windows/include/rxcpp/sources/rx-iterate.hpp +++ b/windows/include/rxcpp/sources/rx-iterate.hpp @@ -183,8 +183,8 @@ auto iterate(Collection c, Coordination cn) */ template auto from() - -> decltype(iterate(std::array(), identity_immediate())) { - return iterate(std::array(), identity_immediate()); + -> decltype(iterate(std::initializer_list(), identity_immediate())) { + return iterate(std::initializer_list(), identity_immediate()); } /*! Returns an observable that sends an empty set of values and then completes, on the specified scheduler. @@ -198,8 +198,8 @@ auto from() template auto from(Coordination cn) -> typename std::enable_if::value, - decltype( iterate(std::array(), std::move(cn)))>::type { - return iterate(std::array(), std::move(cn)); + decltype( iterate(std::initializer_list(), std::move(cn)))>::type { + return iterate(std::initializer_list(), std::move(cn)); } /*! Returns an observable that sends each value from its arguments list. diff --git a/windows/include/rxcpp/sources/rx-never.hpp b/windows/include/rxcpp/sources/rx-never.hpp index cd6f4d03d..ea343996d 100644 --- a/windows/include/rxcpp/sources/rx-never.hpp +++ b/windows/include/rxcpp/sources/rx-never.hpp @@ -7,7 +7,7 @@ #include "../rx-includes.hpp" -/*! \file rx-naver.hpp +/*! \file rx-never.hpp \brief Returns an observable that never sends any items or notifications to observer. diff --git a/windows/include/rxcpp/sources/rx-range.hpp b/windows/include/rxcpp/sources/rx-range.hpp index d55ffffb2..c56ee1f4e 100644 --- a/windows/include/rxcpp/sources/rx-range.hpp +++ b/windows/include/rxcpp/sources/rx-range.hpp @@ -86,7 +86,7 @@ struct range : public source_base return; } - if (std::abs(state.last - state.next) < std::abs(state.step)) { + if (std::max(state.last, state.next) - std::min(state.last, state.next) < std::abs(state.step)) { if (state.last != state.next) { dest.on_next(state.last); } diff --git a/windows/include/rxcpp/subjects/rx-behavior.hpp b/windows/include/rxcpp/subjects/rx-behavior.hpp index ff3becb63..fd0cad3a4 100644 --- a/windows/include/rxcpp/subjects/rx-behavior.hpp +++ b/windows/include/rxcpp/subjects/rx-behavior.hpp @@ -91,11 +91,11 @@ public: observable get_observable() const { auto keepAlive = s; - return make_observable_dynamic([=](subscriber o){ + return make_observable_dynamic([keepAlive](subscriber o){ if (keepAlive.get_subscription().is_subscribed()) { - o.on_next(get_value()); + o.on_next(keepAlive.get_value()); } - keepAlive.add(s.get_subscriber(), std::move(o)); + keepAlive.add(keepAlive.get_subscriber(), std::move(o)); }); } }; diff --git a/windows/include/rxcpp/subjects/rx-replaysubject.hpp b/windows/include/rxcpp/subjects/rx-replaysubject.hpp index b7f7d68f0..0e9d73286 100644 --- a/windows/include/rxcpp/subjects/rx-replaysubject.hpp +++ b/windows/include/rxcpp/subjects/rx-replaysubject.hpp @@ -43,6 +43,7 @@ class replay_observer : public detail::multicast_observer mutable std::list time_points; mutable count_type count; mutable period_type period; + mutable composite_subscription replayLifetime; public: mutable coordination_type coordination; mutable coordinator_type coordinator; @@ -56,9 +57,13 @@ class replay_observer : public detail::multicast_observer } public: - explicit replay_observer_state(count_type _count, period_type _period, coordination_type _coordination, coordinator_type _coordinator) + ~replay_observer_state(){ + replayLifetime.unsubscribe(); + } + explicit replay_observer_state(count_type _count, period_type _period, coordination_type _coordination, coordinator_type _coordinator, composite_subscription _replayLifetime) : count(_count) , period(_period) + , replayLifetime(_replayLifetime) , coordination(std::move(_coordination)) , coordinator(std::move(_coordinator)) { @@ -66,6 +71,7 @@ class replay_observer : public detail::multicast_observer void add(T v) const { std::unique_lock guard(lock); + if (!count.empty()) { if (values.size() == count.get()) remove_oldest(); @@ -89,11 +95,12 @@ class replay_observer : public detail::multicast_observer std::shared_ptr state; public: - replay_observer(count_type count, period_type period, coordination_type coordination, composite_subscription cs) - : base_type(cs) + replay_observer(count_type count, period_type period, coordination_type coordination, composite_subscription replayLifetime, composite_subscription subscriberLifetime) + : base_type(subscriberLifetime) { - auto coordinator = coordination.create_coordinator(cs); - state = std::make_shared(std::move(count), std::move(period), std::move(coordination), std::move(coordinator)); + replayLifetime.add(subscriberLifetime); + auto coordinator = coordination.create_coordinator(replayLifetime); + state = std::make_shared(std::move(count), std::move(period), std::move(coordination), std::move(coordinator), std::move(replayLifetime)); } subscriber get_subscriber() const { @@ -129,22 +136,22 @@ class replay public: explicit replay(Coordination cn, composite_subscription cs = composite_subscription()) - : s(count_type(), period_type(), cn, cs) + : s(count_type(), period_type(), cn, cs, composite_subscription{}) { } replay(std::size_t count, Coordination cn, composite_subscription cs = composite_subscription()) - : s(count_type(std::move(count)), period_type(), cn, cs) + : s(count_type(std::move(count)), period_type(), cn, cs, composite_subscription{}) { } replay(rxsc::scheduler::clock_type::duration period, Coordination cn, composite_subscription cs = composite_subscription()) - : s(count_type(), period_type(period), cn, cs) + : s(count_type(), period_type(period), cn, cs, composite_subscription{}) { } replay(std::size_t count, rxsc::scheduler::clock_type::duration period, Coordination cn, composite_subscription cs = composite_subscription()) - : s(count_type(count), period_type(period), cn, cs) + : s(count_type(count), period_type(period), cn, cs, composite_subscription{}) { } @@ -162,10 +169,9 @@ public: observable get_observable() const { auto keepAlive = s; - auto observable = make_observable_dynamic([=](subscriber o){ - if (keepAlive.get_subscription().is_subscribed()) { - for (auto&& value: get_values()) - o.on_next(value); + auto observable = make_observable_dynamic([keepAlive](subscriber o){ + for (auto&& value: keepAlive.get_values()) { + o.on_next(value); } keepAlive.add(keepAlive.get_subscriber(), std::move(o)); }); diff --git a/windows/include/rxcpp/subjects/rx-subject.hpp b/windows/include/rxcpp/subjects/rx-subject.hpp index 066f1527b..4ce96b473 100644 --- a/windows/include/rxcpp/subjects/rx-subject.hpp +++ b/windows/include/rxcpp/subjects/rx-subject.hpp @@ -40,7 +40,7 @@ class multicast_observer } std::mutex lock; typename mode::type current; - std::exception_ptr error; + rxu::error_ptr error; composite_subscription lifetime; }; @@ -191,7 +191,7 @@ public: } } } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { std::unique_lock guard(b->state->lock); if (b->state->current == mode::Casting) { b->state->error = e; @@ -254,6 +254,10 @@ public: return s.has_observers(); } + composite_subscription get_subscription() const { + return s.get_subscription(); + } + subscriber_type get_subscriber() const { return s.get_subscriber(); } diff --git a/windows/include/rxcpp/subjects/rx-synchronize.hpp b/windows/include/rxcpp/subjects/rx-synchronize.hpp index 161b42293..88f33ca48 100644 --- a/windows/include/rxcpp/subjects/rx-synchronize.hpp +++ b/windows/include/rxcpp/subjects/rx-synchronize.hpp @@ -56,7 +56,7 @@ class synchronize_observer : public detail::multicast_observer auto keepAlive = this->shared_from_this(); auto drain_queue = [keepAlive, this](const rxsc::schedulable& self){ - try { + RXCPP_TRY { std::unique_lock guard(lock); if (!destination.is_subscribed()) { current = mode::Disposed; @@ -74,8 +74,8 @@ class synchronize_observer : public detail::multicast_observer guard.unlock(); notification->accept(destination); self(); - } catch(...) { - destination.on_error(std::current_exception()); + } RXCPP_CATCH(...) { + destination.on_error(rxu::current_exception()); std::unique_lock guard(lock); current = mode::Empty; } @@ -89,6 +89,7 @@ class synchronize_observer : public detail::multicast_observer } auto processor = coordinator.get_worker(); + guard.unlock(); processor.schedule(lifetime, selectedDrain.get()); } } @@ -110,7 +111,7 @@ class synchronize_observer : public detail::multicast_observer } wake.notify_one(); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { if (lifetime.is_subscribed()) { std::unique_lock guard(lock); fill_queue.push_back(notification_type::on_error(e)); @@ -150,7 +151,7 @@ public: void on_next(V v) const { state->on_next(std::move(v)); } - void on_error(std::exception_ptr e) const { + void on_error(rxu::error_ptr e) const { state->on_error(e); } void on_completed() const {