File: PWONumber.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 (180 lines) | stat: -rwxr-xr-x 5,285 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
168
169
170
171
172
173
174
175
176
177
178
179
180
/******************************************** 
  copyright 1999 McMillan Enterprises, Inc.
  www.mcmillan-inc.com
*********************************************/
#if !defined(PWONUMBER_H_INCLUDED_)
#define PWONUMBER_H_INCLUDED_

#include "PWOBase.h"
#include "PWOSequence.h"

class PWONumber : public PWOBase
{
public:
  PWONumber() : PWOBase() {};
  PWONumber(int i) : PWOBase (PyInt_FromLong(i)) { LoseRef(_obj); }
  PWONumber(long i) : PWOBase (PyInt_FromLong(i)) { LoseRef(_obj); }
  PWONumber(unsigned long i) : PWOBase (PyLong_FromUnsignedLong(i)) { LoseRef(_obj); }
  PWONumber(double d) : PWOBase (PyFloat_FromDouble(d)) { LoseRef(_obj); }

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

  virtual PWONumber& operator=(const PWONumber& other) {
    GrabRef(other);
    return *this;
  };
  /*virtual*/ PWONumber& operator=(const PWOBase& other) {
    GrabRef(other);
    _violentTypeCheck();
    return *this;
  };
  virtual void _violentTypeCheck() {
    if (!PyNumber_Check(_obj)) {
      GrabRef(0);
      Fail(PyExc_TypeError, "Not a number");
    }
  };
  //PyNumber_Absolute
  PWONumber abs() const {
    PyObject* rslt = PyNumber_Absolute(_obj);
    if (rslt==0)
      Fail(PyExc_TypeError, "Failed to get absolute value");
    return LoseRef(rslt);
  };
  //PyNumber_Add
  PWONumber operator+(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Add(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for +");
    return LoseRef(rslt);
  };
  //PyNumber_And
  PWONumber operator&(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_And(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for &");
    return LoseRef(rslt);
  };
  //PyNumber_Coerce
  //PyNumber_Divide
  PWONumber operator/(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Divide(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for /");
    return LoseRef(rslt);
  };
  //PyNumber_Divmod
  PWOSequence divmod(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Divmod(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for divmod");
    return LoseRef(rslt);
  };
  //PyNumber_Float
    operator double () const {
  PyObject*  F = PyNumber_Float(_obj);
  if (F==0)
      Fail(PyExc_TypeError, "Cannot convert to double");
  double r = PyFloat_AS_DOUBLE(F);
  Py_DECREF(F);
  return r;
    };
  operator float () const {
    double rslt = (double) *this;
    //if (rslt > INT_MAX)
    //  Fail(PyExc_TypeError, "Cannot convert to a float");
    return (float) rslt;
  };
  //PyNumber_Int
    operator long () const {
  PyObject*  Int = PyNumber_Int(_obj);
  if (Int==0)
      Fail(PyExc_TypeError, "Cannot convert to long");
  long r = PyInt_AS_LONG(Int);
  Py_DECREF(Int);
  return r;
    };
  operator int () const {
    long rslt = (long) *this;
    if (rslt > INT_MAX)
      Fail(PyExc_TypeError, "Cannot convert to an int");
    return (int) rslt;
  };
  //PyNumber_Invert
  PWONumber operator~ () const {
    PyObject* rslt = PyNumber_Invert(_obj);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper type for ~");
    return LoseRef(rslt);
  };
  //PyNumber_Long
  //PyNumber_Lshift
  PWONumber operator<<(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Lshift(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for <<");
    return LoseRef(rslt);
  };
  //PyNumber_Multiply
  PWONumber operator*(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Multiply(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for *");
    return LoseRef(rslt);
  };
  //PyNumber_Negative
  PWONumber operator- () const {
    PyObject* rslt = PyNumber_Negative(_obj);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper type for unary -");
    return LoseRef(rslt);
  };
  //PyNumber_Or
  PWONumber operator|(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Or(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for |");
    return LoseRef(rslt);
  };
  //PyNumber_Positive
  PWONumber operator+ () const {
    PyObject* rslt = PyNumber_Positive(_obj);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper type for unary +");
    return LoseRef(rslt);
  };
  //PyNumber_Remainder
  PWONumber operator%(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Remainder(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for %");
    return LoseRef(rslt);
  };
  //PyNumber_Rshift
  PWONumber operator>>(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Rshift(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for >>");
    return LoseRef(rslt);
  };
  //PyNumber_Subtract
  PWONumber operator-(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Subtract(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for -");
    return LoseRef(rslt);
  };
  //PyNumber_Xor
  PWONumber operator^(const PWONumber& rhs) const {
    PyObject*  rslt = PyNumber_Xor(_obj, rhs);
    if (rslt==0)
      Fail(PyExc_TypeError, "Improper rhs for ^");
    return LoseRef(rslt);
  };
};

#endif //PWONUMBER_H_INCLUDED_