PDI 1.5.2

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
65#define PLUGIN_API_VERSION_MAJOR (0ul)
66
67#define PLUGIN_API_VERSION_MINOR (0ul)
68
69#define PLUGIN_API_VERSION_PATCH (1ul)
70
71#define PLUGIN_API_VERSION ((PLUGIN_API_VERSION_MAJOR<<24) + (PLUGIN_API_VERSION_MINOR<<16) + (PLUGIN_API_VERSION_PATCH<<8))
72
80unsigned long PDI_EXPORT plugin_api_version(unsigned long expected_version=0);
81
82} // namespace PDI
83
84namespace {
85
88template <class T>
89struct has_dependencies {
90 template <typename C>
91 static constexpr decltype(C::dependencies(), bool()) test(int)
92 {
93 return true;
94 }
95 template <typename C>
96 static constexpr bool test(...)
97 {
98 return false;
99 }
100 static constexpr bool value = test<T>(int());
101};
102
108template <class T>
109typename std::enable_if<has_dependencies<T>::value, std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>>::type plugin_dependencies()
110{
111 return T::dependencies();
112}
113
119template <class T>
120typename std::enable_if<!has_dependencies<T>::value, std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>>::type plugin_dependencies()
121{
122 return {};
123}
124
127template <class T>
128struct has_pretty_name {
129 template <typename C>
130 static constexpr decltype(C::pretty_name(), bool()) test(int)
131 {
132 return true;
133 }
134 template <typename C>
135 static constexpr bool test(...)
136 {
137 return false;
138 }
139 static constexpr bool value = test<T>(int());
140};
141
147template <class T>
148typename std::enable_if<has_pretty_name<T>::value, std::string>::type plugin_pretty_name(const std::string& plugin_name)
149{
150 return T::pretty_name();
151}
152
158template <class T>
159typename std::enable_if<!has_pretty_name<T>::value, std::string>::type plugin_pretty_name(const std::string& plugin_name)
160{
161 return plugin_name;
162}
163
164} // namespace <anonymous>
165
183#define PDI_PLUGIN(name)\
184 _Pragma("clang diagnostic push")\
185 _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"")\
186 _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"")\
187 extern "C" ::std::unique_ptr<::PDI::Plugin> PDI_EXPORT PDI_plugin_##name##_loader(::PDI::Context& ctx, PC_tree_t conf) \
188 {\
189 auto plugin = ::std::unique_ptr<name##_plugin>{new name##_plugin{ctx, conf}};\
190 ::PDI::plugin_api_version(PLUGIN_API_VERSION);\
191 return plugin;\
192 }\
193 extern "C" ::std::pair<::std::unordered_set<::std::string>, ::std::unordered_set<::std::string>> PDI_EXPORT PDI_plugin_##name##_dependencies() \
194 {\
195 return ::plugin_dependencies<name##_plugin>();\
196 }\
197 extern "C" ::std::string PDI_EXPORT PDI_plugin_##name##_pretty_name() \
198 {\
199 return ::plugin_pretty_name<name##_plugin>(#name);\
200 }\
201 _Pragma("clang diagnostic pop")
202
203#endif // PDI_PLUGIN_H_
Definition: context.h:45
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.