resonance 1.0.1
Loading...
Searching...
No Matches
seqbase.h
Go to the documentation of this file.
1#pragma once
2
3#include "script/cxx/config.h"
5#include "script/cxx/object.h"
6#include "script/cxx/seqref.h"
7
8namespace Py {
9
37template <typename T>
38class SeqBase : public Object {
39protected:
48 }
49
58 explicit SeqBase(PyObject *pyob) : Object(pyob) {
59 validate();
60 }
61
62public:
73 explicit SeqBase(const Object &ob) : Object(ob) {
74 validate();
75 }
76
87 virtual bool accepts(PyObject *pyob) const {
88 return pyob != nullptr && PySequence_Check(pyob) != 0;
89 }
90
109 virtual int max_size() const;
110
123 virtual int capacity() const {
124 return size();
125 }
126
140 virtual void swap(SeqBase<T> &other);
141
157 virtual int size() const;
158
172 virtual T getItem(int i) const {
173 return T(FromAPI(PySequence_GetItem(mPtr, i)).mPtr);
174 }
175
186 virtual void setItem(int i, const T &value) {
187 if (PySequence_SetItem(mPtr, i, value.mPtr) == -1) {
188 throw Exception();
189 }
190 }
191
200 int length() const {
201 return PySequence_Length(mPtr);
202 }
203
213 return seqref<T>(*this, i);
214 }
215
225 class iterator {
226 public:
233 iterator(SeqBase<T> *pSequence, int nWhere) : mSequence(pSequence), mCount(nWhere) {
234 }
235
242 bool operator==(const iterator &other) const {
243 return mSequence == other.mSequence && mCount == other.mCount;
244 }
245
252 bool operator!=(const iterator &other) const {
253 return mSequence != other.mSequence || mCount != other.mCount;
254 }
255
262 return seqref<T>(*mSequence, mCount);
263 }
264
271 ++mCount;
272 return *this;
273 }
274
275 private:
276 SeqBase<T> *mSequence;
277 int mCount;
278 };
279
285 iterator begin() {
286 return iterator(this, 0);
287 }
288
294 iterator end() {
295 return iterator(this, length());
296 }
297};
298
301
302} // namespace Py
Root of the PyCXX exception hierarchy.
Definition exception.h:19
Temporary owner of a new reference returned by the CPython API.
Definition fromapi.h:25
Object(PyObject *pyob=Py_None)
Take a borrowed reference and add a count of its own.
Definition object.h:52
void validate()
Enforce this type's accepts() test on the stored reference.
Definition object.h:237
PyObject * mPtr
Wrapped reference, null only between release() and the next assignment.
Definition object.h:191
seqref< T > operator*()
Address the element at this position.
Definition seqbase.h:261
bool operator==(const iterator &other) const
Report whether two iterators address the same position of the same sequence.
Definition seqbase.h:242
iterator & operator++()
Step to the next position.
Definition seqbase.h:270
bool operator!=(const iterator &other) const
Report whether two iterators address different positions.
Definition seqbase.h:252
iterator(SeqBase< T > *pSequence, int nWhere)
Address one position.
Definition seqbase.h:233
Handle on any Python sequence, and the base of Py::List, Py::Tuple, and Py::String.
Definition seqbase.h:38
virtual T getItem(int i) const
Read one element.
Definition seqbase.h:172
iterator begin()
Address the first element.
Definition seqbase.h:285
virtual void setItem(int i, const T &value)
Write one element.
Definition seqbase.h:186
int length() const
Number of elements, read without the table.
Definition seqbase.h:200
SeqBase(PyObject *pyob)
Take a borrowed reference on behalf of a derived class.
Definition seqbase.h:58
virtual int size() const
Number of elements.
seqref< T > operator[](int i)
Address one element.
Definition seqbase.h:212
virtual int max_size() const
Longest sequence this handle could address.
virtual bool accepts(PyObject *pyob) const
Accept any reference the sequence protocol supports.
Definition seqbase.h:87
virtual void swap(SeqBase< T > &other)
Exchange references with another handle of the same kind.
iterator end()
Address one past the last element.
Definition seqbase.h:294
SeqBase(const Object &ob)
Take a new reference to an existing object as a sequence.
Definition seqbase.h:73
SeqBase()
Produce a handle on None for a derived class that fills it in afterwards.
Definition seqbase.h:47
virtual int capacity() const
Elements this handle could address without reallocating, which is its length.
Definition seqbase.h:123
Proxy for one element of a sequence, which SeqBase::operator[]() returns.
Definition seqref.h:19
Configuration header of the PyCXX binding layer.
C++ wrappers for the embedded Python interpreter's objects, types, and extension modules.
Definition attributeerror.h:7
SeqBase< Object > Sequence
The sequence handle over plain objects, as released PyCXX spells it.
Definition seqbase.h:300