DCCL v5
Loading...
Searching...
No Matches
type_helper.h
1// Copyright 2014-2023:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7//
8//
9// This file is part of the Dynamic Compact Control Language Library
10// ("DCCL").
11//
12// DCCL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// DCCL is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24#ifndef TypeHelper20110405H
25#define TypeHelper20110405H
26
27#include <map>
28
29#include <memory>
30
31#include "protobuf_cpp_type_helpers.h"
32
33namespace dccl
34{
35class FieldCodecManagerLocal;
36
37namespace internal
38{
41{
42 public:
43 TypeHelper() { initialize(); }
44 ~TypeHelper() = default;
45
46 std::shared_ptr<FromProtoTypeBase> find(google::protobuf::FieldDescriptor::Type type) const;
47 std::shared_ptr<FromProtoCppTypeBase> find(const google::protobuf::FieldDescriptor* field) const
48 {
49 if (field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
50 return find(field->message_type());
51 else
52 return find(field->cpp_type());
53 }
54
55 std::shared_ptr<FromProtoCppTypeBase> find(const google::protobuf::Descriptor* desc) const
56 {
57 return find(google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE,
58 std::string(desc->full_name()));
59 }
60
61 std::shared_ptr<FromProtoCppTypeBase> find(google::protobuf::FieldDescriptor::CppType cpptype,
62 const std::string& type_name = "") const;
63
64 private:
65 friend class ::dccl::FieldCodecManagerLocal;
66 template <typename ProtobufMessage> void add()
67 {
68 custom_message_map_.emplace(
69 ProtobufMessage::descriptor()->full_name(),
70 std::shared_ptr<FromProtoCppTypeBase>(new FromProtoCustomMessage<ProtobufMessage>));
71 }
72 template <typename ProtobufMessage> void remove()
73 {
74 custom_message_map_.erase(std::string(ProtobufMessage::descriptor()->full_name()));
75 }
76 void reset()
77 {
78 type_map_.clear();
79 cpptype_map_.clear();
80 custom_message_map_.clear();
81 }
82
83 void initialize();
84
85 public:
86 using TypeMap =
87 std::map<google::protobuf::FieldDescriptor::Type, std::shared_ptr<FromProtoTypeBase>>;
88 TypeMap type_map_;
89
90 using CppTypeMap =
91 std::map<google::protobuf::FieldDescriptor::CppType, std::shared_ptr<FromProtoCppTypeBase>>;
92 CppTypeMap cpptype_map_;
93
94 using CustomMessageMap = std::map<std::string, std::shared_ptr<FromProtoCppTypeBase>>;
95 CustomMessageMap custom_message_map_;
96};
97} // namespace internal
98} // namespace dccl
99
100#endif
Implements FromProtoCppTypeBase for CPPTYPE_MESSAGE using a specific statically generated Protobuf cl...
Provides FromProtoTypeBase and FromProtoCppTypeBase type identification helper classes for various re...
Definition type_helper.h:41
Dynamic Compact Control Language namespace.
Definition any.h:28