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
8ac9715f
Commit
8ac9715f
authored
Sep 05, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enum serialization support (fixes #380)
parent
70f5a4dc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
9 deletions
+13
-9
include/pybind11/pybind11.h
+13
-9
No files found.
include/pybind11/pybind11.h
View file @
8ac9715f
...
@@ -1017,30 +1017,34 @@ private:
...
@@ -1017,30 +1017,34 @@ private:
/// Binds C++ enumerations and enumeration classes to Python
/// Binds C++ enumerations and enumeration classes to Python
template
<
typename
Type
>
class
enum_
:
public
class_
<
Type
>
{
template
<
typename
Type
>
class
enum_
:
public
class_
<
Type
>
{
public
:
public
:
using
class_
<
Type
>::
def
;
using
UnderlyingType
=
typename
std
::
underlying_type
<
Type
>::
type
;
using
UnderlyingType
=
typename
std
::
underlying_type
<
Type
>::
type
;
template
<
typename
...
Extra
>
template
<
typename
...
Extra
>
enum_
(
const
handle
&
scope
,
const
char
*
name
,
const
Extra
&
...
extra
)
enum_
(
const
handle
&
scope
,
const
char
*
name
,
const
Extra
&
...
extra
)
:
class_
<
Type
>
(
scope
,
name
,
extra
...),
m_parent
(
scope
)
{
:
class_
<
Type
>
(
scope
,
name
,
extra
...),
m_parent
(
scope
)
{
auto
entries
=
new
std
::
unordered_map
<
UnderlyingType
,
const
char
*>
();
auto
entries
=
new
std
::
unordered_map
<
UnderlyingType
,
const
char
*>
();
this
->
def
(
"__repr__"
,
[
name
,
entries
](
Type
value
)
->
std
::
string
{
def
(
"__repr__"
,
[
name
,
entries
](
Type
value
)
->
std
::
string
{
auto
it
=
entries
->
find
((
UnderlyingType
)
value
);
auto
it
=
entries
->
find
((
UnderlyingType
)
value
);
return
std
::
string
(
name
)
+
"."
+
return
std
::
string
(
name
)
+
"."
+
((
it
==
entries
->
end
())
?
std
::
string
(
"???"
)
((
it
==
entries
->
end
())
?
std
::
string
(
"???"
)
:
std
::
string
(
it
->
second
));
:
std
::
string
(
it
->
second
));
});
});
this
->
def
(
"__init__"
,
[](
Type
&
value
,
UnderlyingType
i
)
{
value
=
(
Type
)
i
;
});
def
(
"__init__"
,
[](
Type
&
value
,
UnderlyingType
i
)
{
value
=
(
Type
)
i
;
});
this
->
def
(
"__init__"
,
[](
Type
&
value
,
UnderlyingType
i
)
{
new
(
&
value
)
Type
((
Type
)
i
);
});
def
(
"__init__"
,
[](
Type
&
value
,
UnderlyingType
i
)
{
new
(
&
value
)
Type
((
Type
)
i
);
});
this
->
def
(
"__int__"
,
[](
Type
value
)
{
return
(
UnderlyingType
)
value
;
});
def
(
"__int__"
,
[](
Type
value
)
{
return
(
UnderlyingType
)
value
;
});
this
->
def
(
"__eq__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
value2
&&
value
==
*
value2
;
});
def
(
"__eq__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
value2
&&
value
==
*
value2
;
});
this
->
def
(
"__ne__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
!
value2
||
value
!=
*
value2
;
});
def
(
"__ne__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
!
value2
||
value
!=
*
value2
;
});
if
(
std
::
is_convertible
<
Type
,
UnderlyingType
>::
value
)
{
if
(
std
::
is_convertible
<
Type
,
UnderlyingType
>::
value
)
{
// Don't provide comparison with the underlying type if the enum isn't convertible,
// Don't provide comparison with the underlying type if the enum isn't convertible,
// i.e. if Type is a scoped enum, mirroring the C++ behaviour. (NB: we explicitly
// i.e. if Type is a scoped enum, mirroring the C++ behaviour. (NB: we explicitly
// convert Type to UnderlyingType below anyway because this needs to compile).
// convert Type to UnderlyingType below anyway because this needs to compile).
this
->
def
(
"__eq__"
,
[](
const
Type
&
value
,
UnderlyingType
value2
)
{
return
(
UnderlyingType
)
value
==
value2
;
});
def
(
"__eq__"
,
[](
const
Type
&
value
,
UnderlyingType
value2
)
{
return
(
UnderlyingType
)
value
==
value2
;
});
this
->
def
(
"__ne__"
,
[](
const
Type
&
value
,
UnderlyingType
value2
)
{
return
(
UnderlyingType
)
value
!=
value2
;
});
def
(
"__ne__"
,
[](
const
Type
&
value
,
UnderlyingType
value2
)
{
return
(
UnderlyingType
)
value
!=
value2
;
});
}
}
this
->
def
(
"__hash__"
,
[](
const
Type
&
value
)
{
return
(
UnderlyingType
)
value
;
});
def
(
"__hash__"
,
[](
const
Type
&
value
)
{
return
(
UnderlyingType
)
value
;
});
// Pickling and unpickling -- needed for use with the 'multiprocessing' module
def
(
"__getstate__"
,
[](
const
Type
&
value
)
{
return
pybind11
::
make_tuple
((
UnderlyingType
)
value
);
});
def
(
"__setstate__"
,
[](
Type
&
p
,
tuple
t
)
{
new
(
&
p
)
Type
((
Type
)
t
[
0
].
cast
<
UnderlyingType
>
());
});
m_entries
=
entries
;
m_entries
=
entries
;
}
}
...
...
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