Free Electron
Initialized.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __core_Initialized_h__
8 #define __core_Initialized_h__
9 
10 namespace fe
11 {
12 
13 #define FE_INIT_DEBUG FALSE
14 
15 /**************************************************************************//**
16  @brief Base class providing collective initialization (post-constructor)
17 
18  @ingroup core
19 
20  This mechanism allows you to supply multiple initialization functions
21  in a class hierarchy, all of which are called. This operation is
22  similar to construction and is intended to supply objects with the ability
23  to do initialization that is not safe or possible during construction.
24 
25  This mechanism works for multiple inheritance and virtual inheritance.
26  In order to follow the same call order as the constructors, any base
27  classes that may contain initialize functions should precede the
28  Initialize template in the inheritance specification.
29 
30  To participate, any derived class @em T supplying an @em initialize method
31  needs to also derive from Initialize <T>.
32 
33  @code
34 class A: public Initialize<A>
35 {
36  public:
37 virtual ~A(void) {}
38  void initialize(void) { feLog("initialize A\n");
39 };
40 
41 class B: public A, public Initialize<B>
42 {
43  public:
44 virtual ~B(void) {}
45  void initialize(void) { feLog("initialize B\n");
46 };
47 
48 int main(int argc,char** argv,char** env)
49 {
50  B b;
51  b.initialize(); // just calls B::initialize
52  b.initializeAll(); // calls A::initialize and B::initialize
53 }
54  @endcode
55 *//***************************************************************************/
56 class Initialized: virtual public Castable
57 {
58  public:
59  Initialized(void):
60  m_active(true)
61  {
62 #if FE_INIT_DEBUG
63  feLog("Initialized::Initialized %p\n",this);
64 #endif
65 
66 #if FE_CODEGEN>FE_DEBUG && FE_COMPILER==FE_GNU
67  // HACK revisit and retry later
68  // gnu optimized can try to eliminate constructor entirely
69 
70  // something trivial to con compiler to keep constructor
71  SystemTicker t; t.timeMS();
72 #endif
73  }
74 virtual ~Initialized(void)
75  {
76 #if FE_INIT_DEBUG
77  feLog("Initialized::~Initialized %p\n",this);
78 #endif
79  }
80 
81  void initializeAll(void)
82  {
83 #if FE_INIT_DEBUG
84  feLog("Initialized::initializeAll %p %d %d\n",
85  this,m_active,m_initializers.size());
86 #endif
87  if(!m_active)
88  {
89  return;
90  }
91  const int size=m_initializers.size();
92  for(int m=0;m<size;m++)
93  {
94  m_initializers[m](this);
95  }
96  }
97 
98  void setActive(bool a_active) { m_active = a_active; }
99 
100  protected:
101 
102 typedef void (*InitializeFunction)(Initialized*);
103 
104  void addInitializer(InitializeFunction function)
105  { m_initializers.push_back(function); }
106 
107  private:
108 
109  Array<InitializeFunction> m_initializers;
110  bool m_active;
111 };
112 
113 /**************************************************************************//**
114  @brief Per-class participation in the Initialized <> mechanism
115 *//***************************************************************************/
116 template<class T>
117 class Initialize: public CastableAs<T>, virtual public Initialized
118 {
119  public:
120  Initialize(void)
121  {
122 #if FE_INIT_DEBUG
123  feLog("Initialize::Initialize %p \"%s\"\n",
124  (T*)this,FE_TYPESTRING(T).c_str());
125 #endif
126  addInitializer(&Initialize<T>::static_initialize);
127  }
128 
129  private:
130 
131 static void static_initialize(Initialized* obj)
132  {
133 #if FE_INIT_DEBUG
134  feLog("Initialize::static_initialize %p \"%s\"\n",
135  obj,FE_TYPESTRING(T).c_str());
136 #endif
137  fe_cast<T>(obj)->initialize();
138  }
139 };
140 
141 } // namespace
142 
143 #endif // __core_Initialized_h__
Base participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:93
kernel
Definition: namespace.dox:3
Per-class participation in the Initialized <> mechanism.
Definition: Initialized.h:117
unsigned long timeMS(void)
Returns a time in milliseconds from an arbitrary fixed reference point.
Definition: SystemTicker.cc:190
High precision timer.
Definition: SystemTicker.h:174
Base class providing collective initialization (post-constructor)
Definition: Initialized.h:56
Per-class participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:192