forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parserHelpers.h
352 lines (298 loc) · 12.2 KB
/
parserHelpers.h
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// Copyright 2016 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
#ifndef PXR_USD_SDF_PARSER_HELPERS_H
#define PXR_USD_SDF_PARSER_HELPERS_H
#include "pxr/pxr.h"
#include "pxr/usd/sdf/assetPath.h"
#include "pxr/usd/sdf/valueTypeName.h"
#include "pxr/base/arch/inttypes.h"
#include "pxr/base/vt/value.h"
#include <boost/numeric/conversion/cast.hpp>
#include <boost/variant.hpp>
#include <functional>
#include <limits>
#include <string>
#include <type_traits>
#include <vector>
PXR_NAMESPACE_OPEN_SCOPE
bool Sdf_BoolFromString(const std::string &, bool *parseOk);
namespace Sdf_ParserHelpers {
// Internal variant type.
typedef boost::variant<uint64_t, int64_t, double,
std::string, TfToken, SdfAssetPath> _Variant;
////////////////////////////////////////////////////////////////////////
// Utilities that implement the Sdf_ParserHelpers::Value::Get<T>() method. The
// _GetImpl<T> class template provides the right implementation for T. There
// are several partial specializations that provide the right behavior for
// various types.
// General Get case, requires exact match.
template <class T, class Enable = void>
struct _GetImpl
{
typedef const T &ResultType;
static const T &Visit(_Variant const &variant) {
return boost::get<T>(variant);
}
};
////////////////////////////////////////////////////////////////////////
// _GetImpl<T> for integral type T. Convert finite doubles by static_cast,
// throw bad_get for non-finite doubles. Throw bad_get for out-of-range
// integral values.
template <class T>
struct _GetImpl<
T, std::enable_if_t<std::is_integral<T>::value>>
: public boost::static_visitor<T>
{
typedef T ResultType;
T Visit(_Variant const &variant) {
return boost::apply_visitor(*this, variant);
}
// Fallback case: throw bad_get.
template <class Held>
T operator()(Held held) { throw boost::bad_get(); }
// Attempt to cast unsigned and signed int64_t.
T operator()(uint64_t in) { return _Cast(in); }
T operator()(int64_t in) { return _Cast(in); }
// Attempt to cast finite doubles, throw otherwise.
T operator()(double in) {
if (std::isfinite(in))
return _Cast(in);
throw boost::bad_get();
}
private:
template <class In>
T _Cast(In in) {
try {
return boost::numeric_cast<T>(in);
} catch (const boost::bad_numeric_cast &) {
throw boost::bad_get();
}
}
};
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// _GetImpl<T> for floating point type T. Attempts to cast numeric values.
// Also handles strings like "inf", "-inf", and "nan" to produce +/- infinity
// and a quiet NaN.
template <class T>
struct _GetImpl<
T, std::enable_if_t<std::is_floating_point<T>::value>>
: public boost::static_visitor<T>
{
typedef T ResultType;
T Visit(_Variant const &variant) {
return boost::apply_visitor(*this, variant);
}
// Fallback case: throw bad_get.
template <class Held>
T operator()(Held held) { throw boost::bad_get(); }
// For numeric types, attempt to cast.
T operator()(uint64_t in) { return _Cast(in); }
T operator()(int64_t in) { return _Cast(in); }
T operator()(double in) { return static_cast<T>(in); }
// Convert special strings if possible.
T operator()(const std::string &str) { return _FromString(str); }
T operator()(const TfToken &tok) { return _FromString(tok.GetString()); }
private:
T _FromString(const std::string &str) const {
// Special case the strings 'inf', '-inf' and 'nan'.
if (str == "inf")
return std::numeric_limits<T>::infinity();
if (str == "-inf")
return -std::numeric_limits<T>::infinity();
if (str == "nan")
return std::numeric_limits<T>::quiet_NaN();
throw boost::bad_get();
}
template <class In>
T _Cast(In in) {
try {
return boost::numeric_cast<T>(in);
} catch (const boost::bad_numeric_cast &) {
throw boost::bad_get();
}
}
};
////////////////////////////////////////////////////////////////////////
// Get an asset path: converts string to asset path, otherwise throw bad_get.
template <>
struct _GetImpl<SdfAssetPath>
{
typedef SdfAssetPath ResultType;
SdfAssetPath Visit(_Variant const &variant) {
if (std::string const *str = boost::get<std::string>(&variant))
return SdfAssetPath(*str);
return boost::get<SdfAssetPath>(variant);
}
};
// Get a bool. Numbers are considered true if nonzero, false otherwise.
// Strings and tokens get parsed via Sdf_BoolFromString. Otherwise throw
// bad_get.
template <>
struct _GetImpl<bool> : public boost::static_visitor<bool>
{
typedef bool ResultType;
bool Visit(_Variant const &variant) {
return boost::apply_visitor(*this, variant);
}
// Parse string via Sdf_BoolFromString.
bool operator()(const std::string &str) {
bool parseOK = false;
bool result = Sdf_BoolFromString(str, &parseOK);
if (!parseOK)
throw boost::bad_get();
return result;
}
// Treat tokens as strings.
bool operator()(const TfToken &tok) { return (*this)(tok.GetString()); }
// For numbers, return true if not zero.
template <class Number>
std::enable_if_t<std::is_arithmetic<Number>::value, bool>
operator()(Number val) {
return val != static_cast<Number>(0);
}
// For anything else, throw bad_get().
template <class T>
std::enable_if_t<!std::is_arithmetic<T>::value, bool>
operator()(T) { throw boost::bad_get(); }
};
// A parser value. This is used as the fundamental value object in the text
// parser. It can hold one of a few different types: (u)int64_t, double,
// string, TfToken, and SdfAssetPath. The lexer only ever produces Value
// objects holding (u)int64_t, double, and string. The presence of TfToken and
// SdfAssetPath here are for a relatively obscure case where we're parsing a
// value whose type is unknown to the parser. See
// StartRecordingString/StopRecordingString/IsRecordingString in
// parserValueContext.{cpp.h}. We'd like to change this.
//
// Value's primary function is to provide a Get<T>() convenience API that
// handles appropriate conversions from the held types. For example, it is okay
// to call Get<float>() on a Value that's holding an integral type, a double, or
// a string if that string's value is one of 'inf', '-inf', or 'nan'. Similarly
// Get<bool>() works on numbers and strings like 'yes', 'no', 'on', 'off',
// 'true', 'false'. If a Get<T>() call fails, it throws boost::bad_get, which
// the parser responds to and raises a parse error.
//
// The lexer constructs Value objects from input tokens. It creates them to
// retain all the input information possible. For example, negative integers
// are stored as int64_t Values, positive numbers are stored as uint64_t values,
// and so on. As a special case of this, '-0' is stored as a double, since it
// is the only way to preserve a signed zero (integral types have no signed
// zero).
struct Value
{
// Default constructor leaves the value in an undefined state.
Value() {}
// Construct and implicitly convert from an integral type \p Int. If \p Int
// is signed, the resulting value holds an 'int64_t' internally. If \p Int
// is unsigned, the result value holds an 'uint64_t'.
template <class Int>
Value(Int in, std::enable_if_t<std::is_integral<Int>::value> * = 0) {
if (std::is_signed<Int>::value) {
_variant = static_cast<int64_t>(in);
} else {
_variant = static_cast<uint64_t>(in);
}
}
// Construct and implicitly convert from a floating point type \p Flt. The
// resulting value holds a double internally.
template <class Flt>
Value(Flt in, std::enable_if_t<std::is_floating_point<Flt>::value> * = 0) :
_variant(static_cast<double>(in)) {}
// Construct and implicitly convert from std::string.
Value(const std::string &in) : _variant(in) {}
// Construct and implicitly convert from TfToken.
Value(const TfToken &in) : _variant(in) {}
// Construct and implicitly convert from SdfAssetPath.
Value(const SdfAssetPath &in) : _variant(in) {}
// Attempt to get a value of type T from this Value, applying appropriate
// conversions. If this value cannot be converted to T, throw
// boost::bad_get.
template <class T>
typename _GetImpl<T>::ResultType Get() const {
return _GetImpl<T>().Visit(_variant);
}
// Hopefully short-lived API that applies an external visitor to the held
// variant type.
template <class Visitor>
typename Visitor::result_type
ApplyVisitor(const Visitor &visitor) {
return boost::apply_visitor(visitor, _variant);
}
template <class Visitor>
typename Visitor::result_type
ApplyVisitor(Visitor &visitor) {
return boost::apply_visitor(visitor, _variant);
}
template <class Visitor>
typename Visitor::result_type
ApplyVisitor(const Visitor &visitor) const {
return _variant.apply_visitor(visitor);
}
template <class Visitor>
typename Visitor::result_type
ApplyVisitor(Visitor &visitor) const {
return _variant.apply_visitor(visitor);
}
private:
_Variant _variant;
};
typedef std::function<VtValue (std::vector<unsigned int> const &,
std::vector<Value> const &,
size_t &, std::string *)> ValueFactoryFunc;
struct ValueFactory {
ValueFactory() {}
ValueFactory(std::string typeName_, SdfTupleDimensions dimensions_,
bool isShaped_, ValueFactoryFunc func_)
: typeName(typeName_),
dimensions(dimensions_),
isShaped(isShaped_),
func(func_) {}
std::string typeName;
SdfTupleDimensions dimensions;
bool isShaped;
ValueFactoryFunc func;
};
ValueFactory const &GetValueFactoryForMenvaName(std::string const &name,
bool *found);
}
/// Converts a string to a bool.
/// Accepts case insensitive "yes", "no", "false", true", "0", "1".
/// Defaults to "true" if the string is not recognized.
///
/// If parseOK is supplied, the pointed-to bool will be set to indicate
/// whether the parse was successful.
bool Sdf_BoolFromString(const std::string &s, bool *parseOk = NULL);
// Read the quoted string at [x..x+n], trimming 'trimBothSides' number
// of chars from either side, and evaluating any embedded escaped characters.
// If numLines is given, it will be populated with the number of newline
// characters present in the original string.
std::string Sdf_EvalQuotedString(const char* x, size_t n, size_t trimBothSides,
unsigned int* numLines = NULL);
// Read the string representing an asset path at [x..x+n]. If tripleDelimited
// is true, the string is assumed to have 3 delimiters on both sides of the
// asset path, otherwise the string is assumed to have just 1.
std::string Sdf_EvalAssetPath(const char* x, size_t n, bool tripleDelimited);
PXR_NAMESPACE_CLOSE_SCOPE
#endif // PXR_USD_SDF_PARSER_HELPERS_H