~ubuntu-branches/ubuntu/breezy/pysvn/breezy

« back to all changes in this revision

Viewing changes to Import/pycxx_5_3_4/Demo/range.hxx

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2005-09-08 05:13:33 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050908051333-qgsa2rksrb4az1h4
Tags: 1.3.0-1
Package from release tarball.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef __r__h
 
2
#define __r__h
 
3
#include "CXX/Extensions.hxx"
 
4
 
 
5
#include STR_STREAM
 
6
 
 
7
 
 
8
// Making an extension object
 
9
class range: public Py::PythonExtension<range>
 
10
        {
 
11
public:
 
12
        long    start;
 
13
        long    stop;
 
14
        long    step;
 
15
        range(long start_, long stop_, long step_ = 1L) 
 
16
                {
 
17
                start = start_;
 
18
                stop = stop_;
 
19
                step = step_;
 
20
                std::cout << "range object created " << this << std::endl;
 
21
                }
 
22
 
 
23
        virtual ~range()
 
24
                {
 
25
                std::cout << "range object destroyed " << this << std::endl;
 
26
                }
 
27
 
 
28
        static void init_type(void);
 
29
 
 
30
        long length() const
 
31
                {
 
32
                return (stop - start + 1)/step;
 
33
                }
 
34
 
 
35
        long item(int i) const
 
36
                {
 
37
                if( i >= length() )
 
38
                        // this exception stops a Python for loop over range.
 
39
                        throw Py::IndexError("index too large");
 
40
                return start + i * step;
 
41
                }
 
42
 
 
43
        range* slice(int i, int j) const
 
44
                {
 
45
                int first = start + i * step;
 
46
                int last = start + j * step;
 
47
                return new range(first, last, step);
 
48
                }
 
49
 
 
50
        range* extend(int k) const
 
51
                {
 
52
                return new range(start, stop + k, step);      
 
53
                }
 
54
 
 
55
        std::string asString() const
 
56
                {
 
57
                std::OSTRSTREAM s;
 
58
                s << "range(" << start << ", " << stop << ", " << step << ")" << std::ends;
 
59
                return std::string(s.str());
 
60
                }
 
61
 
 
62
        // override functions from PythonExtension
 
63
        virtual Py::Object repr();
 
64
        virtual Py::Object getattr( const char *name );
 
65
 
 
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 );
 
70
 
 
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) 
 
76
                {
 
77
                return Py::Int(this->ob_refcnt);
 
78
                }
 
79
 
 
80
        Py::Object c_value(const Py::Tuple&) const
 
81
                {
 
82
                Py::List result;
 
83
                for(int i = start; i <= stop; i += step)
 
84
                        {
 
85
                        result.append(Py::Int(i));
 
86
                        }
 
87
                return result;
 
88
                }
 
89
 
 
90
        void c_assign(const Py::Tuple&, const Py::Object& rhs)
 
91
                {
 
92
                Py::Tuple w(rhs);
 
93
                w.verify_length(3);
 
94
                start = long(Py::Int(w[0]));
 
95
                stop = long(Py::Int(w[1]));
 
96
                step = long(Py::Int(w[2]));
 
97
                }
 
98
        };
 
99
 
 
100
class RangeSequence: public Py::SeqBase<Py::Int>
 
101
        {
 
102
public:
 
103
 
 
104
        explicit RangeSequence (PyObject *pyob, bool owned = false): Py::SeqBase<Py::Int>(pyob, owned)
 
105
                {
 
106
                validate();
 
107
                }
 
108
 
 
109
        explicit RangeSequence(int start, int stop, int step = 1) 
 
110
                {
 
111
                set (new range(start, stop, step), true);
 
112
                }
 
113
 
 
114
        RangeSequence(const RangeSequence& other): Py::SeqBase<Py::Int>(*other)
 
115
                {
 
116
                validate();
 
117
                }
 
118
 
 
119
        RangeSequence& operator= (const Py::Object& rhs)
 
120
                {
 
121
                return (*this = *rhs);
 
122
                }
 
123
 
 
124
        RangeSequence& operator= (PyObject* rhsp)
 
125
                {
 
126
                if(ptr() == rhsp) return *this;
 
127
                set(rhsp);
 
128
                return *this;
 
129
                }
 
130
 
 
131
        virtual bool accepts(PyObject *pyob) const
 
132
                {
 
133
                return pyob && range::check(pyob);
 
134
                }
 
135
 
 
136
        Py::Object value(const Py::Tuple& t) const
 
137
                {
 
138
                return static_cast<range *>(ptr())->c_value(t);
 
139
                }
 
140
 
 
141
        void assign(const Py::Tuple& t, const Py::Object& rhs)
 
142
                {
 
143
                static_cast<range *>(ptr())->c_assign(t, rhs);
 
144
                }
 
145
        };
 
146
#endif
 
147