LUMIERA.clone/src/lib/lifecycleregistry.hpp
Ichthyostega 8e250839f4 Application: Adjustments to the Layer structure
During the last months, a lot of gradual changes to policies and guidelines
had been finalised and documented. Moreover, recent clarifications regarding
memory management in the render engine will lead to a clearer definition
of the »Vault« Layer's role, with some ''engine code'' expected to move down.

On this occasion, I reworked relevant sections in the documentation to
confirm the role that now falls to central parts of the overall architecture.
Notably I decided to rename those parts related to the application backbone
as »Vessel« — to underpin the more active and defining role they assumed.
2026-03-03 22:16:09 +01:00

102 lines
2.7 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
LIFECYCLEREGISTRY.hpp - registry for application lifecycle callbacks
Copyright (C)
2008, Christian Thaeter <ct@pipapo.org>
2008, Hermann Vosseler <Ichthyostega@web.de>
  **Lumiera** is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version. See the file COPYING for further details.
*/
/** @file lifecycleregistry.hpp
** Helper for registering lifecycle event callbacks, which are
** provided as an application service within the »vessel«. This service
** allows to enrol functions under a given label and then to call
** all those registered functions.
** @note this is in fact an event mechanism, and if we start using
** more than just this basic functionality, we should switch to
** boost::signals. (which has the downside of being an binary
** dependency).
**
** @see voyage.hpp
*/
#ifndef VESSEL_LIFECYCLE_REGISTRY_H
#define VESSEL_LIFECYCLE_REGISTRY_H
#include "lib/util.hpp"
#include "lib/nocopy.hpp"
#include <functional>
#include <string>
#include <set>
#include <map>
namespace vessel {
using util::contains;
using std::function;
using std::string;
/**
* Registry of callback functions accessible by a label (ID)
* provided at registration. Registered functions will be added
* to a list, which can be triggered via label. Used by vessel::Voyage
* to implement the lumiera lifecycle (init, shutdown) hooks.
*/
class LifecycleRegistry
: util::NonCopyable
{
public:
typedef void (*Hook)(void);
typedef std::set<Hook> Callbacks;
typedef Callbacks::iterator Iter;
/** @note only one copy of each distinct callback remembered */
bool enrol (Symbol label, Hook toCall)
{
return table_[label]
.insert(toCall)
.second; // true if actually stored
}
void execute (Symbol label)
{
Callbacks& cbs (table_[label]);
Iter e = cbs.end();
for (Iter p = cbs.begin();
p != e; ++p)
(*p)(); // invoke callback
}
/** get the (single) LifecycleRegistry instance.
* @warning don't use it after the end of main()! */
static LifecycleRegistry& instance();
private:
std::map<Symbol, Callbacks> table_;
LifecycleRegistry () {
execute (ON_BASIC_INIT); // just to be sure, typically a NOP, because nothing is registered yet
}
~LifecycleRegistry () { }
};
} // namespace vessel
#endif /*VESSEL_LIFECYCLE_REGISTRY_H*/