/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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. */ #pragma once #include #include #include #include #include #include #include #include namespace apache { namespace thrift { namespace python { using capi::Bytes; using capi::list; using capi::map; using capi::set; using capi::String; template PyObject* __roundtrip_pyobject(PyObject* obj) { auto cpp = capi::Extractor{}(obj); if (cpp.hasValue()) { return capi::Constructor{}(std::move(*cpp)); } return nullptr; } inline PyObject* __roundtrip_bytes(PyObject* obj) { return __roundtrip_pyobject(obj); } inline PyObject* __roundtrip_unicode(PyObject* obj) { return __roundtrip_pyobject(obj); } template inline PyObject* __make_numeric(PyObject* obj) { if constexpr (std::is_integral_v) { auto cpp = capi::Extractor{}(obj); if (cpp.hasValue()) { return capi::Constructor{}(static_cast(*cpp)); } } else if constexpr (std::is_floating_point_v) { auto cpp = capi::Extractor{}(obj); if (cpp.hasValue()) { return capi::Constructor{}(static_cast(*cpp)); } } else { static_assert(folly::always_false, "Use for numerics only"); } } inline PyObject* __make_unicode(PyObject* bytes) { auto cpp = capi::Extractor{}(bytes); if (cpp.hasValue()) { return capi::Constructor{}(std::move(*cpp)); } return nullptr; } template inline PyObject* __roundtrip_list(PyObject* obj) { return __roundtrip_pyobject>(obj); } inline PyObject* __roundtrip_bytes_list(PyObject* obj) { return __roundtrip_pyobject>(obj); } inline PyObject* __roundtrip_unicode_list(PyObject* obj) { return __roundtrip_pyobject>(obj); } inline PyObject* __make_unicode_list(PyObject* obj) { auto cpp = capi::Extractor>{}(obj); if (cpp.hasValue()) { return capi::Constructor>{}(std::move(*cpp)); } return nullptr; } template inline PyObject* __roundtrip_set(PyObject* obj) { return __roundtrip_pyobject>(obj); } inline PyObject* __roundtrip_bytes_set(PyObject* obj) { return __roundtrip_pyobject>>(obj); } inline PyObject* __roundtrip_unicode_set(PyObject* obj) { return __roundtrip_pyobject>(obj); } inline PyObject* __make_unicode_set(PyObject* obj) { auto cpp = capi::Extractor>{}(obj); if (cpp.hasValue()) { return capi::Constructor>{}(std::move(*cpp)); } return nullptr; } template inline PyObject* __roundtrip_map(PyObject* obj) { return __roundtrip_pyobject>(obj); } template inline PyObject* __roundtrip_bytes_key_map(PyObject* obj) { return __roundtrip_pyobject>>( obj); } template inline PyObject* __roundtrip_bytes_val_map(PyObject* obj) { return __roundtrip_pyobject>(obj); } template inline PyObject* __roundtrip_unicode_key_map(PyObject* obj) { return __roundtrip_pyobject>(obj); } template inline PyObject* __roundtrip_unicode_val_map(PyObject* obj) { return __roundtrip_pyobject< map>>(obj); } inline PyObject* __make_unicode_key_map(PyObject* obj) { auto cpp = capi::Extractor>{}(obj); if (cpp.hasValue()) { return capi::Constructor>{}(std::move(*cpp)); } return nullptr; } inline PyObject* __make_unicode_val_map(PyObject* obj) { auto cpp = capi::Extractor>{}(obj); if (cpp.hasValue()) { return capi::Constructor>{}(std::move(*cpp)); } return nullptr; } } // namespace python } // namespace thrift } // namespace apache