DCCL v4
type_helper.cpp
1 // Copyright 2011-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 // Chris Murphy <cmurphy@aphysci.com>
8 //
9 //
10 // This file is part of the Dynamic Compact Control Language Library
11 // ("DCCL").
12 //
13 // DCCL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU Lesser General Public License as published by
15 // the Free Software Foundation, either version 2.1 of the License, or
16 // (at your option) any later version.
17 //
18 // DCCL is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public License
24 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25 #include "type_helper.h"
26 
27 #include <memory>
28 
29 template <google::protobuf::FieldDescriptor::Type t>
30 void insertType(dccl::internal::TypeHelper::TypeMap* type_map)
31 {
32  type_map->insert(std::make_pair(t, std::shared_ptr<dccl::internal::FromProtoTypeBase>(
34 }
35 
36 template <google::protobuf::FieldDescriptor::CppType t>
37 void insert(dccl::internal::TypeHelper::CppTypeMap* cpptype_map)
38 {
39  cpptype_map->insert(std::make_pair(t, std::shared_ptr<dccl::internal::FromProtoCppTypeBase>(
41 }
42 
43 //
44 // TypeHelper
45 //
46 void dccl::internal::TypeHelper::initialize()
47 {
48  using namespace google::protobuf;
49  using std::make_pair;
50  using std::shared_ptr;
51 
52  type_map_.insert(
53  make_pair(static_cast<FieldDescriptor::Type>(0), std::make_shared<FromProtoTypeBase>()));
54  insertType<FieldDescriptor::TYPE_DOUBLE>(&type_map_);
55  insertType<FieldDescriptor::TYPE_FLOAT>(&type_map_);
56  insertType<FieldDescriptor::TYPE_UINT64>(&type_map_);
57  insertType<FieldDescriptor::TYPE_UINT32>(&type_map_);
58  insertType<FieldDescriptor::TYPE_FIXED64>(&type_map_);
59  insertType<FieldDescriptor::TYPE_FIXED32>(&type_map_);
60  insertType<FieldDescriptor::TYPE_INT64>(&type_map_);
61  insertType<FieldDescriptor::TYPE_INT32>(&type_map_);
62  insertType<FieldDescriptor::TYPE_SFIXED32>(&type_map_);
63  insertType<FieldDescriptor::TYPE_SFIXED64>(&type_map_);
64  insertType<FieldDescriptor::TYPE_SINT32>(&type_map_);
65  insertType<FieldDescriptor::TYPE_SINT64>(&type_map_);
66  insertType<FieldDescriptor::TYPE_BOOL>(&type_map_);
67  insertType<FieldDescriptor::TYPE_STRING>(&type_map_);
68  insertType<FieldDescriptor::TYPE_BYTES>(&type_map_);
69  insertType<FieldDescriptor::TYPE_MESSAGE>(&type_map_);
70  insertType<FieldDescriptor::TYPE_GROUP>(&type_map_);
71  insertType<FieldDescriptor::TYPE_ENUM>(&type_map_);
72 
73  cpptype_map_.insert(make_pair(static_cast<FieldDescriptor::CppType>(0),
74  std::make_shared<FromProtoCppTypeBase>()));
75 
76  insert<FieldDescriptor::CPPTYPE_DOUBLE>(&cpptype_map_);
77  insert<FieldDescriptor::CPPTYPE_FLOAT>(&cpptype_map_);
78  insert<FieldDescriptor::CPPTYPE_UINT64>(&cpptype_map_);
79  insert<FieldDescriptor::CPPTYPE_UINT32>(&cpptype_map_);
80  insert<FieldDescriptor::CPPTYPE_INT64>(&cpptype_map_);
81  insert<FieldDescriptor::CPPTYPE_INT32>(&cpptype_map_);
82  insert<FieldDescriptor::CPPTYPE_BOOL>(&cpptype_map_);
83  insert<FieldDescriptor::CPPTYPE_STRING>(&cpptype_map_);
84  insert<FieldDescriptor::CPPTYPE_MESSAGE>(&cpptype_map_);
85  insert<FieldDescriptor::CPPTYPE_ENUM>(&cpptype_map_);
86 }
87 
88 std::shared_ptr<dccl::internal::FromProtoCppTypeBase>
89 dccl::internal::TypeHelper::find(google::protobuf::FieldDescriptor::CppType cpptype,
90  const std::string& type_name /*= ""*/) const
91 {
92  if (!type_name.empty())
93  {
94  auto it = custom_message_map_.find(type_name);
95  if (it != custom_message_map_.end())
96  return it->second;
97  }
98 
99  auto it = cpptype_map_.find(cpptype);
100  if (it != cpptype_map_.end())
101  return it->second;
102  else
103  return std::shared_ptr<FromProtoCppTypeBase>();
104 }
105 
106 std::shared_ptr<dccl::internal::FromProtoTypeBase>
107 dccl::internal::TypeHelper::find(google::protobuf::FieldDescriptor::Type type) const
108 {
109  auto it = type_map_.find(type);
110  if (it != type_map_.end())
111  return it->second;
112  else
113  return std::shared_ptr<FromProtoTypeBase>();
114 }
dccl::internal::FromProtoCppType
Definition: protobuf_cpp_type_helpers.h:156
dccl::internal::FromProtoType
Definition: protobuf_cpp_type_helpers.h:46