blob: 281eafed5669b82186939f7041987dd1b366bb3e [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
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02005
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
Wenzel Jakob15f6a002016-01-24 14:05:12 +010024 static EMode test_function(EMode mode) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020025 std::cout << "Example4::test_function(enum=" << mode << ")" << std::endl;
Wenzel Jakob15f6a002016-01-24 14:05:12 +010026 return mode;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020027 }
28};
29
30bool test_function1() {
31 std::cout << "test_function()" << std::endl;
32 return false;
33}
34
Wenzel Jakob2cf192f2015-10-04 15:17:12 +020035void test_function2(EMyEnumeration k) {
36 std::cout << "test_function(enum=" << k << ")" << std::endl;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020037}
38
Wenzel Jakob2cf192f2015-10-04 15:17:12 +020039float test_function3(int i) {
40 std::cout << "test_function(" << i << ")" << std::endl;
41 return i / 2.f;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020042}
43
Wenzel Jakob27e8e102016-01-17 22:36:37 +010044py::bytes return_bytes() {
45 const char *data = "\x01\x00\x02\x00";
Wenzel Jakob15f6a002016-01-24 14:05:12 +010046 return std::string(data, 4);
Wenzel Jakob27e8e102016-01-17 22:36:37 +010047}
48
49void print_bytes(py::bytes bytes) {
50 std::string value = (std::string) bytes;
51 for (size_t i = 0; i < value.length(); ++i)
52 std::cout << "bytes[" << i << "]=" << (int) value[i] << std::endl;
53}
54
Wenzel Jakob38bd7112015-07-05 20:05:44 +020055void init_ex4(py::module &m) {
56 m.def("test_function", &test_function1);
57 m.def("test_function", &test_function2);
58 m.def("test_function", &test_function3);
59 m.attr("some_constant") = py::int_(14);
60
61 py::enum_<EMyEnumeration>(m, "EMyEnumeration")
62 .value("EFirstEntry", EFirstEntry)
63 .value("ESecondEntry", ESecondEntry)
64 .export_values();
65
66 py::class_<Example4> ex4_class(m, "Example4");
67 ex4_class.def_static("test_function", &Example4::test_function);
68 py::enum_<Example4::EMode>(ex4_class, "EMode")
69 .value("EFirstMode", Example4::EFirstMode)
70 .value("ESecondMode", Example4::ESecondMode)
71 .export_values();
Wenzel Jakob27e8e102016-01-17 22:36:37 +010072
73 m.def("return_bytes", &return_bytes);
74 m.def("print_bytes", &print_bytes);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020075}