VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
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-global singleton manager for the @c SchemaPluginInterface dynamic plugin.
27 */
28
29#pragma once
30
31#include <memory>
32#include <string>
33
35
36namespace vlink {
37
38/**
39 * @class SchemaPluginManager
40 * @brief Singleton manager that owns and provides access to the @c SchemaPluginInterface.
41 *
42 * @details
43 * @c SchemaPluginManager is a process-level singleton that loads and holds a single
44 * @c SchemaPluginInterface plugin. The plugin path is resolved in the following order:
45 * 1. The @p schema_plugin_path argument passed to @c get() on first call.
46 * 2. The @c VLINK_SCHEMA_PLUGIN environment variable if @p schema_plugin_path is empty.
47 * 3. No plugin loaded (the manager is invalid) if neither is set.
48 *
49 * @par Usage
50 * @code
51 * // Load by environment variable VLINK_SCHEMA_PLUGIN:
52 * auto& mgr = vlink::SchemaPluginManager::get();
53 *
54 * // Or explicitly (first call wins):
55 * auto& mgr = vlink::SchemaPluginManager::get("/path/to/my_schema_plugin.so");
56 *
57 * if (mgr.is_valid()) {
58 * auto iface = mgr.get_interface();
59 * auto schema = iface->search_schema("my_pkg.MyMessage", SchemaType::kProtobuf);
60 * }
61 * @endcode
62 *
63 * @note
64 * - @c get() creates the singleton on first call; subsequent calls ignore @p schema_plugin_path.
65 * - @c is_valid() returns @c false when no plugin was loaded.
66 * - The plugin interface is released before the @c Plugin loader on destruction, ensuring
67 * safe unloading.
68 */
69class VLINK_EXPORT SchemaPluginManager final {
70 public:
71 /**
72 * @brief Returns the process-global @c SchemaPluginManager singleton.
73 *
74 * @details
75 * On the first call, loads the plugin from @p schema_plugin_path if non-empty,
76 * or from the @c VLINK_SCHEMA_PLUGIN environment variable. Subsequent calls
77 * return the same singleton regardless of @p schema_plugin_path.
78 *
79 * @param schema_plugin_path Path to the plugin shared library. Empty = use env var.
80 * @return Reference to the singleton.
81 */
82 [[nodiscard]] static SchemaPluginManager& get(const std::string& schema_plugin_path = "");
83
84 /**
85 * @brief Returns @c true if a @c SchemaPluginInterface was successfully loaded.
86 *
87 * @return @c true if @c get_interface() will return a non-null pointer.
88 */
89 [[nodiscard]] bool is_valid() const;
90
91 /**
92 * @brief Returns the loaded @c SchemaPluginInterface instance.
93 *
94 * @return Shared pointer to the interface, or @c nullptr if not loaded.
95 */
96 [[nodiscard]] std::shared_ptr<SchemaPluginInterface> get_interface() const;
97
98 private:
99 explicit SchemaPluginManager(std::string schema_plugin_path);
100
101 ~SchemaPluginManager();
102
103 std::unique_ptr<struct SchemaPluginManagerImpl> impl_;
104
105 VLINK_DISALLOW_COPY_AND_ASSIGN(SchemaPluginManager)
106};
107} // namespace vlink
#define VLINK_EXPORT
Definition macros.h:85
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184
Runtime schema plugin interface for Protobuf and FlatBuffers metadata loading.