-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathFactory.hh
More file actions
151 lines (138 loc) · 5.67 KB
/
Factory.hh
File metadata and controls
151 lines (138 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (C) 2016 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef GZ_MSGS_FACTORY_HH_
#define GZ_MSGS_FACTORY_HH_
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4100 4512 4127 4068 4244 4267 4251 4146)
#endif
#include <google/protobuf/message.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <string>
#include <map>
#include <memory>
#include <vector>
#include "gz/msgs/config.hh"
#include "gz/msgs/Export.hh"
namespace gz
{
namespace msgs
{
// Inline bracket to help doxygen filtering.
inline namespace GZ_MSGS_VERSION_NAMESPACE {
//
/// \typedef FactoryFn
/// \brief Prototype for message factory generation
typedef std::unique_ptr<google::protobuf::Message> (*FactoryFn) ();
/// \class Factory Factory.hh gz/msgs.hh
/// \brief A factory that generates protobuf message based on a string type.
/// This class will also try to load all Protobuf descriptors specified
/// in the GZ_DESCRIPTOR_PATH environment variable on program start.
class GZ_MSGS_VISIBLE Factory
{
/// \brief Register a message.
/// \param[in] _msgType Type of message to register.
/// \param[in] _factoryfn Function that generates the message.
public: static void Register(const std::string &_msgType,
FactoryFn _factoryfn);
/// \brief Create a new instance of a message.
/// \param[in] _msgType Type of message to create.
/// \return Pointer to a google protobuf message. Null if the message
/// type could not be handled.
public: template<typename T>
static std::unique_ptr<T> New(const std::string &_msgType)
{
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Use [" << msgType << "] instead."
<< std::endl;
}
return std::unique_ptr<T>(
static_cast<T*>(New(msgType).release()));
}
/// \brief Create a new instance of a message.
/// \param[in] _msgType Type of message to create.
/// \param[in] _args Message arguments. This will populate the message.
/// \return Pointer to a google protobuf message. Null if the message
/// type could not be handled.
public: template<typename T>
static std::unique_ptr<T> New(const std::string &_msgType,
const std::string &_args)
{
auto msgType = _msgType;
if (msgType.find("ignition") == 0)
{
msgType.replace(0, 8, "gz");
std::cerr << "Trying to create deprecated message type ["
<< _msgType << "]. Use [" << msgType << "] instead."
<< std::endl;
}
return std::unique_ptr<T>(
static_cast<T*>(New(msgType, _args).release()));
}
/// \brief Create a new instance of a message.
/// \param[in] _msgType Type of message to create.
/// \return Pointer to a google protobuf message. Null if the message
/// type could not be handled.
public: static std::unique_ptr<google::protobuf::Message> New(
const std::string &_msgType);
/// \brief Create a new instance of a message.
/// \param[in] _msgType Type of message to create.
/// \param[in] _args Message arguments. This will populate the message.
/// \return Pointer to a google protobuf message. Null if the message
/// type could not be handled.
public: static std::unique_ptr<google::protobuf::Message> New(
const std::string &_msgType, const std::string &_args);
/// \brief Get all the message types
/// \param[out] _types Vector of strings of the message types.
public: static void Types(std::vector<std::string> &_types);
/// \brief Load a collection of descriptor .desc files.
/// \param[in] _paths A set of directories containing .desc decriptor
/// files. Each directory should be separated by ":".
public: static void LoadDescriptors(const std::string &_paths);
/// \brief A list of registered message types
private: static std::map<std::string, FactoryFn> *msgMap;
};
/// \brief Static message registration macro
///
/// Use this macro to register messages.
/// \param[in] _msgtype Message type name.
/// \param[in] _classname Class name for message.
#define GZ_REGISTER_STATIC_MSG(_msgtype, _classname) \
GZ_MSGS_VISIBLE \
std::unique_ptr<google::protobuf::Message> New##_classname() \
{ \
return std::unique_ptr<gz::msgs::_classname>(\
new gz::msgs::_classname); \
} \
class GZ_MSGS_VISIBLE GzMsg##_classname \
{ \
public: GzMsg##_classname() \
{ \
gz::msgs::Factory::Register(_msgtype, New##_classname);\
} \
}; \
static GzMsg##_classname GzMessagesInitializer##_classname;
}
}
}
#endif