Gazebo Msgs

API Reference

8.7.0
ignition/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 IGNITION_MSGS_FACTORY_HH_
18 #define IGNITION_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 "ignition/msgs/config.hh"
35 #include "ignition/msgs/Export.hh"
36 
37 namespace ignition
38 {
39  namespace msgs
40  {
41  // Inline bracket to help doxygen filtering.
42  inline namespace IGNITION_MSGS_VERSION_NAMESPACE {
43  //
47 
52  class IGNITION_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  return std::unique_ptr<T>(
68  static_cast<T*>(New(_msgType).release()));
69  }
70 
76  public: template<typename T>
77  static std::unique_ptr<T> New(const std::string &_msgType,
78  const std::string &_args)
79  {
80  return std::unique_ptr<T>(
81  static_cast<T*>(New(_msgType, _args).release()));
82  }
83 
89  const std::string &_msgType);
90 
97  const std::string &_msgType, const std::string &_args);
98 
101  public: static void Types(std::vector<std::string> &_types);
102 
106  public: static void LoadDescriptors(const std::string &_paths);
107 
109  private: static std::map<std::string, FactoryFn> *msgMap;
110  };
111 
117  #define IGN_REGISTER_STATIC_MSG(_msgtype, _classname) \
118  IGNITION_MSGS_VISIBLE \
119  std::unique_ptr<google::protobuf::Message> New##_classname() \
120  { \
121  return std::unique_ptr<ignition::msgs::_classname>(\
122  new ignition::msgs::_classname); \
123  } \
124  class IGNITION_MSGS_VISIBLE IgnMsg##_classname \
125  { \
126  public: IgnMsg##_classname() \
127  { \
128  ignition::msgs::Factory::Register(_msgtype, New##_classname);\
129  } \
130  }; \
131  static IgnMsg##_classname IgnitionMessagesInitializer##_classname;
132  }
133  }
134 }
135 #endif
STL class.
STL class.
static void Register(const std::string &_msgType, FactoryFn _factoryfn)
Register a message.
static void Types(std::vector< std::string > &_types)
Get all the message types.
std::unique_ptr< google::protobuf::Message >(* FactoryFn)()
Prototype for message factory generation.
Definition: ignition/msgs/Factory.hh:46
A factory that generates protobuf message based on a string type. This class will also try to load al...
Definition: ignition/msgs/Factory.hh:52
static std::unique_ptr< T > New(const std::string &_msgType)
Create a new instance of a message.
Definition: ignition/msgs/Factory.hh:65
static void LoadDescriptors(const std::string &_paths)
Load a collection of descriptor .desc files.
#define IGNITION_MSGS_VERSION_NAMESPACE
Definition: build/ignition-msgs8/include/ignition/msgs/config.hh:11
STL class.
static std::unique_ptr< T > New(const std::string &_msgType, const std::string &_args)
Create a new instance of a message.
Definition: ignition/msgs/Factory.hh:77