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
4ddd65de
Commit
4ddd65de
authored
Jan 13, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
static handle cast implementations for rtrn_uqmp, rtrn_uqcp.
parent
68bee938
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
10 deletions
+44
-10
tests/test_classh_wip.cpp
+42
-8
tests/test_classh_wip.py
+2
-2
No files found.
tests/test_classh_wip.cpp
View file @
4ddd65de
...
@@ -35,7 +35,7 @@ std::string pass_mpty_shmp(std::shared_ptr<mpty> obj) { return "pass_shmp:
...
@@ -35,7 +35,7 @@ std::string pass_mpty_shmp(std::shared_ptr<mpty> obj) { return "pass_shmp:
std
::
string
pass_mpty_shcp
(
std
::
shared_ptr
<
mpty
const
>
obj
)
{
return
"pass_shcp:"
+
obj
->
mtxt
;
}
std
::
string
pass_mpty_shcp
(
std
::
shared_ptr
<
mpty
const
>
obj
)
{
return
"pass_shcp:"
+
obj
->
mtxt
;
}
std
::
unique_ptr
<
mpty
>
rtrn_mpty_uqmp
()
{
return
std
::
unique_ptr
<
mpty
>
(
new
mpty
{
"rtrn_uqmp"
});
}
std
::
unique_ptr
<
mpty
>
rtrn_mpty_uqmp
()
{
return
std
::
unique_ptr
<
mpty
>
(
new
mpty
{
"rtrn_uqmp"
});
}
std
::
unique_ptr
<
mpty
const
>
rtrn_mpty_uqcp
()
{
return
std
::
unique_ptr
<
mpty
const
>
(
new
mpty
{
"rtrn_uq
m
p"
});
}
std
::
unique_ptr
<
mpty
const
>
rtrn_mpty_uqcp
()
{
return
std
::
unique_ptr
<
mpty
const
>
(
new
mpty
{
"rtrn_uq
c
p"
});
}
std
::
string
pass_mpty_uqmp
(
std
::
unique_ptr
<
mpty
>
obj
)
{
return
"pass_uqmp:"
+
obj
->
mtxt
;
}
std
::
string
pass_mpty_uqmp
(
std
::
unique_ptr
<
mpty
>
obj
)
{
return
"pass_uqmp:"
+
obj
->
mtxt
;
}
std
::
string
pass_mpty_uqcp
(
std
::
unique_ptr
<
mpty
const
>
obj
)
{
return
"pass_uqcp:"
+
obj
->
mtxt
;
}
std
::
string
pass_mpty_uqcp
(
std
::
unique_ptr
<
mpty
const
>
obj
)
{
return
"pass_uqcp:"
+
obj
->
mtxt
;
}
...
@@ -354,9 +354,41 @@ template <>
...
@@ -354,9 +354,41 @@ template <>
struct
type_caster
<
std
::
unique_ptr
<
mpty
>>
:
smart_holder_type_caster_load
<
mpty
>
{
struct
type_caster
<
std
::
unique_ptr
<
mpty
>>
:
smart_holder_type_caster_load
<
mpty
>
{
static
constexpr
auto
name
=
_
<
std
::
unique_ptr
<
mpty
>>
();
static
constexpr
auto
name
=
_
<
std
::
unique_ptr
<
mpty
>>
();
static
handle
static
handle
cast
(
std
::
unique_ptr
<
mpty
>
&&
src
,
return_value_policy
policy
,
handle
parent
)
{
cast
(
std
::
unique_ptr
<
mpty
>
&&
/*src*/
,
return_value_policy
/*policy*/
,
handle
/*parent*/
)
{
if
(
policy
!=
return_value_policy
::
automatic
return
str
(
"cast_uqmp"
).
release
();
&&
policy
!=
return_value_policy
::
reference_internal
)
{
// IMPROVEABLE: Error message.
throw
cast_error
(
"Invalid return_value_policy for unique_ptr."
);
}
auto
src_raw_ptr
=
src
.
get
();
auto
st
=
type_caster
<
mpty
>::
src_and_type
(
src_raw_ptr
);
if
(
st
.
first
==
nullptr
)
return
none
().
release
();
// PyErr was set already.
void
*
src_raw_void_ptr
=
static_cast
<
void
*>
(
src_raw_ptr
);
const
detail
::
type_info
*
tinfo
=
st
.
second
;
auto
it_instances
=
get_internals
().
registered_instances
.
equal_range
(
src_raw_void_ptr
);
// Loop copied from type_caster_generic::cast.
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
)))
{
if
(
instance_type
&&
same_type
(
*
instance_type
->
cpptype
,
*
tinfo
->
cpptype
))
throw
cast_error
(
"Invalid unique_ptr: another instance owns this pointer already."
);
}
}
object
inst
=
reinterpret_steal
<
object
>
(
make_new_instance
(
tinfo
->
type
));
instance
*
inst_raw_ptr
=
reinterpret_cast
<
instance
*>
(
inst
.
ptr
());
inst_raw_ptr
->
owned
=
false
;
// Not actually used.
auto
smhldr
=
pybindit
::
memory
::
smart_holder
::
from_unique_ptr
(
std
::
move
(
src
));
tinfo
->
init_instance
(
inst_raw_ptr
,
static_cast
<
const
void
*>
(
&
smhldr
));
if
(
policy
==
return_value_policy
::
reference_internal
)
keep_alive_impl
(
inst
,
parent
);
return
inst
.
release
();
}
}
template
<
typename
>
template
<
typename
>
...
@@ -372,10 +404,12 @@ template <>
...
@@ -372,10 +404,12 @@ template <>
struct
type_caster
<
std
::
unique_ptr
<
mpty
const
>>
:
smart_holder_type_caster_load
<
mpty
>
{
struct
type_caster
<
std
::
unique_ptr
<
mpty
const
>>
:
smart_holder_type_caster_load
<
mpty
>
{
static
constexpr
auto
name
=
_
<
std
::
unique_ptr
<
mpty
const
>>
();
static
constexpr
auto
name
=
_
<
std
::
unique_ptr
<
mpty
const
>>
();
static
handle
cast
(
std
::
unique_ptr
<
mpty
const
>
&&
/*src*/
,
static
handle
return_value_policy
/*policy*/
,
cast
(
std
::
unique_ptr
<
mpty
const
>
&&
src
,
return_value_policy
policy
,
handle
parent
)
{
handle
/*parent*/
)
{
return
type_caster
<
std
::
unique_ptr
<
mpty
>>::
cast
(
return
str
(
"cast_uqcp"
).
release
();
std
::
unique_ptr
<
mpty
>
(
const_cast
<
mpty
*>
(
src
.
release
())),
// Const2Mutbl
policy
,
parent
);
}
}
template
<
typename
>
template
<
typename
>
...
...
tests/test_classh_wip.py
View file @
4ddd65de
...
@@ -49,8 +49,8 @@ def test_load_shared_ptr():
...
@@ -49,8 +49,8 @@ def test_load_shared_ptr():
def
test_cast_unique_ptr
():
def
test_cast_unique_ptr
():
assert
m
.
rtrn_mpty_uqmp
()
==
"cast
_uqmp"
assert
m
.
get_mtxt
(
m
.
rtrn_mpty_uqmp
())
==
"rtrn
_uqmp"
assert
m
.
rtrn_mpty_uqcp
()
==
"cast
_uqcp"
assert
m
.
get_mtxt
(
m
.
rtrn_mpty_uqcp
())
==
"rtrn
_uqcp"
def
test_load_unique_ptr
():
def
test_load_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