// Copyright 2019 The Marl 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. // Finally can be used to execute a lambda or function when the final reference // to the Finally is dropped. // // The purpose of a finally is to perform cleanup or termination logic and is // especially useful when there are multiple early returns within a function. // // A moveable Finally can be constructed with marl::make_finally(). // A sharable Finally can be constructed with marl::make_shared_finally(). #ifndef marl_finally_h #define marl_finally_h #include "export.h" #include #include namespace marl { // Finally is a pure virtual base class, implemented by the templated // FinallyImpl. class Finally { public: virtual ~Finally() = default; }; // FinallyImpl implements a Finally. // The template parameter F is the function type to be called when the finally // is destructed. F must have the signature void(). template class FinallyImpl : public Finally { public: MARL_NO_EXPORT inline FinallyImpl(const F& func); MARL_NO_EXPORT inline FinallyImpl(F&& func); MARL_NO_EXPORT inline FinallyImpl(FinallyImpl&& other); MARL_NO_EXPORT inline ~FinallyImpl(); private: FinallyImpl(const FinallyImpl& other) = delete; FinallyImpl& operator=(const FinallyImpl& other) = delete; FinallyImpl& operator=(FinallyImpl&&) = delete; F func; bool valid = true; }; template FinallyImpl::FinallyImpl(const F& func) : func(func) {} template FinallyImpl::FinallyImpl(F&& func) : func(std::move(func)) {} template FinallyImpl::FinallyImpl(FinallyImpl&& other) : func(std::move(other.func)) { other.valid = false; } template FinallyImpl::~FinallyImpl() { if (valid) { func(); } } template inline FinallyImpl make_finally(F&& f) { return FinallyImpl(std::forward(f)); } template inline std::shared_ptr make_shared_finally(F&& f) { return std::make_shared>(std::forward(f)); } } // namespace marl #endif // marl_finally_h