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
2d463aa3
"git@developer.sourcefind.cn:gaoqiong/pybind11.git" did not exist on "4e27f7bb13f041b81ef6b180dcbdd0979c6619ee"
Commit
2d463aa3
authored
Jan 23, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Purging obsolete pybind11/vptr_holder.h and associated test.
parent
d659a141
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
0 additions
and
192 deletions
+0
-192
include/pybind11/vptr_holder.h
include/pybind11/vptr_holder.h
+0
-77
tests/test_variant_unique_shared.cpp
tests/test_variant_unique_shared.cpp
+0
-54
tests/test_variant_unique_shared.py
tests/test_variant_unique_shared.py
+0
-61
No files found.
include/pybind11/vptr_holder.h
deleted
100644 → 0
View file @
d659a141
#pragma once
#include <pybind11/pybind11.h>
#include <iostream>
#include <memory>
#include <variant>
PYBIND11_NAMESPACE_BEGIN
(
PYBIND11_NAMESPACE
)
// Could this be a holder for a `class_`-like `vclass`?
// To enable passing of unique_ptr as in pure C++.
template
<
typename
T
>
class
vptr
{
public:
explicit
vptr
(
T
*
ptr
=
nullptr
)
:
vptr_
{
std
::
unique_ptr
<
T
>
(
ptr
)}
{
std
::
cout
<<
std
::
endl
<<
"explicit vptr(T *ptr = nullptr)"
<<
std
::
endl
;
//TRIGGER_SEGSEV
}
explicit
vptr
(
std
::
unique_ptr
<
T
>
u
)
:
vptr_
{
std
::
move
(
u
)}
{
std
::
cout
<<
std
::
endl
<<
"explicit vptr(std::unique_ptr<T> u)"
<<
std
::
endl
;
}
explicit
vptr
(
std
::
shared_ptr
<
T
>
s
)
:
vptr_
{
s
}
{
std
::
cout
<<
std
::
endl
<<
"explicit vptr(std::shared_ptr<T> s)"
<<
std
::
endl
;
}
int
ownership_type
()
const
{
if
(
std
::
get_if
<
0
>
(
&
vptr_
))
{
return
0
;
}
if
(
std
::
get_if
<
1
>
(
&
vptr_
))
{
return
1
;
}
return
-
1
;
}
T
*
get
()
{
std
::
cout
<<
std
::
endl
<<
"vptr::get"
<<
std
::
endl
;
auto
u
=
std
::
get_if
<
0
>
(
&
vptr_
);
if
(
u
)
{
return
u
->
get
();
}
auto
s
=
std
::
get_if
<
1
>
(
&
vptr_
);
if
(
s
)
{
return
s
->
get
();
}
return
nullptr
;
}
std
::
unique_ptr
<
T
>
get_unique
()
{
auto
u
=
std
::
get_if
<
0
>
(
&
vptr_
);
if
(
u
)
{
return
std
::
move
(
*
u
);
}
throw
std
::
runtime_error
(
"get_unique failure."
);
}
std
::
shared_ptr
<
T
>
get_shared
()
{
auto
s
=
std
::
get_if
<
1
>
(
&
vptr_
);
if
(
s
)
{
return
*
s
;
}
auto
u
=
std
::
get_if
<
0
>
(
&
vptr_
);
if
(
u
)
{
auto
result
=
std
::
shared_ptr
<
T
>
(
std
::
move
(
*
u
));
vptr_
=
result
;
return
result
;
}
throw
std
::
runtime_error
(
"get_shared failure."
);
}
private:
std
::
variant
<
std
::
unique_ptr
<
T
>
,
std
::
shared_ptr
<
T
>>
vptr_
;
};
template
<
typename
T
>
class
vptr_holder
:
public
vptr
<
T
>
{
using
vptr
<
T
>::
vptr
;
// GET_STACK -1
};
PYBIND11_NAMESPACE_END
(
PYBIND11_NAMESPACE
)
PYBIND11_DECLARE_HOLDER_TYPE
(
T
,
pybind11
::
vptr_holder
<
T
>
);
tests/test_variant_unique_shared.cpp
deleted
100644 → 0
View file @
d659a141
#include "pybind11_tests.h"
#include <pybind11/vptr_holder.h>
#include <memory>
#include <variant>
namespace
pybind11_tests
{
using
pybind11
::
vptr
;
vptr
<
double
>
from_raw
()
{
return
vptr
<
double
>
{
new
double
{
3
}};
}
vptr
<
double
>
from_unique
()
{
return
vptr
<
double
>
{
std
::
unique_ptr
<
double
>
(
new
double
{
5
})};
}
vptr
<
double
>
from_shared
()
{
return
vptr
<
double
>
{
std
::
shared_ptr
<
double
>
(
new
double
{
7
})};
}
TEST_SUBMODULE
(
variant_unique_shared
,
m
)
{
m
.
def
(
"from_raw"
,
from_raw
);
m
.
def
(
"from_unique"
,
from_unique
);
m
.
def
(
"from_shared"
,
from_shared
);
py
::
class_
<
vptr
<
double
>>
(
m
,
"vptr_double"
)
.
def
(
py
::
init
<>
())
.
def
(
"ownership_type"
,
&
vptr
<
double
>::
ownership_type
)
.
def
(
"get_value"
,
[](
vptr
<
double
>
&
v
)
{
auto
p
=
v
.
get
();
if
(
p
)
return
*
p
;
return
-
1.
;
})
.
def
(
"get_unique"
,
[](
vptr
<
double
>
&
v
)
{
v
.
get_unique
();
return
;
})
.
def
(
"get_shared"
,
[](
vptr
<
double
>
&
v
)
{
v
.
get_shared
();
return
;
})
.
def
(
"disown_unique"
,
[](
vptr
<
double
>
&
v
)
{
v
.
get_unique
().
reset
();
return
;
});
}
}
// namespace pybind11_tests
tests/test_variant_unique_shared.py
deleted
100644 → 0
View file @
d659a141
# -*- coding: utf-8 -*-
import
pytest
from
pybind11_tests
import
variant_unique_shared
as
m
def
test_default_constructed
():
v
=
m
.
vptr_double
()
assert
v
.
ownership_type
()
==
0
assert
v
.
get_value
()
==
-
1
def
test_from_raw
():
v
=
m
.
from_raw
()
assert
v
.
ownership_type
()
==
0
assert
v
.
get_value
()
==
3
def
test_from_unique
():
v
=
m
.
from_unique
()
assert
v
.
ownership_type
()
==
0
assert
v
.
get_value
()
==
5
def
test_from_shared
():
v
=
m
.
from_shared
()
assert
v
.
ownership_type
()
==
1
assert
v
.
get_value
()
==
7
def
test_promotion_to_shared
():
v
=
m
.
from_raw
()
v
.
get_unique
()
assert
v
.
ownership_type
()
==
0
v
.
get_shared
()
# Promotion to shared_ptr.
assert
v
.
ownership_type
()
==
1
v
.
get_shared
()
# Existing shared_ptr.
with
pytest
.
raises
(
RuntimeError
)
as
exc_info
:
v
.
get_unique
()
assert
str
(
exc_info
.
value
)
==
"get_unique failure."
v
.
get_shared
()
# Still works.
def
test_shared_from_birth
():
v
=
m
.
from_shared
()
assert
v
.
ownership_type
()
==
1
with
pytest
.
raises
(
RuntimeError
)
as
exc_info
:
v
.
get_unique
()
assert
str
(
exc_info
.
value
)
==
"get_unique failure."
v
.
get_shared
()
# Still works.
def
test_promotion_of_disowned_to_shared
():
v
=
m
.
from_unique
()
assert
v
.
get_value
()
==
5
v
.
disown_unique
()
assert
v
.
ownership_type
()
==
0
assert
v
.
get_value
()
==
-
1
v
.
get_shared
()
# Promotion of disowned to shared_ptr.
assert
v
.
ownership_type
()
==
1
assert
v
.
get_value
()
==
-
1
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