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
e820f34a
Commit
e820f34a
authored
Jan 11, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding rvalue_ref, renaming const_value_ref to lvalue_ref & removing const.
parent
b5b42e29
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
14 deletions
+40
-14
include/pybind11/smart_holder_poc.h
+10
-2
tests/core/smart_holder_poc_test.cpp
+30
-12
No files found.
include/pybind11/smart_holder_poc.h
View file @
e820f34a
...
...
@@ -158,14 +158,22 @@ struct smart_holder {
}
template
<
typename
T
>
const
T
&
const_
value_ref
()
const
{
static
const
char
*
context
=
"
const_
value_ref"
;
T
&
l
value_ref
()
const
{
static
const
char
*
context
=
"
l
value_ref"
;
ensure_compatible_rtti_held
<
T
>
(
context
);
ensure_has_pointee
(
context
);
return
*
static_cast
<
T
*>
(
vptr
.
get
());
}
template
<
typename
T
>
T
&&
rvalue_ref
()
const
{
static
const
char
*
context
=
"rvalue_ref"
;
ensure_compatible_rtti_held
<
T
>
(
context
);
ensure_has_pointee
(
context
);
return
std
::
move
(
*
static_cast
<
T
*>
(
vptr
.
get
()));
}
template
<
typename
T
>
static
smart_holder
from_raw_ptr_take_ownership
(
T
*
raw_ptr
)
{
smart_holder
hld
;
hld
.
rtti_held
=
&
typeid
(
T
);
...
...
tests/core/smart_holder_poc_test.cpp
View file @
e820f34a
...
...
@@ -7,6 +7,15 @@ using pybindit::memory::smart_holder;
namespace
helpers
{
struct
movable_int
{
int
valu
;
movable_int
(
int
v
)
:
valu
{
v
}
{}
movable_int
(
movable_int
&&
other
)
{
valu
=
other
.
valu
;
other
.
valu
=
91
;
}
};
template
<
typename
T
>
struct
functor_builtin_delete
{
void
operator
()(
T
*
ptr
)
{
delete
ptr
;
}
...
...
@@ -23,10 +32,20 @@ TEST_CASE("from_raw_ptr_unowned+as_raw_ptr_unowned", "[S]") {
REQUIRE
(
*
hld
.
as_raw_ptr_unowned
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_raw_ptr_unowned+
const_
value_ref"
,
"[S]"
)
{
TEST_CASE
(
"from_raw_ptr_unowned+
l
value_ref"
,
"[S]"
)
{
static
int
value
=
19
;
auto
hld
=
smart_holder
::
from_raw_ptr_unowned
(
&
value
);
REQUIRE
(
hld
.
const_value_ref
<
int
>
()
==
19
);
REQUIRE
(
hld
.
lvalue_ref
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_raw_ptr_unowned+rvalue_ref"
,
"[S]"
)
{
helpers
::
movable_int
orig
(
19
);
{
auto
hld
=
smart_holder
::
from_raw_ptr_unowned
(
&
orig
);
helpers
::
movable_int
othr
(
hld
.
rvalue_ref
<
helpers
::
movable_int
>
());
REQUIRE
(
othr
.
valu
==
19
);
REQUIRE
(
orig
.
valu
==
91
);
}
}
TEST_CASE
(
"from_raw_ptr_unowned+as_raw_ptr_release_ownership"
,
"[E]"
)
{
...
...
@@ -61,10 +80,10 @@ TEST_CASE("from_raw_ptr_unowned+as_shared_ptr", "[S]") {
REQUIRE
(
*
hld
.
as_shared_ptr
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_raw_ptr_take_ownership+
const_
value_ref"
,
"[S]"
)
{
TEST_CASE
(
"from_raw_ptr_take_ownership+
l
value_ref"
,
"[S]"
)
{
auto
hld
=
smart_holder
::
from_raw_ptr_take_ownership
(
new
int
(
19
));
REQUIRE
(
hld
.
has_pointee
());
REQUIRE
(
hld
.
const_
value_ref
<
int
>
()
==
19
);
REQUIRE
(
hld
.
l
value_ref
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_raw_ptr_take_ownership+as_raw_ptr_release_ownership1"
,
"[S]"
)
{
...
...
@@ -114,11 +133,11 @@ TEST_CASE("from_raw_ptr_take_ownership+as_shared_ptr", "[S]") {
REQUIRE
(
*
new_owner
==
19
);
}
TEST_CASE
(
"from_unique_ptr+
const_
value_ref"
,
"[S]"
)
{
TEST_CASE
(
"from_unique_ptr+
l
value_ref"
,
"[S]"
)
{
std
::
unique_ptr
<
int
>
orig_owner
(
new
int
(
19
));
auto
hld
=
smart_holder
::
from_unique_ptr
(
std
::
move
(
orig_owner
));
REQUIRE
(
orig_owner
.
get
()
==
nullptr
);
REQUIRE
(
hld
.
const_
value_ref
<
int
>
()
==
19
);
REQUIRE
(
hld
.
l
value_ref
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_unique_ptr+as_raw_ptr_release_ownership1"
,
"[S]"
)
{
...
...
@@ -180,12 +199,12 @@ TEST_CASE("from_unique_ptr+as_shared_ptr", "[S]") {
REQUIRE
(
*
new_owner
==
19
);
}
TEST_CASE
(
"from_unique_ptr_with_deleter+
const_
value_ref"
,
"[S]"
)
{
TEST_CASE
(
"from_unique_ptr_with_deleter+
l
value_ref"
,
"[S]"
)
{
std
::
unique_ptr
<
int
,
helpers
::
functor_builtin_delete
<
int
>>
orig_owner
(
new
int
(
19
));
auto
hld
=
smart_holder
::
from_unique_ptr_with_deleter
(
std
::
move
(
orig_owner
));
REQUIRE
(
orig_owner
.
get
()
==
nullptr
);
REQUIRE
(
hld
.
const_
value_ref
<
int
>
()
==
19
);
REQUIRE
(
hld
.
l
value_ref
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_unique_ptr_with_deleter+as_raw_ptr_release_ownership"
,
"[E]"
)
{
...
...
@@ -242,10 +261,10 @@ TEST_CASE("from_unique_ptr_with_deleter+as_shared_ptr", "[S]") {
REQUIRE
(
*
new_owner
==
19
);
}
TEST_CASE
(
"from_shared_ptr+
const_
value_ref"
,
"[S]"
)
{
TEST_CASE
(
"from_shared_ptr+
l
value_ref"
,
"[S]"
)
{
std
::
shared_ptr
<
int
>
orig_owner
(
new
int
(
19
));
auto
hld
=
smart_holder
::
from_shared_ptr
(
orig_owner
);
REQUIRE
(
hld
.
const_
value_ref
<
int
>
()
==
19
);
REQUIRE
(
hld
.
l
value_ref
<
int
>
()
==
19
);
}
TEST_CASE
(
"from_shared_ptr+as_raw_ptr_release_ownership"
,
"[E]"
)
{
...
...
@@ -296,6 +315,5 @@ TEST_CASE("error_incompatible_type", "[E]") {
TEST_CASE
(
"error_disowned_holder"
,
"[E]"
)
{
auto
hld
=
smart_holder
::
from_raw_ptr_take_ownership
(
new
int
(
19
));
hld
.
as_unique_ptr
<
int
>
();
REQUIRE_THROWS_WITH
(
hld
.
const_value_ref
<
int
>
(),
"Disowned holder (const_value_ref)."
);
REQUIRE_THROWS_WITH
(
hld
.
lvalue_ref
<
int
>
(),
"Disowned holder (lvalue_ref)."
);
}
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