PDI 1.7.0-alpha.2024-06-17

the PDI data interface

plugin.h
1/*******************************************************************************
2 * Copyright (C) 2015-2019 Commissariat a l'energie atomique et aux energies alternatives (CEA)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of CEA nor the names of its contributors may be used to
13 * endorse or promote products derived from this software without specific
14 * prior written permission.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 ******************************************************************************/
24
25#ifndef PDI_PLUGIN_H_
26#define PDI_PLUGIN_H_
27
28#include <memory>
29#include <string>
30#include <type_traits>
31#include <unordered_set>
32#include <utility>
33
34#include <pdi/pdi_fwd.h>
35#include <pdi/logger.h>
36
37namespace PDI {
38
39
42class PDI_EXPORT Plugin
43{
44 Context& m_context;
45
46public:
47 Plugin(const Plugin&) = delete;
48
49 Plugin(Plugin&&) = delete;
50
55
56 virtual ~Plugin() noexcept(false);
57
60 Context& context();
61
62}; // class Plugin
63
64#define PLUGIN_API_VERSION_MAJOR (0ul)
65
66#define PLUGIN_API_VERSION_MINOR (0ul)
67
68#define PLUGIN_API_VERSION_PATCH (1ul)
69
70#define PLUGIN_API_VERSION ((PLUGIN_API_VERSION_MAJOR << 24) + (PLUGIN_API_VERSION_MINOR << 16) + (PLUGIN_API_VERSION_PATCH << 8))
71
79unsigned long PDI_EXPORT plugin_api_version(unsigned long expected_version = 0);
80
81} // namespace PDI
82
83namespace {
84
87template <class T>
88struct has_dependencies {
89 template <typename C>
90 static constexpr decltype(C::dependencies(), bool()) test(int)
91 {
92 return true;
93 }
94
95 template <typename C>
96 static constexpr bool test(...)
97 {
98 return false;
99 }
100
101 static constexpr bool value = test<T>(int());
102};
103
109template <class T>
110typename std::enable_if<has_dependencies<T>::value, std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>>::type
111plugin_dependencies()
112{
113 return T::dependencies();
114}
115
121template <class T>
122typename std::enable_if<!has_dependencies<T>::value, std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>>::type
123plugin_dependencies()
124{
125 return {};
126}
127
130template <class T>
131struct has_pretty_name {
132 template <typename C>
133 static constexpr decltype(C::pretty_name(), bool()) test(int)
134 {
135 return true;
136 }
137
138 template <typename C>
139 static constexpr bool test(...)
140 {
141 return false;
142 }
143
144 static constexpr bool value = test<T>(int());
145};
146
152template <class T>
153typename std::enable_if<has_pretty_name<T>::value, std::string>::type plugin_pretty_name(const std::string& plugin_name)
154{
155 return T::pretty_name();
156}
157
163template <class T>
164typename std::enable_if<!has_pretty_name<T>::value, std::string>::type plugin_pretty_name(const std::string& plugin_name)
165{
166 return plugin_name;
167}
168
169} // namespace
170
188#define PDI_PLUGIN(name) \
189 _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"") \
190 _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"" \
191 ) extern "C" ::std::unique_ptr<::PDI::Plugin> PDI_EXPORT PDI_plugin_##name##_loader(::PDI::Context& ctx, PC_tree_t conf) \
192 { \
193 auto plugin = ::std::unique_ptr<name##_plugin>{new name##_plugin{ctx, conf}}; \
194 ::PDI::plugin_api_version(PLUGIN_API_VERSION); \
195 return plugin; \
196 } \
197 extern "C" ::std::pair<::std::unordered_set<::std::string>, ::std::unordered_set<::std::string>> PDI_EXPORT PDI_plugin_##name##_dependencies() \
198 { \
199 return ::plugin_dependencies<name##_plugin>(); \
200 } \
201 extern "C" ::std::string PDI_EXPORT PDI_plugin_##name##_pretty_name() \
202 { \
203 return ::plugin_pretty_name<name##_plugin>(#name); \
204 } \
205 _Pragma("clang diagnostic pop")
206
207#endif // PDI_PLUGIN_H_
Definition: context.h:44
The class PDI plugins should implement.
Definition: plugin.h:43
virtual ~Plugin() noexcept(false)
Plugin(Context &ctx)
Initialization of the plugin.
Plugin(Plugin &&)=delete
Plugin(const Plugin &)=delete
Definition: array_datatype.h:38
unsigned long plugin_api_version(unsigned long expected_version=0)
Checks compatibility with a plugin API.