-
Notifications
You must be signed in to change notification settings - Fork 37
Add zip operator #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add zip operator #523
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
676c98c
Add zip operator
CorentinBT 9f3b950
Address comments
CorentinBT ab0ea35
Address comment
CorentinBT 4196c6d
Add nolint notations
CorentinBT 42c9e77
Use emplace_back instead of push_back
CorentinBT e7b414a
Fix gcc10 test
CorentinBT 4272abb
Merge branch 'v2' of https://github.com/victimsnino/ReactivePlusPlus …
CorentinBT ef57bbb
Fix code smells
CorentinBT cf77e2c
Merge branch 'v2' of https://github.com/victimsnino/ReactivePlusPlus …
CorentinBT 2548ba0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // ReactivePlusPlus library | ||
| // | ||
| // Copyright Aleksey Loginov 2023 - present. | ||
| // Distributed under the Boost Software License, Version 1.0. | ||
| // (See accompanying file LICENSE_1_0.txt or copy at | ||
| // https://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
| // Project home: https://github.com/victimsnino/ReactivePlusPlus | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <rpp/operators/fwd.hpp> | ||
|
|
||
| #include <rpp/defs.hpp> | ||
| #include <rpp/disposables/composite_disposable.hpp> | ||
| #include <rpp/operators/details/strategy.hpp> | ||
| #include <rpp/operators/details/utils.hpp> | ||
| #include <rpp/schedulers/current_thread.hpp> | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace rpp::operators::details | ||
| { | ||
| template<rpp::constraint::observer Observer, rpp::constraint::decayed_type... Args> | ||
| class combining_disposable : public composite_disposable | ||
| { | ||
| public: | ||
| explicit combining_disposable(Observer&& observer) | ||
| : m_observer_with_mutex{std::move(observer)} | ||
| { | ||
| } | ||
|
|
||
| pointer_under_lock<Observer> get_observer_under_lock() { return pointer_under_lock{m_observer_with_mutex}; } | ||
|
|
||
| bool decrement_on_completed() | ||
| { | ||
| // just need atomicity, not guarding anything | ||
| return m_on_completed_needed.fetch_sub(1, std::memory_order::seq_cst) == 1; | ||
| } | ||
|
|
||
| private: | ||
| value_with_mutex<Observer> m_observer_with_mutex{}; | ||
|
|
||
| std::atomic_size_t m_on_completed_needed{sizeof...(Args)}; | ||
| }; | ||
|
|
||
| template<typename TDisposable> | ||
| struct combining_observer_strategy | ||
| { | ||
| std::shared_ptr<TDisposable> disposable{}; | ||
|
|
||
| void set_upstream(const rpp::disposable_wrapper& d) const | ||
| { | ||
| disposable->add(d); | ||
| } | ||
|
|
||
| bool is_disposed() const | ||
| { | ||
| return disposable->is_disposed(); | ||
| } | ||
|
|
||
| void on_error(const std::exception_ptr& err) const | ||
| { | ||
| disposable->dispose(); | ||
| disposable->get_observer_under_lock()->on_error(err); | ||
| } | ||
|
|
||
| void on_completed() const | ||
| { | ||
| if (disposable->decrement_on_completed()) | ||
| { | ||
| disposable->dispose(); | ||
| disposable->get_observer_under_lock()->on_completed(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template<template<typename...> typename TDisposable, template<auto, typename...> typename TStrategy, typename TSelector, rpp::constraint::observable... TObservables> | ||
| struct combining_operator_t | ||
| { | ||
| RPP_NO_UNIQUE_ADDRESS rpp::utils::tuple<TObservables...> observables; | ||
| RPP_NO_UNIQUE_ADDRESS TSelector selector; | ||
|
|
||
| template<rpp::constraint::decayed_type T> | ||
| struct operator_traits | ||
| { | ||
| static_assert(std::invocable<TSelector, T, rpp::utils::extract_observable_type_t<TObservables>...>, "Selector is not callable with passed T type"); | ||
|
|
||
| using result_type = std::invoke_result_t<TSelector, T, rpp::utils::extract_observable_type_t<TObservables>...>; | ||
| }; | ||
|
|
||
| template<rpp::details::observables::constraint::disposable_strategy Prev> | ||
| using updated_disposable_strategy = rpp::details::observables::fixed_disposable_strategy_selector<1>; | ||
|
|
||
| template<rpp::constraint::observer Observer, typename... Strategies> | ||
| void subscribe(Observer&& observer, const observable_chain_strategy<Strategies...>& observable_strategy) const | ||
| { | ||
| // Need to take ownership over current_thread in case of inner-observables also using it | ||
| auto drain_on_exit = rpp::schedulers::current_thread::own_queue_and_drain_finally_if_not_owned(); | ||
| observables.apply(&subscribe_impl<Observer, Strategies...>, std::forward<Observer>(observer), observable_strategy, selector); | ||
| } | ||
|
|
||
| private: | ||
| template<rpp::constraint::observer Observer, typename... Strategies> | ||
| static void subscribe_impl(Observer&& observer, const observable_chain_strategy<Strategies...>& observable_strategy, const TSelector& selector, const TObservables&... observables) | ||
| { | ||
| using ExpectedValue = typename observable_chain_strategy<Strategies...>::value_type; | ||
| using Disposable = TDisposable<Observer, TSelector, ExpectedValue, rpp::utils::extract_observable_type_t<TObservables>...>; | ||
|
|
||
| const auto disposable = disposable_wrapper_impl<Disposable>::make(std::forward<Observer>(observer), selector); | ||
| auto locked = disposable.lock(); | ||
| locked->get_observer_under_lock()->set_upstream(disposable.as_weak()); | ||
| subscribe<std::decay_t<ExpectedValue>>(locked, std::index_sequence_for<TObservables...>{}, observables...); | ||
|
|
||
| observable_strategy.subscribe(rpp::observer<ExpectedValue, TStrategy<0, std::decay_t<Observer>, TSelector, ExpectedValue, rpp::utils::extract_observable_type_t<TObservables>...>>{std::move(locked)}); | ||
| } | ||
|
|
||
| template<typename ExpectedValue, rpp::constraint::observer Observer, size_t... I> | ||
| static void subscribe(const std::shared_ptr<TDisposable<Observer, TSelector, ExpectedValue, rpp::utils::extract_observable_type_t<TObservables>...>>& disposable, std::index_sequence<I...>, const TObservables&... observables) | ||
| { | ||
| (..., observables.subscribe(rpp::observer<rpp::utils::extract_observable_type_t<TObservables>, TStrategy<I + 1, Observer, TSelector, ExpectedValue, rpp::utils::extract_observable_type_t<TObservables>...>>{disposable})); | ||
| } | ||
| }; | ||
| } // namespace rpp::operators::details |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.