MCPcopy Create free account
hub / github.com/pybind/pybind11 / tuple_caster

Class tuple_caster

include/pybind11/cast.h:750–848  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

748// Base implementation for std::tuple and std::pair
749template <template <typename...> class Tuple, typename... Ts>
750class tuple_caster {
751 using type = Tuple<Ts...>;
752 static constexpr auto size = sizeof...(Ts);
753 using indices = make_index_sequence<size>;
754
755public:
756 bool load(handle src, bool convert) {
757 if (!isinstance<sequence>(src)) {
758 return false;
759 }
760 const auto seq = reinterpret_borrow<sequence>(src);
761 if (seq.size() != size) {
762 return false;
763 }
764 return load_impl(seq, convert, indices{});
765 }
766
767 template <typename T>
768 static handle cast(T &&src, return_value_policy policy, handle parent) {
769 return cast_impl(std::forward<T>(src), policy, parent, indices{});
770 }
771
772 // copied from the PYBIND11_TYPE_CASTER macro
773 template <typename T>
774 static handle cast(T *src, return_value_policy policy, handle parent) {
775 if (!src) {
776 return none().release();
777 }
778 if (policy == return_value_policy::take_ownership) {
779 auto h = cast(std::move(*src), policy, parent);
780 delete src;
781 return h;
782 }
783 return cast(*src, policy, parent);
784 }
785
786 static constexpr auto name = const_name("tuple[")
787 + ::pybind11::detail::concat(make_caster<Ts>::name...)
788 + const_name("]");
789
790 template <typename T>
791 using cast_op_type = type;
792
793 explicit operator type() & { return implicit_cast(indices{}); }
794 explicit operator type() && { return std::move(*this).implicit_cast(indices{}); }
795
796protected:
797 template <size_t... Is>
798 type implicit_cast(index_sequence<Is...>) & {
799 return type(cast_op<Ts>(std::get<Is>(subcasters))...);
800 }
801 template <size_t... Is>
802 type implicit_cast(index_sequence<Is...>) && {
803 return type(cast_op<Ts>(std::move(std::get<Is>(subcasters)))...);
804 }
805
806 static constexpr bool load_impl(const sequence &, bool, index_sequence<>) { return true; }
807

Callers

nothing calls this directly

Calls 4

const_nameFunction · 0.85
moveFunction · 0.85
implicit_castMethod · 0.80
concatFunction · 0.50

Tested by

no test coverage detected