OR-Tools  8.2
cleanup.h
Go to the documentation of this file.
1// Copyright 2010-2018 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_CLEANUP_H_
15#define OR_TOOLS_BASE_CLEANUP_H_
16
17#include <utility>
18
19#include "absl/base/macros.h"
21
22namespace absl {
23
24namespace cleanup_internal {
25
26template <typename Callback>
27class Storage {
28 using InvokeT = absl::base_internal::invoke_result_t<Callback>;
29 static_assert(std::is_same<InvokeT, void>::value, "");
30 static_assert(!std::is_reference<Callback>::value, "");
31
32 public:
33 Storage() : contains_callback_(false), callback_() {}
34
35 Storage(Storage&& other_storage)
36 : contains_callback_(other_storage.ContainsCallback()),
37 callback_(other_storage.ReleaseCallback()) {}
38
39 template <typename TheCallback>
40 explicit Storage(TheCallback&& the_callback)
41 : contains_callback_(true),
42 callback_(std::forward<TheCallback>(the_callback)) {}
43
44 template <typename OtherCallback>
45 Storage(Storage<OtherCallback>&& other_storage) // NOLINT
46 : contains_callback_(other_storage.ContainsCallback()),
47 callback_(other_storage.ReleaseCallback()) {}
48
49 Storage& operator=(Storage&& other_storage) {
50 if (ContainsCallback()) std::move(callback_)();
51 contains_callback_ = other_storage.ContainsCallback();
52 callback_ = other_storage.ReleaseCallback();
53 return *this;
54 }
55
56 bool ContainsCallback() const { return contains_callback_; }
57
58 Callback ReleaseCallback() {
59 contains_callback_ = false;
60 return std::move(callback_);
61 }
62
63 void CancelCallback() { contains_callback_ = false; }
64
67
68 std::move(callback_)();
69 }
70
71 private:
72 bool contains_callback_;
73 Callback callback_;
74};
75
77 template <template <typename> class Cleanup, typename Callback>
79 return cleanup.storage_;
80 }
81};
82
83} // namespace cleanup_internal
84
85template <typename Callback>
86class ABSL_MUST_USE_RESULT Cleanup {
89
90 public:
91 Cleanup() = default;
92
93 Cleanup(Cleanup&&) = default;
94
95 template <typename TheCallback>
96 explicit Cleanup(TheCallback&& the_callback)
97 : storage_(std::forward<TheCallback>(the_callback)) {}
98
99 template <typename OtherCallback>
100 Cleanup(Cleanup<OtherCallback>&& other_cleanup) // NOLINT
101 : storage_(std::move(AccessStorage::From(other_cleanup))) {}
102
104 if (storage_.ContainsCallback()) storage_.InvokeCallback();
105 }
106
107 // Assignment to a cleanup object behaves like destroying it and making a new
108 // one in its place (analogous to `std::unique_ptr<T>` semantics).
109 Cleanup& operator=(Cleanup&&) = default;
110
111 bool is_released() const { return !storage_.ContainsCallback(); }
112
113 private:
114 friend AccessStorage;
115
116 Storage storage_;
117};
118
119template <int&... PreventExplicitTemplateArguments, typename Callback>
122 std::forward<Callback>(callback));
123}
124
125} // namespace absl
126
127#endif // OR_TOOLS_BASE_CLEANUP_H_
Cleanup(Cleanup< OtherCallback > &&other_cleanup)
Definition: cleanup.h:100
Cleanup(TheCallback &&the_callback)
Definition: cleanup.h:96
bool is_released() const
Definition: cleanup.h:111
Cleanup()=default
Cleanup(Cleanup &&)=default
Cleanup & operator=(Cleanup &&)=default
Storage(TheCallback &&the_callback)
Definition: cleanup.h:40
Storage(Storage &&other_storage)
Definition: cleanup.h:35
Storage(Storage< OtherCallback > &&other_storage)
Definition: cleanup.h:45
Storage & operator=(Storage &&other_storage)
Definition: cleanup.h:49
bool ContainsCallback() const
Definition: cleanup.h:56
int64 value
MPCallback * callback
Definition: cleanup.h:22
absl::Cleanup< absl::decay_t< Callback > > MakeCleanup(Callback &&callback)
Definition: cleanup.h:120
STL namespace.
static Storage< Callback > & From(Cleanup< Callback > &cleanup)
Definition: cleanup.h:78