File: SimulatorFactory.h

package info (click to toggle)
camitk 4.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 427,532 kB
  • sloc: cpp: 79,494; xml: 1,176; sh: 1,137; ansic: 142; makefile: 107; perl: 84; sed: 20
file content (153 lines) | stat: -rw-r--r-- 5,379 bytes parent folder | download | duplicates (2)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2018 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/


#ifndef SIMULATOR_SIMULATORFACTORY_H
#define SIMULATOR_SIMULATORFACTORY_H
// Simulator includes
#include "Simulator.h"

#include "MMLAPI.h"
/**
 *
 * @ingroup group_cepmodeling_libraries_mml
 *
 * @brief
 * A factory to create Simulator
 */
class MML_API SimulatorFactory {
public:

    /// return the unique instance of the factory
    static SimulatorFactory* getInstance();

    /**
    * Register a class into the map
    * A registered class can be created using createMonitorDisplay()
    *
    * @param C a subclass of Simulator
    * @param id unique id to associate with the Class C
    */
    template<typename C>
    bool registerClass(std::string id, bool isInteractive) {
        if (mapObjectCreator.find(id) != mapObjectCreator.end()) {
            return false;
        }
        else {
            mapObjectCreator.insert(std::pair<std::string, CreateSimulatorFunctionPointer>(id, &createTheSimulator<C>));
            mapObjectCreator2.insert(std::pair<std::string, CreateSimulatorFunctionPointer2>(id, &createTheSimulator2<C>));
            mapInteractive.insert(std::pair<std::string, bool>(id, isInteractive));
            if (isInteractive) {
                interactiveSimulators.push_back(id);
            }
            else {
                nonInteractiveSimulators.push_back(id);
            }
            return true;
        }
    }

    /// Returns true if id is in the map
    bool isRegistered(std::string id);

    ///Creates a Simulator based on its string id or return null if there is no id in the map
    Simulator* createSimulator(std::string id, MonitoringManager* monitoringManager);

    ///Creates a Simulator using a specific simulator file based on its string id or return null if there is no id in the map
    Simulator* createSimulator(std::string id, MonitoringManager* monitoringManager, const char* file);

    /// give the number of interactive simulators registered
    int getNumberOfInteractiveSimulators();

    /// give the number of interactive simulators registered
    int getNumberOfNonInteractiveSimulators();

    /// get a interactive simulator name by its index
    std::string getInteractiveSimulator(const unsigned int index);

    /// get a non interactive simulator name by its index
    std::string getNonInteractiveSimulator(const unsigned int index);

    /// return true if the registered simulator id is interactive
    bool isInteractive(std::string id);

private:

    SimulatorFactory() = default;

    typedef Simulator* (*CreateSimulatorFunctionPointer)(MonitoringManager* monitoringManager);

    typedef Simulator* (*CreateSimulatorFunctionPointer2)(MonitoringManager* monitoringManager, const char* file);

    /// A map between Simulator name as string to functions
    std::map<std::string, CreateSimulatorFunctionPointer> mapObjectCreator;

    /// A map between Simulator name as string to functions (for the 2nd constructor that uses a simulator file)
    std::map<std::string, CreateSimulatorFunctionPointer2> mapObjectCreator2;



    /**
     * map the simulator name with the 1 parameter constructor.
     *
    * function whose pointers are inserted into the map
    * @param C  type of Simulator
    * @return an MonitorDisplay which type is C
    */
    template<typename C>
    static Simulator* createTheSimulator(MonitoringManager* monitoringManager) {
        return new C(monitoringManager);
    }

    /**
     * map the simulator name with the 2 parameters constructor (using the given specific simulator file)
     *
    * function whose pointers are inserted into the map
    * @param C  type of Simulator
    * @param file specific simulator file
    * @return an MonitorDisplay which type is C
    *
    */
    template<typename C>
    static Simulator* createTheSimulator2(MonitoringManager* monitoringManager, const char* file) {
        return new C(monitoringManager, file);
    }

    /// map between Simulator name as string to booleen, true if interactive
    std::map<std::string, bool> mapInteractive;

    /// list of interactive simulators
    std::vector<std::string> interactiveSimulators;

    /// list of non interactive simulators
    std::vector<std::string> nonInteractiveSimulators;

    /// unique instance of the factory
    static SimulatorFactory* instance;

};


#endif // SIMULATOR_SIMULATORFACTORY_H