tjstatic.h
1 /***************************************************************************
2  tjstatic.h - description
3  -------------------
4  begin : Sun Jul 7 2002
5  copyright : (C) 2000-2015 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJSTATIC_H
19 #define TJSTATIC_H
20 
21 
22 #include <tjutils/tjutils.h>
23 #include <tjutils/tjthread.h>
24 
31 
33 class Static {
34 
35  public:
36 
37  Static() {}
38  virtual ~Static() {}
39 
40  // destroy all static objects manually
41  static void destroy_all();
42 
43  static void append_to_destructor_list(Static* sp);
44 
45  protected:
46 
47  static STD_list<Static*>* destructor_list;
48 };
49 
51 
52 
53 template<class T>
54 struct StaticAlloc : public virtual Static {
55 
56  StaticAlloc() {
57  }
58 
59  ~StaticAlloc() {
60  T::destroy_static();
61  }
62 };
63 
64 
66 
94 template<class T>
96 
97  public:
98  StaticHandler() {
99  if(!staticdone) { // fast check
100  staticdone=true; // must be assigned before call to T::init_static() to avoid infinite loops
101  Static* sp=new StaticAlloc<T>;
102  Static::append_to_destructor_list(sp);
103  T::init_static();
104  }
105  }
106 
107 
108  private:
109  static bool staticdone;
110 
111 };
112 
115 #endif