1
//-----------------------------------------------------------------------------
3
// Copyright (c) 1998 - 2007, The Regents of the University of California
4
// Produced at the Lawrence Livermore National Laboratory
5
// All rights reserved.
7
// This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
8
// full copyright notice is contained in the file COPYRIGHT located at the root
9
// of the PyCXX distribution.
11
// Redistribution and use in source and binary forms, with or without
12
// modification, are permitted provided that the following conditions are met:
14
// - Redistributions of source code must retain the above copyright notice,
15
// this list of conditions and the disclaimer below.
16
// - Redistributions in binary form must reproduce the above copyright notice,
17
// this list of conditions and the disclaimer (as noted below) in the
18
// documentation and/or materials provided with the distribution.
19
// - Neither the name of the UC/LLNL nor the names of its contributors may be
20
// used to endorse or promote products derived from this software without
21
// specific prior written permission.
23
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
// ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF
27
// CALIFORNIA, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR
28
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36
//-----------------------------------------------------------------------------
40
#include "CXX/Extensions.hxx"
45
// Making an extension object
46
class range: public Py::PythonExtension<range>
53
range(long start_, long stop_, long step_ = 1L)
58
std::cout << "range object created " << this << std::endl;
63
std::cout << "range object destroyed " << this << std::endl;
66
static void init_type(void);
70
return (stop - start + 1)/step;
73
long item(int i) const
76
// this exception stops a Python for loop over range.
77
throw Py::IndexError("index too large");
78
return start + i * step;
81
range* slice(int i, int j) const
83
int first = start + i * step;
84
int last = start + j * step;
85
return new range(first, last, step);
88
range* extend(int k) const
90
return new range(start, stop + k, step);
93
std::string asString() const
96
s << "range(" << start << ", " << stop << ", " << step << ")" << std::ends;
97
return std::string(s.str());
100
// override functions from PythonExtension
101
virtual Py::Object repr();
102
virtual Py::Object getattr( const char *name );
104
virtual int sequence_length();
105
virtual Py::Object sequence_item( Py_ssize_t i );
106
virtual Py::Object sequence_concat( const Py::Object &j );
107
virtual Py::Object sequence_slice( Py_ssize_t i, Py_ssize_t j );
109
// define python methods of this object
110
Py::Object amethod (const Py::Tuple& args);
111
Py::Object value (const Py::Tuple& args);
112
Py::Object assign (const Py::Tuple& args);
113
Py::Object reference_count (const Py::Tuple& args)
115
return Py::Int(this->ob_refcnt);
118
Py::Object c_value(const Py::Tuple&) const
121
for(int i = start; i <= stop; i += step)
123
result.append(Py::Int(i));
128
void c_assign(const Py::Tuple&, const Py::Object& rhs)
132
start = long(Py::Int(w[0]));
133
stop = long(Py::Int(w[1]));
134
step = long(Py::Int(w[2]));
138
class RangeSequence: public Py::SeqBase<Py::Int>
142
explicit RangeSequence (PyObject *pyob, bool owned = false): Py::SeqBase<Py::Int>(pyob, owned)
147
explicit RangeSequence(int start, int stop, int step = 1)
149
set (new range(start, stop, step), true);
152
RangeSequence(const RangeSequence& other): Py::SeqBase<Py::Int>(*other)
157
RangeSequence& operator= (const Py::Object& rhs)
159
return (*this = *rhs);
162
RangeSequence& operator= (PyObject* rhsp)
164
if(ptr() == rhsp) return *this;
169
virtual bool accepts(PyObject *pyob) const
171
return pyob && range::check(pyob);
174
Py::Object value(const Py::Tuple& t) const
176
return static_cast<range *>(ptr())->c_value(t);
179
void assign(const Py::Tuple& t, const Py::Object& rhs)
181
static_cast<range *>(ptr())->c_assign(t, rhs);