blob: b71856cbe39fc0f500010a54a0dd9e0b62022262 [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 Rhinelanderb3f3d792016-07-18 16:43:18 -040017class ExampleWithEnum {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020018public:
19 enum EMode {
20 EFirstMode = 1,
21 ESecondMode
22 };
23
Wenzel Jakob15f6a002016-01-24 14:05:12 +010024 static EMode test_function(EMode mode) {
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040025 std::cout << "ExampleWithEnum::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;
Boris Schäling20ee9352016-05-28 12:26:18 +020041 return (float) 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
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040055void init_ex_constants_and_functions(py::module &m) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020056 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
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040066 py::class_<ExampleWithEnum> exenum_class(m, "ExampleWithEnum");
67 exenum_class.def_static("test_function", &ExampleWithEnum::test_function);
68 py::enum_<ExampleWithEnum::EMode>(exenum_class, "EMode")
69 .value("EFirstMode", ExampleWithEnum::EFirstMode)
70 .value("ESecondMode", ExampleWithEnum::ESecondMode)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020071 .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}