PDI 1.1.0

Data exchange made easy

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 
37 namespace PDI {
38 
39 
42 class PDI_EXPORT Plugin
43 {
44  Context& m_context;
45 
46 public:
47  Plugin(const Plugin&) = delete;
48 
49  Plugin(Plugin&&) = delete;
50 
54  Plugin(Context& ctx);
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 
80 unsigned long PDI_EXPORT plugin_api_version(unsigned long expected_version=0);
81 
82 } // namespace PDI
83 
84 namespace {
85 
88 template <class T>
89 struct 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 
108 template <class T>
109 typename 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 
119 template <class T>
120 typename 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 
125 } // namespace <anonymous>
126 
144 #define PDI_PLUGIN(name)\
145  _Pragma("clang diagnostic push")\
146  _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"")\
147  _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"")\
148  extern "C" ::std::unique_ptr<::PDI::Plugin> PDI_EXPORT PDI_plugin_##name##_loader(::PDI::Context& ctx, PC_tree_t conf) \
149  {\
150  auto plugin = ::std::unique_ptr<name##_plugin>{new name##_plugin{ctx, conf}};\
151  ::PDI::plugin_api_version(PLUGIN_API_VERSION);\
152  return plugin;\
153  }\
154  extern "C" ::std::pair<::std::unordered_set<::std::string>, ::std::unordered_set<::std::string>> PDI_EXPORT PDI_plugin_##name##_dependencies() \
155  {\
156  return ::plugin_dependencies<name##_plugin>();\
157  }\
158  _Pragma("clang diagnostic pop")
159 
160 #endif // PDI_PLUGIN_H_
Definition: context.h:42
unsigned long plugin_api_version(unsigned long expected_version=0)
Checks compatibility with a plugin API.
The class PDI plugins should implement.
Definition: plugin.h:42
Definition: array_datatype.h:37