#pragma once
#include <memory>
#include <span>
#include <string>
#include <string_view>
#include <vector>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/descriptor_database.h>
#include <google/protobuf/dynamic_message.h>
#include "wpi/StringMap.h"
namespace wpi {
class ProtobufMessageDatabase {
public:
bool Add(std::string_view filename, std::span<const uint8_t> data);
google::protobuf::Message* Find(std::string_view name) const;
google::protobuf::MessageFactory* GetMessageFactory() {
return m_factory.get();
}
private:
struct ProtoFile {
std::unique_ptr<google::protobuf::FileDescriptorProto> proto;
std::vector<std::string> uses;
bool complete = false;
bool inPool = false;
};
void Build(std::string_view filename, ProtoFile& file);
bool Rebuild(ProtoFile& file);
std::unique_ptr<google::protobuf::DescriptorPool> m_pool =
std::make_unique<google::protobuf::DescriptorPool>();
std::unique_ptr<google::protobuf::DynamicMessageFactory> m_factory =
std::make_unique<google::protobuf::DynamicMessageFactory>();
wpi::StringMap<ProtoFile> m_files; mutable wpi::StringMap<std::unique_ptr<google::protobuf::Message>> m_msgs;
};
}