Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
76a090e3
Commit
76a090e3
authored
Jan 25, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reusing type_caster_base make_copy_constructor, make_move_constructor with a trick.
parent
02c4e24b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
30 deletions
+9
-30
include/pybind11/detail/smart_holder_type_casters.h
+9
-30
No files found.
include/pybind11/detail/smart_holder_type_casters.h
View file @
76a090e3
...
@@ -23,7 +23,7 @@ namespace detail {
...
@@ -23,7 +23,7 @@ namespace detail {
inline
std
::
pair
<
bool
,
handle
>
find_existing_python_instance
(
void
*
src_void_ptr
,
inline
std
::
pair
<
bool
,
handle
>
find_existing_python_instance
(
void
*
src_void_ptr
,
const
detail
::
type_info
*
tinfo
)
{
const
detail
::
type_info
*
tinfo
)
{
// Loop copied from type_caster_generic::cast.
// Loop copied from type_caster_generic::cast.
// IMPROV
E
ABLE: Factor out of type_caster_generic::cast.
// IMPROVABLE: Factor out of type_caster_generic::cast.
auto
it_instances
=
get_internals
().
registered_instances
.
equal_range
(
src_void_ptr
);
auto
it_instances
=
get_internals
().
registered_instances
.
equal_range
(
src_void_ptr
);
for
(
auto
it_i
=
it_instances
.
first
;
it_i
!=
it_instances
.
second
;
++
it_i
)
{
for
(
auto
it_i
=
it_instances
.
first
;
it_i
!=
it_instances
.
second
;
++
it_i
)
{
for
(
auto
instance_type
:
detail
::
all_type_info
(
Py_TYPE
(
it_i
->
second
)))
{
for
(
auto
instance_type
:
detail
::
all_type_info
(
Py_TYPE
(
it_i
->
second
)))
{
...
@@ -54,7 +54,7 @@ public:
...
@@ -54,7 +54,7 @@ public:
void
load_value_and_holder
(
value_and_holder
&&
v_h
)
{
void
load_value_and_holder
(
value_and_holder
&&
v_h
)
{
loaded_v_h
=
std
::
move
(
v_h
);
loaded_v_h
=
std
::
move
(
v_h
);
if
(
!
loaded_v_h
.
holder_constructed
())
{
if
(
!
loaded_v_h
.
holder_constructed
())
{
// IMPROV
E
ABLE: Error message. A change to the existing internals is
// IMPROVABLE: Error message. A change to the existing internals is
// needed to cleanly distinguish between uninitialized or disowned.
// needed to cleanly distinguish between uninitialized or disowned.
throw
std
::
runtime_error
(
"Missing value for wrapped C++ type:"
throw
std
::
runtime_error
(
"Missing value for wrapped C++ type:"
" Python instance is uninitialized or was disowned."
);
" Python instance is uninitialized or was disowned."
);
...
@@ -307,33 +307,12 @@ private:
...
@@ -307,33 +307,12 @@ private:
}
}
};
};
// type_caster_base BEGIN
// IMPROVABLE: Formally factor out of type_caster_base.
// clang-format off
struct
make_constructor
:
private
type_caster_base
<
int
>
{
// Any type, nothing special about int.
// Helper factored out of type_caster_base.
using
type_caster_base
<
int
>::
Constructor
;
struct
make_constructor
{
using
type_caster_base
<
int
>::
make_copy_constructor
;
using
Constructor
=
void
*
(
*
)(
const
void
*
);
using
type_caster_base
<
int
>::
make_move_constructor
;
/* Only enabled when the types are {copy,move}-constructible *and* when the type
does not have a private operator new implementation. */
template
<
typename
T
,
typename
=
enable_if_t
<
is_copy_constructible
<
T
>::
value
>>
static
auto
make_copy_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
*
x
),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
*
reinterpret_cast
<
const
T
*>
(
arg
));
};
}
template
<
typename
T
,
typename
=
enable_if_t
<
std
::
is_move_constructible
<
T
>::
value
>>
static
auto
make_move_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
x
))),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
reinterpret_cast
<
const
T
*>
(
arg
))));
};
}
static
Constructor
make_copy_constructor
(...)
{
return
nullptr
;
}
static
Constructor
make_move_constructor
(...)
{
return
nullptr
;
}
};
};
// clang-format on
// type_caster_base END
template
<
typename
T
>
template
<
typename
T
>
struct
smart_holder_type_caster
:
smart_holder_type_caster_load
<
T
>
{
struct
smart_holder_type_caster
:
smart_holder_type_caster_load
<
T
>
{
...
@@ -500,7 +479,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
...
@@ -500,7 +479,7 @@ struct smart_holder_type_caster<std::shared_ptr<T>> : smart_holder_type_caster_l
static
handle
cast
(
const
std
::
shared_ptr
<
T
>
&
src
,
return_value_policy
policy
,
handle
parent
)
{
static
handle
cast
(
const
std
::
shared_ptr
<
T
>
&
src
,
return_value_policy
policy
,
handle
parent
)
{
if
(
policy
!=
return_value_policy
::
automatic
if
(
policy
!=
return_value_policy
::
automatic
&&
policy
!=
return_value_policy
::
reference_internal
)
{
&&
policy
!=
return_value_policy
::
reference_internal
)
{
// IMPROV
E
ABLE: Error message.
// IMPROVABLE: Error message.
throw
cast_error
(
"Invalid return_value_policy for shared_ptr."
);
throw
cast_error
(
"Invalid return_value_policy for shared_ptr."
);
}
}
...
@@ -563,7 +542,7 @@ struct smart_holder_type_caster<std::unique_ptr<T>> : smart_holder_type_caster_l
...
@@ -563,7 +542,7 @@ struct smart_holder_type_caster<std::unique_ptr<T>> : smart_holder_type_caster_l
static
handle
cast
(
std
::
unique_ptr
<
T
>
&&
src
,
return_value_policy
policy
,
handle
parent
)
{
static
handle
cast
(
std
::
unique_ptr
<
T
>
&&
src
,
return_value_policy
policy
,
handle
parent
)
{
if
(
policy
!=
return_value_policy
::
automatic
if
(
policy
!=
return_value_policy
::
automatic
&&
policy
!=
return_value_policy
::
reference_internal
)
{
&&
policy
!=
return_value_policy
::
reference_internal
)
{
// IMPROV
E
ABLE: Error message.
// IMPROVABLE: Error message.
throw
cast_error
(
"Invalid return_value_policy for unique_ptr."
);
throw
cast_error
(
"Invalid return_value_policy for unique_ptr."
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment