Lumiera  0.pre.03
»edit your freedom«
lifecycle.cpp
Go to the documentation of this file.
1 /*
2  Lifecycle - registering and triggering lifecycle callbacks
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
29 #include "lib/error.hpp"
30 #include "include/lifecycle.h"
32 #include "lib/util.hpp"
33 
34 
35 
36 
37 namespace lumiera {
38 
39  // ==== implementation Lifecycle Registry =======
40 
49  LifecycleRegistry&
51  {
52  static LifecycleRegistry theRegistry;
53  return theRegistry; // Meyer's singleton
54  }
55 
56 
57 
58  // ==== implementation LifecycleHook class =======
59 
60  typedef LifecycleRegistry::Hook Callback;
61 
62 
63  LifecycleHook::LifecycleHook (Symbol eventLabel, Callback callbackFun)
64  {
65  add (eventLabel,callbackFun);
66  }
67 
68  void
69  LifecycleHook::add (Symbol eventLabel, Callback callbackFun)
70  {
71  bool isNew = LifecycleRegistry::instance().enrol (eventLabel,callbackFun);
72 
73  if (isNew and eventLabel == ON_BASIC_INIT)
74  callbackFun(); // when this code executes,
75  // then per definition we are already post "basic init"
76  // (which happens in the AppState ctor); thus fire it immediately
77  }
78 
79 
80  void
82  {
83  LifecycleRegistry::instance().execute (eventLabel);
84  }
85 
86 
87 
88  const char * ON_BASIC_INIT ("ON_BASIC_INIT");
89  const char * ON_GLOBAL_INIT ("ON_GLOBAL_INIT");
90  const char * ON_GLOBAL_SHUTDOWN ("ON_GLOBAL_SHUTDOWN");
91 
92  const char * ON_EMERGENCY ("ON_EMERGENCY");
93 
94 
95 } // namespace lumiera
96 
97 
98 extern "C" { /* ==== implementation C interface for lifecycle hooks ======= */
99 
100 
101  const char * lumiera_ON_BASIC_INIT = lumiera::ON_BASIC_INIT;
102  const char * lumiera_ON_GLOBAL_INIT = lumiera::ON_GLOBAL_INIT;
103  const char * lumiera_ON_GLOBAL_SHUTDOWN = lumiera::ON_GLOBAL_SHUTDOWN;
104 
105  const char * lumiera_ON_EMERGENCY = lumiera::ON_EMERGENCY;
106 
107 
108 
109  void
110  lumiera_LifecycleHook_add (const char* eventLabel, void callbackFun(void))
111  {
112  lumiera::LifecycleHook (eventLabel, callbackFun);
113  }
114 
115 
116  void
117  lumiera_Lifecycle_trigger (const char* eventLabel)
118  {
119  lumiera::LifecycleRegistry::instance().execute (eventLabel);
120  }
121 
122 }
const char * ON_BASIC_INIT
automatic static init. treated specially to run as soon as possible
static LifecycleRegistry & instance()
get the (single) LifecycleRegistry instance.
Definition: lifecycle.cpp:50
Installing and invoking of application lifecycle event callbacks.
Registry of callback functions accessible by a label (ID) provided at registration.
const char * ON_GLOBAL_SHUTDOWN
to be triggered at the end of main()
static void add(Symbol eventLabel, Hook callbackFun)
alternative, static interface for registering a callback
Definition: lifecycle.cpp:69
static void trigger(Symbol eventLabel)
trigger lifecycle callbacks registered under the given label
Definition: lifecycle.cpp:81
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
bool enrol(Symbol label, Hook toCall)
const char * ON_GLOBAL_INIT
to be triggered in main()
define and register a callback for a specific lifecycle event.
Definition: lifecycle.h:76
Lumiera error handling (C++ interface).
Lumiera public interface.
Definition: advice.cpp:113
Helper for registering lifecycle event callbacks, which are provided as a global service by lumiera::...
const char * ON_EMERGENCY
activated on shutdown after premature failure of a subsystem