Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xavier Thompson
cython
Commits
cf8980bf
Commit
cf8980bf
authored
Sep 07, 2020
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable custom cypclass hash and equality methods for use in cypclass dicts
parent
93935811
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
3 deletions
+35
-3
Cython/Utility/CyObjects.cpp
Cython/Utility/CyObjects.cpp
+35
-3
No files found.
Cython/Utility/CyObjects.cpp
View file @
cf8980bf
...
...
@@ -33,6 +33,10 @@
#include <sys/syscall.h>
#include <vector>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <type_traits>
...
...
@@ -88,6 +92,18 @@
int
CyObject_TRYWLOCK
();
};
template
<
typename
T
,
typename
=
void
>
struct
Cy_has_equality
:
std
::
false_type
{};
template
<
typename
T
>
struct
Cy_has_equality
<
T
,
typename
std
::
enable_if
<
std
::
is_convertible
<
decltype
(
std
::
declval
<
T
>
().
operator
==
(
std
::
declval
<
T
*>
())
),
bool
>::
value
>::
type
>
:
std
::
true_type
{};
template
<
typename
T
,
typename
=
void
>
struct
Cy_has_hash
:
std
::
false_type
{};
template
<
typename
T
>
struct
Cy_has_hash
<
T
,
typename
std
::
enable_if
<
std
::
is_convertible
<
decltype
(
std
::
declval
<
T
>
().
__hash__
()
),
std
::
size_t
>::
value
>::
type
>
:
std
::
true_type
{};
template
<
typename
T
>
struct
Cy_Ref_impl
{
T
*
uobj
=
nullptr
;
...
...
@@ -215,9 +231,28 @@
namespace
std
{
template
<
typename
T
>
struct
hash
<
Cy_Ref_impl
<
T
>>
{
template
<
typename
U
=
T
,
typename
std
::
enable_if
<!
Cy_has_hash
<
U
>
::
value
,
int
>::
type
=
0
>
size_t
operator
()(
const
Cy_Ref_impl
<
T
>&
ref
)
const
{
static_assert
(
!
Cy_has_equality
<
U
>::
value
,
"Cypclasses that define __eq__ must also define __hash__ to be hashable"
);
return
std
::
hash
<
T
*>
()(
ref
.
uobj
);
}
template
<
typename
U
=
T
,
typename
std
::
enable_if
<
Cy_has_hash
<
U
>
::
value
,
int
>::
type
=
0
>
size_t
operator
()(
const
Cy_Ref_impl
<
T
>&
ref
)
const
{
static_assert
(
Cy_has_equality
<
U
>::
value
,
"Cypclasses that define __hash__ must also define __eq__ to be hashable"
);
return
ref
.
uobj
->
__hash__
();
}
};
template
<
typename
T
>
struct
equal_to
<
Cy_Ref_impl
<
T
>>
{
template
<
typename
U
=
T
,
typename
std
::
enable_if
<!
Cy_has_equality
<
U
>
::
value
,
int
>::
type
=
0
>
bool
operator
()(
const
Cy_Ref_impl
<
T
>&
lhs
,
const
Cy_Ref_impl
<
T
>&
rhs
)
const
{
return
lhs
.
uobj
==
rhs
.
uobj
;
}
template
<
typename
U
=
T
,
typename
std
::
enable_if
<
Cy_has_equality
<
U
>
::
value
,
int
>::
type
=
0
>
bool
operator
()(
const
Cy_Ref_impl
<
T
>&
lhs
,
const
Cy_Ref_impl
<
T
>&
rhs
)
const
{
return
lhs
.
uobj
->
operator
==
(
rhs
.
uobj
);
}
};
}
...
...
@@ -500,9 +535,6 @@
#ifdef __cplusplus
#include <cstdlib>
#include <cstddef>
#include <sstream>
#include <iostream>
#include <stdexcept>
// atomic is already included in ModuleSetupCode
// #include <atomic>
#else
...
...
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