Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
72d1b611
Commit
72d1b611
authored
Jan 05, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Snapshot of WIP, TODO: shared_ptr deleter with on/off switch
parent
76e99f1c
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
115 additions
and
0 deletions
+115
-0
include/pybind11/smart_holder_poc.h
include/pybind11/smart_holder_poc.h
+75
-0
tests/test_smart_holder_poc.cpp
tests/test_smart_holder_poc.cpp
+32
-0
tests/test_smart_holder_poc.py
tests/test_smart_holder_poc.py
+8
-0
No files found.
include/pybind11/smart_holder_poc.h
0 → 100644
View file @
72d1b611
#pragma once
#include <memory>
#include <typeinfo>
PYBIND11_NAMESPACE_BEGIN
(
PYBIND11_NAMESPACE
)
struct
smart_holder
{
std
::
shared_ptr
<
void
>
vptr
;
const
std
::
type_info
*
rtti_held
;
const
std
::
type_info
*
rtti_uqp_del
;
bool
have_external_shp
;
smart_holder
()
:
rtti_held
{
nullptr
},
rtti_uqp_del
{
nullptr
},
have_external_shp
(
false
)
{}
template
<
typename
T
>
void
ensure_compatible_rtti
(
const
char
*
context
)
{
const
std
::
type_info
*
rtti_requested
=
&
typeid
(
T
);
if
(
!
(
*
rtti_requested
==
*
rtti_held
))
{
throw
std
::
runtime_error
(
std
::
string
(
"Incompatible RTTI ("
)
+
context
+
")."
);
}
}
void
ensure_use_count_1
(
const
char
*
context
)
{
if
(
vptr
.
use_count
()
!=
1
)
{
throw
std
::
runtime_error
(
std
::
string
(
"Cannot disown use_count != 1 ("
)
+
context
+
")."
);
}
}
void
ensure_unique_ptr_default_deleter
(
const
char
*
context
)
{
if
(
rtti_uqp_del
!=
nullptr
)
{
throw
std
::
runtime_error
(
std
::
string
(
"Cannot disown unique_ptr deleter ("
)
+
context
+
")."
);
}
}
void
ensure_internal_shared_ptr
(
const
char
*
context
)
{
if
(
have_external_shp
)
{
throw
std
::
runtime_error
(
std
::
string
(
"Cannot disown external shared_ptr ("
)
+
context
+
")."
);
}
}
template
<
typename
T
>
void
from_raw_ptr_owned
(
T
*
raw_ptr
)
{
vptr
.
reset
(
raw_ptr
);
rtti_held
=
&
typeid
(
T
);
}
template
<
typename
T
>
T
*
as_raw_ptr_owned
()
{
static
const
char
*
context
=
"as_raw_ptr_owned"
;
ensure_compatible_rtti
<
T
>
(
context
);
ensure_use_count_1
(
context
);
ensure_unique_ptr_default_deleter
(
context
);
ensure_internal_shared_ptr
(
context
);
std
::
shared_ptr
<
T
>
tptr
=
std
::
static_pointer_cast
<
T
>
(
vptr
);
vptr
.
reset
();
T
*
result
=
tptr
.
get
();
// TODO tptr.release();
return
result
;
}
template
<
typename
T
>
std
::
shared_ptr
<
T
>
as_shared_ptr
()
{
static
const
char
*
context
=
"as_shared_ptr"
;
ensure_compatible_rtti
<
T
>
(
context
);
return
std
::
static_pointer_cast
<
T
>
(
vptr
);
}
};
PYBIND11_NAMESPACE_END
(
PYBIND11_NAMESPACE
)
tests/test_smart_holder_poc.cpp
0 → 100644
View file @
72d1b611
#include "pybind11_tests.h"
#include <pybind11/smart_holder_poc.h>
#include <iostream>
#include <string>
namespace
pybind11_tests
{
namespace
smart_holder_poc
{
inline
void
to_cout
(
std
::
string
msg
)
{
std
::
cout
<<
msg
<<
std
::
endl
;
}
inline
void
exercise
()
{
to_cout
(
""
);
namespace
py
=
pybind11
;
py
::
smart_holder
hld
;
hld
.
from_raw_ptr_owned
(
new
int
(
13
));
to_cout
(
hld
.
rtti_held
->
name
());
{
std
::
shared_ptr
<
int
>
val
=
hld
.
as_shared_ptr
<
int
>
();
to_cout
(
std
::
to_string
(
*
val
));
}
{
std
::
unique_ptr
<
int
>
val
(
hld
.
as_raw_ptr_owned
<
int
>
());
to_cout
(
std
::
to_string
(
*
val
));
}
}
TEST_SUBMODULE
(
smart_holder_poc
,
m
)
{
m
.
def
(
"exercise"
,
exercise
);
}
}
// namespace smart_holder_poc
}
// namespace pybind11_tests
tests/test_smart_holder_poc.py
0 → 100644
View file @
72d1b611
# -*- coding: utf-8 -*-
import
pytest
from
pybind11_tests
import
smart_holder_poc
as
m
def
test_exercise
():
m
.
exercise
()
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