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