File: PWOSequence.h

package info (click to toggle)
metakit 2.4.3-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,468 kB
  • ctags: 3,548
  • sloc: xml: 29,455; cpp: 23,339; sh: 9,051; tcl: 1,195; python: 577; makefile: 254; ansic: 14
file content (167 lines) | stat: -rwxr-xr-x 4,784 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/******************************************** 
  copyright 1999 McMillan Enterprises, Inc.
  www.mcmillan-inc.com
*********************************************/
#if !defined(PWOSEQUENCE_H_INCLUDED_)
#define PWOSEQUENCE_H_INCLUDED_

#include "PWOBase.h"

class PWOSequence : public PWOBase
{
public:
  PWOSequence() : PWOBase() {};
  PWOSequence(const PWOSequence& other) : PWOBase(other) {};
  PWOSequence(PyObject* obj) : PWOBase(obj) {
    _violentTypeCheck();
  };
  virtual ~PWOSequence() {}

  virtual PWOSequence& operator=(const PWOSequence& other) {
    GrabRef(other);
    return *this;
  };
  /*virtual*/ PWOSequence& operator=(const PWOBase& other) {
    GrabRef(other);
    _violentTypeCheck();
    return *this;
  };
  virtual void _violentTypeCheck() {
    if (!PySequence_Check(_obj)) {
      GrabRef(0);
      Fail(PyExc_TypeError, "Not a sequence");
    }
  };
  //PySequence_Concat
  PWOSequence operator+(const PWOSequence& rhs) const {
    PyObject*  rslt = PySequence_Concat(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for +");
    return LoseRef(rslt);
  };
  //PySequence_Count
  int count(const PWOBase& value) const {
    int rslt = PySequence_Count(_obj, value);
    if (rslt == -1)
      Fail(PyExc_RuntimeError, "failure in count");
    return rslt;
  };
  //PySequence_GetItem  ##lists - return PWOListMmbr (mutable) otherwise just a PWOBase
  PWOBase operator [] (int i) const { //can't be virtual
    PyObject* o = PySequence_GetItem(_obj, i);
    if (o == 0)
      Fail(PyExc_IndexError, "index out of range");
    return LoseRef(o);
  };
  //PySequence_GetSlice
  //virtual PWOSequence& operator [] (PWSlice& x) {...};
  PWOSequence getSlice(int lo, int hi) const {
    PyObject* o = PySequence_GetSlice(_obj, lo, hi);
    if (o == 0)
      Fail(PyExc_IndexError, "could not obtain slice");
    return LoseRef(o);
  };
  //PySequence_In
  bool in(const PWOBase& value) const {
    int rslt = PySequence_In(_obj, value);
    if (rslt==-1)
      Fail(PyExc_RuntimeError, "problem in in");
    return (rslt==1);
  };
  //PySequence_Index
  int index(const PWOBase& value) const {
    int rslt = PySequence_Index(_obj, value);
    if (rslt==-1)
      Fail(PyExc_IndexError, "value not found");
    return rslt;
  };
  //PySequence_Length
  int len() const {
    return PySequence_Length(_obj);
  };
  //PySequence_Repeat
  PWOSequence operator * (int count) const {
    PyObject* rslt = PySequence_Repeat(_obj, count);
    if (rslt==0)
      Fail(PyExc_RuntimeError, "sequence repeat failed");
    return LoseRef(rslt);
  };
  //PySequence_Tuple
};

class PWOList;

class PWOTuple : public PWOSequence
{
public:
  PWOTuple(int sz=0) : PWOSequence (PyTuple_New(sz))  { LoseRef(_obj); }
  PWOTuple(const PWOTuple& other) : PWOSequence(other) { }
  PWOTuple(PyObject* obj) : PWOSequence(obj) { _violentTypeCheck(); }
  PWOTuple(const PWOList& list);
  virtual ~PWOTuple() {};

  virtual PWOTuple& operator=(const PWOTuple& other) {
    GrabRef(other);
    return *this;
  };
  /*virtual*/ PWOTuple& operator=(const PWOBase& other) {
    GrabRef(other);
    _violentTypeCheck();
    return *this;
  };
  virtual void _violentTypeCheck() {
    if (!PyTuple_Check(_obj)) {
      GrabRef(0);
      Fail(PyExc_TypeError, "Not a Python Tuple");
    }
  };
  void setItem(int ndx, PWOBase& val) {
    int rslt = PyTuple_SetItem(_obj, ndx, val);
    val.disOwn(); //when using PyTuple_SetItem, he steals my reference
    if (rslt==-1)
      Fail(PyExc_IndexError, "Index out of range");
  };
};

class PWOString : public PWOSequence
{
public:
  PWOString() : PWOSequence() {};
  PWOString(const char* s)
    : PWOSequence(PyString_FromString((char* )s)) { LoseRef(_obj); }
  PWOString(const char* s, int sz)
    : PWOSequence(PyString_FromStringAndSize((char* )s, sz)) {  LoseRef(_obj); }
  PWOString(const PWOString& other)
    : PWOSequence(other) {};
  PWOString(PyObject* obj)
    : PWOSequence(obj) { _violentTypeCheck(); };
  PWOString(const PWOBase& other)
    : PWOSequence(other) { _violentTypeCheck(); };
  virtual ~PWOString() {};

  virtual PWOString& operator=(const PWOString& other) {
    GrabRef(other);
    return *this;
  };
  PWOString& operator=(const PWOBase& other) {
    GrabRef(other);
    _violentTypeCheck();
    return *this;
  };
  virtual void _violentTypeCheck() {
    if (!PyString_Check(_obj)) {
      GrabRef(0);
      Fail(PyExc_TypeError, "Not a Python String");
    }
  };
  operator const char* () const {
    return PyString_AsString(_obj);
  };
  static PWOString format(const PWOString& fmt, PWOTuple& args){
    PyObject * rslt =PyString_Format(fmt, args);
    if (rslt==0)
      Fail(PyExc_RuntimeError, "string format failed");
    return LoseRef(rslt);
  };
};
#endif // PWOSEQUENCE_H_INCLUDED_