VLink  2.0.0
A high-performance communication middleware
schema_plugin_interface.h
浏览该文件的文档.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file schema_plugin_interface.h
26  * @brief Abstract contract for runtime Protobuf / FlatBuffers schema discovery plugins.
27  *
28  * @details
29  * @c SchemaPluginInterface is the plugin-side contract shared between VLink and any out-of-tree
30  * shared library that supplies serialisation metadata for runtime introspection. Plugins are
31  * loaded by @c SchemaPluginManager through the generic @c Plugin loader and surface a uniform
32  * lookup API regardless of whether the underlying metadata lives in a linked Protobuf descriptor
33  * pool, in @c flatc-generated BFBS blobs, or in some bespoke registry.
34  *
35  * | Capability | Method | Returns when missing |
36  * | ------------------------- | ----------------------------------- | ----------------------- |
37  * | Version probe | @c get_version_info() | populated info |
38  * | Generic schema lookup | @c search_schema() | name-only @c SchemaData |
39  * | Bulk enumeration | @c get_all_schemas() | empty vector |
40  * | Protobuf descriptor | @c search_protobuf_descriptor() | @c nullptr |
41  * | Protobuf dynamic message | @c create_protobuf_message() | @c nullptr |
42  * | FlatBuffers BFBS schema | @c search_flatbuffers_schema() | @c nullptr |
43  * | FlatBuffers parser | @c create_flatbuffers_parser() | @c nullptr |
44  *
45  * @par Plugin lifecycle
46  * @code
47  * load .so --> factory ctor --> search/get_all --> create_message/parser --> dtor
48  * | ^
49  * v |
50  * caches built lazily under an internal mutex of the implementation ----+
51  * @endcode
52  *
53  * Concrete plugins normally derive from @c SchemaPluginBase, which already implements every
54  * method against a linked Protobuf descriptor pool plus the process-local FlatBuffers registry.
55  */
56 
57 #pragma once
58 
59 #include <string>
60 #include <vector>
61 
62 #include "../base/plugin.h"
63 #include "../impl/types.h"
64 
65 namespace vlink {
66 
67 /**
68  * @class SchemaPluginInterface
69  * @brief Polymorphic contract for runtime schema lookup and dynamic message construction.
70  *
71  * @details
72  * Instances are obtained through @c Plugin::load<SchemaPluginInterface>() or, more commonly,
73  * via the @c SchemaPluginManager singleton. Opaque @c void* aliases keep the contract free
74  * of Protobuf and FlatBuffers headers so that downstream binaries that do not link those
75  * libraries can still consume the interface; callers that statically link the matching
76  * library are expected to @c reinterpret_cast back to the concrete pointer type.
77  */
80 
81  protected:
82  SchemaPluginInterface() = default;
83 
84  virtual ~SchemaPluginInterface() = default;
85 
86  public:
87  /**
88  * @brief Opaque alias for a @c google::protobuf::Descriptor pointer.
89  *
90  * @details
91  * Callers that statically link Protobuf may cast this back to
92  * @c google::protobuf::Descriptor* and use the full reflection API.
93  */
94  using ProtobufDescriptorPtr = void*;
95 
96  /**
97  * @brief Opaque alias for a @c google::protobuf::Message pointer.
98  *
99  * @details
100  * The lifetime of the underlying message is owned by the plugin; callers must
101  * not @c delete the returned pointer.
102  */
103  using ProtobufMessagePtr = void*;
104 
105  /**
106  * @brief Opaque alias for a @c reflection::Schema pointer (FlatBuffers BFBS).
107  */
108  using FlatbuffersSchemaPtr = void*;
109 
110  /**
111  * @brief Opaque alias for a @c flatbuffers::Parser pointer pre-loaded with a root type.
112  */
113  using FlatbuffersParserPtr = void*;
114 
115  /**
116  * @struct VersionInfo
117  * @brief Build provenance reported by a concrete plugin implementation.
118  *
119  * @details
120  * Provides enough metadata for diagnostic logging and version pinning when a process
121  * needs to assert that loaded plugins match the running binary.
122  */
123  struct VersionInfo final {
124  std::string name; ///< Human-readable plugin identifier.
125  std::string version; ///< Semantic version string such as @c "2.0.0".
126  std::string timestamp; ///< ISO-8601 build timestamp captured at compile time.
127  std::string tag; ///< Optional source-control tag tied to the binary.
128  std::string commit_id; ///< Optional source-control commit hash.
129  };
130 
131  /**
132  * @brief Reports the plugin's version and build identity.
133  *
134  * @return Populated @c VersionInfo describing the loaded plugin binary.
135  */
136  [[nodiscard]] virtual VersionInfo get_version_info() const = 0;
137 
138  /**
139  * @brief Resolves a single schema record by name, optionally restricted to one family.
140  *
141  * @details
142  * When @p schema_type is @c SchemaType::kUnknown the plugin probes every supported family
143  * and returns the unique match; ambiguous names yield an empty record. Supplying a concrete
144  * @c SchemaType selects the matching backend directly and skips the probing.
145  *
146  * @param name Serialisation type name or fully qualified message name.
147  * @param schema_type Family hint, or @c SchemaType::kUnknown for family-agnostic lookup.
148  * @return Matching @c SchemaData on success, or a name-only record on miss.
149  */
150  [[nodiscard]] virtual SchemaData search_schema(const std::string& name,
151  SchemaType schema_type = SchemaType::kUnknown) = 0;
152 
153  /**
154  * @brief Enumerates every schema known to the plugin, optionally filtered by family.
155  *
156  * @param schema_type Family filter, or @c SchemaType::kUnknown to return all families.
157  * @return Snapshot vector of @c SchemaData entries owned by the caller.
158  */
159  [[nodiscard]] virtual std::vector<SchemaData> get_all_schemas(SchemaType schema_type = SchemaType::kUnknown) = 0;
160 
161  /**
162  * @brief Returns the Protobuf descriptor for a fully qualified message name.
163  *
164  * @param name Fully qualified Protobuf message name (e.g. @c "demo.proto.PointCloud").
165  * @return Opaque @c Descriptor pointer, or @c nullptr when the name is unknown.
166  */
167  [[nodiscard]] virtual ProtobufDescriptorPtr search_protobuf_descriptor(const std::string& name) = 0;
168 
169  /**
170  * @brief Returns a cached dynamic @c Message instance for the given Protobuf type.
171  *
172  * @details
173  * The instance is owned by the plugin; callers may copy from it but must not delete it.
174  *
175  * @param name Fully qualified Protobuf message name.
176  * @return Opaque @c Message pointer, or @c nullptr when the name is unknown.
177  */
178  [[nodiscard]] virtual ProtobufMessagePtr create_protobuf_message(const std::string& name) = 0;
179 
180  /**
181  * @brief Returns a verified BFBS reflection schema handle for a FlatBuffers root type.
182  *
183  * @param name Fully qualified FlatBuffers root type name.
184  * @return Opaque @c reflection::Schema pointer, or @c nullptr when the name is unknown.
185  */
186  [[nodiscard]] virtual FlatbuffersSchemaPtr search_flatbuffers_schema(const std::string& name) = 0;
187 
188  /**
189  * @brief Returns a freshly built @c flatbuffers::Parser pre-loaded with the requested root.
190  *
191  * @details
192  * Each successful call returns a distinct parser instance whose lifetime is owned by
193  * the plugin. This avoids cross-thread parser reuse, which is unsafe.
194  *
195  * @param name Fully qualified FlatBuffers root type name.
196  * @return Opaque parser pointer, or @c nullptr when the name is unknown.
197  */
198  [[nodiscard]] virtual FlatbuffersParserPtr create_flatbuffers_parser(const std::string& name) = 0;
199 
200  private:
202 };
203 
204 } // namespace vlink
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
#define VLINK_PLUGIN_REGISTER(InterfaceType)
Declares a plugin's identity from the demangled name of its abstract interface.
Definition: plugin.h:345