13#include "detail/common.h"
20#include <unordered_map>
21#include <unordered_set>
25#if defined(PYBIND11_HAS_OPTIONAL)
27#elif defined(PYBIND11_HAS_EXP_OPTIONAL)
28# include <experimental/optional>
31#if defined(PYBIND11_HAS_VARIANT)
40template <
typename T,
typename U>
47template <
typename T,
typename U>
49 return std::forward<detail::forwarded_type<T, U>>(std::forward<U>(u));
52template <
typename Type,
typename Key>
58 if (!isinstance<pybind11::set>(src)) {
61 auto s = reinterpret_borrow<pybind11::set>(src);
63 for (
auto entry : s) {
65 if (!conv.load(entry, convert)) {
68 value.insert(cast_op<Key &&>(std::move(conv)));
75 if (!std::is_lvalue_reference<T>::value) {
79 for (
auto &&value : src) {
80 auto value_ = reinterpret_steal<object>(
82 if (!value_ || !s.add(value_)) {
92template <
typename Type,
typename Key,
typename Value>
98 if (!isinstance<dict>(src)) {
101 auto d = reinterpret_borrow<dict>(src);
106 if (!kconv.
load(it.first.ptr(), convert) || !vconv.
load(it.second.ptr(), convert)) {
109 value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
114 template <
typename T>
119 if (!std::is_lvalue_reference<T>::value) {
123 for (
auto &&kv : src) {
124 auto key = reinterpret_steal<object>(
126 auto value = reinterpret_steal<object>(
128 if (!key || !value) {
141template <
typename Type,
typename Value>
146 if (!isinstance<sequence>(src) || isinstance<bytes>(src) || isinstance<str>(src)) {
149 auto s = reinterpret_borrow<sequence>(src);
154 if (!conv.load(it, convert)) {
157 value.push_back(cast_op<Value &&>(std::move(conv)));
167 value.reserve(s.
size());
172 template <
typename T>
174 if (!std::is_lvalue_reference<T>::value) {
179 for (
auto &&value : src) {
180 auto value_ = reinterpret_steal<object>(
185 PyList_SET_ITEM(l.
ptr(), index++, value_.release().ptr());
193template <
typename Type,
typename Alloc>
196template <
typename Type,
typename Alloc>
199template <
typename Type,
typename Alloc>
202template <
typename ArrayType,
typename Value,
bool Resizable,
size_t Size = 0>
207 template <
bool R = Resizable>
209 if (value.size() != size) {
214 template <
bool R = Resizable>
221 if (!isinstance<sequence>(src)) {
224 auto l = reinterpret_borrow<sequence>(src);
231 if (!conv.load(it, convert)) {
234 value[ctr++] = cast_op<Value &&>(std::move(conv));
239 template <
typename T>
243 for (
auto &&value : src) {
244 auto value_ = reinterpret_steal<object>(
249 PyList_SET_ITEM(l.
ptr(), index++, value_.release().ptr());
262template <
typename Type,
size_t Size>
264 :
array_caster<std::array<Type, Size>, Type, false, Size> {};
266template <
typename Type>
269template <
typename Key,
typename Compare,
typename Alloc>
271 :
set_caster<std::set<Key, Compare, Alloc>, Key> {};
273template <
typename Key,
typename Hash,
typename Equal,
typename Alloc>
275 :
set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
277template <
typename Key,
typename Value,
typename Compare,
typename Alloc>
279 :
map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
281template <
typename Key,
typename Value,
typename Hash,
typename Equal,
typename Alloc>
283 :
map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
286template <
typename Type,
typename Value =
typename Type::value_type>
290 template <
typename T>
295 if (!std::is_lvalue_reference<T>::value) {
309 if (!inner_caster.
load(src, convert)) {
313 value.emplace(cast_op<Value &&>(std::move(inner_caster)));
320#if defined(PYBIND11_HAS_OPTIONAL)
328#if defined(PYBIND11_HAS_EXP_OPTIONAL)
335 :
public void_caster<std::experimental::nullopt_t> {};
345 template <
typename T>
355template <
template <
typename...>
class Variant>
357 template <
typename... Args>
358 static auto call(Args &&...
args) ->
decltype(visit(std::forward<Args>(
args)...)) {
359 return visit(std::forward<Args>(
args)...);
364template <
typename Variant>
367template <
template <
typename...>
class V,
typename... Ts>
369 static_assert(
sizeof...(Ts) > 0,
"Variant must consist of at least one alternative.");
371 template <
typename U,
typename... Us>
374 if (caster.load(src, convert)) {
375 value = cast_op<U>(caster);
394 template <
typename Variant>
397 std::forward<Variant>(src));
406#if defined(PYBIND11_HAS_VARIANT)
407template <
typename... Ts>
414#ifdef PYBIND11_HAS_STRING_VIEW
415 os <<
str(obj).
cast<std::string_view>();
417 os << (std::string)
str(obj);
\rst Holds a reference to a Python object (no reference counting)
const handle & inc_ref() const &
\rst Manually increase the reference count of the Python object.
PyObject * ptr() const
Return the underlying PyObject * pointer.
handle release()
\rst Resets the internal pointer to nullptr without decreasing the object's reference count.
static constexpr auto name
static PYBIND11_NOINLINE handle cast(const void *_src, return_value_policy policy, handle parent, const detail::type_info *tinfo, void *(*copy_constructor)(const void *), void *(*move_constructor)(const void *), const void *existing_holder=nullptr)
bool load(handle src, bool convert)
typename std::enable_if< B, T >::type enable_if_t
from cpp_future import (convenient aliases from C++14/17)
typename std::remove_reference< T >::type remove_reference_t
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
typename std::conditional< B, T, F >::type conditional_t
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
constexpr descr< N - 1 > const_name(char const (&text)[N])
conditional_t< std::is_lvalue_reference< T >::value, remove_reference_t< U > &, remove_reference_t< U > && > forwarded_type
Extracts an const lvalue reference or rvalue reference for U based on the type of T (e....
forwarded_type< T, U > forward_like(U &&u)
Forwards a value U as rvalue or lvalue according to whether T is rvalue or lvalue; typically used for...
static handle cast(T &&src, return_value_policy policy, handle parent)
bool require_size(enable_if_t<!R, size_t > size)
PYBIND11_TYPE_CASTER(ArrayType, const_name("List[")+value_conv::name+const_name< Resizable >(const_name(""), const_name("[")+const_name< Size >()+const_name("]"))+const_name("]"))
bool require_size(enable_if_t< R, size_t > size)
bool load(handle src, bool convert)
void reserve_maybe(const sequence &s, Type *)
bool load(handle src, bool convert)
static handle cast(T &&src, return_value_policy policy, handle parent)
PYBIND11_TYPE_CASTER(Type, const_name("List[")+value_conv::name+const_name("]"))
void reserve_maybe(const sequence &, void *)
static handle cast(T &&src, return_value_policy policy, handle parent)
PYBIND11_TYPE_CASTER(Type, const_name("Dict[")+key_conv::name+const_name(", ")+value_conv::name+const_name("]"))
bool load(handle src, bool convert)
static handle cast(T &&src, return_value_policy policy, handle parent)
bool load(handle src, bool convert)
PYBIND11_TYPE_CASTER(Type, const_name("Optional[")+value_conv::name+const_name("]"))
static return_value_policy policy(return_value_policy p)
static handle cast(T &&src, return_value_policy policy, handle parent)
PYBIND11_TYPE_CASTER(type, const_name("Set[")+key_conv::name+const_name("]"))
bool load(handle src, bool convert)
Helper template which holds a list of types.
bool load(handle src, bool convert)
PYBIND11_TYPE_CASTER(Type, const_name("Union[")+detail::concat(make_caster< Ts >::name...)+const_name("]"))
static handle cast(Variant &&src, return_value_policy policy, handle parent)
bool load_alternative(handle, bool, type_list<>)
bool load_alternative(handle src, bool convert, type_list< U, Us... >)
Visit a variant and cast any found type to Python.
return_value_policy policy
result_type operator()(T &&src) const
Helper class which abstracts away variant's visit function.
static auto call(Args &&...args) -> decltype(visit(std::forward< Args >(args)...))