Geant4 Cross Reference |
1 // 2 // MIT License 3 // Copyright (c) 2020 Jonathan R. Madsen 4 // Permission is hereby granted, free of charge, to any person obtaining a copy 5 // of this software and associated documentation files (the "Software"), to deal 6 // in the Software without restriction, including without limitation the rights 7 // to use, copy, modify, merge, publish, distribute, sublicense, and 8 // copies of the Software, and to permit persons to whom the Software is 9 // furnished to do so, subject to the following conditions: 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 12 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 13 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 14 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 15 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 16 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 17 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 // 19 20 #pragma once 21 22 #include <functional> 23 #include <utility> 24 25 namespace PTL 26 { 27 struct ScopeDestructor 28 { 29 template <typename FuncT> 30 ScopeDestructor(FuncT&& _func) 31 : m_functor(std::forward<FuncT>(_func)) 32 {} 33 34 // delete copy operations 35 ScopeDestructor(const ScopeDestructor&) = delete; 36 ScopeDestructor& operator=(const ScopeDestructor&) = delete; 37 38 // allow move operations 39 ScopeDestructor(ScopeDestructor&& rhs) noexcept 40 : m_functor(std::move(rhs.m_functor)) 41 { 42 rhs.m_functor = []() {}; 43 } 44 45 ScopeDestructor& operator=(ScopeDestructor&& rhs) noexcept 46 { 47 if(this != &rhs) 48 { 49 m_functor = std::move(rhs.m_functor); 50 rhs.m_functor = []() {}; 51 } 52 return *this; 53 } 54 55 ~ScopeDestructor() { m_functor(); } 56 57 private: 58 std::function<void()> m_functor = []() {}; 59 }; 60 61 } // namespace PTL 62