VLink  2.0.0
A high-performance communication middleware
protobuf_registry.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 protobuf_registry.h
26  * @brief Header probe that enables Protobuf-backed schema-plugin code when available.
27  *
28  * @details
29  * Unlike FlatBuffers, Protobuf already provides a process-wide registry of generated
30  * descriptors through @c google::protobuf::DescriptorPool::generated_pool(), so VLink
31  * does not need its own table for Protobuf schemas. This header exists purely to:
32  *
33  * - Centralise the protobuf reflection includes used by the schema plugin.
34  * - Probe for protobuf availability and expose a feature macro to the rest of the
35  * schema-plugin code.
36  *
37  * Registration API surface:
38  *
39  * | Source | API used | Notes |
40  * | -------------- | --------------------------------------------------- | -------------------------------- |
41  * | Generated code | @c DescriptorPool::generated_pool() | Populated by Protobuf at startup |
42  * | Schema plugin | @c VLINK_HAS_SCHEMA_PLUGIN_PROTOBUF (defined below) | Enables protobuf code paths |
43  *
44  * @par Example
45  * @code
46  * #include <vlink/extension/protobuf_registry.h>
47  *
48  * #ifdef VLINK_HAS_SCHEMA_PLUGIN_PROTOBUF
49  * auto* pool = google::protobuf::DescriptorPool::generated_pool();
50  * if (auto* desc = pool->FindMessageTypeByName("demo.proto.PointCloud")) {
51  * // ... reflectively walk the message ...
52  * (void)desc;
53  * }
54  * #endif
55  * @endcode
56  */
57 
58 #pragma once
59 
60 /**
61  * @brief Defined when the Protobuf reflection headers are visible in the build.
62  *
63  * @details
64  * The probe relies on @c __has_include to detect the protobuf installation. When the
65  * macro is defined, dependent schema-plugin code may safely use protobuf descriptors,
66  * reflection and dynamic messages.
67  */
68 #if __has_include(<google/protobuf/dynamic_message.h>)
69 #include <google/protobuf/descriptor.h>
70 #include <google/protobuf/descriptor.pb.h>
71 #include <google/protobuf/dynamic_message.h>
72 #include <google/protobuf/message.h>
73 #define VLINK_HAS_SCHEMA_PLUGIN_PROTOBUF
74 #endif