blob: 9865931aaec1bc8036674555fa64d51951086226 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakob27e8e102016-01-17 22:36:37 +01002 example/example4.cpp -- global constants and functions, enumerations, raw byte strings
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
4 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE file.
8*/
9
10#include "example.h"
11
12enum EMyEnumeration {
13 EFirstEntry = 1,
14 ESecondEntry
15};
16
17class Example4 {
18public:
19 enum EMode {
20 EFirstMode = 1,
21 ESecondMode
22 };
23
24 static void test_function(EMode mode) {
25 std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl;
26 }
27};
28
29bool test_function1() {
30 std::cout << "test_function()" << std::endl;
31 return false;
32}
33
Wenzel Jakob2cf192f2015-10-04 15:17:12 +020034void test_function2(EMyEnumeration k) {
35 std::cout << "test_function(enum=" << k << ")" << std::endl;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020036}
37
Wenzel Jakob2cf192f2015-10-04 15:17:12 +020038float test_function3(int i) {
39 std::cout << "test_function(" << i << ")" << std::endl;
40 return i / 2.f;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020041}
42
Wenzel Jakob27e8e102016-01-17 22:36:37 +010043py::bytes return_bytes() {
44 const char *data = "\x01\x00\x02\x00";
45 return py::bytes(std::string(data, 4));
46}
47
48void print_bytes(py::bytes bytes) {
49 std::string value = (std::string) bytes;
50 for (size_t i = 0; i < value.length(); ++i)
51 std::cout << "bytes[" << i << "]=" << (int) value[i] << std::endl;
52}
53
Wenzel Jakob38bd7112015-07-05 20:05:44 +020054void init_ex4(py::module &m) {
55 m.def("test_function", &test_function1);
56 m.def("test_function", &test_function2);
57 m.def("test_function", &test_function3);
58 m.attr("some_constant") = py::int_(14);
59
60 py::enum_<EMyEnumeration>(m, "EMyEnumeration")
61 .value("EFirstEntry", EFirstEntry)
62 .value("ESecondEntry", ESecondEntry)
63 .export_values();
64
65 py::class_<Example4> ex4_class(m, "Example4");
66 ex4_class.def_static("test_function", &Example4::test_function);
67 py::enum_<Example4::EMode>(ex4_class, "EMode")
68 .value("EFirstMode", Example4::EFirstMode)
69 .value("ESecondMode", Example4::ESecondMode)
70 .export_values();
Wenzel Jakob27e8e102016-01-17 22:36:37 +010071
72 m.def("return_bytes", &return_bytes);
73 m.def("print_bytes", &print_bytes);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020074}