blob: ff3becb6385537aa07cdd122995391f90eb490b8 (
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
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#pragma once
#if !defined(RXCPP_RX_BEHAVIOR_HPP)
#define RXCPP_RX_BEHAVIOR_HPP
#include "../rx-includes.hpp"
namespace rxcpp {
namespace subjects {
namespace detail {
template<class T>
class behavior_observer : public detail::multicast_observer<T>
{
typedef behavior_observer<T> this_type;
typedef detail::multicast_observer<T> base_type;
class behavior_observer_state : public std::enable_shared_from_this<behavior_observer_state>
{
mutable std::mutex lock;
mutable T value;
public:
behavior_observer_state(T first)
: value(first)
{
}
void reset(T v) const {
std::unique_lock<std::mutex> guard(lock);
value = std::move(v);
}
T get() const {
std::unique_lock<std::mutex> guard(lock);
return value;
}
};
std::shared_ptr<behavior_observer_state> state;
public:
behavior_observer(T f, composite_subscription l)
: base_type(l)
, state(std::make_shared<behavior_observer_state>(std::move(f)))
{
}
subscriber<T> get_subscriber() const {
return make_subscriber<T>(this->get_id(), this->get_subscription(), observer<T, detail::behavior_observer<T>>(*this)).as_dynamic();
}
T get_value() const {
return state->get();
}
template<class V>
void on_next(V v) const {
state->reset(v);
base_type::on_next(std::move(v));
}
};
}
template<class T>
class behavior
{
detail::behavior_observer<T> s;
public:
explicit behavior(T f, composite_subscription cs = composite_subscription())
: s(std::move(f), cs)
{
}
bool has_observers() const {
return s.has_observers();
}
T get_value() const {
return s.get_value();
}
subscriber<T> get_subscriber() const {
return s.get_subscriber();
}
observable<T> get_observable() const {
auto keepAlive = s;
return make_observable_dynamic<T>([=](subscriber<T> o){
if (keepAlive.get_subscription().is_subscribed()) {
o.on_next(get_value());
}
keepAlive.add(s.get_subscriber(), std::move(o));
});
}
};
}
}
#endif
|