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#define DEBUG_DECLARE_ONLY
22141cc406Sopenharmony_ci
23141cc406Sopenharmony_ci#include "static_init.h"
24141cc406Sopenharmony_ci#include <vector>
25141cc406Sopenharmony_ci
26141cc406Sopenharmony_cinamespace genesys {
27141cc406Sopenharmony_ci
28141cc406Sopenharmony_cistatic std::unique_ptr<std::vector<std::function<void()>>> s_functions_run_at_backend_exit;
29141cc406Sopenharmony_ci
30141cc406Sopenharmony_civoid add_function_to_run_at_backend_exit(const std::function<void()>& function)
31141cc406Sopenharmony_ci{
32141cc406Sopenharmony_ci    if (!s_functions_run_at_backend_exit)
33141cc406Sopenharmony_ci        s_functions_run_at_backend_exit.reset(new std::vector<std::function<void()>>());
34141cc406Sopenharmony_ci    s_functions_run_at_backend_exit->push_back(std::move(function));
35141cc406Sopenharmony_ci}
36141cc406Sopenharmony_ci
37141cc406Sopenharmony_civoid run_functions_at_backend_exit()
38141cc406Sopenharmony_ci{
39141cc406Sopenharmony_ci    if (s_functions_run_at_backend_exit)
40141cc406Sopenharmony_ci    {
41141cc406Sopenharmony_ci        for (auto it = s_functions_run_at_backend_exit->rbegin();
42141cc406Sopenharmony_ci             it != s_functions_run_at_backend_exit->rend(); ++it)
43141cc406Sopenharmony_ci        {
44141cc406Sopenharmony_ci            (*it)();
45141cc406Sopenharmony_ci        }
46141cc406Sopenharmony_ci        s_functions_run_at_backend_exit.reset();
47141cc406Sopenharmony_ci    }
48141cc406Sopenharmony_ci}
49141cc406Sopenharmony_ci
50141cc406Sopenharmony_ci} // namespace genesys
51