Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Trans.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_UTIL_TRANS_HPP
18#define TOOLBOX_UTIL_TRANS_HPP
19
20#include <type_traits>
21
22namespace toolbox {
23inline namespace util {
24
25template <typename TargetT>
27 static void start(TargetT& target) { target.start(); }
28 static void commit(TargetT& target) { target.commit(); }
29 static void rollback(TargetT& target) noexcept
30 {
31 static_assert(std::is_nothrow_invocable_v<decltype(&TargetT::rollback), TargetT>);
32 target.rollback();
33 }
34};
35
36template <typename TargetT, typename TraitsT = TransTraits<TargetT>>
38 public:
40 : target_{target}
41 {
42 TraitsT::start(target_);
43 }
45 {
46 if (!done_) {
47 TraitsT::rollback(target_);
48 }
49 }
50
51 // Copy.
52 BasicTrans(const BasicTrans&) = delete;
53 BasicTrans& operator=(const BasicTrans&) = delete;
54
55 // Move.
58
59 void commit()
60 {
61 TraitsT::commit(target_);
62 done_ = true;
63 }
64
65 private:
66 TargetT& target_;
67 bool done_{false};
68};
69
70} // namespace util
71} // namespace toolbox
72
73#endif // TOOLBOX_UTIL_TRANS_HPP
BasicTrans(BasicTrans &&)=delete
BasicTrans(const BasicTrans &)=delete
BasicTrans & operator=(BasicTrans &&)=delete
BasicTrans & operator=(const BasicTrans &)=delete
BasicTrans(TargetT &target)
Definition Trans.hpp:39
constexpr auto bind() noexcept
Definition Slot.hpp:92
static void start(TargetT &target)
Definition Trans.hpp:27
static void rollback(TargetT &target) noexcept
Definition Trans.hpp:29
static void commit(TargetT &target)
Definition Trans.hpp:28