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));
57template <
typename Type,
typename Key>
63 template <typename T = Type, enable_if_t<has_reserve_method<T>::value,
int> = 0>
65 value.reserve(s.
size());
71 if (!isinstance<anyset>(src)) {
74 auto s = reinterpret_borrow<anyset>(src);
77 for (
auto entry : s) {
79 if (!conv.load(entry, convert)) {
82 value.insert(cast_op<Key &&>(std::move(conv)));
89 if (!std::is_lvalue_reference<T>::value) {
93 for (
auto &&value : src) {
94 auto value_ = reinterpret_steal<object>(
96 if (!value_ || !s.add(std::move(value_))) {
106template <
typename Type,
typename Key,
typename Value>
112 template <typename T = Type, enable_if_t<has_reserve_method<T>::value,
int> = 0>
114 value.reserve(d.
size());
120 if (!isinstance<dict>(src)) {
123 auto d = reinterpret_borrow<dict>(src);
129 if (!kconv.
load(it.first.ptr(), convert) || !vconv.
load(it.second.ptr(), convert)) {
132 value.emplace(cast_op<Key &&>(std::move(kconv)), cast_op<Value &&>(std::move(vconv)));
137 template <
typename T>
142 if (!std::is_lvalue_reference<T>::value) {
146 for (
auto &&kv : src) {
147 auto key = reinterpret_steal<object>(
148 key_conv::cast(detail::forward_like<T>(kv.first), policy_key, parent));
149 auto value = reinterpret_steal<object>(
150 value_conv::cast(detail::forward_like<T>(kv.second), policy_value, parent));
151 if (!key || !value) {
154 d[std::move(key)] = std::move(value);
164template <
typename Type,
typename Value>
169 if (!isinstance<sequence>(src) || isinstance<bytes>(src) || isinstance<str>(src)) {
172 auto s = reinterpret_borrow<sequence>(src);
177 if (!conv.load(it, convert)) {
180 value.push_back(cast_op<Value &&>(std::move(conv)));
186 template <typename T = Type, enable_if_t<has_reserve_method<T>::value,
int> = 0>
188 value.reserve(s.
size());
193 template <
typename T>
195 if (!std::is_lvalue_reference<T>::value) {
200 for (
auto &&value : src) {
201 auto value_ = reinterpret_steal<object>(
206 PyList_SET_ITEM(l.
ptr(), index++, value_.release().ptr());
214template <
typename Type,
typename Alloc>
217template <
typename Type,
typename Alloc>
220template <
typename Type,
typename Alloc>
223template <
typename ArrayType,
typename Value,
bool Resizable,
size_t Size = 0>
228 template <
bool R = Resizable>
230 if (value.size() != size) {
235 template <
bool R = Resizable>
242 if (!isinstance<sequence>(src)) {
245 auto l = reinterpret_borrow<sequence>(src);
252 if (!conv.load(it, convert)) {
255 value[ctr++] = cast_op<Value &&>(std::move(conv));
260 template <
typename T>
264 for (
auto &&value : src) {
265 auto value_ = reinterpret_steal<object>(
270 PyList_SET_ITEM(l.
ptr(), index++, value_.release().ptr());
283template <
typename Type,
size_t Size>
285 :
array_caster<std::array<Type, Size>, Type, false, Size> {};
287template <
typename Type>
290template <
typename Key,
typename Compare,
typename Alloc>
292 :
set_caster<std::set<Key, Compare, Alloc>, Key> {};
294template <
typename Key,
typename Hash,
typename Equal,
typename Alloc>
296 :
set_caster<std::unordered_set<Key, Hash, Equal, Alloc>, Key> {};
298template <
typename Key,
typename Value,
typename Compare,
typename Alloc>
300 :
map_caster<std::map<Key, Value, Compare, Alloc>, Key, Value> {};
302template <
typename Key,
typename Value,
typename Hash,
typename Equal,
typename Alloc>
303struct type_caster<
std::unordered_map<Key, Value, Hash, Equal, Alloc>>
304 :
map_caster<std::unordered_map<Key, Value, Hash, Equal, Alloc>, Key, Value> {};
307template <
typename Type,
typename Value =
typename Type::value_type>
311 template <
typename T>
316 if (!std::is_lvalue_reference<T>::value) {
330 if (!inner_caster.
load(src, convert)) {
334 value.emplace(cast_op<Value &&>(std::move(inner_caster)));
341#if defined(PYBIND11_HAS_OPTIONAL)
349#if defined(PYBIND11_HAS_EXP_OPTIONAL)
356 :
public void_caster<std::experimental::nullopt_t> {};
366 template <
typename T>
376template <
template <
typename...>
class Variant>
378 template <
typename... Args>
379 static auto call(Args &&...
args) ->
decltype(visit(std::forward<Args>(
args)...)) {
380 return visit(std::forward<Args>(
args)...);
385template <
typename Variant>
388template <
template <
typename...>
class V,
typename... Ts>
390 static_assert(
sizeof...(Ts) > 0,
"Variant must consist of at least one alternative.");
392 template <
typename U,
typename... Us>
395 if (caster.load(src, convert)) {
396 value = cast_op<U>(std::move(caster));
415 template <
typename Variant>
418 std::forward<Variant>(src));
427#if defined(PYBIND11_HAS_VARIANT)
428template <
typename... Ts>
438#ifdef PYBIND11_HAS_STRING_VIEW
439 os <<
str(obj).
cast<std::string_view>();
441 os << (std::string)
str(obj);
\rst Holds a reference to a Python object (no reference counting)
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...
std::is_same< decltype(std::declval< C >().reserve(0)), void > has_reserve_method
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)
void reserve_maybe(const sequence &s, Type *)
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 *)
void reserve_maybe(const dict &d, Type *)
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)
void reserve_maybe(const dict &, void *)
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)
void reserve_maybe(const anyset &s, Type *)
PYBIND11_TYPE_CASTER(type, const_name("Set[")+key_conv::name+const_name("]"))
bool load(handle src, bool convert)
void reserve_maybe(const anyset &, void *)
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)...))