VLink  2.0.0
A high-performance communication middleware
schema_plugin_manager.h
Go to the documentation of this file.
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_manager.h
26  * @brief Process-wide singleton wrapper that loads and owns a @c SchemaPluginInterface implementation.
27  *
28  * @details
29  * @c SchemaPluginManager hides the dynamic-loader plumbing required to surface a single shared
30  * @c SchemaPluginInterface to every component of the running process. It is the recommended
31  * entry point for CLI tools (@c eproto, @c efbs), webviz converters, and bag writers that need
32  * to resolve Protobuf and FlatBuffers metadata without each subsystem rolling its own loader.
33  *
34  * @par Manager state machine
35  * @code
36  * +-------------+ first get(path) +-----------+ library missing +---------+
37  * | not built | ------------------------> | resolving | ------------------> | invalid |
38  * +-------------+ +-----------+ +---------+
39  * | load ok
40  * v
41  * +---------+
42  * | valid | <-- get_interface() returns plugin
43  * +---------+
44  * |
45  * v process exit
46  * +---------+
47  * | unloaded | interface released before loader
48  * +---------+
49  * @endcode
50  *
51  * @par Resolution order
52  * 1. The @p schema_plugin_path argument supplied to the very first @c get() call.
53  * 2. The @c VLINK_SCHEMA_PLUGIN environment variable when the argument is empty.
54  * 3. No plugin loaded (the manager reports @c is_valid() == @c false).
55  *
56  * @par Example
57  * @code
58  * auto& mgr = vlink::SchemaPluginManager::get("/opt/vlink/libschema_plugin.so");
59  *
60  * if (mgr.is_valid()) {
61  * auto plugin = mgr.get_interface();
62  * auto schema = plugin->search_schema("demo.proto.PointCloud", vlink::SchemaType::kProtobuf);
63  * VLOG_I("loaded schema: ", schema.name);
64  * }
65  * @endcode
66  */
67 
68 #pragma once
69 
70 #include <memory>
71 #include <string>
72 
74 
75 namespace vlink {
76 
77 /**
78  * @class SchemaPluginManager
79  * @brief Singleton accessor that owns a lazily loaded @c SchemaPluginInterface plugin.
80  *
81  * @details
82  * Subsequent @c get() invocations are cheap and return the cached singleton regardless of
83  * the argument passed; the very first call wins. The destructor releases the contained
84  * interface before tearing down the @c Plugin loader, ensuring the shared object outlives
85  * any dependent global objects inside it.
86  */
88  public:
89  /**
90  * @brief Returns the process-wide manager, building it on the first call.
91  *
92  * @param schema_plugin_path Absolute path to the plugin shared object. Empty means
93  * fall back to the @c VLINK_SCHEMA_PLUGIN environment variable.
94  * @return Reference to the singleton instance.
95  */
96  [[nodiscard]] static SchemaPluginManager& get(const std::string& schema_plugin_path = "");
97 
98  /**
99  * @brief Reports whether a plugin was successfully loaded.
100  *
101  * @return @c true when @c get_interface() will yield a non-null pointer.
102  */
103  [[nodiscard]] bool is_valid() const;
104 
105  /**
106  * @brief Returns the shared plugin instance, or @c nullptr when the manager is invalid.
107  *
108  * @return Shared pointer to the loaded @c SchemaPluginInterface implementation.
109  */
110  [[nodiscard]] std::shared_ptr<SchemaPluginInterface> get_interface() const;
111 
112  private:
113  explicit SchemaPluginManager(std::string schema_plugin_path);
114 
116 
117  struct Impl;
118  std::unique_ptr<Impl> impl_;
119 
121 };
122 } // namespace vlink
#define VLINK_EXPORT
Definition: macros.h:81
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Abstract contract for runtime Protobuf / FlatBuffers schema discovery plugins.