OR-Tools  8.1
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"
20 #include "ortools/base/logging.h"
21 
22 namespace absl {
23 
24 namespace cleanup_internal {
25 
26 template <typename Callback>
27 class 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 
65  void InvokeCallback() {
67 
68  std::move(callback_)();
69  }
70 
71  private:
72  bool contains_callback_;
73  Callback callback_;
74 };
75 
76 struct AccessStorage {
77  template <template <typename> class Cleanup, typename Callback>
79  return cleanup.storage_;
80  }
81 };
82 
83 } // namespace cleanup_internal
84 
85 template <typename Callback>
86 class 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 
119 template <int&... PreventExplicitTemplateArguments, typename Callback>
122  std::forward<Callback>(callback));
123 }
124 
125 } // namespace absl
126 
127 #endif // OR_TOOLS_BASE_CLEANUP_H_
absl::cleanup_internal::Storage::CancelCallback
void CancelCallback()
Definition: cleanup.h:63
absl::Cleanup::~Cleanup
~Cleanup()
Definition: cleanup.h:103
logging.h
absl::Cleanup::Cleanup
Cleanup(Cleanup &&)=default
value
int64 value
Definition: demon_profiler.cc:43
absl::cleanup_internal::AccessStorage::From
static Storage< Callback > & From(Cleanup< Callback > &cleanup)
Definition: cleanup.h:78
absl::MakeCleanup
absl::Cleanup< absl::decay_t< Callback > > MakeCleanup(Callback &&callback)
Definition: cleanup.h:120
absl::cleanup_internal::Storage
Definition: cleanup.h:27
absl::cleanup_internal::Storage::InvokeCallback
void InvokeCallback()
Definition: cleanup.h:65
absl::Cleanup::is_released
bool is_released() const
Definition: cleanup.h:111
absl::Cleanup::Cleanup
Cleanup()=default
absl::cleanup_internal::Storage::Storage
Storage(TheCallback &&the_callback)
Definition: cleanup.h:40
absl::Cleanup
Definition: cleanup.h:86
absl::cleanup_internal::Storage::ContainsCallback
bool ContainsCallback() const
Definition: cleanup.h:56
callback
MPCallback * callback
Definition: gurobi_interface.cc:510
absl::cleanup_internal::Storage::Storage
Storage()
Definition: cleanup.h:33
absl::cleanup_internal::Storage::ReleaseCallback
Callback ReleaseCallback()
Definition: cleanup.h:58
absl::cleanup_internal::Storage::Storage
Storage(Storage< OtherCallback > &&other_storage)
Definition: cleanup.h:45
absl::Cleanup::Cleanup
Cleanup(Cleanup< OtherCallback > &&other_cleanup)
Definition: cleanup.h:100
absl::Cleanup::Cleanup
Cleanup(TheCallback &&the_callback)
Definition: cleanup.h:96
absl
Definition: cleanup.h:22
absl::cleanup_internal::Storage::operator=
Storage & operator=(Storage &&other_storage)
Definition: cleanup.h:49
absl::cleanup_internal::AccessStorage
Definition: cleanup.h:76
absl::Cleanup::operator=
Cleanup & operator=(Cleanup &&)=default
absl::cleanup_internal::Storage::Storage
Storage(Storage &&other_storage)
Definition: cleanup.h:35