cleanup.h 3.61 KB
Newer Older
Valentin Platzgummer's avatar
Valentin Platzgummer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef OR_TOOLS_BASE_CLEANUP_H_
#define OR_TOOLS_BASE_CLEANUP_H_

#include <utility>

19 20 21
#include "absl/base/macros.h"
#include "ortools/base/logging.h"

Valentin Platzgummer's avatar
Valentin Platzgummer committed
22 23
namespace absl {

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
namespace cleanup_internal {

template <typename Callback>
class Storage {
  using InvokeT = absl::base_internal::InvokeT<Callback>;
  static_assert(std::is_same<InvokeT, void>::value, "");
  static_assert(!std::is_reference<Callback>::value, "");

 public:
  Storage() : contains_callback_(false), callback_() {}

  Storage(Storage&& other_storage)
      : contains_callback_(other_storage.ContainsCallback()),
        callback_(other_storage.ReleaseCallback()) {}

  template <typename TheCallback>
  explicit Storage(TheCallback&& the_callback)
      : contains_callback_(true),
        callback_(std::forward<TheCallback>(the_callback)) {}

  template <typename OtherCallback>
  Storage(Storage<OtherCallback>&& other_storage)  // NOLINT
      : contains_callback_(other_storage.ContainsCallback()),
        callback_(other_storage.ReleaseCallback()) {}

  Storage& operator=(Storage&& other_storage) {
    if (ContainsCallback()) std::move(callback_)();
    contains_callback_ = other_storage.ContainsCallback();
    callback_ = other_storage.ReleaseCallback();
    return *this;
  }

  bool ContainsCallback() const { return contains_callback_; }

  Callback ReleaseCallback() {
    contains_callback_ = false;
    return std::move(callback_);
  }

  void CancelCallback() { contains_callback_ = false; }

  void InvokeCallback() {
    CancelCallback();

    std::move(callback_)();
  }

 private:
  bool contains_callback_;
  Callback callback_;
};

struct AccessStorage {
  template <template <typename> class Cleanup, typename Callback>
  static Storage<Callback>& From(Cleanup<Callback>& cleanup) {
    return cleanup.storage_;
  }
};

}  // namespace cleanup_internal

template <typename Callback>
class ABSL_MUST_USE_RESULT Cleanup {
  using Storage = cleanup_internal::Storage<Callback>;
  using AccessStorage = cleanup_internal::AccessStorage;

Valentin Platzgummer's avatar
Valentin Platzgummer committed
90
 public:
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  Cleanup() = default;

  Cleanup(Cleanup&&) = default;

  template <typename TheCallback>
  explicit Cleanup(TheCallback&& the_callback)
      : storage_(std::forward<TheCallback>(the_callback)) {}

  template <typename OtherCallback>
  Cleanup(Cleanup<OtherCallback>&& other_cleanup)  // NOLINT
      : storage_(std::move(AccessStorage::From(other_cleanup))) {}

  ~Cleanup() {
    if (storage_.ContainsCallback()) storage_.InvokeCallback();
  }

  // Assignment to a cleanup object behaves like destroying it and making a new
  // one in its place (analogous to `std::unique_ptr<T>` semantics).
  Cleanup& operator=(Cleanup&&) = default;

  bool is_released() const { return !storage_.ContainsCallback(); }
Valentin Platzgummer's avatar
Valentin Platzgummer committed
112 113

 private:
114 115 116
  friend AccessStorage;

  Storage storage_;
Valentin Platzgummer's avatar
Valentin Platzgummer committed
117 118
};

119 120 121 122
template <int&... PreventExplicitTemplateArguments, typename Callback>
absl::Cleanup<absl::decay_t<Callback>> MakeCleanup(Callback&& callback) {
  return absl::Cleanup<absl::decay_t<Callback>>(
      std::forward<Callback>(callback));
Valentin Platzgummer's avatar
Valentin Platzgummer committed
123 124 125 126 127
}

}  // namespace absl

#endif  // OR_TOOLS_BASE_CLEANUP_H_