DCCL v3
type_helper.h
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 #ifndef TypeHelper20110405H
23 #define TypeHelper20110405H
24 
25 #include <map>
26 
27 #include <boost/shared_ptr.hpp>
28 
29 #include "protobuf_cpp_type_helpers.h"
30 
31 namespace dccl
32 {
33  class FieldCodecManager;
34 
35  namespace internal
36  {
37 
39  class TypeHelper
40  {
41  public:
42  static boost::shared_ptr<FromProtoTypeBase> find(google::protobuf::FieldDescriptor::Type type);
43  static boost::shared_ptr<FromProtoCppTypeBase> find(const google::protobuf::FieldDescriptor* field)
44  {
45  if(field->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE)
46  return find(field->message_type());
47  else
48  return find(field->cpp_type());
49  }
50 
51  static boost::shared_ptr<FromProtoCppTypeBase> find(const google::protobuf::Descriptor* desc)
52  {
53  return find(google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE,
54  desc->full_name());
55  }
56 
57  static boost::shared_ptr<FromProtoCppTypeBase> find(google::protobuf::FieldDescriptor::CppType cpptype, const std::string& type_name = "");
58 
59  private:
60  friend class ::dccl::FieldCodecManager;
61  template<typename ProtobufMessage>
62  static void add()
63  {
64  custom_message_map_.insert(std::make_pair(ProtobufMessage::descriptor()->full_name(),
65  boost::shared_ptr<FromProtoCppTypeBase>(new FromProtoCustomMessage<ProtobufMessage>)));
66  }
67  template<typename ProtobufMessage>
68  static void remove()
69  {
70  custom_message_map_.erase(ProtobufMessage::descriptor()->full_name());
71  }
72  static void reset()
73  {
74  inst_.reset();
75  }
76 
77 
78  TypeHelper() { initialize(); }
79  ~TypeHelper()
80  {
81  type_map_.clear();
82  cpptype_map_.clear();
83  custom_message_map_.clear();
84  }
85  TypeHelper(const TypeHelper&);
86  TypeHelper& operator= (const TypeHelper&);
87  void initialize();
88 
89  public:
90  // so we can use shared_ptr to hold the singleton
91  template<typename T>
92  friend void boost::checked_delete(T*);
93  static boost::shared_ptr<TypeHelper> inst_;
94 
95  typedef std::map<google::protobuf::FieldDescriptor::Type,
96  boost::shared_ptr<FromProtoTypeBase> > TypeMap;
97  static TypeMap type_map_;
98 
99  typedef std::map<google::protobuf::FieldDescriptor::CppType,
100  boost::shared_ptr<FromProtoCppTypeBase> > CppTypeMap;
101  static CppTypeMap cpptype_map_;
102 
103  typedef std::map<std::string,
104  boost::shared_ptr<FromProtoCppTypeBase> > CustomMessageMap;
105  static CustomMessageMap custom_message_map_;
106  };
107  }
108 }
109 
110 #endif
Provides FromProtoTypeBase and FromProtoCppTypeBase type identification helper classes for various re...
Definition: type_helper.h:39
Dynamic Compact Control Language namespace.
Implements FromProtoCppTypeBase for CPPTYPE_MESSAGE using a specific statically generated Protobuf cl...