DCCL v4
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 
33 namespace dccl
34 {
35 class FieldCodecManagerLocal;
36 
37 namespace 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, desc->full_name());
58  }
59 
60  std::shared_ptr<FromProtoCppTypeBase> find(google::protobuf::FieldDescriptor::CppType cpptype,
61  const std::string& type_name = "") const;
62 
63  private:
64  friend class ::dccl::FieldCodecManagerLocal;
65  template <typename ProtobufMessage> void add()
66  {
67  custom_message_map_.insert(std::make_pair(
68  ProtobufMessage::descriptor()->full_name(),
69  std::shared_ptr<FromProtoCppTypeBase>(new FromProtoCustomMessage<ProtobufMessage>)));
70  }
71  template <typename ProtobufMessage> void remove()
72  {
73  custom_message_map_.erase(ProtobufMessage::descriptor()->full_name());
74  }
75  void reset()
76  {
77  type_map_.clear();
78  cpptype_map_.clear();
79  custom_message_map_.clear();
80  }
81 
82  void initialize();
83 
84  public:
85  using TypeMap =
86  std::map<google::protobuf::FieldDescriptor::Type, std::shared_ptr<FromProtoTypeBase>>;
87  TypeMap type_map_;
88 
89  using CppTypeMap =
90  std::map<google::protobuf::FieldDescriptor::CppType, std::shared_ptr<FromProtoCppTypeBase>>;
91  CppTypeMap cpptype_map_;
92 
93  using CustomMessageMap = std::map<std::string, std::shared_ptr<FromProtoCppTypeBase>>;
94  CustomMessageMap custom_message_map_;
95 };
96 } // namespace internal
97 } // namespace dccl
98 
99 #endif
dccl::internal::FromProtoCustomMessage
Implements FromProtoCppTypeBase for CPPTYPE_MESSAGE using a specific statically generated Protobuf cl...
Definition: protobuf_cpp_type_helpers.h:500
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:46
dccl::internal::TypeHelper
Provides FromProtoTypeBase and FromProtoCppTypeBase type identification helper classes for various re...
Definition: type_helper.h:40