All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MsgFactory.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2012 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 
27 namespace gazebo
28 {
29  namespace msgs
30  {
33  typedef boost::shared_ptr<google::protobuf::Message> (*MsgFactoryFn) ();
34 
40  class MsgFactory
41  {
45  public: static void RegisterMsg(const std::string &_msgType,
46  MsgFactoryFn _factoryfn);
47 
52  public: static boost::shared_ptr<google::protobuf::Message> NewMsg(
53  const std::string &_msgType);
54 
57  public: static void GetMsgTypes(std::vector<std::string> &_types);
58 
60  private: static std::map<std::string, MsgFactoryFn> *msgMap;
61  };
62 
63 
69  #define GZ_REGISTER_STATIC_MSG(_msgtype, _classname) \
70  boost::shared_ptr<google::protobuf::Message> New##_classname() \
71  { \
72  return boost::shared_ptr<gazebo::msgs::_classname>(\
73  new gazebo::msgs::_classname); \
74  } \
75  class Msg##_classname \
76  { \
77  public: Msg##_classname() \
78  { \
79  gazebo::msgs::MsgFactory::RegisterMsg(_msgtype, New##_classname);\
80  } \
81  }; \
82  static Msg##_classname GzMsgInitializer;
83 
85  }
86 }
87 
88 #endif