forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactiveBase.h
More file actions
158 lines (123 loc) · 4.37 KB
/
ReactiveBase.h
File metadata and controls
158 lines (123 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
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
// Copyright Sebastian Jeckel 2014.
// 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_DETAIL_REACTIVEBASE_H_INCLUDED
#define REACT_DETAIL_REACTIVEBASE_H_INCLUDED
#pragma once
#include "react/detail/Defs.h"
#include <memory>
#include <utility>
#include "react/detail/graph/GraphBase.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
template <typename L, typename R>
bool Equals(const L& lhs, const R& rhs)
{
return lhs == rhs;
}
template <typename L, typename R>
bool Equals(const std::reference_wrapper<L>& lhs, const std::reference_wrapper<R>& rhs)
{
return lhs.get() == rhs.get();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ReactiveBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode>
class ReactiveBase
{
public:
using DomainT = typename TNode::DomainT;
// Default ctor
ReactiveBase() = default;
// Copy ctor
ReactiveBase(const ReactiveBase&) = default;
// Move ctor (VS2013 doesn't default generate that yet)
ReactiveBase(ReactiveBase&& other) :
ptr_( std::move(other.ptr_) )
{}
// Explicit node ctor
explicit ReactiveBase(std::shared_ptr<TNode>&& ptr) :
ptr_( std::move(ptr) )
{}
// Copy assignment
ReactiveBase& operator=(const ReactiveBase&) = default;
// Move assignment
ReactiveBase& operator=(ReactiveBase&& other)
{
ptr_.reset(std::move(other));
return *this;
}
bool IsValid() const
{
return ptr_ != nullptr;
}
void SetWeightHint(WeightHint weight)
{
assert(IsValid());
ptr_->SetWeightHint(weight);
}
protected:
std::shared_ptr<TNode> ptr_;
template <typename TNode_>
friend const std::shared_ptr<TNode_>& GetNodePtr(const ReactiveBase<TNode_>& node);
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// CopyableReactive
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode>
class CopyableReactive : public ReactiveBase<TNode>
{
public:
CopyableReactive() = default;
CopyableReactive(const CopyableReactive&) = default;
CopyableReactive(CopyableReactive&& other) :
CopyableReactive::ReactiveBase( std::move(other) )
{}
explicit CopyableReactive(std::shared_ptr<TNode>&& ptr) :
CopyableReactive::ReactiveBase( std::move(ptr) )
{}
CopyableReactive& operator=(const CopyableReactive&) = default;
CopyableReactive& operator=(CopyableReactive&& other)
{
CopyableReactive::ReactiveBase::operator=(std::move(other));
return *this;
}
bool Equals(const CopyableReactive& other) const
{
return this->ptr_ == other.ptr_;
}
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// UniqueReactiveBase
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode>
class MovableReactive : public ReactiveBase<TNode>
{
public:
MovableReactive() = default;
MovableReactive(MovableReactive&& other) :
MovableReactive::ReactiveBase( std::move(other) )
{}
explicit MovableReactive(std::shared_ptr<TNode>&& ptr) :
MovableReactive::ReactiveBase( std::move(ptr) )
{}
MovableReactive& operator=(MovableReactive&& other)
{
MovableReactive::ReactiveBase::operator=(std::move(other));
return *this;
}
// Deleted copy ctor and assignment
MovableReactive(const MovableReactive&) = delete;
MovableReactive& operator=(const MovableReactive&) = delete;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
/// GetNodePtr
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TNode>
const std::shared_ptr<TNode>& GetNodePtr(const ReactiveBase<TNode>& node)
{
return node.ptr_;
}
/****************************************/ REACT_IMPL_END /***************************************/
#endif // REACT_DETAIL_REACTIVEBASE_H_INCLUDED