summaryrefslogtreecommitdiff
path: root/Rx/v2/src/rxcpp/operators/rx-switch_if_empty.hpp
blob: cf51109660b55131a0a0ff2f641ad7f5edb570a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#pragma once

#if !defined(RXCPP_OPERATORS_RX_SWITCH_IF_EMPTY_HPP)
#define RXCPP_OPERATORS_RX_SWITCH_IF_EMPTY_HPP

#include "../rx-includes.hpp"

namespace rxcpp {

namespace operators {

namespace detail {

template<class T, class BackupSource>
struct switch_if_empty
{
    typedef rxu::decay_t<T> source_value_type;
    typedef rxu::decay_t<BackupSource> backup_source_type;

    backup_source_type backup;

    switch_if_empty(backup_source_type b)
        : backup(std::move(b))
    {
    }

    template<class Subscriber>
    struct switch_if_empty_observer
    {
        typedef switch_if_empty_observer<Subscriber> this_type;
        typedef source_value_type value_type;
        typedef rxu::decay_t<Subscriber> dest_type;
        typedef observer<value_type, this_type> observer_type;

        dest_type dest;
        composite_subscription lifetime;
        backup_source_type backup;
        mutable bool is_empty;

        switch_if_empty_observer(dest_type d, composite_subscription cs, backup_source_type b)
            : dest(std::move(d))
            , lifetime(std::move(cs))
            , backup(std::move(b))
            , is_empty(true)
        {
            dest.add(lifetime);
        }
        void on_next(source_value_type v) const {
            is_empty = false;
            dest.on_next(std::move(v));
        }
        void on_error(std::exception_ptr e) const {
            dest.on_error(std::move(e));
        }
        void on_completed() const {
            if(!is_empty) {
                dest.on_completed();
            } else {
                backup.subscribe(dest);
            }
        }

        static subscriber<value_type, observer_type> make(dest_type d, backup_source_type b) {
            auto cs = composite_subscription();
            return make_subscriber<value_type>(cs, observer_type(this_type(std::move(d), cs, std::move(b))));
        }
    };

    template<class Subscriber>
    auto operator()(Subscriber dest) const
        -> decltype(switch_if_empty_observer<Subscriber>::make(std::move(dest), std::move(backup))) {
        return      switch_if_empty_observer<Subscriber>::make(std::move(dest), std::move(backup));
    }
};

template<class BackupSource>
class switch_if_empty_factory
{
    typedef rxu::decay_t<BackupSource> backup_source_type;
    backup_source_type backup;
public:
    switch_if_empty_factory(backup_source_type b) : backup(std::move(b)) {}
    template<class Observable>
    auto operator()(Observable&& source)
        -> decltype(source.template lift<rxu::value_type_t<rxu::decay_t<Observable>>>(switch_if_empty<rxu::value_type_t<rxu::decay_t<Observable>>, backup_source_type>(backup))) {
        return      source.template lift<rxu::value_type_t<rxu::decay_t<Observable>>>(switch_if_empty<rxu::value_type_t<rxu::decay_t<Observable>>, backup_source_type>(backup));
    }
};

}

template<class BackupSource>
auto switch_if_empty(BackupSource&& b)
    ->      detail::switch_if_empty_factory<BackupSource> {
    return  detail::switch_if_empty_factory<BackupSource>(std::forward<BackupSource>(b));
}

}

}

#endif