// 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 #include "absl/base/macros.h" #include "ortools/base/logging.h" namespace absl { namespace cleanup_internal { template class Storage { using InvokeT = absl::base_internal::InvokeT; static_assert(std::is_same::value, ""); static_assert(!std::is_reference::value, ""); public: Storage() : contains_callback_(false), callback_() {} Storage(Storage&& other_storage) : contains_callback_(other_storage.ContainsCallback()), callback_(other_storage.ReleaseCallback()) {} template explicit Storage(TheCallback&& the_callback) : contains_callback_(true), callback_(std::forward(the_callback)) {} template Storage(Storage&& 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