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) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6  Hermann Vosseler <Ichthyostega@web.de>
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of
11  the License, or (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 
22 */
23 
38 #ifndef LUMIERA_LIFECYCLEREGISTRY_H
39 #define LUMIERA_LIFECYCLEREGISTRY_H
40 
41 
42 #include "lib/util.hpp"
43 #include "lib/nocopy.hpp"
44 
45 #include <functional>
46 #include <string>
47 #include <set>
48 #include <map>
49 
50 
51 namespace lumiera {
52 
53  using util::contains;
54  using std::function;
55  using std::string;
56 
57 
58 
67  {
68  public:
69  typedef void (*Hook)(void);
70  typedef std::set<Hook> Callbacks;
71  typedef Callbacks::iterator Iter;
72 
73 
75  bool enrol (Symbol label, Hook toCall)
76  {
77  return table_[label]
78  .insert(toCall)
79  .second; // true if actually stored
80  }
81 
82  void execute (Symbol label)
83  {
84  Callbacks& cbs (table_[label]);
85  Iter e = cbs.end();
86  for (Iter p = cbs.begin();
87  p != e; ++p)
88  (*p)(); // invoke callback
89  }
90 
91 
94  static LifecycleRegistry& instance();
95 
96 
97  private:
98  std::map<Symbol, Callbacks> table_;
99 
100  LifecycleRegistry () {
101  execute (ON_BASIC_INIT); // just to be sure, typically a NOP, because nothing is registered yet
102  }
103 
104  ~LifecycleRegistry () { }
105 
106  };
107 
108 
109 
110 } // namespace lumiera
111 #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:50
Registry of callback functions accessible by a label (ID) provided at registration.
Any copy and copy construction prohibited.
Definition: nocopy.hpp:46
Token or Atom with distinct identity.
Definition: symbol.hpp:126
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:113
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255