1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy. 2141cc406Sopenharmony_ci 3141cc406Sopenharmony_ci Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt> 4141cc406Sopenharmony_ci 5141cc406Sopenharmony_ci This file is part of the SANE package. 6141cc406Sopenharmony_ci 7141cc406Sopenharmony_ci This program is free software; you can redistribute it and/or 8141cc406Sopenharmony_ci modify it under the terms of the GNU General Public License as 9141cc406Sopenharmony_ci published by the Free Software Foundation; either version 2 of the 10141cc406Sopenharmony_ci License, or (at your option) any later version. 11141cc406Sopenharmony_ci 12141cc406Sopenharmony_ci This program is distributed in the hope that it will be useful, but 13141cc406Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 14141cc406Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15141cc406Sopenharmony_ci General Public License for more details. 16141cc406Sopenharmony_ci 17141cc406Sopenharmony_ci You should have received a copy of the GNU General Public License 18141cc406Sopenharmony_ci along with this program. If not, see <https://www.gnu.org/licenses/>. 19141cc406Sopenharmony_ci*/ 20141cc406Sopenharmony_ci 21141cc406Sopenharmony_ci#ifndef BACKEND_GENESYS_STATIC_INIT_H 22141cc406Sopenharmony_ci#define BACKEND_GENESYS_STATIC_INIT_H 23141cc406Sopenharmony_ci 24141cc406Sopenharmony_ci#include <functional> 25141cc406Sopenharmony_ci#include <memory> 26141cc406Sopenharmony_ci 27141cc406Sopenharmony_cinamespace genesys { 28141cc406Sopenharmony_ci 29141cc406Sopenharmony_civoid add_function_to_run_at_backend_exit(const std::function<void()>& function); 30141cc406Sopenharmony_ci 31141cc406Sopenharmony_ci// calls functions added via add_function_to_run_at_backend_exit() in reverse order of being 32141cc406Sopenharmony_ci// added. 33141cc406Sopenharmony_civoid run_functions_at_backend_exit(); 34141cc406Sopenharmony_ci 35141cc406Sopenharmony_citemplate<class T> 36141cc406Sopenharmony_ciclass StaticInit { 37141cc406Sopenharmony_cipublic: 38141cc406Sopenharmony_ci StaticInit() = default; 39141cc406Sopenharmony_ci StaticInit(const StaticInit&) = delete; 40141cc406Sopenharmony_ci StaticInit& operator=(const StaticInit&) = delete; 41141cc406Sopenharmony_ci 42141cc406Sopenharmony_ci template<class... Args> 43141cc406Sopenharmony_ci void init(Args&& ... args) 44141cc406Sopenharmony_ci { 45141cc406Sopenharmony_ci ptr_ = std::unique_ptr<T>(new T(std::forward<Args>(args)...)); 46141cc406Sopenharmony_ci add_function_to_run_at_backend_exit([this](){ deinit(); }); 47141cc406Sopenharmony_ci } 48141cc406Sopenharmony_ci 49141cc406Sopenharmony_ci void deinit() 50141cc406Sopenharmony_ci { 51141cc406Sopenharmony_ci ptr_.reset(); 52141cc406Sopenharmony_ci } 53141cc406Sopenharmony_ci 54141cc406Sopenharmony_ci const T* operator->() const { return ptr_.get(); } 55141cc406Sopenharmony_ci T* operator->() { return ptr_.get(); } 56141cc406Sopenharmony_ci const T& operator*() const { return *ptr_.get(); } 57141cc406Sopenharmony_ci T& operator*() { return *ptr_.get(); } 58141cc406Sopenharmony_ci 59141cc406Sopenharmony_ciprivate: 60141cc406Sopenharmony_ci std::unique_ptr<T> ptr_; 61141cc406Sopenharmony_ci}; 62141cc406Sopenharmony_ci 63141cc406Sopenharmony_ci} // namespace genesys 64141cc406Sopenharmony_ci 65141cc406Sopenharmony_ci#endif // BACKEND_GENESYS_STATIC_INIT_H 66