|
8 | 8 | #pragma once |
9 | 9 | #include "ScriptEngineManager.h" |
10 | 10 |
|
| 11 | +using namespace std; |
| 12 | + |
11 | 13 | #define _CRT_SECURE_NO_WARNINGS |
12 | 14 |
|
13 | 15 | #define __EXPAND(x) x |
@@ -45,17 +47,128 @@ struct mspydelete { |
45 | 47 |
|
46 | 48 | // Smart pointer declaration |
47 | 49 | PYBIND11_DECLARE_HOLDER_TYPE(RefCountedBaseT, RefCountedPtr<RefCountedBaseT>, false); |
| 50 | +PYBIND11_DECLARE_HOLDER_TYPE(T, TempObjectOwner<T>, false); |
48 | 51 |
|
49 | 52 | // Non-delete smart pointer |
50 | 53 | #define DEFINE_NODELETE_HOLDER_TYPE(type) using type##Ptr = std::unique_ptr<type, py::nodelete> |
51 | 54 |
|
| 55 | +namespace std { |
| 56 | + template <typename _Ty1, typename _Ty2> |
| 57 | + struct tuple_size<Bentley::Bstdcxx::bpair<_Ty1, _Ty2>> : std::integral_constant<size_t, 2> {}; |
| 58 | + |
| 59 | + template<class _Ty1,class _Ty2> |
| 60 | + struct tuple_element<0, Bentley::Bstdcxx::bpair<_Ty1, _Ty2> > |
| 61 | + { // struct to determine type of element 0 in pair |
| 62 | + typedef _Ty1 type; |
| 63 | + }; |
| 64 | + |
| 65 | + template<class _Ty1, class _Ty2> |
| 66 | + struct tuple_element<1, Bentley::Bstdcxx::bpair<_Ty1, _Ty2> > |
| 67 | + { // struct to determine type of element 1 in pair |
| 68 | + typedef _Ty2 type; |
| 69 | + }; |
| 70 | + |
| 71 | + template <typename _Rtype, typename _Ty1, typename _Ty2> |
| 72 | + constexpr _Rtype _Pair_get(Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr, std::integral_constant<size_t, 0>) noexcept { |
| 73 | + return _Pr.first; |
| 74 | + } |
| 75 | + |
| 76 | + template <typename _Rtype, typename _Ty1, typename _Ty2> |
| 77 | + constexpr _Rtype _Pair_get(Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr, std::integral_constant<size_t, 1>) noexcept { |
| 78 | + return _Pr.second; |
| 79 | + } |
| 80 | + |
| 81 | + template<size_t _Idx, |
| 82 | + class _Ty1, |
| 83 | + class _Ty2> inline |
| 84 | + constexpr typename tuple_element<_Idx, Bentley::Bstdcxx::bpair<_Ty1, _Ty2> >::type& |
| 85 | + get (Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr) noexcept |
| 86 | + { // get reference to element at _Idx in pair _Pr |
| 87 | + typedef typename tuple_element<_Idx, pair<_Ty1, _Ty2> >::type& _Rtype; |
| 88 | + return (_Pair_get<_Rtype>(_Pr, integral_constant<size_t, _Idx>())); |
| 89 | + }; |
| 90 | + |
| 91 | + template<class _Ty1, |
| 92 | + class _Ty2> inline |
| 93 | + constexpr _Ty1& get(Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr) noexcept |
| 94 | + { // get reference to element _Ty1 in pair _Pr |
| 95 | + return (_STD get<0>(_Pr)); |
| 96 | + }; |
| 97 | + |
| 98 | + template<class _Ty2, |
| 99 | + class _Ty1> inline |
| 100 | + constexpr _Ty2& get(Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr) noexcept |
| 101 | + { // get reference to element _Ty2 in pair _Pr |
| 102 | + return (_STD get<1>(_Pr)); |
| 103 | + }; |
| 104 | + |
| 105 | + template<size_t _Idx, |
| 106 | + class _Ty1, |
| 107 | + class _Ty2> inline |
| 108 | + constexpr typename tuple_element<_Idx, Bentley::Bstdcxx::bpair<_Ty1, _Ty2> >::type&& |
| 109 | + get (Bentley::Bstdcxx::bpair<_Ty1, _Ty2>&& _Pr) noexcept |
| 110 | + { // get rvalue reference to element at _Idx in pair _Pr |
| 111 | + typedef typename tuple_element<_Idx, pair<_Ty1, _Ty2> >::type&& _RRtype; |
| 112 | + return (_STD forward<_RRtype>(_STD get<_Idx>(_Pr))); |
| 113 | + }; |
| 114 | + |
| 115 | + template<class _Ty1, |
| 116 | + class _Ty2> inline |
| 117 | + constexpr _Ty1&& get(Bentley::Bstdcxx::bpair<_Ty1, _Ty2>&& _Pr) noexcept |
| 118 | + { // get rvalue reference to element _Ty1 in pair _Pr |
| 119 | + return (_STD get<0>(_STD move(_Pr))); |
| 120 | + }; |
| 121 | + |
| 122 | + template<class _Ty2, |
| 123 | + class _Ty1> inline |
| 124 | + constexpr _Ty2&& get(Bentley::Bstdcxx::bpair<_Ty1, _Ty2>&& _Pr) noexcept |
| 125 | + { // get rvalue reference to element _Ty2 in pair _Pr |
| 126 | + return (_STD get<1>(_STD move(_Pr))); |
| 127 | + }; |
| 128 | + |
| 129 | + template<size_t _Idx, |
| 130 | + class _Ty1, |
| 131 | + class _Ty2> inline |
| 132 | + constexpr const typename tuple_element<_Idx, Bentley::Bstdcxx::bpair<_Ty1, _Ty2> >::type& |
| 133 | + get (const Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr) noexcept |
| 134 | + { // get const reference to element at _Idx in pair _Pr |
| 135 | + typedef const typename tuple_element<_Idx, pair<_Ty1, _Ty2> >::type& |
| 136 | + _Ctype; |
| 137 | + return (_Pair_get<_Ctype>(_Pr, integral_constant<size_t, _Idx>())); |
| 138 | + }; |
| 139 | + |
| 140 | + template<class _Ty1, |
| 141 | + class _Ty2> inline |
| 142 | + constexpr const _Ty1& get(const Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr) noexcept |
| 143 | + { // get const reference to element _Ty1 in pair _Pr |
| 144 | + return (_STD get<0>(_Pr)); |
| 145 | + } |
| 146 | + |
| 147 | + template<class _Ty2, |
| 148 | + class _Ty1> inline |
| 149 | + constexpr const _Ty2& get(const Bentley::Bstdcxx::bpair<_Ty1, _Ty2>& _Pr) noexcept |
| 150 | + { // get const reference to element _Ty2 in pair _Pr |
| 151 | + return (_STD get<1>(_Pr)); |
| 152 | + } |
| 153 | +} |
| 154 | + |
52 | 155 | namespace PYBIND11_NAMESPACE { |
53 | 156 | namespace detail { |
54 | 157 | template <typename T> |
55 | 158 | struct type_caster<boost::optional<T>> : optional_caster<boost::optional<T>> {}; |
| 159 | + |
| 160 | + template <typename T1, typename T2> |
| 161 | + class type_caster<Bentley::Bstdcxx::bpair<T1, T2>> : public tuple_caster<Bentley::Bstdcxx::bpair, T1, T2> {}; |
| 162 | + |
| 163 | + template <typename Key, typename Compare, typename Alloc> |
| 164 | + struct type_caster<Bentley::bset<Key, Compare, 32, Alloc>> |
| 165 | + : set_caster<Bentley::bset<Key, Compare, 32, Alloc>, Key> {}; |
56 | 166 | } |
57 | 167 | } |
58 | 168 |
|
| 169 | + |
| 170 | + |
| 171 | + |
59 | 172 | // PYBIND11_OVERRIDE with exception capture and has return value. |
60 | 173 | #define PYBIND11_OVERRIDE_EXR(ret_type, cname, fn, error, ...) \ |
61 | 174 | try \ |
@@ -215,3 +328,72 @@ py::class_<bvector<ValueType>, holder_type> bind_PointerVector(py::handle scope, |
215 | 328 |
|
216 | 329 | return cls; |
217 | 330 | } |
| 331 | + |
| 332 | +// Convert Python list to an existing C++ array |
| 333 | +template <typename arrayType, typename itemType> |
| 334 | +void ConvertPyListToCppArray(py::list const& pyList, arrayType& cppArray) |
| 335 | +{ |
| 336 | + cppArray.clear(); |
| 337 | + for (auto item : pyList) |
| 338 | + { |
| 339 | + if (!py::isinstance<itemType>(item)) |
| 340 | + { |
| 341 | + throw std::invalid_argument("All items in the list must be of the correct item type"); |
| 342 | + } |
| 343 | + auto cppItem = item.cast<itemType>(); |
| 344 | + cppArray.push_back(cppItem); |
| 345 | + } |
| 346 | +} |
| 347 | + |
| 348 | +// Convert Python list to a new C++ array |
| 349 | +template <typename arrayType, typename itemType> |
| 350 | +arrayType ConvertPyListToCppArray(py::list const& pyList) |
| 351 | +{ |
| 352 | + arrayType cppArray; |
| 353 | + for (auto item : pyList) |
| 354 | + { |
| 355 | + if (!py::isinstance<itemType>(item)) |
| 356 | + { |
| 357 | + throw std::invalid_argument("All items in the list must be of the correct item type"); |
| 358 | + } |
| 359 | + auto cppItem = item.cast<itemType>(); |
| 360 | + cppArray.push_back(cppItem); |
| 361 | + } |
| 362 | + return cppArray; |
| 363 | +} |
| 364 | + |
| 365 | +// Convert C++ array to an existing Python list |
| 366 | +template <typename arrayType, typename itemType> |
| 367 | +void ConvertCppArrayToPyList(py::list& pyList, arrayType const& cppArray) |
| 368 | +{ |
| 369 | + pyList.attr("clear")(); |
| 370 | + for (const itemType& item : cppArray) |
| 371 | + { |
| 372 | + pyList.append(py::cast(item)); |
| 373 | + } |
| 374 | +} |
| 375 | + |
| 376 | +// Convert C++ array to a new Python list |
| 377 | +template <typename arrayType, typename itemType> |
| 378 | +py::list ConvertCppArrayToPyList(arrayType const& cppArray) |
| 379 | +{ |
| 380 | + py::list pyList; |
| 381 | + for (const itemType& item : cppArray) |
| 382 | + { |
| 383 | + pyList.append(py::cast(item)); |
| 384 | + } |
| 385 | + return pyList; |
| 386 | +} |
| 387 | + |
| 388 | +// Macros |
| 389 | +#define CONVERT_PYLIST_TO_CPPARRAY(pyList, cppArray, cppArrayType, cppItemType) \ |
| 390 | + ConvertPyListToCppArray<cppArrayType, cppItemType>(pyList, cppArray); |
| 391 | + |
| 392 | +#define CONVERT_PYLIST_TO_NEW_CPPARRAY(pyList, cppArray, cppArrayType, cppItemType) \ |
| 393 | + cppArrayType cppArray = ConvertPyListToCppArray<cppArrayType, cppItemType>(pyList); |
| 394 | + |
| 395 | +#define CONVERT_CPPARRAY_TO_PYLIST(pyList, cppArray, cppArrayType, cppItemType) \ |
| 396 | + ConvertCppArrayToPyList<cppArrayType, cppItemType>(pyList, cppArray); |
| 397 | + |
| 398 | +#define CONVERT_CPPARRAY_TO_NEW_PYLIST(pyList, cppArray, cppArrayType, cppItemType) \ |
| 399 | + py::list pyList = ConvertCppArrayToPyList<cppArrayType, cppItemType>(cppArray); |
0 commit comments