PDI 1.13.0

the PDI data interface

plugin.h
1/*******************************************************************************
2 * Copyright (C) 2015-2026 Commissariat a l'energie atomique et aux energies alternatives (CEA)
3 * Copyright (C) 2026 Julien Bigot <julien@julien-bigot.fr>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of CEA nor the names of its contributors may be used to
14 * endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 ******************************************************************************/
25
26#ifndef PDI_PLUGIN_H_
27#define PDI_PLUGIN_H_
28
29#include <cstdint>
30#include <memory>
31#include <string>
32#include <unordered_set>
33#include <utility>
34
35#include <pdi/pdi_fwd.h>
36#include <pdi/logger.h>
37#include <pdi/version.h>
38
39namespace PDI {
40
41
44class PDI_EXPORT Plugin
45{
46 Context& m_context;
47
48 Logger& m_logger;
49
50public:
51 Plugin(const Plugin&) = delete;
52
53 Plugin(Plugin&&) = delete;
54
60
61 virtual ~Plugin() noexcept(false);
62
63protected:
67
71
72}; // class Plugin
73
78#define PDI_PLUGIN_API_VERSION_MAJOR 0
79
81#define PDI_PLUGIN_API_VERSION_MINOR (PDI_VERSION_MAJOR * 128 + PDI_VERSION_MINOR)
82
84#define PDI_PLUGIN_API_VERSION_PATCH PDI_VERSION_PATCH
85
91#define PDI_COMPUTE_PLUGIN_API_VERSION(MAJOR, MINOR, PATCH) \
92 (((UINT64_C(0) + (MAJOR)) << 48) | ((UINT64_C(0) + (MINOR)) << 32) | ((UINT64_C(0) + (PATCH)) << 16))
93
95#define PDI_PLUGIN_API_VERSION \
96 PDI_COMPUTE_PLUGIN_API_VERSION(PDI_PLUGIN_API_VERSION_MAJOR, PDI_PLUGIN_API_VERSION_MINOR, PDI_PLUGIN_API_VERSION_PATCH)
97
99#define PLUGIN_API_VERSION_MAJOR PDI_PLUGIN_API_VERSION_MAJOR
100
102#define PLUGIN_API_VERSION_MINOR PDI_PLUGIN_API_VERSION_MINOR
103
105#define PLUGIN_API_VERSION_PATCH PDI_PLUGIN_API_VERSION_PATCH
106
108#define PLUGIN_API_VERSION PDI_PLUGIN_API_VERSION
109
110// GCC has no way to deprecate a macro that works in `#if` too, Clang does
111#if defined(__clang__) && __clang_major__ >= 14
112#pragma clang deprecated(PLUGIN_API_VERSION_MAJOR, "use PDI_PLUGIN_API_VERSION_MAJOR instead")
113#pragma clang deprecated(PLUGIN_API_VERSION_MINOR, "use PDI_PLUGIN_API_VERSION_MINOR instead")
114#pragma clang deprecated(PLUGIN_API_VERSION_PATCH, "use PDI_PLUGIN_API_VERSION_PATCH instead")
115#pragma clang deprecated(PLUGIN_API_VERSION, "use PDI_PLUGIN_API_VERSION instead")
116#endif
117
129uint64_t PDI_EXPORT plugin_api_version(uint64_t expected_version = 0);
130
131} // namespace PDI
132
133#if defined(__clang__)
139#define PDI_PLUGIN_IGNORE_WARNINGS_BEGIN \
140 _Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"") \
141 _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"")
142
144#define PDI_PLUGIN_IGNORE_WARNINGS_END _Pragma("clang diagnostic pop")
145#else
146#define PDI_PLUGIN_IGNORE_WARNINGS_BEGIN
147#define PDI_PLUGIN_IGNORE_WARNINGS_END
148#endif
149
167#define PDI_PLUGIN(name) \
168 PDI_PLUGIN_IGNORE_WARNINGS_BEGIN extern "C" ::std::unique_ptr<::PDI::Plugin> PDI_EXPORT PDI_plugin_##name##_loader( \
169 ::PDI::Logger& logger, \
170 ::PDI::Context& ctx, \
171 PC_tree_t conf \
172 ) \
173 { \
174 auto plugin = ::std::unique_ptr<name##_plugin>{new name##_plugin{logger, ctx, conf}}; \
175 ::PDI::plugin_api_version(PDI_PLUGIN_API_VERSION); \
176 return plugin; \
177 } \
178 extern "C" ::std::pair<::std::unordered_set<::std::string>, ::std::unordered_set<::std::string>> PDI_EXPORT PDI_plugin_##name##_dependencies() \
179 { \
180 return []<class P>() { \
181 if constexpr (requires { P::dependencies(); }) { \
182 return P::dependencies(); \
183 } else { \
184 return ::std::pair<::std::unordered_set<::std::string>, ::std::unordered_set<::std::string>>{}; \
185 } \
186 }.template operator()<name##_plugin>(); \
187 } \
188 extern "C" ::std::string PDI_EXPORT PDI_plugin_##name##_pretty_name() \
189 { \
190 return []<class P>(const char* fallback) -> ::std::string { \
191 if constexpr (requires { P::pretty_name(); }) { \
192 return P::pretty_name(); \
193 } else { \
194 return fallback; \
195 } \
196 }.template operator()<name##_plugin>(#name); \
197 } \
198 PDI_PLUGIN_IGNORE_WARNINGS_END
199
200#endif // PDI_PLUGIN_H_
Definition context.h:43
Wrapper for spdlog::logger with additional pattern getter method.
Definition logger.h:44
virtual ~Plugin() noexcept(false)
Plugin(Logger &logger, Context &ctx)
Initialization of the plugin.
Context & context()
Provides access to the PDI context for this plugin instance.
Plugin(Plugin &&)=delete
Logger & logger()
Provides access to the logger for this plugin instance.
Plugin(const Plugin &)=delete
Definition array_datatype.h:38
uint64_t plugin_api_version(uint64_t expected_version=0)
Checks compatibility with a plugin API.