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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
14 
20 #include "lib/error.hpp"
21 #include "include/lifecycle.h"
23 #include "lib/util.hpp"
24 
25 
26 
27 
28 namespace lumiera {
29 
30  // ==== implementation Lifecycle Registry =======
31 
40  LifecycleRegistry&
42  {
43  static LifecycleRegistry theRegistry;
44  return theRegistry; // Meyer's singleton
45  }
46 
47 
48 
49  // ==== implementation LifecycleHook class =======
50 
51  typedef LifecycleRegistry::Hook Callback;
52 
53 
54  LifecycleHook::LifecycleHook (Symbol eventLabel, Callback callbackFun)
55  {
56  add (eventLabel,callbackFun);
57  }
58 
59  void
60  LifecycleHook::add (Symbol eventLabel, Callback callbackFun)
61  {
62  bool isNew = LifecycleRegistry::instance().enrol (eventLabel,callbackFun);
63 
64  if (isNew and eventLabel == ON_BASIC_INIT)
65  callbackFun(); // when this code executes,
66  // then per definition we are already post "basic init"
67  // (which happens in the AppState ctor); thus fire it immediately
68  }
69 
70 
71  void
73  {
74  LifecycleRegistry::instance().execute (eventLabel);
75  }
76 
77 
78 
79  const char * ON_BASIC_INIT ("ON_BASIC_INIT");
80  const char * ON_GLOBAL_INIT ("ON_GLOBAL_INIT");
81  const char * ON_GLOBAL_SHUTDOWN ("ON_GLOBAL_SHUTDOWN");
82 
83  const char * ON_EMERGENCY ("ON_EMERGENCY");
84 
85 
86 } // namespace lumiera
87 
88 
89 extern "C" { /* ==== implementation C interface for lifecycle hooks ======= */
90 
91 
92  const char * lumiera_ON_BASIC_INIT = lumiera::ON_BASIC_INIT;
93  const char * lumiera_ON_GLOBAL_INIT = lumiera::ON_GLOBAL_INIT;
94  const char * lumiera_ON_GLOBAL_SHUTDOWN = lumiera::ON_GLOBAL_SHUTDOWN;
95 
96  const char * lumiera_ON_EMERGENCY = lumiera::ON_EMERGENCY;
97 
98 
99 
100  void
101  lumiera_LifecycleHook_add (const char* eventLabel, void callbackFun(void))
102  {
103  lumiera::LifecycleHook (eventLabel, callbackFun);
104  }
105 
106 
107  void
108  lumiera_Lifecycle_trigger (const char* eventLabel)
109  {
110  lumiera::LifecycleRegistry::instance().execute (eventLabel);
111  }
112 
113 }
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:41
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:60
static void trigger(Symbol eventLabel)
trigger lifecycle callbacks registered under the given label
Definition: lifecycle.cpp:72
Token or Atom with distinct identity.
Definition: symbol.hpp:117
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:67
Lumiera error handling (C++ interface).
Lumiera public interface.
Definition: advice.cpp:104
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