ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
first.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2022 - present.
4// TC Wang 2022 - present.
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// https://www.boost.org/LICENSE_1_0.txt)
8//
9// Project home: https://github.com/victimsnino/ReactivePlusPlus
10//
11
12#pragma once
13
14#include <rpp/operators/lift.hpp> // required due to operator uses lift
15#include <rpp/operators/take.hpp> // take_state
16#include <rpp/operators/details/subscriber_with_state.hpp> // create_subscriber_with_state
17#include <rpp/operators/fwd/first.hpp> // own forwarding
18#include <rpp/subscribers/constraints.hpp> // constraint::subscriber
19#include <rpp/utils/exceptions.hpp> // not_enough_emissions
20#include <rpp/utils/functors.hpp> // forwarding_on_error
21
22IMPLEMENTATION_FILE(first_tag);
23
24namespace rpp::details
25{
27{
29 : take_state{1} {}
30};
31
33
35{
36 void operator()(const constraint::subscriber auto& subscriber, const first_state&) const
37 {
38 subscriber.on_error(std::make_exception_ptr(utils::not_enough_emissions{"first() operator expects at least one emission from observable before completion"}));
39 }
40};
41
42template<constraint::decayed_type Type>
44{
45public:
46 template<constraint::subscriber_of_type<Type> TSub>
47 auto operator()(TSub&& subscriber) const
48 {
49 auto subscription = subscriber.get_subscription();
50
51 // dynamic_state there to make shared_ptr for observer instead of making shared_ptr for state
52 return create_subscriber_with_dynamic_state<Type>(std::move(subscription),
54 utils::forwarding_on_error{},
56 std::forward<TSub>(subscriber),
57 first_state{});
58 }
59};
60} // namespace rpp::details
Definition: constraints.hpp:19
Definition: first.hpp:44
Definition: first.hpp:35
Definition: first.hpp:27
Definition: take.hpp:29
Definition: take.hpp:24