blob: d7f8ad17465fdb7e6f00d432e73385855499fada [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04002 example/example-constants-and-functions.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
Jason Rhinelander61354192016-08-03 23:45:08 -040017enum class ECMyEnum {
18 Two = 2,
19 Three
20};
21
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040022class ExampleWithEnum {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020023public:
24 enum EMode {
25 EFirstMode = 1,
26 ESecondMode
27 };
28
Wenzel Jakob15f6a002016-01-24 14:05:12 +010029 static EMode test_function(EMode mode) {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040030 std::cout << "ExampleWithEnum::test_function(enum=" << mode << ")" << std::endl;
Wenzel Jakob15f6a002016-01-24 14:05:12 +010031 return mode;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020032 }
33};
34
35bool test_function1() {
36 std::cout << "test_function()" << std::endl;
37 return false;
38}
39
Wenzel Jakob2cf192f2015-10-04 15:17:12 +020040void test_function2(EMyEnumeration k) {
41 std::cout << "test_function(enum=" << k << ")" << std::endl;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020042}
43
Wenzel Jakob2cf192f2015-10-04 15:17:12 +020044float test_function3(int i) {
45 std::cout << "test_function(" << i << ")" << std::endl;
Boris Schäling20ee9352016-05-28 12:26:18 +020046 return (float) i / 2.f;
Wenzel Jakob38bd7112015-07-05 20:05:44 +020047}
48
Jason Rhinelander61354192016-08-03 23:45:08 -040049void test_ecenum(ECMyEnum z) {
50 std::cout << "test_ecenum(ECMyEnum::" << (z == ECMyEnum::Two ? "Two" : "Three") << ")" << std::endl;
51}
52
Wenzel Jakob27e8e102016-01-17 22:36:37 +010053py::bytes return_bytes() {
54 const char *data = "\x01\x00\x02\x00";
Wenzel Jakob15f6a002016-01-24 14:05:12 +010055 return std::string(data, 4);
Wenzel Jakob27e8e102016-01-17 22:36:37 +010056}
57
58void print_bytes(py::bytes bytes) {
59 std::string value = (std::string) bytes;
60 for (size_t i = 0; i < value.length(); ++i)
61 std::cout << "bytes[" << i << "]=" << (int) value[i] << std::endl;
62}
63
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040064void init_ex_constants_and_functions(py::module &m) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020065 m.def("test_function", &test_function1);
66 m.def("test_function", &test_function2);
67 m.def("test_function", &test_function3);
Jason Rhinelander61354192016-08-03 23:45:08 -040068 m.def("test_ecenum", &test_ecenum);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020069 m.attr("some_constant") = py::int_(14);
70
71 py::enum_<EMyEnumeration>(m, "EMyEnumeration")
72 .value("EFirstEntry", EFirstEntry)
73 .value("ESecondEntry", ESecondEntry)
74 .export_values();
75
Jason Rhinelander61354192016-08-03 23:45:08 -040076 py::enum_<ECMyEnum>(m, "ECMyEnum")
77 .value("Two", ECMyEnum::Two)
78 .value("Three", ECMyEnum::Three)
79 ;
80
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040081 py::class_<ExampleWithEnum> exenum_class(m, "ExampleWithEnum");
82 exenum_class.def_static("test_function", &ExampleWithEnum::test_function);
83 py::enum_<ExampleWithEnum::EMode>(exenum_class, "EMode")
84 .value("EFirstMode", ExampleWithEnum::EFirstMode)
85 .value("ESecondMode", ExampleWithEnum::ESecondMode)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020086 .export_values();
Wenzel Jakob27e8e102016-01-17 22:36:37 +010087
88 m.def("return_bytes", &return_bytes);
89 m.def("print_bytes", &print_bytes);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020090}