WPILibC++ 2024.3.2
ProtobufMessageDatabase.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <memory>
8#include <span>
9#include <string>
10#include <string_view>
11#include <vector>
12
13#include <google/protobuf/descriptor.pb.h>
14#include <google/protobuf/descriptor_database.h>
15#include <google/protobuf/dynamic_message.h>
16
17#include "wpi/StringMap.h"
18
19namespace wpi {
20
21/**
22 * Database of protobuf dynamic messages. Unlike the protobuf descriptor pools
23 * and databases, this gracefully handles adding descriptors out of order and
24 * descriptors being replaced.
25 */
27 public:
28 /**
29 * Adds a file descriptor to the database. If the file references other
30 * files that have not yet been added, no messages in that file will be
31 * available until those files are added. Replaces any existing file with
32 * the same name.
33 *
34 * @param filename filename
35 * @param data FileDescriptorProto serialized data
36 * @return False if could not parse data
37 */
38 bool Add(std::string_view filename, std::span<const uint8_t> data);
39
40 /**
41 * Finds a message in the database by name.
42 *
43 * @param name type name
44 * @return protobuf message, or nullptr if not found
45 */
46 google::protobuf::Message* Find(std::string_view name) const;
47
48 /**
49 * Gets message factory.
50 *
51 * @return message factory
52 */
53 google::protobuf::MessageFactory* GetMessageFactory() {
54 return m_factory.get();
55 }
56
57 private:
58 struct ProtoFile {
59 std::unique_ptr<google::protobuf::FileDescriptorProto> proto;
60 std::vector<std::string> uses;
61 bool complete = false;
62 bool inPool = false;
63 };
64
65 void Build(std::string_view filename, ProtoFile& file);
66 bool Rebuild(ProtoFile& file);
67
68 std::unique_ptr<google::protobuf::DescriptorPool> m_pool =
69 std::make_unique<google::protobuf::DescriptorPool>();
70 std::unique_ptr<google::protobuf::DynamicMessageFactory> m_factory =
71 std::make_unique<google::protobuf::DynamicMessageFactory>();
72 wpi::StringMap<ProtoFile> m_files; // indexed by filename
73 // indexed by type string
75};
76
77} // namespace wpi
This file defines the StringMap class.
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
Definition: ThirdPartyNotices.txt:192
Database of protobuf dynamic messages.
Definition: ProtobufMessageDatabase.h:26
google::protobuf::MessageFactory * GetMessageFactory()
Gets message factory.
Definition: ProtobufMessageDatabase.h:53
bool Add(std::string_view filename, std::span< const uint8_t > data)
Adds a file descriptor to the database.
google::protobuf::Message * Find(std::string_view name) const
Finds a message in the database by name.
basic_string_view< char > string_view
Definition: core.h:501
Definition: ntcore_cpp.h:26