-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
ProcessRegistry.h
131 lines (100 loc) · 4.07 KB
/
ProcessRegistry.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef PhysicsTools_MVAComputer_ProcessRegistry_h
#define PhysicsTools_MVAComputer_ProcessRegistry_h
// -*- C++ -*-
//
// Package: MVAComputer
// Class : ProcessRegistry
//
//
// Author: Christophe Saout <christophe.saout@cern.ch>
// Created: Sat Apr 24 15:18 CEST 2007
//
// Refactoring for gcc 4.7.0 and higher
// by Gena Kukartsev following design and advice from Chris Jones
// January 2013
//
#include <string>
#include "tbb/concurrent_unordered_map.h"
namespace PhysicsTools
{
// forward declaration
template<class Base_t, class CalibBase_t, class Parent_t, class Instance_t, class Calibration_t> class ProcessRegistryImpl;
/** \class ProcessRegistry
*
* \short Generic registry template for polymorphic processor implementations
*
* template parameters are: base class, calibration base class and a
* pointer to a user-definable "parent type".
* Template allows registration by name of a given base type using the factory.
* The variable processors can register themselves with the registry of the
* common base class.
*
************************************************************/
template<class Base_t, class CalibBase_t, class Parent_t>
class ProcessRegistry {
public:
#ifndef __GCCXML__
// template alias to replace the former Registry class
template<class Instance_t, class Calibration_t>
using Registry = ProcessRegistryImpl<Base_t,CalibBase_t,Parent_t,Instance_t,Calibration_t>;
#endif
/** \class Factory
*
* \short Factory helper class to instantiate a processor.
*
* The common base class of a processor can inherit from this
* helper class to provide a create() method to instantiate variable
* processor instances.
*
************************************************************/
class Factory {
public:
static Base_t *create(const char *name,
const CalibBase_t *calib,
Parent_t *parent = 0);
};
protected:
friend class Factory;
/// instantiate registry and registers itself with \a name
ProcessRegistry(const char *name) : name(name)
{ registerProcess(name, this); }
virtual ~ProcessRegistry() { unregisterProcess(name); }
/// create an instance of \a name, given a calibration \a calib and parent \a parent
static Base_t *create(const char *name, const CalibBase_t *calib,
Parent_t *parent);
/// virtual method to implement by respective processor instance classes
virtual Base_t *instance(const char *name, const CalibBase_t *calib,
Parent_t *parent) const = 0;
private:
static void registerProcess(const char *name,
const ProcessRegistry *process);
static void unregisterProcess(const char *name);
typedef tbb::concurrent_unordered_map<std::string, const ProcessRegistry*> RegistryMap;
/// return map of all registered processes, allocate if necessary
static RegistryMap *getRegistry();
const char *name;
}; // class ProcessRegistry
/** \class ProcessRegistryImpl (formerly ProcessRegistry::Registry)
*
* \short template to generate a registry singleton for a type.
*
* Instantiating an instance of this type registers that class
* with the registry of the base type and provides a factory that
* calls the constructor of the instance type.
*
************************************************************/
template<class Base_t, class CalibBase_t, class Parent_t, class Instance_t, class Calibration_t>
class ProcessRegistryImpl : public ProcessRegistry<Base_t, CalibBase_t, Parent_t> {
public:
ProcessRegistryImpl<Base_t, CalibBase_t, Parent_t, Instance_t, Calibration_t>(const char *name) : ProcessRegistry<Base_t, CalibBase_t, Parent_t>(name){}
protected:
Base_t *instance(const char *name, const CalibBase_t *calib,
Parent_t *parent) const
{
return new Instance_t(name,
dynamic_cast<const Calibration_t*>(calib),
parent);
}
}; // class ProcessRegistryImpl
} // namespace PhysicsTools
#endif // PhysicsTools_MVAComputer_ProcessRegistry_h