forked from gazebosim/gz-msgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.cc
More file actions
205 lines (175 loc) · 7.05 KB
/
Generator.cc
File metadata and controls
205 lines (175 loc) · 7.05 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright 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.
*
*/
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4100 4512 4127 4068 4244 4267 4251 4146)
#endif
#include <google/protobuf/descriptor.h>
#include <google/protobuf/compiler/code_generator.h>
#include <google/protobuf/io/printer.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/descriptor.pb.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "Generator.hh"
namespace google {
namespace protobuf {
namespace compiler {
namespace cpp {
/////////////////////////////////////////////////
void replaceAll(std::string &_src, const std::string &_oldValue,
const std::string &_newValue)
{
for (size_t i = 0; (i = _src.find(_oldValue, i)) != std::string::npos;)
{
_src.replace(i, _oldValue.length(), _newValue);
i += _newValue.length() - _oldValue.length() + 1;
}
}
/////////////////////////////////////////////////
Generator::Generator(const std::string &/*_name*/)
{
}
/////////////////////////////////////////////////
Generator::~Generator()
{
}
/////////////////////////////////////////////////
bool Generator::Generate(const FileDescriptor *_file,
const std::string &/*_parameter*/,
OutputDirectory *_generatorContext,
std::string * /*_error*/) const
{
std::string headerFilename = _file->name();
std::string delim = ".proto";
size_t pos = headerFilename.rfind(delim);
headerFilename.replace(pos, delim.size(), ".pb.h");
std::string sourceFilename = _file->name();
pos = sourceFilename.rfind(delim);
sourceFilename.replace(pos, delim.size(), ".pb.cc");
// Inject code in the auto-generated header immediately following
// the #include <google/protobuf/*.h> calls
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
_generatorContext->OpenForInsert(headerFilename, "includes"));
io::Printer printer(output.get(), '$');
// Suppress expected warnings
printer.Print("#ifndef _MSC_VER\n", "name", "includes");
printer.Print("#pragma GCC system_header\n", "name", "includes");
printer.Print("#else\n", "name", "includes");
printer.Print("#pragma warning(push)\n", "name", "includes");
printer.Print("#pragma warning(disable: 4244 4267 4100 4244 4512",
"name", "includes");
printer.Print(" 4127 4068 4275 4251)\n", "name", "includes");
printer.Print("#endif\n", "name", "includes");
// Include sys/sysmacros.h to prevent warnings about "major" and "minor"
// defines. Major and minor are used in the version.proto file.
printer.Print("#ifdef __linux__\n", "name", "includes");
printer.Print("#include <sys/sysmacros.h>\n", "name", "include");
printer.Print("#endif\n", "name", "includes");
// Add the <memory> header so that we can define std::unique_ptr
// and std::shared_ptr types for our message later on
printer.Print("#include <memory>\n", "name", "includes");
// Add the Export header so that we can apply visibility macros
// to the messages
printer.Print("#include <ignition/msgs/Export.hh>\n", "name",
"includes");
}
// Inject code in the auto-generated source files immediately following
// the #include <google/protobuf*.h> calls.
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
_generatorContext->OpenForInsert(sourceFilename, "includes"));
io::Printer printer(output.get(), '$');
// Add the ign-msgs Factory header
printer.Print("#include \"ignition/msgs/Factory.hh\"\n", "name",
"includes");
// Suppress warnings
printer.Print("#ifndef _MSC_VER\n", "name", "includes");
printer.Print("#pragma GCC diagnostic ignored \"-Wshadow\"\n", "name",
"includes");
printer.Print("#pragma GCC diagnostic ignored \"-Wfloat-equal\"\n", "name",
"includes");
printer.Print("#pragma GCC diagnostic ignored \"-Wunused-parameter\"\n",
"name", "includes");
printer.Print(
"#pragma GCC diagnostic ignored \"-Wdeprecated-declarations\"\n",
"name", "includes");
printer.Print("#else\n", "name", "includes");
printer.Print("#pragma warning(disable: 4244 4267 4100 4244 4512",
"name", "includes");
printer.Print(" 4127 4068)\n", "name", "includes");
printer.Print("#endif\n", "name", "includes");
// Call the IGN_REGISTER_STATIC_MSG macro for each message
for (auto i = 0; i < _file->message_type_count(); ++i)
{
std::string factory = "IGN_REGISTER_STATIC_MSG(\"ign_msgs.";
factory += _file->message_type(i)->name() + "\", " +
_file->message_type(i)->name() +")\n";
printer.Print(factory.c_str(), "name", "includes");
}
}
// Inject code in the auto-generated header files immediately before closing
// the namespace that the messages are in
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
_generatorContext->OpenForInsert(headerFilename, "namespace_scope"));
io::Printer printer(output.get(), '$');
for (auto i = 0; i < _file->message_type_count(); ++i)
{
// Define std::unique_ptr types for our messages
std::string ptrTypes = "typedef std::unique_ptr<"
+ _file->message_type(i)->name() + "> "
+ _file->message_type(i)->name() + "UniquePtr;\n";
// Define const std::unique_ptr types for our messages
ptrTypes += "typedef std::unique_ptr<const "
+ _file->message_type(i)->name() + "> Const"
+ _file->message_type(i)->name() + "UniquePtr;\n";
// Define std::shared_ptr types for our messages
ptrTypes += "typedef std::shared_ptr<"
+ _file->message_type(i)->name() + "> "
+ _file->message_type(i)->name() + "SharedPtr;\n";
// Define const std::shared_ptr types for our messages
ptrTypes += "typedef std::shared_ptr<const "
+ _file->message_type(i)->name() + "> Const"
+ _file->message_type(i)->name() + "SharedPtr;\n";
printer.Print(ptrTypes.c_str(), "name", "namespace_scope");
}
}
// Pop the warning suppression stack for MSVC
{
std::unique_ptr<io::ZeroCopyOutputStream> output(
_generatorContext->OpenForInsert(headerFilename, "global_scope"));
io::Printer printer(output.get(), '$');
std::string warningPop = "#ifdef _MSC_VER\n";
warningPop += "#pragma warning(pop)\n";
warningPop += "#endif";
printer.Print(warningPop.c_str(), "name", "global_scope");
}
return true;
}
}
}
}
}