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.

75 lines
2.0 KiB

  1. /*
  2. example/example4.cpp -- global constants and functions, enumerations, raw byte strings
  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 "example.h"
  8. enum EMyEnumeration {
  9. EFirstEntry = 1,
  10. ESecondEntry
  11. };
  12. class Example4 {
  13. public:
  14. enum EMode {
  15. EFirstMode = 1,
  16. ESecondMode
  17. };
  18. static EMode test_function(EMode mode) {
  19. std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl;
  20. return mode;
  21. }
  22. };
  23. bool test_function1() {
  24. std::cout << "test_function()" << std::endl;
  25. return false;
  26. }
  27. void test_function2(EMyEnumeration k) {
  28. std::cout << "test_function(enum=" << k << ")" << std::endl;
  29. }
  30. float test_function3(int i) {
  31. std::cout << "test_function(" << i << ")" << std::endl;
  32. return (float) i / 2.f;
  33. }
  34. py::bytes return_bytes() {
  35. const char *data = "\x01\x00\x02\x00";
  36. return std::string(data, 4);
  37. }
  38. void print_bytes(py::bytes bytes) {
  39. std::string value = (std::string) bytes;
  40. for (size_t i = 0; i < value.length(); ++i)
  41. std::cout << "bytes[" << i << "]=" << (int) value[i] << std::endl;
  42. }
  43. void init_ex4(py::module &m) {
  44. m.def("test_function", &test_function1);
  45. m.def("test_function", &test_function2);
  46. m.def("test_function", &test_function3);
  47. m.attr("some_constant") = py::int_(14);
  48. py::enum_<EMyEnumeration>(m, "EMyEnumeration")
  49. .value("EFirstEntry", EFirstEntry)
  50. .value("ESecondEntry", ESecondEntry)
  51. .export_values();
  52. py::class_<Example4> ex4_class(m, "Example4");
  53. ex4_class.def_static("test_function", &Example4::test_function);
  54. py::enum_<Example4::EMode>(ex4_class, "EMode")
  55. .value("EFirstMode", Example4::EFirstMode)
  56. .value("ESecondMode", Example4::ESecondMode)
  57. .export_values();
  58. m.def("return_bytes", &return_bytes);
  59. m.def("print_bytes", &print_bytes);
  60. }