blob: 26552b7e30b6a008d2f34a5c6e635be25cab1ee3 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02002 example/example6.cpp -- supporting Pythons' sequence protocol, iterators,
3 etc.
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
7 All rights reserved. Use of this source code is governed by a
8 BSD-style license that can be found in the LICENSE file.
9*/
10
11#include "example.h"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/operators.h>
13#include <pybind11/stl.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020014
15class Sequence {
16public:
17 Sequence(size_t size) : m_size(size) {
18 std::cout << "Value constructor: Creating a sequence with " << m_size << " entries" << std::endl;
19 m_data = new float[size];
20 memset(m_data, 0, sizeof(float) * size);
21 }
22
23 Sequence(const std::vector<float> &value) : m_size(value.size()) {
24 std::cout << "Value constructor: Creating a sequence with " << m_size << " entries" << std::endl;
25 m_data = new float[m_size];
26 memcpy(m_data, &value[0], sizeof(float) * m_size);
27 }
28
29 Sequence(const Sequence &s) : m_size(s.m_size) {
30 std::cout << "Copy constructor: Creating a sequence with " << m_size << " entries" << std::endl;
31 m_data = new float[m_size];
32 memcpy(m_data, s.m_data, sizeof(float)*m_size);
33 }
34
35 Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
36 std::cout << "Move constructor: Creating a sequence with " << m_size << " entries" << std::endl;
37 s.m_size = 0;
38 s.m_data = nullptr;
39 }
40
41 ~Sequence() {
42 std::cout << "Freeing a sequence with " << m_size << " entries" << std::endl;
43 delete[] m_data;
44 }
45
46 Sequence &operator=(const Sequence &s) {
47 std::cout << "Assignment operator: Creating a sequence with " << s.m_size << " entries" << std::endl;
48 delete[] m_data;
49 m_size = s.m_size;
50 m_data = new float[m_size];
51 memcpy(m_data, s.m_data, sizeof(float)*m_size);
52 return *this;
53 }
54
55 Sequence &operator=(Sequence &&s) {
56 std::cout << "Move assignment operator: Creating a sequence with " << s.m_size << " entries" << std::endl;
57 if (&s != this) {
58 delete[] m_data;
59 m_size = s.m_size;
60 m_data = s.m_data;
61 s.m_size = 0;
62 s.m_data = nullptr;
63 }
64 return *this;
65 }
66
67 bool operator==(const Sequence &s) const {
68 if (m_size != s.size())
69 return false;
70 for (size_t i=0; i<m_size; ++i)
71 if (m_data[i] != s[i])
72 return false;
73 return true;
74 }
75
76 bool operator!=(const Sequence &s) const {
77 return !operator==(s);
78 }
79
80 float operator[](size_t index) const {
81 return m_data[index];
82 }
83
84 float &operator[](size_t index) {
85 return m_data[index];
86 }
87
88 bool contains(float v) const {
89 for (size_t i=0; i<m_size; ++i)
90 if (v == m_data[i])
91 return true;
92 return false;
93 }
94
95 Sequence reversed() const {
96 Sequence result(m_size);
97 for (size_t i=0; i<m_size; ++i)
98 result[m_size-i-1] = m_data[i];
99 return result;
100 }
101
102 size_t size() const { return m_size; }
103
Wenzel Jakobb2825952016-04-13 23:33:00 +0200104 const float *begin() const { return m_data; }
105 const float *end() const { return m_data+m_size; }
106
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200107private:
108 size_t m_size;
109 float *m_data;
110};
111
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200112void init_ex6(py::module &m) {
113 py::class_<Sequence> seq(m, "Sequence");
114
115 seq.def(py::init<size_t>())
116 .def(py::init<const std::vector<float>&>())
117 /// Bare bones interface
118 .def("__getitem__", [](const Sequence &s, size_t i) {
119 if (i >= s.size())
120 throw py::index_error();
121 return s[i];
122 })
123 .def("__setitem__", [](Sequence &s, size_t i, float v) {
124 if (i >= s.size())
125 throw py::index_error();
126 s[i] = v;
127 })
128 .def("__len__", &Sequence::size)
129 /// Optional sequence protocol operations
Wenzel Jakobb2825952016-04-13 23:33:00 +0200130 .def("__iter__", [](const Sequence &s) { return py::make_iterator(s.begin(), s.end()); },
131 py::keep_alive<0, 1>() /* Essential: keep object alive while iterator exists */)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200132 .def("__contains__", [](const Sequence &s, float v) { return s.contains(v); })
133 .def("__reversed__", [](const Sequence &s) -> Sequence { return s.reversed(); })
134 /// Slicing protocol (optional)
135 .def("__getitem__", [](const Sequence &s, py::slice slice) -> Sequence* {
136 py::ssize_t start, stop, step, slicelength;
137 if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
138 throw py::error_already_set();
139 Sequence *seq = new Sequence(slicelength);
140 for (int i=0; i<slicelength; ++i) {
141 (*seq)[i] = s[start]; start += step;
142 }
143 return seq;
144 })
145 .def("__setitem__", [](Sequence &s, py::slice slice, const Sequence &value) {
146 py::ssize_t start, stop, step, slicelength;
147 if (!slice.compute(s.size(), &start, &stop, &step, &slicelength))
148 throw py::error_already_set();
149 if ((size_t) slicelength != value.size())
150 throw std::runtime_error("Left and right hand size of slice assignment have different sizes!");
151 for (int i=0; i<slicelength; ++i) {
152 s[start] = value[i]; start += step;
153 }
154 })
155 /// Comparisons
156 .def(py::self == py::self)
157 .def(py::self != py::self);
158 // Could also define py::self + py::self for concatenation, etc.
159
Wenzel Jakobb2825952016-04-13 23:33:00 +0200160#if 0
161 // Obsolete: special data structure for exposing custom iterator types to python
162 // kept here for illustrative purposes because there might be some use cases which
163 // are not covered by the much simpler py::make_iterator
164
165 struct PySequenceIterator {
166 PySequenceIterator(const Sequence &seq, py::object ref) : seq(seq), ref(ref) { }
167
168 float next() {
169 if (index == seq.size())
170 throw py::stop_iteration();
171 return seq[index++];
172 }
173
174 const Sequence &seq;
175 py::object ref; // keep a reference
176 size_t index = 0;
177 };
178
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200179 py::class_<PySequenceIterator>(seq, "Iterator")
180 .def("__iter__", [](PySequenceIterator &it) -> PySequenceIterator& { return it; })
181 .def("__next__", &PySequenceIterator::next);
Wenzel Jakobb2825952016-04-13 23:33:00 +0200182
183 On the actual Sequence object, the iterator would be constructed as follows:
184 .def("__iter__", [](py::object s) { return PySequenceIterator(s.cast<const Sequence &>(), s); })
185#endif
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200186}