Gazebo Msgs

API Reference

9.5.1
gz/msgs/Factory.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef GZ_MSGS_FACTORY_HH_
18 #define GZ_MSGS_FACTORY_HH_
19 
20 #ifdef _MSC_VER
21 #pragma warning(push)
22 #pragma warning(disable: 4100 4512 4127 4068 4244 4267 4251 4146)
23 #endif
24 #include <google/protobuf/message.h>
25 #ifdef _MSC_VER
26 #pragma warning(pop)
27 #endif
28 
29 #include <string>
30 #include <map>
31 #include <memory>
32 #include <vector>
33 
34 #include "gz/msgs/config.hh"
35 #include "gz/msgs/Export.hh"
36 
37 namespace gz
38 {
39  namespace msgs
40  {
41  // Inline bracket to help doxygen filtering.
42  inline namespace GZ_MSGS_VERSION_NAMESPACE {
43  //
47 
52  class GZ_MSGS_VISIBLE Factory
53  {
57  public: static void Register(const std::string &_msgType,
58  FactoryFn _factoryfn);
59 
64  public: template<typename T>
65  static std::unique_ptr<T> New(const std::string &_msgType)
66  {
67  auto msgType = _msgType;
68  if (msgType.find("ignition") == 0)
69  {
70  msgType.replace(0, 8, "gz");
71  std::cerr << "Trying to create deprecated message type ["
72  << _msgType << "]. Using [" << msgType
73  << "] instead." << std::endl;
74  }
75  return std::unique_ptr<T>(
76  static_cast<T*>(New(msgType).release()));
77  }
78 
84  public: template<typename T>
85  static std::unique_ptr<T> New(const std::string &_msgType,
86  const std::string &_args)
87  {
88  auto msgType = _msgType;
89  if (msgType.find("ignition") == 0)
90  {
91  msgType.replace(0, 8, "gz");
92  std::cerr << "Trying to create deprecated message type ["
93  << _msgType << "]. Using [" << msgType
94  << "] instead." << std::endl;
95  }
96  return std::unique_ptr<T>(
97  static_cast<T*>(New(msgType, _args).release()));
98  }
99 
105  const std::string &_msgType);
106 
113  const std::string &_msgType, const std::string &_args);
114 
117  public: static void Types(std::vector<std::string> &_types);
118 
122  public: static void LoadDescriptors(const std::string &_paths);
123 
125  private: static std::map<std::string, FactoryFn> *msgMap;
126  };
127 
133  #define GZ_REGISTER_STATIC_MSG(_msgtype, _classname) \
134  GZ_MSGS_VISIBLE \
135  std::unique_ptr<google::protobuf::Message> New##_classname() \
136  { \
137  return std::unique_ptr<gz::msgs::_classname>(\
138  new gz::msgs::_classname); \
139  } \
140  class GZ_MSGS_VISIBLE GzMsg##_classname \
141  { \
142  public: GzMsg##_classname() \
143  { \
144  gz::msgs::Factory::Register(_msgtype, New##_classname);\
145  } \
146  }; \
147  static GzMsg##_classname GzMessagesInitializer##_classname;
148  }
149  }
150 }
151 #endif