1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/********************************************
copyright 1999 McMillan Enterprises, Inc.
www.mcmillan-inc.com
*********************************************/
#if !defined(PWOMSEQUENCE_H_INCLUDED_)
#define PWOMSEQUENCE_H_INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#include "PWOBase.h"
#include "PWOSequence.h"
class PWOList;
class PWOListMmbr : public PWOBase
{
PWOList& _parent;
int _ndx;
public:
PWOListMmbr(PyObject* obj, PWOList& parent, int ndx);
virtual ~PWOListMmbr() {};
PWOListMmbr& operator=(const PWOBase& other);
};
class PWOList : public PWOSequence
{
public:
PWOList(int size=0) : PWOSequence (PyList_New(size)) { LoseRef(_obj); }
PWOList(const PWOList& other) : PWOSequence(other) {};
PWOList(PyObject* obj) : PWOSequence(obj) {
_violentTypeCheck();
};
virtual ~PWOList() {};
virtual PWOList& operator=(const PWOList& other) {
GrabRef(other);
return *this;
};
PWOList& operator=(const PWOBase& other) {
GrabRef(other);
_violentTypeCheck();
return *this;
};
virtual void _violentTypeCheck() {
if (!PyList_Check(_obj)) { //should probably check the sequence methods for non-0 setitem
GrabRef(0);
Fail(PyExc_TypeError, "Not a mutable sequence");
}
};
//PySequence_DelItem ##lists
bool delItem(int i) {
int rslt = PySequence_DelItem(_obj, i);
if (rslt == -1)
Fail(PyExc_RuntimeError, "cannot delete item");
return true;
};
//PySequence_DelSlice ##lists
bool delSlice(int lo, int hi) {
int rslt = PySequence_DelSlice(_obj, lo, hi);
if (rslt == -1)
Fail(PyExc_RuntimeError, "cannot delete slice");
return true;
};
//PySequence_GetItem ##lists - return PWOListMmbr (mutable) otherwise just a PWOBase
PWOListMmbr operator [] (int i) { // can't be virtual
//PyObject* o = PySequence_GetItem(_obj, i); assumes item is valid
PyObject* o = PyList_GetItem(_obj, i); // get a "borrowed" refcount
//Py_XINCREF(o);
//if (o == 0)
// Fail(PyExc_IndexError, "index out of range");
return PWOListMmbr(o, *this, i); // this increfs
};
//PySequence_SetItem ##Lists
void setItem(int ndx, PWOBase& val) {
//int rslt = PySequence_SetItem(_obj, ndx, val); - assumes old item is valid
int rslt = PyList_SetItem(_obj, ndx, val);
val.disOwn(); //when using PyList_SetItem, he steals my reference
if (rslt==-1)
Fail(PyExc_IndexError, "Index out of range");
};
//PySequence_SetSlice ##Lists
void setSlice(int lo, int hi, const PWOSequence& slice) {
int rslt = PySequence_SetSlice(_obj, lo, hi, slice);
if (rslt==-1)
Fail(PyExc_RuntimeError, "Error setting slice");
};
//PyList_Append
PWOList& append(const PWOBase& other) {
int rslt = PyList_Append(_obj, other);
if (rslt==-1) {
PyErr_Clear(); //Python sets one
Fail(PyExc_RuntimeError, "Error appending");
};
return *this;
};
//PyList_AsTuple
// problem with this is it's created on the heap
//virtual PWOTuple& asTuple() const {
// PyObject* rslt = PyList_AsTuple(_obj);
// PWOTuple rtrn = new PWOTuple(rslt);
// Py_XDECREF(rslt); //AsTuple set refcnt to 1, PWOTuple(rslt) increffed
// return *rtrn;
//};
//PyList_GetItem - inherited OK
//PyList_GetSlice - inherited OK
//PyList_Insert
PWOList& insert(int ndx, PWOBase& other) {
int rslt = PyList_Insert(_obj, ndx, other);
if (rslt==-1) {
PyErr_Clear(); //Python sets one
Fail(PyExc_RuntimeError, "Error inserting");
};
return *this;
};
//PyList_New
//PyList_Reverse
PWOList& reverse() {
int rslt = PyList_Reverse(_obj);
if (rslt==-1) {
PyErr_Clear(); //Python sets one
Fail(PyExc_RuntimeError, "Error reversing");
};
return *this; //HA HA - Guido can't stop me!!!
};
//PyList_SetItem - using abstract
//PyList_SetSlice - using abstract
//PyList_Size - inherited OK
//PyList_Sort
PWOList& sort() {
int rslt = PyList_Sort(_obj);
if (rslt==-1) {
PyErr_Clear(); //Python sets one
Fail(PyExc_RuntimeError, "Error sorting");
};
return *this; //HA HA - Guido can't stop me!!!
};
};
#endif // PWOMSEQUENCE_H_INCLUDED_
|