PDI 0.4.1

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 
29 #ifndef PDI_PLUGIN_H_
30 #define PDI_PLUGIN_H_
31 
32 #include <pdi/pdi_fwd.h>
33 
34 #include <unordered_set>
35 
36 namespace PDI {
37 
38 class PDI_EXPORT Plugin
39 {
40  Context& m_context;
41 
42 public:
43  Plugin(const Plugin&) = delete;
44 
45  Plugin(Plugin&&) = delete;
46 
50  Plugin(Context& ctx);
51 
52  virtual ~Plugin() noexcept(false);
53 
57  virtual void event(const char* event);
58 
62  virtual void empty_desc_access(const char* name);
63 
68  virtual void data(const char* name, Ref ref);
69 
72  Context& context();
73 
74 }; // class Plugin
75 
76 
77 #define PLUGIN_API_VERSION_MAJOR (0ul)
78 
79 #define PLUGIN_API_VERSION_MINOR (0ul)
80 
81 #define PLUGIN_API_VERSION_PATCH (1ul)
82 
83 #define PLUGIN_API_VERSION ((PLUGIN_API_VERSION_MAJOR<<24) + (PLUGIN_API_VERSION_MINOR<<16) + (PLUGIN_API_VERSION_PATCH<<8))
84 
92 unsigned long PDI_EXPORT plugin_api_version(unsigned long expected_version=0);
93 
94 } // namespace PDI
95 
96 namespace {
97 
100 template <class T>
101 struct has_dependencies {
102  template <typename C>
103  static constexpr decltype(C::dependencies(), bool()) test(int)
104  {
105  return true;
106  }
107  template <typename C>
108  static constexpr bool test(...)
109  {
110  return false;
111  }
112  static constexpr bool value = test<T>(int());
113 };
114 
120 template <class T>
121 typename std::enable_if<has_dependencies<T>::value, std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>>::type plugin_dependencies()
122 {
123  return T::dependencies();
124 }
125 
131 template <class T>
132 typename std::enable_if<!has_dependencies<T>::value, std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>>::type plugin_dependencies()
133 {
134  return {};
135 }
136 
137 } // namespace <anonymous>
138 
156 #define PDI_PLUGIN(name)\
157  _Pragma("clang diagnostic push")\
158  _Pragma("clang diagnostic ignored \"-Wmissing-prototypes\"")\
159  _Pragma("clang diagnostic ignored \"-Wreturn-type-c-linkage\"")\
160  extern "C" ::std::unique_ptr<::PDI::Plugin> PDI_EXPORT PDI_plugin_##name##_loader(::PDI::Context& ctx, PC_tree_t conf) \
161  {\
162  auto plugin = ::std::unique_ptr<name##_plugin>{new name##_plugin{ctx, conf}};\
163  ::PDI::plugin_api_version(PLUGIN_API_VERSION);\
164  return plugin;\
165  }\
166  extern "C" ::std::pair<::std::unordered_set<::std::string>, ::std::unordered_set<::std::string>> PDI_EXPORT PDI_plugin_##name##_dependencies() \
167  {\
168  return ::plugin_dependencies<name##_plugin>();\
169  }\
170  _Pragma("clang diagnostic pop")
171 
172 #endif // PDI_PLUGIN_H_
Definition: context.h:44
unsigned long plugin_api_version(unsigned long expected_version=0)
Checks compatibility with a plugin API.
Definition: plugin.h:38
A dynamically typed reference to data with automatic memory management and read/write locking semanti...
Definition: pdi_fwd.h:77
Definition: array_datatype.h:31