enum comparison and conversion operations (closes #80)

This commit is contained in:
Wenzel Jakob 2016-01-24 14:05:12 +01:00
parent a40c27ee0e
commit 15f6a0030e
4 changed files with 56 additions and 2 deletions

View File

@ -21,8 +21,9 @@ public:
ESecondMode
};
static void test_function(EMode mode) {
static EMode test_function(EMode mode) {
std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl;
return mode;
}
};
@ -42,7 +43,7 @@ float test_function3(int i) {
py::bytes return_bytes() {
const char *data = "\x01\x00\x02\x00";
return py::bytes(std::string(data, 4));
return std::string(data, 4);
}
void print_bytes(py::bytes bytes) {

View File

@ -20,10 +20,39 @@ print(test_function())
print(test_function(7))
print(test_function(EMyEnumeration.EFirstEntry))
print(test_function(EMyEnumeration.ESecondEntry))
print("enum->integer = %i" % int(EMyEnumeration.ESecondEntry))
print("integer->enum = %s" % str(EMyEnumeration(2)))
print("A constant = " + str(some_constant))
print(Example4.EMode)
print(Example4.EMode.EFirstMode)
print(Example4.EFirstMode)
Example4.test_function(Example4.EFirstMode)
print("Equality test 1: " + str(
Example4.test_function(Example4.EFirstMode) ==
Example4.test_function(Example4.EFirstMode)))
print("Inequality test 1: " + str(
Example4.test_function(Example4.EFirstMode) !=
Example4.test_function(Example4.EFirstMode)))
print("Equality test 2: " + str(
Example4.test_function(Example4.EFirstMode) ==
Example4.test_function(Example4.ESecondMode)))
print("Inequality test 2: " + str(
Example4.test_function(Example4.EFirstMode) !=
Example4.test_function(Example4.ESecondMode)))
x = {
Example4.test_function(Example4.EFirstMode): 1,
Example4.test_function(Example4.ESecondMode): 2
}
x[Example4.test_function(Example4.EFirstMode)] = 3
x[Example4.test_function(Example4.ESecondMode)] = 4
print("Hashing test = " + str(x))
print_bytes(return_bytes())

View File

@ -10,10 +10,30 @@ test_function(enum=1)
None
test_function(enum=2)
None
enum->integer = 2
integer->enum = EMyEnumeration.ESecondEntry
A constant = 14
<class 'example.EMode'>
EMode.EFirstMode
EMode.EFirstMode
Example4::test_function(enum=1)
Example4::test_function(enum=1)
Example4::test_function(enum=1)
Equality test 1: True
Example4::test_function(enum=1)
Example4::test_function(enum=1)
Inequality test 1: False
Example4::test_function(enum=1)
Example4::test_function(enum=2)
Equality test 2: False
Example4::test_function(enum=1)
Example4::test_function(enum=2)
Inequality test 2: True
Example4::test_function(enum=1)
Example4::test_function(enum=2)
Example4::test_function(enum=1)
Example4::test_function(enum=2)
Hashing test = {EMode.EFirstMode: 3, EMode.ESecondMode: 4}
bytes[0]=1
bytes[1]=0
bytes[2]=2

View File

@ -907,7 +907,11 @@ public:
((it == entries->end()) ? std::string("???")
: std::string(it->second));
});
this->def("__init__", [](Type& value, int i) { value = (Type) i; });
this->def("__int__", [](Type value) { return (int) value; });
this->def("__eq__", [](const Type &value, Type value2) { return value == value2; });
this->def("__ne__", [](const Type &value, Type value2) { return value != value2; });
this->def("__hash__", [](const Type &value) { return (int) value; });
m_entries = entries;
}