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
ec86f100
Commit
ec86f100
authored
Jan 15, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Using factored-out make_constructor (PR #2798), removing duplicate code.
parent
8d18c1e1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
52 deletions
+28
-52
include/pybind11/cast.h
+26
-23
tests/test_classh_wip.cpp
+2
-29
No files found.
include/pybind11/cast.h
View file @
ec86f100
...
...
@@ -824,6 +824,30 @@ template <typename Container> struct is_copy_assignable<Container, enable_if_t<a
template
<
typename
T1
,
typename
T2
>
struct
is_copy_assignable
<
std
::
pair
<
T1
,
T2
>>
:
all_of
<
is_copy_assignable
<
T1
>
,
is_copy_assignable
<
T2
>>
{};
// Helper for type_caster_base.
struct
make_constructor
{
using
Constructor
=
void
*
(
*
)(
const
void
*
);
/* Only enabled when the types are {copy,move}-constructible *and* when the type
does not have a private operator new implementation. */
template
<
typename
T
,
typename
=
enable_if_t
<
is_copy_constructible
<
T
>::
value
>>
static
auto
make_copy_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
*
x
),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
*
reinterpret_cast
<
const
T
*>
(
arg
));
};
}
template
<
typename
T
,
typename
=
enable_if_t
<
std
::
is_move_constructible
<
T
>::
value
>>
static
auto
make_move_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
x
))),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
reinterpret_cast
<
const
T
*>
(
arg
))));
};
}
static
Constructor
make_copy_constructor
(...)
{
return
nullptr
;
}
static
Constructor
make_move_constructor
(...)
{
return
nullptr
;
}
};
PYBIND11_NAMESPACE_END
(
detail
)
// polymorphic_type_hook<itype>::get(src, tinfo) determines whether the object pointed
...
...
@@ -866,7 +890,8 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {};
PYBIND11_NAMESPACE_BEGIN
(
detail
)
/// Generic type caster for objects stored on the heap
template
<
typename
type
>
class
type_caster_base
:
public
type_caster_generic
{
template
<
typename
type
>
class
type_caster_base
:
public
type_caster_generic
,
protected
make_constructor
{
using
itype
=
intrinsic_t
<
type
>
;
public
:
...
...
@@ -927,28 +952,6 @@ public:
operator
itype
*
()
{
return
(
type
*
)
value
;
}
operator
itype
&
()
{
if
(
!
value
)
throw
reference_cast_error
();
return
*
((
itype
*
)
value
);
}
protected
:
using
Constructor
=
void
*
(
*
)(
const
void
*
);
/* Only enabled when the types are {copy,move}-constructible *and* when the type
does not have a private operator new implementation. */
template
<
typename
T
,
typename
=
enable_if_t
<
is_copy_constructible
<
T
>::
value
>>
static
auto
make_copy_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
*
x
),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
*
reinterpret_cast
<
const
T
*>
(
arg
));
};
}
template
<
typename
T
,
typename
=
enable_if_t
<
std
::
is_move_constructible
<
T
>::
value
>>
static
auto
make_move_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
x
))),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
reinterpret_cast
<
const
T
*>
(
arg
))));
};
}
static
Constructor
make_copy_constructor
(...)
{
return
nullptr
;
}
static
Constructor
make_move_constructor
(...)
{
return
nullptr
;
}
};
template
<
typename
type
,
typename
SFINAE
=
void
>
class
type_caster
:
public
type_caster_base
<
type
>
{
};
...
...
tests/test_classh_wip.cpp
View file @
ec86f100
...
...
@@ -123,8 +123,8 @@ struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
policy
,
parent
,
st
.
second
,
make_copy_constructor
(
src
),
make_move_constructor
(
src
));
make_co
nstructor
::
make_co
py_constructor
(
src
),
make_
constructor
::
make_
move_constructor
(
src
));
}
static
handle
cast
(
mpty
*
src
,
return_value_policy
policy
,
handle
parent
)
{
...
...
@@ -156,33 +156,6 @@ struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
// clang-format on
// type_caster_base BEGIN
// clang-format off
using
Constructor
=
void
*
(
*
)(
const
void
*
);
/* Only enabled when the types are {copy,move}-constructible *and* when the type
does not have a private operator new implementation. */
template
<
typename
T
,
typename
=
enable_if_t
<
is_copy_constructible
<
T
>::
value
>>
static
auto
make_copy_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
*
x
),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
*
reinterpret_cast
<
const
T
*>
(
arg
));
};
}
template
<
typename
T
,
typename
=
enable_if_t
<
std
::
is_move_constructible
<
T
>::
value
>>
static
auto
make_move_constructor
(
const
T
*
x
)
->
decltype
(
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
x
))),
Constructor
{})
{
return
[](
const
void
*
arg
)
->
void
*
{
return
new
T
(
std
::
move
(
*
const_cast
<
T
*>
(
reinterpret_cast
<
const
T
*>
(
arg
))));
};
}
static
Constructor
make_copy_constructor
(...)
{
return
nullptr
;
}
static
Constructor
make_move_constructor
(...)
{
return
nullptr
;
}
// clang-format on
// type_caster_base END
// Originally type_caster_generic::cast.
PYBIND11_NOINLINE
static
handle
cast_const_raw_ptr
(
const
void
*
_src
,
return_value_policy
policy
,
...
...
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