You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

2 months ago
  1. /*
  2. tests/test_enums.cpp -- enumerations
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. enum UnscopedEnum {
  9. EOne = 1,
  10. ETwo
  11. };
  12. enum class ScopedEnum {
  13. Two = 2,
  14. Three
  15. };
  16. enum Flags {
  17. Read = 4,
  18. Write = 2,
  19. Execute = 1
  20. };
  21. class ClassWithUnscopedEnum {
  22. public:
  23. enum EMode {
  24. EFirstMode = 1,
  25. ESecondMode
  26. };
  27. static EMode test_function(EMode mode) {
  28. return mode;
  29. }
  30. };
  31. std::string test_scoped_enum(ScopedEnum z) {
  32. return "ScopedEnum::" + std::string(z == ScopedEnum::Two ? "Two" : "Three");
  33. }
  34. test_initializer enums([](py::module &m) {
  35. m.def("test_scoped_enum", &test_scoped_enum);
  36. py::enum_<UnscopedEnum>(m, "UnscopedEnum", py::arithmetic())
  37. .value("EOne", EOne)
  38. .value("ETwo", ETwo)
  39. .export_values();
  40. py::enum_<ScopedEnum>(m, "ScopedEnum", py::arithmetic())
  41. .value("Two", ScopedEnum::Two)
  42. .value("Three", ScopedEnum::Three);
  43. py::enum_<Flags>(m, "Flags", py::arithmetic())
  44. .value("Read", Flags::Read)
  45. .value("Write", Flags::Write)
  46. .value("Execute", Flags::Execute)
  47. .export_values();
  48. py::class_<ClassWithUnscopedEnum> exenum_class(m, "ClassWithUnscopedEnum");
  49. exenum_class.def_static("test_function", &ClassWithUnscopedEnum::test_function);
  50. py::enum_<ClassWithUnscopedEnum::EMode>(exenum_class, "EMode")
  51. .value("EFirstMode", ClassWithUnscopedEnum::EFirstMode)
  52. .value("ESecondMode", ClassWithUnscopedEnum::ESecondMode)
  53. .export_values();
  54. m.def("test_enum_to_int", [](int) { });
  55. m.def("test_enum_to_uint", [](uint32_t) { });
  56. m.def("test_enum_to_long_long", [](long long) { });
  57. });