Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
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 2008, 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
42namespace 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
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
96
97 };
98
99
100
101} // namespace lumiera
102#endif
Token or Atom with distinct identity.
Definition symbol.hpp:120
Registry of callback functions accessible by a label (ID) provided at registration.
static LifecycleRegistry & instance()
get the (single) LifecycleRegistry instance.
Definition lifecycle.cpp:41
std::map< Symbol, Callbacks > table_
bool enrol(Symbol label, Hook toCall)
Any copy and copy construction prohibited.
Definition nocopy.hpp:38
Lumiera public interface.
Definition advice.hpp:102
CStr ON_BASIC_INIT
automatic static init. treated specially to run as soon as possible
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
Definition util.hpp:230
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...