-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
wrapValue.cpp
334 lines (283 loc) · 11.9 KB
/
wrapValue.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// 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.
//
#include "pxr/pxr.h"
#include "pxr/base/vt/value.h"
#include "pxr/base/vt/array.h"
#include "pxr/base/vt/types.h"
#include "pxr/base/vt/typeHeaders.h"
#include "pxr/base/vt/valueFromPython.h"
#include "pxr/base/vt/wrapArray.h"
#include "pxr/base/arch/demangle.h"
#include "pxr/base/arch/inttypes.h"
#include "pxr/base/tf/pyContainerConversions.h"
#include "pxr/base/tf/pyFunction.h"
#include "pxr/base/tf/pyResultConversions.h"
#include "pxr/base/tf/pyUtils.h"
#include "pxr/base/tf/stringUtils.h"
#include "pxr/base/tf/type.h"
#include <boost/numeric/conversion/cast.hpp>
#include <boost/preprocessor.hpp>
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/def.hpp>
#include <boost/python/object.hpp>
#include <boost/python/operators.hpp>
#include <boost/python/return_arg.hpp>
#include <boost/python/type_id.hpp>
#include <boost/python/str.hpp>
#include <iostream>
#include <limits>
#include <map>
#include <string>
#include <vector>
using namespace boost::python;
using std::string;
using std::map;
PXR_NAMESPACE_OPEN_SCOPE
TF_REGISTRY_FUNCTION(VtValue)
{
// Allow converting python sequences to VtArray for certain types. Note
// that sequences of numeric types are handled in arrayNumpy.{cpp,h}.
VtRegisterValueCastsFromPythonSequencesToArray<std::string>();
VtRegisterValueCastsFromPythonSequencesToArray<TfToken>();
}
TfPyObjWrapper
Vt_GetPythonObjectFromHeldValue(VtValue const &self)
{
return self._GetPythonObject();
}
PXR_NAMESPACE_CLOSE_SCOPE
PXR_NAMESPACE_USING_DIRECTIVE
namespace {
// This is only for testing and hitting code coverage.
static string _test_ValueTypeName(VtValue const &val) {
return val.GetTypeName();
}
static VtValue _test_Ident(VtValue const &val) {
return val;
}
static std::string _test_Str(VtValue const &val) {
std::ostringstream stream;
stream << val;
return stream.str();
}
// Lets python explicitly pass in values of C++ types which have no python
// equivalents.
struct Vt_ValueWrapper {
template <class T>
static Vt_ValueWrapper Create(T a) { return Vt_ValueWrapper(a); }
template <typename T> explicit Vt_ValueWrapper(T val) : _val(val) {}
VtValue const &GetValue() const { return _val; }
private:
VtValue _val;
};
struct Vt_ValueToPython {
static PyObject *convert(VtValue const &val) {
return incref(Vt_GetPythonObjectFromHeldValue(val).ptr());
}
};
struct Vt_ValueWrapperFromPython {
Vt_ValueWrapperFromPython() {
converter::registry::
push_back(&_convertible, &_construct,
boost::python::type_id<VtValue>());
}
private:
static void *_convertible(PyObject *obj_ptr) {
return extract<Vt_ValueWrapper>(obj_ptr).check() ? obj_ptr : NULL;
}
static void _construct(PyObject *obj_ptr, converter::
rvalue_from_python_stage1_data *data) {
void *storage = ((converter::rvalue_from_python_storage<VtValue>*)data)
->storage.bytes;
new (storage) VtValue(extract<Vt_ValueWrapper>(obj_ptr)().GetValue());
data->convertible = storage;
}
};
struct Vt_ValueFromPython {
Vt_ValueFromPython() {
converter::registry::
push_back(&_convertible, &_construct,
boost::python::type_id<VtValue>());
}
private:
static void *_convertible(PyObject *obj_ptr) {
// Can always make a VtValue, but disregard wrappers. We let implicit
// conversions handle those.
if (extract<Vt_ValueWrapper>(obj_ptr).check())
return 0;
return obj_ptr;
}
static void _construct(PyObject *obj_ptr, converter::
rvalue_from_python_stage1_data *data) {
void *storage = ((converter::rvalue_from_python_storage<VtValue>*)data)
->storage.bytes;
// A big typeswitch. Note that order matters here -- the first
// one that works is the one that wins.
// Certain python objects like 'None', bool, numbers and strings are
// special-cased.
if (obj_ptr == Py_None) {
// None -> Empty VtValue.
new (storage) VtValue();
data->convertible = storage;
return;
}
if (PyBool_Check(obj_ptr)) {
// Python bool -> C++ bool.
new (storage) VtValue(bool(PyLong_AsLong(obj_ptr)));
data->convertible = storage;
return;
}
if (PyLong_Check(obj_ptr)) {
// Python long -> either c++ int or long or unsigned long or long
// long or unsigned long long or fail, depending on range.
long long val = PyLong_AsLongLong(obj_ptr);
if (!PyErr_Occurred()) {
if (std::numeric_limits<int>::min() <= val &&
val <= std::numeric_limits<int>::max()) {
new (storage) VtValue(boost::numeric_cast<int>(val));
} else if (std::numeric_limits<long>::min() <= val &&
val <= std::numeric_limits<long>::max()) {
new (storage) VtValue(boost::numeric_cast<long>(val));
} else {
new (storage) VtValue(boost::numeric_cast<long long>(val));
}
data->convertible = storage;
return;
} else {
PyErr_Clear();
// Try as unsigned long long.
unsigned long long uval = PyLong_AsUnsignedLongLong(obj_ptr);
if (!PyErr_Occurred()) {
new (storage) VtValue(uval);
data->convertible = storage;
return;
} else {
PyErr_Clear();
}
}
}
if (PyFloat_Check(obj_ptr)) {
// Py float -> c++ double.
new (storage) VtValue(double(PyFloat_AS_DOUBLE(obj_ptr)));
data->convertible = storage;
return;
}
if (PyBytes_Check(obj_ptr) || PyUnicode_Check(obj_ptr)) {
// Py string or unicode -> std::string.
new (storage) VtValue(std::string(extract<std::string>(obj_ptr)));
data->convertible = storage;
return;
}
// Attempt a registered conversion via the registry.
VtValue v = Vt_ValueFromPythonRegistry::Invoke(obj_ptr);
if (!v.IsEmpty()) {
new (storage) VtValue(v);
data->convertible = storage;
return;
} else {
// Fall back to generic python object.
new (storage)
VtValue(TfPyObjWrapper(extract<object>(obj_ptr)()));
data->convertible = storage;
return;
}
}
};
} // anonymous namespace
void wrapValue()
{
def("_test_ValueTypeName", _test_ValueTypeName);
def("_test_Ident", _test_Ident);
def("_test_Str", _test_Str);
to_python_converter<VtValue, Vt_ValueToPython>();
Vt_ValueFromPython();
Vt_ValueWrapperFromPython();
class_<Vt_ValueWrapper>("_ValueWrapper", no_init);
static char const *funcDocString = "%s(value) -> _ValueWrapper\n\n"
"value : %s\n\n"
"Use this function to specify a value with the explicit C++ type %s "
"when calling a C++ wrapped function that expects a VtValue. (There are "
"some C++ types that have no equivalents in Python, such as short.)";
def("Bool", Vt_ValueWrapper::Create<bool>,
TfStringPrintf(funcDocString, "Bool","bool","bool").c_str());
def("UChar", Vt_ValueWrapper::Create<unsigned char>,
TfStringPrintf(funcDocString, "UChar","unsigned char","unsigned char").c_str());
def("Short", Vt_ValueWrapper::Create<short>,
TfStringPrintf(funcDocString, "Short","short","short").c_str());
def("UShort", Vt_ValueWrapper::Create<unsigned short>,
TfStringPrintf(funcDocString, "UShort","unsigned short","unsigned short").c_str());
def("Int", Vt_ValueWrapper::Create<int>,
TfStringPrintf(funcDocString, "Int","int","int").c_str());
def("UInt", Vt_ValueWrapper::Create<unsigned int>,
TfStringPrintf(funcDocString, "UInt","unsigned int","unsigned int").c_str());
def("Long", Vt_ValueWrapper::Create<long>,
TfStringPrintf(funcDocString, "Long","long","long").c_str());
def("ULong", Vt_ValueWrapper::Create<unsigned long>,
TfStringPrintf(funcDocString, "ULong","unsigned long","unsigned long").c_str());
def("Int64", Vt_ValueWrapper::Create<int64_t>,
TfStringPrintf(funcDocString, "Int64","int64_t","int64_t").c_str());
def("UInt64", Vt_ValueWrapper::Create<uint64_t>,
TfStringPrintf(funcDocString, "UInt64","uint64_t","uint64_t").c_str());
def("Half", Vt_ValueWrapper::Create<GfHalf>,
TfStringPrintf(funcDocString, "Half","half","GfHalf").c_str());
def("Float", Vt_ValueWrapper::Create<float>,
TfStringPrintf(funcDocString, "Float","float","float").c_str());
def("Double", Vt_ValueWrapper::Create<double>,
TfStringPrintf(funcDocString, "Double","double","double").c_str());
// Since strings and tokens are indistiguishable in Python-land, users need to
// manually declare when they want a VtValue with a token
def("Token", Vt_ValueWrapper::Create<TfToken>,
TfStringPrintf(funcDocString, "TfToken","TfToken","TfToken").c_str());
// Register conversions for VtValue from python, but first make sure that
// nobody's registered anything before us.
if (Vt_ValueFromPythonRegistry::HasConversions()) {
TF_FATAL_ERROR("Vt was not the first library to register VtValue "
"from-python conversions!");
}
// register conversion types in reverse order, because the extractor
// iterates through the registered list backwards
// Repetitively register conversions for each known class value type.
#define REGISTER_VALUE_FROM_PYTHON(r, unused, elem) \
VtValueFromPythonLValue< VT_TYPE(elem) >();
BOOST_PP_SEQ_FOR_EACH(REGISTER_VALUE_FROM_PYTHON, ~, VT_ARRAY_VALUE_TYPES)
#undef REGISTER_VALUE_FROM_PYTHON
#define REGISTER_VALUE_FROM_PYTHON(r, unused, elem) \
VtValueFromPython< VT_TYPE(elem) >();
BOOST_PP_SEQ_FOR_EACH(REGISTER_VALUE_FROM_PYTHON, ~,
VT_SCALAR_CLASS_VALUE_TYPES VT_NONARRAY_VALUE_TYPES)
#undef REGISTER_VALUE_FROM_PYTHON
VtValueFromPython<string>();
VtValueFromPython<double>();
VtValueFromPython<int>();
// XXX: Disable rvalue conversion of TfType. It causes a mysterious
// crash and we don't need any implicit conversions.
VtValueFromPythonLValue<TfType>();
// Register conversions from sequences of VtValues
TfPyContainerConversions::from_python_sequence<
std::vector< VtValue >,
TfPyContainerConversions::variable_capacity_policy >();
// Conversions for nullary functions returning VtValue.
TfPyFunctionFromPython<VtValue ()>();
}