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
6c19f6e5
Commit
6c19f6e5
authored
Aug 02, 2016
by
Wenzel Jakob
Committed by
GitHub
Aug 02, 2016
Browse files
Merge pull request #309 from lsst-dm/master
Enable comparisons between enums and their underlying types
parents
2160860c
e5b42ef1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
39 additions
and
8 deletions
+39
-8
example/example-constants-and-functions.py
example/example-constants-and-functions.py
+16
-0
example/example-constants-and-functions.ref
example/example-constants-and-functions.ref
+12
-0
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+11
-8
No files found.
example/example-constants-and-functions.py
View file @
6c19f6e5
...
@@ -46,6 +46,22 @@ print("Inequality test 2: " + str(
...
@@ -46,6 +46,22 @@ print("Inequality test 2: " + str(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
)
!=
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
)
!=
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
ESecondMode
)))
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
ESecondMode
)))
print
(
"Equality test 3: "
+
str
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
)
==
int
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
))))
print
(
"Inequality test 3: "
+
str
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
)
!=
int
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
))))
print
(
"Equality test 4: "
+
str
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
)
==
int
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
ESecondMode
))))
print
(
"Inequality test 4: "
+
str
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
)
!=
int
(
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
ESecondMode
))))
x
=
{
x
=
{
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
):
1
,
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
EFirstMode
):
1
,
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
ESecondMode
):
2
ExampleWithEnum
.
test_function
(
ExampleWithEnum
.
ESecondMode
):
2
...
...
example/example-constants-and-functions.ref
View file @
6c19f6e5
...
@@ -30,6 +30,18 @@ ExampleWithEnum::test_function(enum=1)
...
@@ -30,6 +30,18 @@ ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=2)
Inequality test 2: True
Inequality test 2: True
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
Equality test 3: True
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
Inequality test 3: False
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
Equality test 4: False
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
Inequality test 4: True
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=2)
...
...
include/pybind11/pybind11.h
View file @
6c19f6e5
...
@@ -1004,22 +1004,25 @@ private:
...
@@ -1004,22 +1004,25 @@ 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
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
<
int
,
const
char
*>
();
auto
entries
=
new
std
::
unordered_map
<
UnderlyingType
,
const
char
*>
();
this
->
def
(
"__repr__"
,
[
name
,
entries
](
Type
value
)
->
std
::
string
{
this
->
def
(
"__repr__"
,
[
name
,
entries
](
Type
value
)
->
std
::
string
{
auto
it
=
entries
->
find
((
int
)
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
,
int
i
)
{
value
=
(
Type
)
i
;
});
this
->
def
(
"__init__"
,
[](
Type
&
value
,
UnderlyingType
i
)
{
value
=
(
Type
)
i
;
});
this
->
def
(
"__init__"
,
[](
Type
&
value
,
int
i
)
{
new
(
&
value
)
Type
((
Type
)
i
);
});
this
->
def
(
"__init__"
,
[](
Type
&
value
,
UnderlyingType
i
)
{
new
(
&
value
)
Type
((
Type
)
i
);
});
this
->
def
(
"__int__"
,
[](
Type
value
)
{
return
(
int
)
value
;
});
this
->
def
(
"__int__"
,
[](
Type
value
)
{
return
(
UnderlyingType
)
value
;
});
this
->
def
(
"__eq__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
value2
&&
value
==
*
value2
;
});
this
->
def
(
"__eq__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
value2
&&
value
==
*
value2
;
});
this
->
def
(
"__eq__"
,
[](
const
Type
&
value
,
UnderlyingType
value2
)
{
return
value2
&&
value
==
value2
;
});
this
->
def
(
"__ne__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
!
value2
||
value
!=
*
value2
;
});
this
->
def
(
"__ne__"
,
[](
const
Type
&
value
,
Type
*
value2
)
{
return
!
value2
||
value
!=
*
value2
;
});
this
->
def
(
"__hash__"
,
[](
const
Type
&
value
)
{
return
(
int
)
value
;
});
this
->
def
(
"__ne__"
,
[](
const
Type
&
value
,
UnderlyingType
value2
)
{
return
value
!=
value2
;
});
this
->
def
(
"__hash__"
,
[](
const
Type
&
value
)
{
return
(
UnderlyingType
)
value
;
});
m_entries
=
entries
;
m_entries
=
entries
;
}
}
...
@@ -1036,11 +1039,11 @@ public:
...
@@ -1036,11 +1039,11 @@ public:
/// Add an enumeration entry
/// Add an enumeration entry
enum_
&
value
(
char
const
*
name
,
Type
value
)
{
enum_
&
value
(
char
const
*
name
,
Type
value
)
{
this
->
attr
(
name
)
=
pybind11
::
cast
(
value
,
return_value_policy
::
copy
);
this
->
attr
(
name
)
=
pybind11
::
cast
(
value
,
return_value_policy
::
copy
);
(
*
m_entries
)[(
int
)
value
]
=
name
;
(
*
m_entries
)[(
UnderlyingType
)
value
]
=
name
;
return
*
this
;
return
*
this
;
}
}
private:
private:
std
::
unordered_map
<
int
,
const
char
*>
*
m_entries
;
std
::
unordered_map
<
UnderlyingType
,
const
char
*>
*
m_entries
;
handle
m_parent
;
handle
m_parent
;
};
};
...
...
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