resonance 1.0.1
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1#pragma once
2
3#include "script/cxx/config.h"
6#include "script/cxx/object.h"
8
9namespace Py {
10
21class List : public SeqBase<Object> {
22public:
32 explicit List(int nSize = 0) : SeqBase<Object>(FromAPI(PyTuple_New(0)).mPtr) {
33 set(FromAPI(PyList_New(nSize)).mPtr);
34 validate();
35 for (int i = 0; i < nSize; ++i) {
36 Py_INCREF(Py_None);
37 if (PyList_SetItem(mPtr, i, Py_None) == -1) {
38 throw Exception();
39 }
40 }
41 }
42
51 explicit List(const Object &ob) : SeqBase<Object>(ob) {
52 validate();
53 }
54
63 virtual bool accepts(PyObject *pyob) const {
64 return pyob != nullptr && PyList_Check(pyob);
65 }
66
74 virtual int capacity() const {
75 return max_size();
76 }
77
85 void append(const Object &ob) {
86 if (PyList_Append(mPtr, ob.mPtr) == -1) {
87 throw Exception();
88 }
89 }
90};
91
92} // 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
List(const Object &ob)
Take another handle's reference as a list.
Definition list.h:51
List(int nSize=0)
Create a list of a given length.
Definition list.h:32
virtual bool accepts(PyObject *pyob) const
Accept only a list.
Definition list.h:63
void append(const Object &ob)
Append one element.
Definition list.h:85
virtual int capacity() const
Report the sentinel rather than the length, as Py::String does.
Definition list.h:74
Reference-counted handle on any Python object, and the root of the PyCXX object hierarchy.
Definition object.h:42
void set(PyObject *pyob)
Replace the reference with a borrowed one and add a count of its own.
Definition object.h:215
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
virtual int max_size() const
SeqBase()
Definition seqbase.h:47
Configuration header of the PyCXX binding layer.
C++ wrappers for the embedded Python interpreter's objects, types, and extension modules.
Definition attributeerror.h:7