MsgFactory.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-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 
18 #ifndef _MSGFACTORY_HH_
19 #define _MSGFACTORY_HH_
20 
21 #include <string>
22 #include <map>
23 #include <vector>
24 #include <google/protobuf/message.h>
25 #include <boost/shared_ptr.hpp>
26 #include "gazebo/util/system.hh"
27 
28 namespace gazebo
29 {
30  namespace msgs
31  {
34  typedef boost::shared_ptr<google::protobuf::Message> (*MsgFactoryFn) ();
35 
42  {
46  public: static void RegisterMsg(const std::string &_msgType,
47  MsgFactoryFn _factoryfn);
48 
53  public: static boost::shared_ptr<google::protobuf::Message> NewMsg(
54  const std::string &_msgType);
55 
58  public: static void GetMsgTypes(std::vector<std::string> &_types);
59 
61  private: static std::map<std::string, MsgFactoryFn> *msgMap;
62  };
63 
64 
70  #define GZ_REGISTER_STATIC_MSG(_msgtype, _classname) \
71  GAZEBO_VISIBLE \
72  boost::shared_ptr<google::protobuf::Message> New##_classname() \
73  { \
74  return boost::shared_ptr<gazebo::msgs::_classname>(\
75  new gazebo::msgs::_classname); \
76  } \
77  class GAZEBO_VISIBLE Msg##_classname \
78  { \
79  public: Msg##_classname() \
80  { \
81  gazebo::msgs::MsgFactory::RegisterMsg(_msgtype, New##_classname);\
82  } \
83  }; \
84  static Msg##_classname GzMsgInitializer;
85 
87  }
88 }
89 
90 #endif
A factory that generates protobuf message based on a string type.
Definition: MsgFactory.hh:41
boost::shared_ptr< google::protobuf::Message >(* MsgFactoryFn)()
Definition: MsgFactory.hh:34
#define GAZEBO_VISIBLE
Use to represent "symbol visible" if supported.
Definition: system.hh:59