File: StaticModule.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (70 lines) | stat: -rw-r--r-- 2,222 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once

#include <functional>
#include "imodule.h"

namespace module
{

namespace internal
{

/**
 * Static container holding the modules registered by
 * the StaticModuleRegistration<T> helper. They will be picked up
 * and acquired by the ModuleRegistry.
 * To support scenarios like the unit test runner (where the
 * modules are repeatedly loaded, initialised and shut down
 * again) the module list is not cleared after registration,
 * such that another startup event will have the full set of
 * modules properly registered.
 */
typedef std::function<RegisterableModulePtr()> ModuleCreationFunc;

class StaticModuleList :
    private std::list<ModuleCreationFunc>
{
public:
    static void Add(const ModuleCreationFunc& creationFunc);

    // Creates all pre-registered modules and submits them to the registry
    static void RegisterModules();

private:
    static StaticModuleList& Instance();
};

}

/**
 * @brief Helper object to register a specific RegisterableModule subclass.
 *
 * This class is intended to be used statically in the .cpp file of a particular module's
 * implementing class, to register the module implementation with the ModuleRegistry. Since
 * immediate registering is not desired, the constructor adds the incoming modules to a static
 * std::list for a later routine to add the modules to the registry.
 *
 * Note that this object does NOT hold any reference or pointer to the actual module instance. It
 * registers the TYPE of module which will later be instantiated and owned by the ModuleRegistry.
 *
 * @tparam ModuleType
 * A RegisterableModule class which is automatically registered with the ModuleRegistry by the
 * StaticModuleRegistration constructor.
 */
template <class ModuleType> class StaticModuleRegistration
{
    static_assert(std::is_base_of<RegisterableModule, ModuleType>::value,
                  "ModuleType must be of type RegisterableModule");

public:
    StaticModuleRegistration()
    {
        // Add a creator to the list in the backend, it will be called by the registry later
        internal::StaticModuleList::Add(
            []() -> RegisterableModulePtr { return std::make_shared<ModuleType>(); }
        );
    }
};

} // namespace module