File indexing completed on 2025-01-18 10:17:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "pybind11_tests.h"
0011
0012 TEST_SUBMODULE(enums, m) {
0013
0014 enum UnscopedEnum { EOne = 1, ETwo, EThree };
0015 py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic(), "An unscoped enumeration")
0016 .value("EOne", EOne, "Docstring for EOne")
0017 .value("ETwo", ETwo, "Docstring for ETwo")
0018 .value("EThree", EThree, "Docstring for EThree")
0019 .export_values();
0020
0021
0022 enum class ScopedEnum { Two = 2, Three };
0023 py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
0024 .value("Two", ScopedEnum::Two)
0025 .value("Three", ScopedEnum::Three);
0026
0027 m.def("test_scoped_enum", [](ScopedEnum z) {
0028 return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
0029 });
0030
0031
0032 enum Flags { Read = 4, Write = 2, Execute = 1 };
0033 py::enum_<Flags>(m, "Flags", py::arithmetic())
0034 .value("Read", Flags::Read)
0035 .value("Write", Flags::Write)
0036 .value("Execute", Flags::Execute)
0037 .export_values();
0038
0039
0040 class ClassWithUnscopedEnum {
0041 public:
0042 enum EMode { EFirstMode = 1, ESecondMode };
0043
0044 static EMode test_function(EMode mode) { return mode; }
0045 };
0046 py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
0047 exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
0048 py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
0049 .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
0050 .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
0051 .export_values();
0052
0053
0054 m.def("test_enum_to_int", [](int) {});
0055 m.def("test_enum_to_uint", [](uint32_t) {});
0056 m.def("test_enum_to_long_long", [](long long) {});
0057
0058
0059 enum SimpleEnum { ONE, TWO, THREE };
0060
0061 m.def("register_bad_enum", [m]() {
0062 py::enum_<SimpleEnum>(m, "SimpleEnum")
0063 .value("ONE", SimpleEnum::ONE)
0064
0065 .value("ONE", SimpleEnum::TWO)
0066 .value("ONE", SimpleEnum::THREE)
0067 .export_values();
0068 });
0069
0070
0071 enum UnscopedUCharEnum : unsigned char {};
0072 enum class ScopedShortEnum : short {};
0073 enum class ScopedLongEnum : long {};
0074 enum UnscopedUInt64Enum : std::uint64_t {};
0075 static_assert(
0076 py::detail::all_of<
0077 std::is_same<py::enum_<UnscopedUCharEnum>::Scalar, unsigned char>,
0078 std::is_same<py::enum_<ScopedShortEnum>::Scalar, short>,
0079 std::is_same<py::enum_<ScopedLongEnum>::Scalar, long>,
0080 std::is_same<py::enum_<UnscopedUInt64Enum>::Scalar, std::uint64_t>>::value,
0081 "Error during the deduction of enum's scalar type with normal integer underlying");
0082
0083
0084 enum class ScopedCharEnum : char { Zero, Positive };
0085 enum class ScopedWCharEnum : wchar_t { Zero, Positive };
0086 enum class ScopedChar32Enum : char32_t { Zero, Positive };
0087 enum class ScopedChar16Enum : char16_t { Zero, Positive };
0088
0089
0090
0091 static_assert(
0092 py::detail::any_of<
0093 std::is_same<py::enum_<ScopedCharEnum>::Scalar, signed char>,
0094 std::is_same<py::enum_<ScopedCharEnum>::Scalar, unsigned char>
0095 >::value,
0096 "char should be cast to either signed char or unsigned char");
0097 static_assert(sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 2
0098 || sizeof(py::enum_<ScopedWCharEnum>::Scalar) == 4,
0099 "wchar_t should be either 16 bits (Windows) or 32 (everywhere else)");
0100 static_assert(
0101 py::detail::all_of<
0102 std::is_same<py::enum_<ScopedChar32Enum>::Scalar, std::uint_least32_t>,
0103 std::is_same<py::enum_<ScopedChar16Enum>::Scalar, std::uint_least16_t>>::value,
0104 "char32_t, char16_t (and char8_t)'s size, signedness, and alignment is determined");
0105 #if defined(PYBIND11_HAS_U8STRING)
0106 enum class ScopedChar8Enum : char8_t { Zero, Positive };
0107 static_assert(std::is_same<py::enum_<ScopedChar8Enum>::Scalar, unsigned char>::value);
0108 #endif
0109
0110
0111 py::enum_<ScopedCharEnum>(m, "ScopedCharEnum")
0112 .value("Zero", ScopedCharEnum::Zero)
0113 .value("Positive", ScopedCharEnum::Positive);
0114 py::enum_<ScopedWCharEnum>(m, "ScopedWCharEnum")
0115 .value("Zero", ScopedWCharEnum::Zero)
0116 .value("Positive", ScopedWCharEnum::Positive);
0117 py::enum_<ScopedChar32Enum>(m, "ScopedChar32Enum")
0118 .value("Zero", ScopedChar32Enum::Zero)
0119 .value("Positive", ScopedChar32Enum::Positive);
0120 py::enum_<ScopedChar16Enum>(m, "ScopedChar16Enum")
0121 .value("Zero", ScopedChar16Enum::Zero)
0122 .value("Positive", ScopedChar16Enum::Positive);
0123
0124
0125 enum class ScopedBoolEnum : bool { FALSE, TRUE };
0126
0127
0128 static_assert(std::is_same<py::enum_<ScopedBoolEnum>::Scalar, std::uint8_t>::value, "");
0129
0130 py::enum_<ScopedBoolEnum>(m, "ScopedBoolEnum")
0131 .value("FALSE", ScopedBoolEnum::FALSE)
0132 .value("TRUE", ScopedBoolEnum::TRUE);
0133 }