// Copyright 2021 The Abseil Authors. // // 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 // // https://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. // // ----------------------------------------------------------------------------- // File: cleanup.h // ----------------------------------------------------------------------------- // // `y_absl::Cleanup` implements the scope guard idiom, invoking the contained // callback's `operator()() &&` on scope exit. // // Example: // // ``` // y_absl::Status CopyGoodData(const char* source_path, const char* sink_path) { // FILE* source_file = fopen(source_path, "r"); // if (source_file == nullptr) { // return y_absl::NotFoundError("No source file"); // No cleanups execute // } // // // C++17 style cleanup using class template argument deduction // y_absl::Cleanup source_closer = [source_file] { fclose(source_file); }; // // FILE* sink_file = fopen(sink_path, "w"); // if (sink_file == nullptr) { // return y_absl::NotFoundError("No sink file"); // First cleanup executes // } // // // C++11 style cleanup using the factory function // auto sink_closer = y_absl::MakeCleanup([sink_file] { fclose(sink_file); }); // // Data data; // while (ReadData(source_file, &data)) { // if (!data.IsGood()) { // y_absl::Status result = y_absl::FailedPreconditionError("Read bad data"); // return result; // Both cleanups execute // } // SaveData(sink_file, &data); // } // // return y_absl::OkStatus(); // Both cleanups execute // } // ``` // // Methods: // // `std::move(cleanup).Cancel()` will prevent the callback from executing. // // `std::move(cleanup).Invoke()` will execute the callback early, before // destruction, and prevent the callback from executing in the destructor. // // Usage: // // `y_absl::Cleanup` is not an interface type. It is only intended to be used // within the body of a function. It is not a value type and instead models a // control flow construct. Check out `defer` in Golang for something similar. #ifndef Y_ABSL_CLEANUP_CLEANUP_H_ #define Y_ABSL_CLEANUP_CLEANUP_H_ #include #include "y_absl/base/config.h" #include "y_absl/base/macros.h" #include "y_absl/cleanup/internal/cleanup.h" namespace y_absl { Y_ABSL_NAMESPACE_BEGIN template class Y_ABSL_MUST_USE_RESULT Cleanup final { static_assert(cleanup_internal::WasDeduced(), "Explicit template parameters are not supported."); static_assert(cleanup_internal::ReturnsVoid(), "Callbacks that return values are not supported."); public: Cleanup(Callback callback) : storage_(std::move(callback)) {} // NOLINT Cleanup(Cleanup&& other) = default; void Cancel() && { Y_ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged()); storage_.DestroyCallback(); } void Invoke() && { Y_ABSL_HARDENING_ASSERT(storage_.IsCallbackEngaged()); storage_.InvokeCallback(); storage_.DestroyCallback(); } ~Cleanup() { if (storage_.IsCallbackEngaged()) { storage_.InvokeCallback(); storage_.DestroyCallback(); } } private: cleanup_internal::Storage storage_; }; // `y_absl::Cleanup c = /* callback */;` // // C++17 type deduction API for creating an instance of `y_absl::Cleanup` #if defined(Y_ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION) template Cleanup(Callback callback) -> Cleanup; #endif // defined(Y_ABSL_HAVE_CLASS_TEMPLATE_ARGUMENT_DEDUCTION) // `auto c = y_absl::MakeCleanup(/* callback */);` // // C++11 type deduction API for creating an instance of `y_absl::Cleanup` template y_absl::Cleanup MakeCleanup(Callback callback) { static_assert(cleanup_internal::WasDeduced(), "Explicit template parameters are not supported."); static_assert(cleanup_internal::ReturnsVoid(), "Callbacks that return values are not supported."); return {std::move(callback)}; } Y_ABSL_NAMESPACE_END } // namespace y_absl #endif // Y_ABSL_CLEANUP_CLEANUP_H_