1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_XLA_PYTHON_ABSL_CASTERS_H_ 17 #define TENSORFLOW_COMPILER_XLA_PYTHON_ABSL_CASTERS_H_ 18 19 #include "absl/strings/string_view.h" 20 #include "absl/types/optional.h" 21 #include "absl/types/span.h" 22 #include "absl/types/variant.h" 23 #include "pybind11/cast.h" 24 #include "pybind11/pybind11.h" 25 #include "pybind11/stl.h" 26 27 namespace pybind11 { 28 namespace detail { 29 30 // absl::Span 31 template <typename T> 32 struct type_caster<absl::Span<const T>> { 33 using value_conv = make_caster<T>; 34 35 PYBIND11_TYPE_CASTER(absl::Span<const T>, 36 _("Span[") + value_conv::name + _("]")); 37 38 // absl::Span doesn't hold ownership. We therefore need a temporary array. 39 // Pybind appears to keep type_casters alive until the callee has run. 40 std::vector<T> storage; 41 42 bool load(handle src, bool convert) { 43 if (!isinstance<sequence>(src)) { 44 return false; 45 } 46 auto seq = reinterpret_borrow<sequence>(src); 47 storage.clear(); 48 storage.reserve(seq.size()); 49 for (const auto& it : seq) { 50 value_conv conv; 51 if (!conv.load(it, convert)) { 52 return false; 53 } 54 storage.push_back(cast_op<T&&>(std::move(conv))); 55 } 56 value = absl::Span<const T>(storage); 57 return true; 58 } 59 }; 60 61 // When absl::optional is an alias for std::optional, the type_caster 62 // specializations are provided by pybind11. 63 #ifndef ABSL_HAVE_STD_OPTIONAL 64 // absl::optional 65 template <typename T> 66 struct type_caster<absl::optional<T>> : optional_caster<absl::optional<T>> {}; 67 68 template <> 69 struct type_caster<absl::nullopt_t> : public void_caster<absl::nullopt_t> {}; 70 #endif 71 72 #ifndef ABSL_HAVE_STD_VARIANT 73 template <typename... Ts> 74 struct type_caster<absl::variant<Ts...>> 75 : variant_caster<absl::variant<Ts...>> {}; 76 77 #endif 78 79 // Convert between absl::string_view and python. 80 // 81 // pybind11 supports std::string_view, and absl::string_view is meant to be a 82 // drop-in replacement for std::string_view, so we can just use the built in 83 // implementation. This is only needed until absl::string_view becomes an alias 84 // for std::string_view. 85 #ifndef ABSL_USES_STD_STRING_VIEW 86 template <> 87 struct type_caster<absl::string_view> : string_caster<absl::string_view, true> { 88 }; 89 #endif 90 91 } // namespace detail 92 } // namespace pybind11 93 94 #endif // TENSORFLOW_COMPILER_XLA_PYTHON_ABSL_CASTERS_H_ 95