ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
from_signal.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2022 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9//
10
11#pragma once
12
13#include <QObject>
14
15#include <rppqt/sources/fwd.hpp>
16#include <rpp/subjects/publish_subject.hpp>
17
18#include <tuple>
19
20IMPLEMENTATION_FILE(from_signal_qt_tag);
21
22namespace rppqt::observable::details
23{
24template<typename Value>
26{
28 using Subscriber = decltype(std::declval<Subject>().get_subscriber());
29
30 from_signal_on_event_base(const Subscriber& subscriber)
31 : subscriber{subscriber} {}
32
33 from_signal_on_event_base(Subscriber&& subscriber)
34 : subscriber{std::move(subscriber)} {}
35
36 Subscriber subscriber;
37};
38
39template<typename... Args>
40struct from_signal_on_event : from_signal_on_event_base<std::tuple<std::decay_t<Args>...>>
41{
42 template<typename ...Vals>
43 void operator()(Vals&&... vals) const
44 {
45 from_signal_on_event_base<std::tuple<std::decay_t<Args>...>>::subscriber.on_next(std::make_tuple(std::forward<Vals>(vals)...));
46 }
47};
48
49template<typename Arg>
50struct from_signal_on_event<Arg> : from_signal_on_event_base<std::decay_t<Arg>>
51{
52 template<rpp::constraint::decayed_same_as<Arg> Val>
53 void operator()(Val&& val) const
54 {
55 from_signal_on_event_base<std::decay_t<Arg>>::subscriber.on_next(std::forward<Val>(val));
56 }
57};
58
59template<>
61{
62 void operator()() const
63 {
64 subscriber.on_next(rpp::utils::none{});
65 }
66};
67
68}
69namespace rppqt::observable
70{
82template<std::derived_from<QObject> TSignalQObject, std::derived_from<TSignalQObject> TObject, typename R,typename ...Args>
83auto from_signal(const TObject& object, R (TSignalQObject::*signal)(Args...)) requires rpp::details::is_header_included<rpp::details::from_signal_qt_tag, TObject, R, Args...>
84{
85 using on_next_impl = details::from_signal_on_event<Args...>;
86 const auto subj = typename on_next_impl::Subject{};
87
88 const auto subscriber = subj.get_subscriber();
89 QObject::connect(&object, signal, on_next_impl{subscriber});
90 QObject::connect(&object, &QObject::destroyed, [subscriber] { subscriber.on_completed(); });
91
92 return subj.get_observable();
93}
94} // namespace rppqt::observable
Subject which just multicasts values to observers subscribed on it. It contains two parts: subscriber...
Definition: publish_subject.hpp:78
auto from_signal(const TObject &object, R(TSignalQObject::*signal)(Args...))
Creates rpp::specific_observable that emits a items from provided QT signal.
Definition: from_signal.hpp:83