forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.h
More file actions
104 lines (81 loc) · 4.37 KB
/
observer.h
File metadata and controls
104 lines (81 loc) · 4.37 KB
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 Sebastian Jeckel 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef REACT_OBSERVER_H_INCLUDED
#define REACT_OBSERVER_H_INCLUDED
#pragma once
#include "react/detail/defs.h"
#include "react/api.h"
#include "react/group.h"
#include <memory>
#include <utility>
#include "react/detail/observer_nodes.h"
/*****************************************/ REACT_BEGIN /*****************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ObserverBase
///////////////////////////////////////////////////////////////////////////////////////////////////
class Observer : protected REACT_IMPL::ObserverInternals
{
private:
using NodeType = REACT_IMPL::ObserverNode;
public:
// Construct state observer with explicit group
template <typename F, typename T1, typename ... Ts>
static Observer Create(const Group& group, F&& func, const State<T1>& subject1, const State<Ts>& ... subjects)
{ return CreateStateObserverNode(group, std::forward<F>(func), subject1, subjects ...); }
// Construct state observer with implicit group
template <typename F, typename T1, typename ... Ts>
static Observer Create(F&& func, const State<T1>& subject1, const State<Ts>& ... subjects)
{ return CreateStateObserverNode(subject1.GetGroup(), std::forward<F>(func), subject1, subjects ...); }
// Construct event observer with explicit group
template <typename F, typename T>
static Observer Create(const Group& group, F&& func, const Event<T>& subject)
{ return CreateEventObserverNode(group, std::forward<F>(func), subject); }
// Construct event observer with implicit group
template <typename F, typename T>
static Observer Create(F&& func, const Event<T>& subject)
{ return CreateEventObserverNode(subject.GetGroup(), std::forward<F>(func), subject); }
// Constructed synced event observer with explicit group
template <typename F, typename T, typename ... Us>
static Observer Create(const Group& group, F&& func, const Event<T>& subject, const State<Us>& ... states)
{ return CreateSyncedEventObserverNode(group, std::forward<F>(func), subject, states ...); }
// Constructed synced event observer with implicit group
template <typename F, typename T, typename ... Us>
static Observer Create(F&& func, const Event<T>& subject, const State<Us>& ... states)
{ return CreateSyncedEventObserverNode(subject.GetGroup(), std::forward<F>(func), subject, states ...); }
Observer(const Observer&) = default;
Observer& operator=(const Observer&) = default;
Observer(Observer&&) = default;
Observer& operator=(Observer&&) = default;
protected: //Internal
Observer(std::shared_ptr<NodeType>&& nodePtr) :
nodePtr_(std::move(nodePtr))
{ }
private:
template <typename F, typename T1, typename ... Ts>
static auto CreateStateObserverNode(const Group& group, F&& func, const State<T1>& dep1, const State<Ts>& ... deps) -> decltype(auto)
{
using REACT_IMPL::StateObserverNode;
return std::make_shared<StateObserverNode<typename std::decay<F>::type, T1, Ts ...>>(
group, std::forward<F>(func), SameGroupOrLink(group, dep1), SameGroupOrLink(group, deps) ...);
}
template <typename F, typename T>
static auto CreateEventObserverNode(const Group& group, F&& func, const Event<T>& dep) -> decltype(auto)
{
using REACT_IMPL::EventObserverNode;
return std::make_shared<EventObserverNode<typename std::decay<F>::type, T>>(
group, std::forward<F>(func), SameGroupOrLink(group, dep));
}
template <typename F, typename T, typename ... Us>
static auto CreateSyncedEventObserverNode(const Group& group, F&& func, const Event<T>& dep, const State<Us>& ... syncs) -> decltype(auto)
{
using REACT_IMPL::SyncedEventObserverNode;
return std::make_shared<SyncedEventObserverNode<typename std::decay<F>::type, T, Us ...>>(
group, std::forward<F>(func), SameGroupOrLink(group, dep), SameGroupOrLink(group, syncs) ...);
}
private:
std::shared_ptr<NodeType> nodePtr_;
};
/******************************************/ REACT_END /******************************************/
#endif // REACT_OBSERVER_H_INCLUDED