-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGenerateTypedPropertyEnums.cpp
317 lines (271 loc) · 10.3 KB
/
GenerateTypedPropertyEnums.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** @file
@brief Implementation
@date 2016
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
// Copyright 2016 Razer, Inc.
//
// 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.
// Internal Includes
// - none
// Library/third-party includes
#include <boost/range/adaptor/reversed.hpp>
#include <json/reader.h>
#include <json/value.h>
#include <osvr/Util/UniqueContainer.h>
// Standard includes
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <vector>
static const auto indent = " ";
static const auto outFilename = "PropertyTraits.h";
static const auto startOfOutput = R"(/** @file
@brief Header - partially generated from parsing openvr_api.json
@date 2016
@author
Sensics, Inc.
<http://sensics.com/osvr>
*/
/*
Copyright 2016 Razer Inc.
SPDX-License-Identifier: BSD-3-Clause
OpenVR input data:
Copyright (c) 2015, Valve Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef INCLUDED_PropertyTraits_h_GUID_6CC473E5_C8B9_46B7_237B_1E0C08E91076
#define INCLUDED_PropertyTraits_h_GUID_6CC473E5_C8B9_46B7_237B_1E0C08E91076
#ifndef _INCLUDE_VRTYPES_H
#error "Please include exactly one of openvr.h or openvr_driver.h before including this file"
#endif
#include <cstddef>
#include <string>
namespace osvr {
namespace vive {
)";
static const auto endOfOutput = R"(
} // namespace vive
} // namespace osvr
#endif // INCLUDED_PropertyTraits_h_GUID_6CC473E5_C8B9_46B7_237B_1E0C08E91076
)";
inline bool shouldIgnoreType(std::string const &t) {
return t == "Start" ||
t == "End"; // ignore the sentinels that look like types.
}
std::vector<std::pair<std::string, std::string>> g_fullNameToTypeSuffix;
std::vector<std::pair<std::string, std::string>> g_cleanNameToFullName;
std::map<std::string, std::string> g_typeSuffixToTypename = {
{"String", "std::string"}, {"Bool", "bool"},
{"Float", "float"}, {"Matrix34", "vr::HmdMatrix34_t"},
{"Uint64", "uint64_t"}, {"Int32", "int32_t"},
{"Binary", "void *"}};
std::set<std::string> g_ambiguousNames;
osvr::util::UniqueContainer<std::vector<std::string>> g_typeSuffixes;
osvr::util::UniqueContainer<std::vector<std::string>> g_cleanNames;
inline std::string getTypenameForTypeSuffix(std::string const &suffix) {
auto it = g_typeSuffixToTypename.find(suffix);
if (end(g_typeSuffixToTypename) == it) {
throw std::logic_error("Missing mapping for suffix " + suffix);
}
return it->second;
}
/// Structure that decomposes a full name of an enum value into a clean name and
/// a type suffix.
struct NameDecomp {
explicit NameDecomp(std::string const &name) {
static const auto fullDecomposeRegex =
std::regex{"^Prop_(.*)_([^_]*)$"};
std::regex_search(name, m, fullDecomposeRegex);
cleanName = m[1];
typeSuffix = m[2];
}
std::smatch m;
std::ssub_match cleanName;
std::ssub_match typeSuffix;
};
/// Helper function that uses a simpler regex to just extract the type suffix,
/// when that's all you want.
inline std::string getTypeSuffix(std::string const &name) {
static const auto valTypeRegex = std::regex{"_([^_]*)$"};
std::smatch m;
std::regex_search(name, m, valTypeRegex);
return m[1];
}
bool processEnumValues(Json::Value const &values, std::ostream &output) {
std::vector<std::string> names;
for (auto &enumVal : values) {
auto name = enumVal["name"].asString();
auto d = NameDecomp{name};
if (shouldIgnoreType(d.typeSuffix) || (d.typeSuffix.length() == 0)) {
continue;
}
names.push_back(name);
#if 0
std::cout << "Name: " << name << " Clean name: " << d.cleanName << " Value type: " << d.typeSuffix << std::endl;
#endif
if (g_cleanNames.contains(d.cleanName)) {
std::cerr << "Ambiguous clean name found! " << d.cleanName
<< " (second type was " << d.typeSuffix << ")"
<< std::endl;
g_ambiguousNames.insert(d.cleanName);
} else {
g_cleanNames.insert(d.cleanName);
}
g_typeSuffixes.insert(d.typeSuffix);
g_fullNameToTypeSuffix.emplace_back(name, d.typeSuffix);
}
bool success = true;
/// OK, so that was the first pass through the list. Quick safety check.
for (auto &suffix : g_typeSuffixes.container()) {
if (end(g_typeSuffixToTypename) ==
g_typeSuffixToTypename.find(suffix)) {
std::cerr << "Type suffix found in the data file that's not "
"accounted for in the application: "
<< suffix << std::endl;
std::cerr << "Tool must be updated to add a mapping to "
"g_typeSuffixToTypename for this type suffix!"
<< std::endl;
success = false;
}
}
if (!success) {
return success;
}
/// Second pass: output the shortcut enum class for everything but the
/// ambiguous
/// names.
{
std::vector<std::string> lines;
for (auto &name : names) {
std::ostringstream os;
auto d = NameDecomp{name};
auto isAmbiguous =
(end(g_ambiguousNames) != g_ambiguousNames.find(d.cleanName));
if (isAmbiguous) {
os << "// shortcut omitted due to ambiguity for " << name;
} else {
os << d.cleanName << " = vr::" << name << ",";
}
lines.emplace_back(os.str());
}
// take the comma off the last actual entry.
for (auto &line : boost::adaptors::reverse(lines)) {
if (line.front() == '/') {
continue;
}
if (line.back() == ',') {
// should always be true if we get here!
line.pop_back();
break;
}
throw std::logic_error(
"Found a non-comment line that didn't end in a comma!");
}
output << "enum class Props {" << std::endl;
for (auto &line : lines) {
output << indent << line << std::endl;
}
output << "};" << std::endl;
}
/// Third pass: output the traits class specializations to associate types
/// with each property enum.
output << "namespace detail {" << std::endl;
output << indent
<< "template<std::size_t EnumVal> struct PropertyTypeTrait;"
<< std::endl;
output << indent << "template<std::size_t EnumVal> using PropertyType = "
"typename PropertyTypeTrait<EnumVal>::type;"
<< std::endl;
for (auto &name : names) {
auto enumTypename = getTypenameForTypeSuffix(getTypeSuffix(name));
output << indent << "template<> struct PropertyTypeTrait<vr::" << name
<< "> { using type = " << enumTypename << "; };" << std::endl;
}
output << "} // namespace detail" << std::endl;
return success;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Must pass path to openvr_api.json as first argument!"
<< std::endl;
return -1;
}
Json::Value root;
{
std::ifstream is(argv[1]);
if (!is) {
std::cerr << "Could not open expected 'openvr_api.json' file at "
"provided path: "
<< argv[1] << std::endl;
return -1;
}
Json::Reader reader;
if (!reader.parse(is, root)) {
std::cerr << "Could not parse " << argv[1]
<< " as JSON: " << reader.getFormattedErrorMessages()
<< std::endl;
return -1;
}
}
std::ostringstream os;
auto &enums = root["enums"];
bool success = false;
for (auto const &enumObj : enums) {
if (enumObj["enumname"] == "vr::ETrackedDeviceProperty") {
success = processEnumValues(enumObj["values"], os);
break;
}
}
if (success) {
std::cout << "Succeeded in processing JSON: will now write file "
<< outFilename << std::endl;
std::ofstream of(outFilename);
if (!of) {
std::cerr << "Could not open file " << outFilename << std::endl;
return -1;
}
of << startOfOutput;
of << os.str();
of << endOfOutput;
of.close();
std::cout << "Done writing file!" << std::endl;
}
return success ? 0 : 1;
}