summaryrefslogtreecommitdiff
path: root/Rx/v2/src/rxcpp/operators/rx-map.hpp
blob: a670d88b50127268efc5666ffbe09673816407cd (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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

#pragma once

/*! \file rx-map.hpp

    \brief For each item from this observable use Selector to produce an item to emit from the new observable that is returned.

    \tparam Selector the type of the transforming function

    \param s the selector function

    \return  Observable that emits the items from the source observable, transformed by the specified function.

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

#if !defined(RXCPP_OPERATORS_RX_MAP_HPP)
#define RXCPP_OPERATORS_RX_MAP_HPP

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

namespace rxcpp {

namespace operators {

namespace detail {

template<class... AN>
struct map_invalid_arguments {};

template<class... AN>
struct map_invalid : public rxo::operator_base<map_invalid_arguments<AN...>> {
    using type = observable<map_invalid_arguments<AN...>, map_invalid<AN...>>;
};
template<class... AN>
using map_invalid_t = typename map_invalid<AN...>::type;

template<class T, class Selector>
struct map
{
    typedef rxu::decay_t<T> source_value_type;
    typedef rxu::decay_t<Selector> select_type;
    typedef decltype((*(select_type*)nullptr)(*(source_value_type*)nullptr)) value_type;
    select_type selector;

    map(select_type s)
        : selector(std::move(s))
    {
    }

    template<class Subscriber>
    struct map_observer
    {
        typedef map_observer<Subscriber> this_type;
        typedef decltype((*(select_type*)nullptr)(*(source_value_type*)nullptr)) value_type;
        typedef rxu::decay_t<Subscriber> dest_type;
        typedef observer<source_value_type, this_type> observer_type;
        dest_type dest;
        mutable select_type selector;

        map_observer(dest_type d, select_type s)
            : dest(std::move(d))
            , selector(std::move(s))
        {
        }
        template<class Value>
        void on_next(Value&& v) const {
            auto selected = on_exception(
                [&](){
                    return this->selector(std::forward<Value>(v));},
                dest);
            if (selected.empty()) {
                return;
            }
            dest.on_next(std::move(selected.get()));
        }
        void on_error(std::exception_ptr e) const {
            dest.on_error(e);
        }
        void on_completed() const {
            dest.on_completed();
        }

        static subscriber<source_value_type, observer_type> make(dest_type d, select_type s) {
            auto cs = d.get_subscription();
            return make_subscriber<source_value_type>(std::move(cs), observer_type(this_type(std::move(d), std::move(s))));
        }
    };

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

}

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

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

}

template<>
struct member_overload<map_tag>
{
    template<class Observable, class Selector,
        class Enabled = rxu::enable_if_all_true_type_t<
            is_observable<Observable>>,
        class ResolvedSelector = rxu::decay_t<Selector>,
        class SourceValue = rxu::value_type_t<Observable>,
        class Map = rxo::detail::map<SourceValue, ResolvedSelector>,
        class Value = rxu::value_type_t<Map>>
    static auto member(Observable&& o, Selector&& s)
        -> decltype(o.template lift<Value>(Map(std::forward<Selector>(s)))) {
        return      o.template lift<Value>(Map(std::forward<Selector>(s)));
    }

    template<class... AN>
    static operators::detail::map_invalid_t<AN...> member(const AN...) {
        std::terminate();
        return {};
        static_assert(sizeof...(AN) == 10000, "map takes Selector");
    }
};

}

#endif