mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-12 03:37:46 +00:00
enum comparison and conversion operations (closes #80)
This commit is contained in:
parent
a40c27ee0e
commit
15f6a0030e
@ -21,8 +21,9 @@ public:
|
|||||||
ESecondMode
|
ESecondMode
|
||||||
};
|
};
|
||||||
|
|
||||||
static void test_function(EMode mode) {
|
static EMode test_function(EMode mode) {
|
||||||
std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl;
|
std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl;
|
||||||
|
return mode;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -42,7 +43,7 @@ float test_function3(int i) {
|
|||||||
|
|
||||||
py::bytes return_bytes() {
|
py::bytes return_bytes() {
|
||||||
const char *data = "\x01\x00\x02\x00";
|
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) {
|
void print_bytes(py::bytes bytes) {
|
||||||
|
@ -20,10 +20,39 @@ print(test_function())
|
|||||||
print(test_function(7))
|
print(test_function(7))
|
||||||
print(test_function(EMyEnumeration.EFirstEntry))
|
print(test_function(EMyEnumeration.EFirstEntry))
|
||||||
print(test_function(EMyEnumeration.ESecondEntry))
|
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)
|
||||||
print(Example4.EMode.EFirstMode)
|
print(Example4.EMode.EFirstMode)
|
||||||
print(Example4.EFirstMode)
|
print(Example4.EFirstMode)
|
||||||
Example4.test_function(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())
|
print_bytes(return_bytes())
|
||||||
|
@ -10,10 +10,30 @@ test_function(enum=1)
|
|||||||
None
|
None
|
||||||
test_function(enum=2)
|
test_function(enum=2)
|
||||||
None
|
None
|
||||||
|
enum->integer = 2
|
||||||
|
integer->enum = EMyEnumeration.ESecondEntry
|
||||||
|
A constant = 14
|
||||||
<class 'example.EMode'>
|
<class 'example.EMode'>
|
||||||
EMode.EFirstMode
|
EMode.EFirstMode
|
||||||
EMode.EFirstMode
|
EMode.EFirstMode
|
||||||
Example4::test_function(enum=1)
|
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[0]=1
|
||||||
bytes[1]=0
|
bytes[1]=0
|
||||||
bytes[2]=2
|
bytes[2]=2
|
||||||
|
@ -907,7 +907,11 @@ public:
|
|||||||
((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("__int__", [](Type value) { return (int) value; });
|
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;
|
m_entries = entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user