1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_singleton_cleanup_h)
27 #define octave_singleton_cleanup_h 1
28 
29 #include "octave-config.h"
30 
31 #include <set>
32 
33 class
34 OCTAVE_API
35 singleton_cleanup_list
36 {
37 protected:
38 
singleton_cleanup_list(void)39   singleton_cleanup_list (void) : m_fcn_list () { }
40 
41 public:
42 
43   typedef void (*fptr) (void);
44 
45   // No copying!
46 
47   singleton_cleanup_list (const singleton_cleanup_list&) = delete;
48 
49   singleton_cleanup_list& operator = (const singleton_cleanup_list&) = delete;
50 
51   ~singleton_cleanup_list (void);
52 
add(fptr f)53   static void add (fptr f)
54   {
55     if (instance_ok ())
56       s_instance->do_add (f);
57   }
58 
cleanup(void)59   static void cleanup (void)
60   {
61     delete s_instance;
62     s_instance = nullptr;
63   }
64 
65 private:
66 
67   static singleton_cleanup_list *s_instance;
68 
69   static bool instance_ok (void);
70 
cleanup_instance(void)71   static void cleanup_instance (void)
72   {
73     delete s_instance;
74     s_instance = nullptr;
75   }
76 
77   std::set<fptr> m_fcn_list;
78 
do_add(fptr f)79   void do_add (fptr f)
80   {
81     m_fcn_list.insert (f);
82   }
83 };
84 
85 #endif
86