DCCL v5
Loading...
Searching...
No Matches
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
27template <google::protobuf::FieldDescriptor::Type t>
28void insertType(dccl::internal::TypeHelper::TypeMap* type_map)
29{
30 type_map->emplace(t, std::shared_ptr<dccl::internal::FromProtoTypeBase>(
32}
33
34template <google::protobuf::FieldDescriptor::CppType t>
35void insert(dccl::internal::TypeHelper::CppTypeMap* cpptype_map)
36{
37 cpptype_map->emplace(t, std::shared_ptr<dccl::internal::FromProtoCppTypeBase>(
39}
40
41//
42// TypeHelper
43//
44void dccl::internal::TypeHelper::initialize()
45{
46 using namespace google::protobuf;
47 using std::shared_ptr;
48
49 type_map_.emplace(static_cast<FieldDescriptor::Type>(0), std::make_shared<FromProtoTypeBase>());
50 insertType<FieldDescriptor::TYPE_DOUBLE>(&type_map_);
51 insertType<FieldDescriptor::TYPE_FLOAT>(&type_map_);
52 insertType<FieldDescriptor::TYPE_UINT64>(&type_map_);
53 insertType<FieldDescriptor::TYPE_UINT32>(&type_map_);
54 insertType<FieldDescriptor::TYPE_FIXED64>(&type_map_);
55 insertType<FieldDescriptor::TYPE_FIXED32>(&type_map_);
56 insertType<FieldDescriptor::TYPE_INT64>(&type_map_);
57 insertType<FieldDescriptor::TYPE_INT32>(&type_map_);
58 insertType<FieldDescriptor::TYPE_SFIXED32>(&type_map_);
59 insertType<FieldDescriptor::TYPE_SFIXED64>(&type_map_);
60 insertType<FieldDescriptor::TYPE_SINT32>(&type_map_);
61 insertType<FieldDescriptor::TYPE_SINT64>(&type_map_);
62 insertType<FieldDescriptor::TYPE_BOOL>(&type_map_);
63 insertType<FieldDescriptor::TYPE_STRING>(&type_map_);
64 insertType<FieldDescriptor::TYPE_BYTES>(&type_map_);
65 insertType<FieldDescriptor::TYPE_MESSAGE>(&type_map_);
66 insertType<FieldDescriptor::TYPE_GROUP>(&type_map_);
67 insertType<FieldDescriptor::TYPE_ENUM>(&type_map_);
68
69 cpptype_map_.emplace(static_cast<FieldDescriptor::CppType>(0),
70 std::make_shared<FromProtoCppTypeBase>());
71
72 insert<FieldDescriptor::CPPTYPE_DOUBLE>(&cpptype_map_);
73 insert<FieldDescriptor::CPPTYPE_FLOAT>(&cpptype_map_);
74 insert<FieldDescriptor::CPPTYPE_UINT64>(&cpptype_map_);
75 insert<FieldDescriptor::CPPTYPE_UINT32>(&cpptype_map_);
76 insert<FieldDescriptor::CPPTYPE_INT64>(&cpptype_map_);
77 insert<FieldDescriptor::CPPTYPE_INT32>(&cpptype_map_);
78 insert<FieldDescriptor::CPPTYPE_BOOL>(&cpptype_map_);
79 insert<FieldDescriptor::CPPTYPE_STRING>(&cpptype_map_);
80 insert<FieldDescriptor::CPPTYPE_MESSAGE>(&cpptype_map_);
81 insert<FieldDescriptor::CPPTYPE_ENUM>(&cpptype_map_);
82}
83
84std::shared_ptr<dccl::internal::FromProtoCppTypeBase>
85dccl::internal::TypeHelper::find(google::protobuf::FieldDescriptor::CppType cpptype,
86 const std::string& type_name /*= ""*/) const
87{
88 if (!type_name.empty())
89 {
90 auto it = custom_message_map_.find(type_name);
91 if (it != custom_message_map_.end())
92 return it->second;
93 }
94
95 auto it = cpptype_map_.find(cpptype);
96 if (it != cpptype_map_.end())
97 return it->second;
98 else
99 return std::shared_ptr<FromProtoCppTypeBase>();
100}
101
102std::shared_ptr<dccl::internal::FromProtoTypeBase>
103dccl::internal::TypeHelper::find(google::protobuf::FieldDescriptor::Type type) const
104{
105 auto it = type_map_.find(type);
106 if (it != type_map_.end())
107 return it->second;
108 else
109 return std::shared_ptr<FromProtoTypeBase>();
110}