Commit 15f6a003 authored by Wenzel Jakob's avatar Wenzel Jakob
Browse files

enum comparison and conversion operations (closes #80)

parent a40c27ee
......@@ -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) {
......
......@@ -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())
......@@ -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
......
......@@ -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;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment