forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContinuationInput.h
More file actions
61 lines (48 loc) · 1.8 KB
/
ContinuationInput.h
File metadata and controls
61 lines (48 loc) · 1.8 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
// 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)
#pragma once
#include "react/detail/Defs.h"
#include <functional>
#include <memory>
#include <mutex>
#include "tbb/concurrent_vector.h"
/***************************************/ REACT_IMPL_BEGIN /**************************************/
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ContinuationInput
///////////////////////////////////////////////////////////////////////////////////////////////////
class ContinuationInput
{
public:
using InputClosureT = std::function<void()>;
using InputVectT = tbb::concurrent_vector<InputClosureT>;
// Note: Shouldn't this be generated by default? Apparently it isn't.
inline ContinuationInput& operator=(ContinuationInput&& other)
{
bufferedInputsPtr_ = std::move(other.bufferedInputsPtr_);
return *this;
}
inline bool IsEmpty() const { return bufferedInputsPtr_ == nullptr; }
template <typename F>
void Add(F&& input)
{
std::call_once(bufferedInputsInit_, [this] {
bufferedInputsPtr_.reset(new InputVectT());
});
bufferedInputsPtr_->push_back(std::forward<F>(input));
}
inline void Execute()
{
if (bufferedInputsPtr_ != nullptr)
{
for (auto f : *bufferedInputsPtr_)
f();
bufferedInputsPtr_->clear();
}
}
private:
std::once_flag bufferedInputsInit_;
std::unique_ptr<InputVectT> bufferedInputsPtr_ = nullptr;
};
/****************************************/ REACT_IMPL_END /***************************************/