~ubuntu-branches/ubuntu/natty/pysvn/natty

« back to all changes in this revision

Viewing changes to Import/pycxx-5.5.0/Demo/range.hxx

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-23 20:08:08 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090223200808-t946skprxzf6vjqx
Tags: 1.6.3-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//-----------------------------------------------------------------------------
 
2
//
 
3
// Copyright (c) 1998 - 2007, The Regents of the University of California
 
4
// Produced at the Lawrence Livermore National Laboratory
 
5
// All rights reserved.
 
6
//
 
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.
 
10
//
 
11
// Redistribution  and  use  in  source  and  binary  forms,  with  or  without
 
12
// modification, are permitted provided that the following conditions are met:
 
13
//
 
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.
 
22
//
 
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
 
34
// DAMAGE.
 
35
//
 
36
//-----------------------------------------------------------------------------
 
37
 
 
38
#ifndef __r__h
 
39
#define __r__h
 
40
#include "CXX/Extensions.hxx"
 
41
 
 
42
#include STR_STREAM
 
43
 
 
44
 
 
45
// Making an extension object
 
46
class range: public Py::PythonExtension<range>
 
47
{
 
48
public:
 
49
    long    start;
 
50
    long    stop;
 
51
    long    step;
 
52
 
 
53
    range(long start_, long stop_, long step_ = 1L) 
 
54
    {
 
55
        start = start_;
 
56
        stop = stop_;
 
57
        step = step_;
 
58
        std::cout << "range object created " << this << std::endl;
 
59
    }
 
60
 
 
61
    virtual ~range()
 
62
    {
 
63
        std::cout << "range object destroyed " << this << std::endl;
 
64
    }
 
65
 
 
66
    static void init_type(void);
 
67
 
 
68
    long length() const
 
69
    {
 
70
        return (stop - start + 1)/step;
 
71
    }
 
72
 
 
73
    long item(int i) const
 
74
    {
 
75
        if( i >= length() )
 
76
            // this exception stops a Python for loop over range.
 
77
            throw Py::IndexError("index too large");
 
78
        return start + i * step;
 
79
    }
 
80
 
 
81
    range* slice(int i, int j) const
 
82
    {
 
83
        int first = start + i * step;
 
84
        int last = start + j * step;
 
85
        return new range(first, last, step);
 
86
    }
 
87
 
 
88
    range* extend(int k) const
 
89
    {
 
90
        return new range(start, stop + k, step);      
 
91
    }
 
92
 
 
93
    std::string asString() const
 
94
    {
 
95
        std::OSTRSTREAM s;
 
96
        s << "range(" << start << ", " << stop << ", " << step << ")" << std::ends;
 
97
        return std::string(s.str());
 
98
    }
 
99
 
 
100
    // override functions from PythonExtension
 
101
    virtual Py::Object repr();
 
102
    virtual Py::Object getattr( const char *name );
 
103
 
 
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 );
 
108
 
 
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) 
 
114
    {
 
115
        return Py::Int(this->ob_refcnt);
 
116
    }
 
117
 
 
118
    Py::Object c_value(const Py::Tuple&) const
 
119
    {
 
120
        Py::List result;
 
121
        for(int i = start; i <= stop; i += step)
 
122
        {
 
123
            result.append(Py::Int(i));
 
124
        }
 
125
        return result;
 
126
    }
 
127
 
 
128
    void c_assign(const Py::Tuple&, const Py::Object& rhs)
 
129
    {
 
130
        Py::Tuple w(rhs);
 
131
        w.verify_length(3);
 
132
        start = long(Py::Int(w[0]));
 
133
        stop = long(Py::Int(w[1]));
 
134
        step = long(Py::Int(w[2]));
 
135
    }
 
136
};
 
137
 
 
138
class RangeSequence: public Py::SeqBase<Py::Int>
 
139
{
 
140
public:
 
141
 
 
142
    explicit RangeSequence (PyObject *pyob, bool owned = false): Py::SeqBase<Py::Int>(pyob, owned)
 
143
    {
 
144
        validate();
 
145
    }
 
146
 
 
147
    explicit RangeSequence(int start, int stop, int step = 1) 
 
148
    {
 
149
        set (new range(start, stop, step), true);
 
150
    }
 
151
 
 
152
    RangeSequence(const RangeSequence& other): Py::SeqBase<Py::Int>(*other)
 
153
    {
 
154
        validate();
 
155
    }
 
156
 
 
157
    RangeSequence& operator= (const Py::Object& rhs)
 
158
    {
 
159
        return (*this = *rhs);
 
160
    }
 
161
 
 
162
    RangeSequence& operator= (PyObject* rhsp)
 
163
    {
 
164
        if(ptr() == rhsp) return *this;
 
165
        set(rhsp);
 
166
        return *this;
 
167
    }
 
168
 
 
169
    virtual bool accepts(PyObject *pyob) const
 
170
    {
 
171
        return pyob && range::check(pyob);
 
172
    }
 
173
 
 
174
    Py::Object value(const Py::Tuple& t) const
 
175
    {
 
176
        return static_cast<range *>(ptr())->c_value(t);
 
177
    }
 
178
 
 
179
    void assign(const Py::Tuple& t, const Py::Object& rhs)
 
180
    {
 
181
        static_cast<range *>(ptr())->c_assign(t, rhs);
 
182
    }
 
183
};
 
184
#endif