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