summaryrefslogtreecommitdiff
path: root/Rx/v2/src/rxcpp/operators/rx-switch_if_empty.hpp
blob: 8d3e57e082ab790e6821f4ff24884d2e2c2c09c0 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#pragma once

/*! \file rx-switch_if_empty.hpp

    \brief If the source Observable terminates without emitting any items, emits items from a backup Observable.

    \tparam BackupSource the type of the backup observable.

    \param t a backup observable that is used if the source observable is empty.

    \return Observable that emits items from a backup observable if the source observable is empty.

    \sample
    \snippet switch_if_empty.cpp switch_if_empty sample
    \snippet output.txt switch_if_empty sample
*/

#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... AN>
struct switch_if_empty_invalid_arguments {};

template<class... AN>
struct switch_if_empty_invalid : public rxo::operator_base<switch_if_empty_invalid_arguments<AN...>> {
    using type = observable<switch_if_empty_invalid_arguments<AN...>, switch_if_empty_invalid<AN...>>;
};
template<class... AN>
using switch_if_empty_invalid_t = typename switch_if_empty_invalid<AN...>::type;

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(rxu::error_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));
    }
};

}

/*! @copydoc rx-switch_if_empty.hpp
*/
template<class... AN>
auto switch_if_empty(AN&&... an)
    ->      operator_factory<switch_if_empty_tag, AN...> {
     return operator_factory<switch_if_empty_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
}

/*! \brief If the source Observable terminates without emitting any items, emits a default item and completes.

    \tparam Value the type of the value to emit.

    \param v the default value to emit.

    \return Observable that emits the specified default item if the source observable is empty.

    \sample
    \snippet default_if_empty.cpp default_if_empty sample
    \snippet output.txt default_if_empty sample
*/
template<class... AN>
auto default_if_empty(AN&&... an)
    ->      operator_factory<default_if_empty_tag, AN...> {
     return operator_factory<default_if_empty_tag, AN...>(std::make_tuple(std::forward<AN>(an)...));
}

}

template<>
struct member_overload<switch_if_empty_tag>
{
    template<class Observable, class BackupSource,
        class Enabled = rxu::enable_if_all_true_type_t<
            all_observables<Observable, BackupSource>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class SwitchIfEmpty = rxo::detail::switch_if_empty<SourceValue, rxu::decay_t<BackupSource>>>
    static auto member(Observable&& o, BackupSource&& b)
        -> decltype(o.template lift<SourceValue>(SwitchIfEmpty(std::forward<BackupSource>(b)))) {
        return      o.template lift<SourceValue>(SwitchIfEmpty(std::forward<BackupSource>(b)));
    }

    template<class... AN>
    static operators::detail::switch_if_empty_invalid_t<AN...> member(AN...) {
        std::terminate();
        return {};
        static_assert(sizeof...(AN) == 10000, "switch_if_empty takes (BackupSource)");
    }
};

template<>
struct member_overload<default_if_empty_tag>
{
    template<class Observable, class Value,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>>,
        class SourceValue = rxu::value_type_t<Observable>,
        class BackupSource = decltype(rxs::from(std::declval<SourceValue>())),
        class DefaultIfEmpty = rxo::detail::switch_if_empty<SourceValue, BackupSource>>
    static auto member(Observable&& o, Value v)
        -> decltype(o.template lift<SourceValue>(DefaultIfEmpty(rxs::from(std::move(v))))) {
        return      o.template lift<SourceValue>(DefaultIfEmpty(rxs::from(std::move(v))));
    }

    template<class... AN>
    static operators::detail::switch_if_empty_invalid_t<AN...> member(AN...) {
        std::terminate();
        return {};
        static_assert(sizeof...(AN) == 10000, "default_if_empty takes (Value)");
    }
};

}

#endif