3
#include "CXX/Extensions.hxx"
8
// Making an extension object
9
class range: public Py::PythonExtension<range>
15
range(long start_, long stop_, long step_ = 1L)
20
std::cout << "range object created " << this << std::endl;
25
std::cout << "range object destroyed " << this << std::endl;
28
static void init_type(void);
32
return (stop - start + 1)/step;
35
long item(int i) const
38
// this exception stops a Python for loop over range.
39
throw Py::IndexError("index too large");
40
return start + i * step;
43
range* slice(int i, int j) const
45
int first = start + i * step;
46
int last = start + j * step;
47
return new range(first, last, step);
50
range* extend(int k) const
52
return new range(start, stop + k, step);
55
std::string asString() const
58
s << "range(" << start << ", " << stop << ", " << step << ")" << std::ends;
59
return std::string(s.str());
62
// override functions from PythonExtension
63
virtual Py::Object repr();
64
virtual Py::Object getattr( const char *name );
66
virtual int sequence_length();
67
virtual Py::Object sequence_item( int i );
68
virtual Py::Object sequence_concat( const Py::Object &j );
69
virtual Py::Object sequence_slice( int i, int j );
71
// define python methods of this object
72
Py::Object amethod (const Py::Tuple& args);
73
Py::Object value (const Py::Tuple& args);
74
Py::Object assign (const Py::Tuple& args);
75
Py::Object reference_count (const Py::Tuple& args)
77
return Py::Int(this->ob_refcnt);
80
Py::Object c_value(const Py::Tuple&) const
83
for(int i = start; i <= stop; i += step)
85
result.append(Py::Int(i));
90
void c_assign(const Py::Tuple&, const Py::Object& rhs)
94
start = long(Py::Int(w[0]));
95
stop = long(Py::Int(w[1]));
96
step = long(Py::Int(w[2]));
100
class RangeSequence: public Py::SeqBase<Py::Int>
104
explicit RangeSequence (PyObject *pyob, bool owned = false): Py::SeqBase<Py::Int>(pyob, owned)
109
explicit RangeSequence(int start, int stop, int step = 1)
111
set (new range(start, stop, step), true);
114
RangeSequence(const RangeSequence& other): Py::SeqBase<Py::Int>(*other)
119
RangeSequence& operator= (const Py::Object& rhs)
121
return (*this = *rhs);
124
RangeSequence& operator= (PyObject* rhsp)
126
if(ptr() == rhsp) return *this;
131
virtual bool accepts(PyObject *pyob) const
133
return pyob && range::check(pyob);
136
Py::Object value(const Py::Tuple& t) const
138
return static_cast<range *>(ptr())->c_value(t);
141
void assign(const Py::Tuple& t, const Py::Object& rhs)
143
static_cast<range *>(ptr())->c_assign(t, rhs);