Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
ba21f1db
Commit
ba21f1db
authored
Sep 05, 2016
by
Wenzel Jakob
Committed by
GitHub
Sep 05, 2016
Browse files
Merge pull request #386 from wjakob/enum-fix
enum serialization support (fixes #380)
parents
70f5a4dc
8ac9715f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
9 deletions
+13
-9
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+13
-9
No files found.
include/pybind11/pybind11.h
View file @
ba21f1db
...
@@ -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