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
95b9ff57
Commit
95b9ff57
authored
Dec 14, 2020
by
Ralf W. Grosse-Kunstleve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
snapshot of first attempt to replace `void *` with `any` (messy state)
parent
940d7935
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
16 deletions
+46
-16
include/pybind11/attr.h
+1
-1
include/pybind11/cast.h
+14
-6
include/pybind11/detail/internals.h
+2
-1
include/pybind11/pybind11.h
+28
-8
tests/test_tagbased_polymorphic.cpp
+1
-0
No files found.
include/pybind11/attr.h
View file @
95b9ff57
...
...
@@ -245,7 +245,7 @@ struct type_record {
void
*
(
*
operator_new
)(
size_t
)
=
nullptr
;
/// Function pointer to class_<..>::init_instance
void
(
*
init_instance
)(
instance
*
,
const
void
*
)
=
nullptr
;
void
(
*
init_instance
)(
instance
*
,
std
::
any
)
=
nullptr
;
/// Function pointer to class_<..>::dealloc
void
(
*
dealloc
)(
detail
::
value_and_holder
&
)
=
nullptr
;
...
...
include/pybind11/cast.h
View file @
95b9ff57
...
...
@@ -14,6 +14,7 @@
#include "detail/typeid.h"
#include "detail/descr.h"
#include "detail/internals.h"
#include <any>
#include <array>
#include <limits>
#include <tuple>
...
...
@@ -232,6 +233,13 @@ struct value_and_holder {
explicit
operator
bool
()
const
{
return
xxx_value_ptr
<
void
>
();
}
template
<
typename
H
>
H
&
xxx_holder
()
const
{
#ifdef JUNK
to_cout
(
std
::
string
(
"xxx_holder<"
)
+
typeid
(
H
).
name
()
+
">"
);
if
(
std
::
string
(
typeid
(
H
).
name
())
==
"St10shared_ptrIN14pybind11_tests17holder_shared_ptr7pointeeEE"
)
{
long
*
BAD
=
nullptr
;
std
::
cout
<<
*
BAD
;
}
#endif
return
reinterpret_cast
<
H
&>
(
zzz_vh
[
1
]);
}
bool
holder_constructed
()
const
{
...
...
@@ -500,7 +508,7 @@ public:
const
detail
::
type_info
*
tinfo
,
void
*
(
*
copy_constructor
)(
const
void
*
),
void
*
(
*
move_constructor
)(
const
void
*
),
const
void
*
existing_holder
=
nullptr
)
{
std
::
any
existing_holder
=
std
::
any
{}
)
{
if
(
!
tinfo
)
// no type info: error will be set already
return
handle
();
...
...
@@ -586,7 +594,7 @@ public:
throw
cast_error
(
"unhandled return_value_policy: should not happen!"
);
}
tinfo
->
init_instance
(
wrapper
,
existing_holder
);
tinfo
->
init_instance
(
wrapper
,
existing_holder
);
// HOLDER_SHARED_MAKE_UNIQUE STACK #4
return
inst
.
release
();
}
...
...
@@ -913,10 +921,10 @@ public:
make_copy_constructor
(
src
),
make_move_constructor
(
src
));
}
static
handle
cast_holder
(
const
itype
*
src
,
const
void
*
holder
)
{
static
handle
cast_holder
(
const
itype
*
src
,
std
::
any
holder
)
{
auto
st
=
src_and_type
(
src
);
return
type_caster_generic
::
cast
(
st
.
first
,
return_value_policy
::
take_ownership
,
{},
st
.
second
,
return
type_caster_generic
::
cast
(
// HOLDER_SHARED_MAKE_UNIQUE STACK #5
st
.
first
,
return_value_policy
::
take_ownership
,
{},
st
.
second
,
// tinfo=st.second TODODODO return_value_policy::existing_holder
nullptr
,
nullptr
,
holder
);
}
...
...
@@ -1582,7 +1590,7 @@ struct move_only_holder_caster {
static
handle
cast
(
holder_type
&&
src
,
return_value_policy
,
handle
)
{
auto
*
ptr
=
holder_helper
<
holder_type
>::
yyy_get
(
src
);
// move_only_holder_caster::cast
return
type_caster_base
<
type
>::
cast_holder
(
ptr
,
std
::
addressof
(
src
));
return
type_caster_base
<
type
>::
cast_holder
(
ptr
,
std
::
addressof
(
src
));
// HOLDER_SHARED_MAKE_UNIQUE STACK #6
}
static
constexpr
auto
name
=
type_caster_base
<
type
>::
name
;
};
...
...
include/pybind11/detail/internals.h
View file @
95b9ff57
...
...
@@ -10,6 +10,7 @@
#pragma once
#include "../pytypes.h"
#include <any>
PYBIND11_NAMESPACE_BEGIN
(
PYBIND11_NAMESPACE
)
PYBIND11_NAMESPACE_BEGIN
(
detail
)
...
...
@@ -130,7 +131,7 @@ struct type_info {
const
std
::
type_info
*
cpptype
;
size_t
type_size
,
type_align
,
holder_size_in_ptrs
;
void
*
(
*
operator_new
)(
size_t
);
void
(
*
init_instance
)(
instance
*
,
const
void
*
);
void
(
*
init_instance
)(
instance
*
,
std
::
any
);
void
(
*
dealloc
)(
value_and_holder
&
v_h
);
std
::
vector
<
PyObject
*
(
*
)(
PyObject
*
,
PyTypeObject
*
)
>
implicit_conversions
;
std
::
vector
<
std
::
pair
<
const
std
::
type_info
*
,
void
*
(
*
)(
void
*
)
>>
implicit_casts
;
...
...
include/pybind11/pybind11.h
View file @
95b9ff57
...
...
@@ -158,7 +158,7 @@ protected:
"The number of argument annotations does not match the number of function arguments"
);
/* Dispatch code which converts function arguments and performs the actual function call */
rec
->
impl
=
[](
function_call
&
call
)
->
handle
{
rec
->
impl
=
[](
function_call
&
call
)
->
handle
{
// HOLDER_SHARED_MAKE_UNIQUE STACK #8
cast_in
args_converter
;
/* Try to cast the function arguments into the C++ domain */
...
...
@@ -180,7 +180,7 @@ protected:
using
Guard
=
extract_guard_t
<
Extra
...
>
;
/* Perform the function call */
handle
result
=
cast_out
::
cast
(
handle
result
=
cast_out
::
cast
(
// HOLDER_SHARED_MAKE_UNIQUE STACK #7
std
::
move
(
args_converter
).
template
call
<
Return
,
Guard
>
(
cap
->
f
),
policy
,
call
.
parent
);
/* Invoke call policy post-call hook */
...
...
@@ -713,7 +713,7 @@ protected:
// 6. Call the function.
try
{
loader_life_support
guard
{};
loader_life_support
guard
{};
// HOLDER_SHARED_MAKE_UNIQUE STACK #9
result
=
func
.
impl
(
call
);
}
catch
(
reference_cast_error
&
)
{
result
=
PYBIND11_TRY_NEXT_OVERLOAD
;
...
...
@@ -1472,19 +1472,19 @@ private:
static
void
init_holder_from_existing
(
const
detail
::
value_and_holder
&
v_h
,
const
holder_type
*
holder_ptr
,
std
::
true_type
/*is_copy_constructible*/
)
{
new
(
std
::
addressof
(
v_h
.
xxx_holder
<
holder_type
>
()))
holder_type
(
*
reinterpret_cast
<
const
holder_type
*>
(
holder_ptr
));
new
(
std
::
addressof
(
v_h
.
xxx_holder
<
holder_type
>
()))
holder_type
(
*
reinterpret_cast
<
const
holder_type
*>
(
holder_ptr
));
// HOLDER_SHARED_MAKE_UNIQUE STACK #1
}
static
void
init_holder_from_existing
(
const
detail
::
value_and_holder
&
v_h
,
const
holder_type
*
holder_ptr
,
std
::
false_type
/*is_copy_constructible*/
)
{
new
(
std
::
addressof
(
v_h
.
xxx_holder
<
holder_type
>
()))
holder_type
(
std
::
move
(
*
const_cast
<
holder_type
*>
(
holder_ptr
)));
new
(
std
::
addressof
(
v_h
.
xxx_holder
<
holder_type
>
()))
holder_type
(
std
::
move
(
*
const_cast
<
holder_type
*>
(
holder_ptr
)));
// DIRTY CONST_CAST
}
/// Initialize holder object, variant 2: try to construct from existing holder object, if possible
static
void
init_holder
(
detail
::
instance
*
inst
,
detail
::
value_and_holder
&
v_h
,
const
holder_type
*
holder_ptr
,
const
void
*
/* dummy -- not enable_shared_from_this<T>) */
)
{
if
(
holder_ptr
)
{
init_holder_from_existing
(
v_h
,
holder_ptr
,
std
::
is_copy_constructible
<
holder_type
>
());
init_holder_from_existing
(
v_h
,
holder_ptr
,
std
::
is_copy_constructible
<
holder_type
>
());
// HOLDER_SHARED_MAKE_UNIQUE STACK #2
v_h
.
set_holder_constructed
();
}
else
if
(
inst
->
aaa_owned
||
detail
::
always_construct_holder
<
holder_type
>::
value
)
{
new
(
std
::
addressof
(
v_h
.
xxx_holder
<
holder_type
>
()))
holder_type
(
v_h
.
xxx_value_ptr
<
type
>
());
// init_holder
...
...
@@ -1496,13 +1496,33 @@ private:
/// instance. Should be called as soon as the `type` value_ptr is set for an instance. Takes an
/// optional pointer to an existing holder to use; if not specified and the instance is
/// `.aaa_owned`, a new holder will be constructed to manage the value pointer.
static
void
init_instance
(
detail
::
instance
*
inst
,
const
void
*
holder_ptr
)
{
static
void
init_instance
(
detail
::
instance
*
inst
,
std
::
any
holder_ptr
)
{
// TODODODO std::any
auto
v_h
=
inst
->
get_value_and_holder
(
detail
::
get_type_info
(
typeid
(
type
)));
if
(
!
v_h
.
instance_registered
())
{
register_instance
(
inst
,
v_h
.
xxx_value_ptr
<
void
>
(),
v_h
.
type
);
// before init_holder
v_h
.
set_instance_registered
();
}
init_holder
(
inst
,
v_h
,
(
const
holder_type
*
)
holder_ptr
,
v_h
.
xxx_value_ptr
<
type
>
());
// calling init_holder
const
holder_type
*
holder_ptr_raw
=
nullptr
;
if
(
holder_ptr
.
has_value
()
&&
std
::
any_cast
<
std
::
nullptr_t
>
(
&
holder_ptr
)
==
nullptr
)
{
detail
::
to_cout
(
"before any_cast"
);
std
::
string
htn
(
holder_ptr
.
type
().
name
());
detail
::
clean_type_id
(
htn
);
detail
::
to_cout
(
std
::
string
(
"holder_ptr.type().name() "
)
+
htn
);
detail
::
to_cout
(
std
::
string
(
" typeid(holder_type *) "
)
+
type_id
<
holder_type
*>
());
auto
cast_ptr_mutbl
=
std
::
any_cast
<
holder_type
*>
(
&
holder_ptr
);
if
(
cast_ptr_mutbl
!=
nullptr
)
{
holder_ptr_raw
=
*
cast_ptr_mutbl
;
}
else
{
auto
cast_ptr_const
=
std
::
any_cast
<
const
holder_type
*>
(
&
holder_ptr
);
if
(
cast_ptr_const
==
nullptr
)
{
detail
::
to_cout
(
"any_cast mutbl + const failure"
);
throw
std
::
runtime_error
(
"Incompatible holder types."
);
}
holder_ptr_raw
=
*
cast_ptr_const
;
}
detail
::
to_cout
(
"after any_cast"
);
}
init_holder
(
inst
,
v_h
,
holder_ptr_raw
,
v_h
.
xxx_value_ptr
<
type
>
());
// calling init_holder // HOLDER_SHARED_MAKE_UNIQUE STACK #3
}
/// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
...
...
tests/test_tagbased_polymorphic.cpp
View file @
95b9ff57
...
...
@@ -80,6 +80,7 @@ std::vector<std::unique_ptr<Animal>> create_zoo()
ret
.
emplace_back
(
new
Chihuahua
(
"Hertzl"
));
ret
.
emplace_back
(
new
Cat
(
"Tiger"
,
Cat
::
Kind
::
Cat
));
ret
.
emplace_back
(
new
Panther
(
"Leo"
));
py
::
detail
::
to_cout
(
"before ret"
);
return
ret
;
}
...
...
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