File: runtime.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (282 lines) | stat: -rw-r--r-- 9,402 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight 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 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#pragma once

#include <sight/core/config.hpp>

#include "core/base.hpp"
#include "core/runtime/detail/extension.hpp"
#include "core/runtime/executable_factory.hpp"
#include "core/runtime/plugin.hpp"

#include <regex>
#include <set>

namespace sight::core::runtime::detail
{

class extension_point;
class module;

/**
 * @brief   Defines the runtime class.
 *
 */
class runtime
{
public:

    /**
     * @name    Type Definitions
     */
    //@{
    /// Defines the module container type.
    using module_container = std::set<std::shared_ptr<core::runtime::module> >;
    /// Defines the extension container type.
    using extension_container = std::set<std::shared_ptr<extension> >;
    /// Defines the extension container type.
    using extension_iterator = extension_container::iterator;
    //@}

    /// Regex used in different places to find the module part in a path
    static const std::regex MATCH_MODULE_PATH;

    /// Retrieves the singleton
    static runtime& get();

    /**
     * @brief   Constructor.
     */
    runtime();
    /**
     * @brief   Destructor : does nothing.
     */
    ~runtime();

    /**
     * @name    Public API implementation
     * @{
     */

    /**
     * @brief       Adds all module found in the given path.
     *
     * @param[in]   _repository  a path that may containing modules
     */
    void add_modules(const std::filesystem::path& _repository);

    /**
     * @brief       Retrieves the module for the specified identifier.
     *
     * @param[in]   _identifier  a string containing a module identifier
     *
     * @return      a shared pointer to the found module or null if none
     */
    [[nodiscard]] SPTR(core::runtime::module) find_module(const std::string& _identifier) const;

    /**
     * @brief   Create an instance of the given executable object type.
     *
     * An attempt is made to retrieve a registered executable factory. If none
     * is found, the creation will fail.
     *
     * @remark      This method will not try to load any module.
     *
     * @param[in]   _type    a string containing an executable type
     *
     * @return      a pointer to the created executable instance
     */
    [[nodiscard]] executable* create_executable_instance(const std::string& _type) const;

    /**
     * @brief       Retrieves the extension instance matching the specified identifier.
     *
     * @param[in]   _identifier  a string containing an extension identifier
     *
     * @return      a shared pointer to the found extension instance or null if none
     */
    [[nodiscard]] std::shared_ptr<core::runtime::extension> find_extension(const std::string& _identifier) const;

    /// Return all modules known by the runtime
    module_container modules() const;

    /// Get the path where libraries, modules and share folder are located.
    [[nodiscard]] std::filesystem::path working_path() const;

    /// Get the path where libraries, modules and share folder are located, for each known location
    [[nodiscard]] std::vector<std::pair<std::filesystem::path,
                                        std::filesystem::path> > get_repositories_path() const;
    //@}

    /**
     * @name    Modules
     *
     * @{
     */

    /**
     * @brief       Adds a new module instance to the runtime system.
     *
     * @remark      The given module state will be altered according to the current configuration rules.
     * @param[in]   _module  a shared pointer to the module instance to add
     */
    void add_module(std::shared_ptr<detail::module> _module);

    /**
     * @brief       Unregister a module instance to the runtime system.
     *
     * @param[in]   _module  a shared pointer to the module instance to unregister
     */
    void unregister_module(std::shared_ptr<detail::module> _module);

    /**
     * @brief       Retrieves the enabled module for the specified identifier.
     *
     * @param[in]   _identifier  a string containing a module identifier
     *
     * @return      a shared pointer to the found module or null if none
     */
    [[nodiscard]] std::shared_ptr<core::runtime::module> find_enabled_module(const std::string& _identifier) const;

    //@}

    /**
     * @name    Executable, Executable Factories & Dynamic Executable Instantiation
     *
     * @{
     */

    /**
     * @brief       Adds a new executable factory instance to the runtime system.
     *
     * @param[in]   _factory a shared pointer to an executable factory
     */
    void add_executable_factory(std::shared_ptr<executable_factory> _factory);

    /**
     * @brief       Unregister a new executable factory instance to the runtime system.
     *
     * @param[in]   _factory a shared pointer to an executable factory
     */
    void unregister_executable_factory(std::shared_ptr<executable_factory> _factory);

    /**
     * @brief       Retrieves the executable factory for the given identifier.
     *
     * @param[in]   _type    a string containing a type identifier
     *
     * @return      a shared pointer to the found executable factory or null of none
     */
    [[nodiscard]] std::shared_ptr<executable_factory> find_executable_factory(const std::string& _type) const;

    /**
     * @brief       Retrieves the plugin instance for the specified module.
     *
     * @param[in]   _module  a shared pointer to a module instance
     *
     * @return      a shared pointer to a plugin instance or null if none
     */
    [[nodiscard]] std::shared_ptr<plugin> get_plugin(std::shared_ptr<module> _module) const;

    //@}

    /**
     * @name    Extensions
     *
     * @{
     */

    /**
     * @brief       Registers a new extension.
     *
     * @param[in]   _extension   a shared pointer to the extension to register
     */
    void add_extension(std::shared_ptr<detail::extension> _extension);

    /**
     * @brief       Unregister a new extension.
     *
     * @param[in]   _extension   a shared pointer to the extension to register
     */
    void unregister_extension(std::shared_ptr<detail::extension> _extension);

    /// Retrieves the extension collection.
    extension_container extensions() const;
    extension_iterator extensions_begin() const;
    extension_iterator extensions_end() const;
    //@}

    /**
     * @name    Extension Points
     */
    //@{
    /**
     * @brief       Registers a new extension point.
     *
     * @param[in]   _point   a pointer to the extension point to register
     */
    void add_extension_point(std::shared_ptr<extension_point> _point);

    /**
     * @brief       Unregister a new extension point.
     *
     * @param[in]   _point   a pointer to the extension point to register
     */
    void unregister_extension_point(std::shared_ptr<extension_point> _point);

    /**
     * @brief       Retrieves the extension point instance matching the specified identifier.
     *
     * @param[in]   _identifier  a string containing an extension point identifier
     *
     * @return      a shared pointer to the found extension point instance or null if none
     */
    [[nodiscard]] std::shared_ptr<extension_point> find_extension_point(const std::string& _identifier) const;
    //@}

private:

    ///< Defines the executable factory container type.
    using executable_factory_container = std::set<std::shared_ptr<executable_factory> >;

    ///< Defines the extension point container type.
    using extension_point_container = std::set<std::shared_ptr<extension_point> >;

    ///< Defines the plugin container type.
    using plugin_container = std::vector<std::shared_ptr<plugin> >;

    static std::shared_ptr<runtime> s_instance; ///< The runtime instance.

    executable_factory_container m_executable_factories; ///< Contains all executable factories.
    extension_container m_extensions;                    ///< Contains all registered extensions.
    extension_point_container m_extension_points;        ///< Contains all registered extension points.
    module_container m_modules;                          ///< Contains all modules.
    plugin_container m_plugins;                          ///< Contains all plugins.

    std::filesystem::path m_working_path; ///< Main path where Modules and share folder are located.

    /// All paths as a pair of <binary path/resource path> containing modules, added from the CLI
    std::vector<std::pair<std::filesystem::path, std::filesystem::path> > m_repositories;
};

} // namespace sight::core::runtime::detail