1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Hechong.xyf
17 * \date Apr 2018
18 * \brief Interface of AiTheta Index Plugin
19 */
20
21#ifndef __AITHETA2_INDEX_PLUGIN_H__
22#define __AITHETA2_INDEX_PLUGIN_H__
23
24#include <string>
25#include <vector>
26
27namespace aitheta2 {
28
29/*! Index Plugin
30 */
31class IndexPlugin {
32 public:
33 //! Constructor
34 IndexPlugin(void) : handle_(nullptr) {}
35
36 //! Constructor
37 IndexPlugin(IndexPlugin &&plugin) : handle_(plugin.handle_) {
38 plugin.handle_ = nullptr;
39 }
40
41 //! Constructor
42 explicit IndexPlugin(const std::string &path) : handle_(nullptr) {
43 this->load(path);
44 }
45
46 //! Destructor
47 ~IndexPlugin(void) {}
48
49 //! Test if the plugin is valid
50 bool is_valid(void) const {
51 return (!!handle_);
52 }
53
54 //! Retrieve the handle
55 void *handle(void) const {
56 return handle_;
57 }
58
59 //! Load the library path
60 bool load(const std::string &path);
61
62 //! Load the library path
63 bool load(const std::string &path, std::string *err);
64
65 //! Unload plugin
66 void unload(void);
67
68 private:
69 //! Disable them
70 IndexPlugin(const IndexPlugin &) = delete;
71 IndexPlugin &operator=(const IndexPlugin &) = delete;
72
73 //! Members
74 void *handle_;
75};
76
77/*! Index Plugin Broker
78 */
79class IndexPluginBroker {
80 public:
81 //! Constructor
82 IndexPluginBroker(void) : plugins_() {}
83
84 //! Constructor
85 IndexPluginBroker(IndexPluginBroker &&broker)
86 : plugins_(std::move(broker.plugins_)) {}
87
88 //! Destructor
89 ~IndexPluginBroker(void) {}
90
91 //! Emplace a plugin
92 bool emplace(IndexPlugin &&plugin);
93
94 //! Emplace a plugin via library path
95 bool emplace(const std::string &path) {
96 return this->emplace(IndexPlugin(path));
97 }
98
99 //! Emplace a plugin via library path
100 bool emplace(const std::string &path, std::string *err) {
101 aitheta2::IndexPlugin plugin;
102 if (!plugin.load(path, err)) {
103 return false;
104 }
105 return this->emplace(std::move(plugin));
106 }
107
108 //! Retrieve count of plugins in broker
109 size_t count(void) const {
110 return plugins_.size();
111 }
112
113 private:
114 //! Disable them
115 IndexPluginBroker(const IndexPluginBroker &) = delete;
116 IndexPluginBroker &operator=(const IndexPluginBroker &) = delete;
117
118 //! Members
119 std::vector<IndexPlugin> plugins_;
120};
121
122} // namespace aitheta2
123
124#endif // __AITHETA2_INDEX_PLUGIN_H__
125