Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:28:09

0001 //
0002 // MIT License
0003 // Copyright (c) 2020 Jonathan R. Madsen
0004 // Permission is hereby granted, free of charge, to any person obtaining a copy
0005 // of this software and associated documentation files (the "Software"), to deal
0006 // in the Software without restriction, including without limitation the rights
0007 // to use, copy, modify, merge, publish, distribute, sublicense, and
0008 // copies of the Software, and to permit persons to whom the Software is
0009 // furnished to do so, subject to the following conditions:
0010 // The above copyright notice and this permission notice shall be included in
0011 // all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
0012 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
0013 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
0014 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0015 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
0016 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0017 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0018 //
0019 
0020 #pragma once
0021 
0022 #include <functional>
0023 #include <utility>
0024 
0025 namespace PTL
0026 {
0027 struct ScopeDestructor
0028 {
0029     template <typename FuncT>
0030     ScopeDestructor(FuncT&& _func)
0031     : m_functor(std::forward<FuncT>(_func))
0032     {}
0033 
0034     // delete copy operations
0035     ScopeDestructor(const ScopeDestructor&) = delete;
0036     ScopeDestructor& operator=(const ScopeDestructor&) = delete;
0037 
0038     // allow move operations
0039     ScopeDestructor(ScopeDestructor&& rhs) noexcept
0040     : m_functor(std::move(rhs.m_functor))
0041     {
0042         rhs.m_functor = []() {};
0043     }
0044 
0045     ScopeDestructor& operator=(ScopeDestructor&& rhs) noexcept
0046     {
0047         if(this != &rhs)
0048         {
0049             m_functor     = std::move(rhs.m_functor);
0050             rhs.m_functor = []() {};
0051         }
0052         return *this;
0053     }
0054 
0055     ~ScopeDestructor() { m_functor(); }
0056 
0057 private:
0058     std::function<void()> m_functor = []() {};
0059 };
0060 
0061 }  // namespace PTL