Lumiera  0.pre.03
»edit your freedom«
lifecycleregistry.hpp
Go to the documentation of this file.
1 /*
2  LIFECYCLEREGISTRY.hpp - registry for application lifecycle callbacks
3 
4  Copyright (C)
5  2008, Christian Thaeter <ct@pipapo.org>
6  Hermann Vosseler <Ichthyostega@web.de>
7 
8   **Lumiera** is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by the
10   Free Software Foundation; either version 2 of the License, or (at your
11   option) any later version. See the file COPYING for further details.
12 
13 */
14 
29 #ifndef LUMIERA_LIFECYCLEREGISTRY_H
30 #define LUMIERA_LIFECYCLEREGISTRY_H
31 
32 
33 #include "lib/util.hpp"
34 #include "lib/nocopy.hpp"
35 
36 #include <functional>
37 #include <string>
38 #include <set>
39 #include <map>
40 
41 
42 namespace lumiera {
43 
44  using util::contains;
45  using std::function;
46  using std::string;
47 
48 
49 
58  {
59  public:
60  typedef void (*Hook)(void);
61  typedef std::set<Hook> Callbacks;
62  typedef Callbacks::iterator Iter;
63 
64 
66  bool enrol (Symbol label, Hook toCall)
67  {
68  return table_[label]
69  .insert(toCall)
70  .second; // true if actually stored
71  }
72 
73  void execute (Symbol label)
74  {
75  Callbacks& cbs (table_[label]);
76  Iter e = cbs.end();
77  for (Iter p = cbs.begin();
78  p != e; ++p)
79  (*p)(); // invoke callback
80  }
81 
82 
85  static LifecycleRegistry& instance();
86 
87 
88  private:
89  std::map<Symbol, Callbacks> table_;
90 
92  execute (ON_BASIC_INIT); // just to be sure, typically a NOP, because nothing is registered yet
93  }
94 
95  ~LifecycleRegistry () { }
96 
97  };
98 
99 
100 
101 } // namespace lumiera
102 #endif
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
Registry of callback functions accessible by a label (ID) provided at registration.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
bool enrol(Symbol label, Hook toCall)
Lumiera public interface.
Definition: advice.cpp:104
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255