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

« back to all changes in this revision

Viewing changes to Import/pycxx_5_3_4/CXX/Objects.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
//----------------------------------*-C++-*----------------------------------//
 
2
// Copyright 1998 The Regents of the University of California.
 
3
// All rights reserved. See LEGAL.LLNL for full text and disclaimer.
 
4
//---------------------------------------------------------------------------//
 
5
 
 
6
#ifndef __CXX_Objects__h
 
7
#define __CXX_Objects__h
 
8
 
 
9
#include "Python.h"
 
10
#include "CXX/Version.hxx"
 
11
#include "CXX/Config.hxx"
 
12
#include "CXX/Exception.hxx"
 
13
 
 
14
 
 
15
#include <iostream>
 
16
#include STR_STREAM
 
17
#include <string>
 
18
#include <iterator>
 
19
#include <utility>
 
20
#include <typeinfo>
 
21
 
 
22
namespace Py
 
23
        {
 
24
        typedef int sequence_index_type;        // type of an index into a sequence
 
25
 
 
26
        // Forward declarations
 
27
        class Object;
 
28
        class Type;
 
29
        template<TEMPLATE_TYPENAME T> class SeqBase;
 
30
        class String;
 
31
        template<TEMPLATE_TYPENAME T> class MapBase;
 
32
 
 
33
        // new_reference_to also overloaded below on Object
 
34
        inline PyObject* new_reference_to(PyObject* p)
 
35
                {
 
36
                Py::_XINCREF(p);
 
37
                return p;
 
38
                }
 
39
 
 
40
        // returning Null() from an extension method triggers a
 
41
        // Python exception
 
42
        inline PyObject* Null()
 
43
                {
 
44
                return (static_cast<PyObject*>(0));
 
45
                }
 
46
 
 
47
        //===========================================================================//
 
48
        // class Object
 
49
        // The purpose of this class is to serve as the most general kind of
 
50
        // Python object, for the purpose of writing C++ extensions in Python
 
51
        // Objects hold a PyObject* which they own. This pointer is always a
 
52
        // valid pointer to a Python object. In children we must maintain this behavior.
 
53
        //
 
54
        // Instructions on how to make your own class MyType descended from Object:
 
55
        // (0) Pick a base class, either Object or perhaps SeqBase<T> or MapBase<T>.
 
56
        //     This example assumes Object.
 
57
 
 
58
        // (1) Write a routine int MyType_Check (PyObject *) modeled after PyInt_Check,
 
59
        //     PyFloat_Check, etc.
 
60
 
 
61
        // (2) Add method accepts:
 
62
        //     virtual bool accepts (PyObject *pyob) const {
 
63
        //         return pyob && MyType_Check (pyob);
 
64
        //     }
 
65
 
 
66
        // (3) Include the following constructor and copy constructor
 
67
        //
 
68
        /*
 
69
        explicit MyType (PyObject *pyob): Object(pyob) {
 
70
        validate();
 
71
        }
 
72
 
 
73
        MyType(const Object& other): Object(other.ptr()) {
 
74
        validate();
 
75
        }
 
76
        */
 
77
 
 
78
        // Alernate version for the constructor to allow for construction from owned pointers:
 
79
        /*
 
80
        explicit MyType (PyObject *pyob): Object(pyob) {
 
81
        validate();
 
82
        }
 
83
        */
 
84
 
 
85
        // You may wish to add other constructors; see the classes below for examples.
 
86
        // Each constructor must use "set" to set the pointer
 
87
        // and end by validating the pointer you have created.
 
88
 
 
89
        // (4) Each class needs at least these two assignment operators:
 
90
        /*
 
91
        MyType& operator= (const Object& rhs) {
 
92
        return (*this = *rhs);
 
93
        }
 
94
 
 
95
        Mytype& operator= (PyObject* rhsp) {
 
96
        if(ptr() == rhsp) return *this;
 
97
        set(rhsp);
 
98
        return *this;
 
99
        }
 
100
        */
 
101
        // Note on accepts: constructors call the base class
 
102
        // version of a virtual when calling the base class constructor,
 
103
        // so the test has to be done explicitly in a descendent.
 
104
 
 
105
        // If you are inheriting from PythonExtension<T> to define an object
 
106
        // note that it contains PythonExtension<T>::check
 
107
        // which you can use in accepts when writing a wrapper class.
 
108
        // See Demo/range.h and Demo/range.cxx for an example.
 
109
 
 
110
        class Object
 
111
                {
 
112
        private:
 
113
                // the pointer to the Python object
 
114
                // Only Object sets this directly.
 
115
                // The default constructor for Object sets it to Py_None and
 
116
                // child classes must use "set" to set it
 
117
                //
 
118
                PyObject* p;
 
119
 
 
120
        protected:
 
121
 
 
122
                void set (PyObject* pyob, bool owned = false)
 
123
                        {
 
124
                        release();
 
125
                        p = pyob;
 
126
                        if (!owned)
 
127
                                {
 
128
                                Py::_XINCREF (p);
 
129
                                }
 
130
                        validate();
 
131
                        }
 
132
 
 
133
                void release ()
 
134
                        {
 
135
                        Py::_XDECREF (p);
 
136
                        p = 0;
 
137
                        }
 
138
 
 
139
                void validate()
 
140
                        {
 
141
                        // release pointer if not the right type
 
142
                        if (! accepts (p))
 
143
                                {
 
144
                                release ();
 
145
                                if(PyErr_Occurred())
 
146
                                        { // Error message already set
 
147
                                        throw Exception();
 
148
                                        }
 
149
                                // Better error message if RTTI available
 
150
#if defined( _CPPRTTI ) || defined(__GNUG__)
 
151
                                std::string s("CXX : Error creating object of type ");
 
152
                                s += (typeid (*this)).name();
 
153
                                throw TypeError (s);
 
154
#else
 
155
                                throw TypeError ("CXX: type error.");
 
156
#endif
 
157
                                }
 
158
                        }
 
159
 
 
160
        public:
 
161
                // Constructor acquires new ownership of pointer unless explicitly told not to.
 
162
                explicit Object (PyObject* pyob=Py::_None(), bool owned = false): p (pyob)
 
163
                        {
 
164
                        if(!owned)
 
165
                                {
 
166
                                Py::_XINCREF (p);
 
167
                                }
 
168
                        validate();
 
169
                        }
 
170
 
 
171
                // Copy constructor acquires new ownership of pointer
 
172
                Object (const Object& ob): p(ob.p)
 
173
                        {
 
174
                        Py::_XINCREF (p);
 
175
                        validate();
 
176
                        }
 
177
 
 
178
                // Assignment acquires new ownership of pointer
 
179
                Object& operator= (const Object& rhs)
 
180
                        {
 
181
                        set(rhs.p);
 
182
                        return *this;
 
183
                        }
 
184
 
 
185
                Object& operator= (PyObject* rhsp)
 
186
                        {
 
187
                        if(ptr() == rhsp) return *this;
 
188
                        set (rhsp);
 
189
                        return *this;
 
190
                        }
 
191
 
 
192
                // Destructor
 
193
                virtual ~Object ()
 
194
                        {
 
195
                        release ();
 
196
                        }
 
197
 
 
198
                // Loaning the pointer to others, retain ownership
 
199
                PyObject* operator* () const
 
200
                        {
 
201
                        return p;
 
202
                        }
 
203
 
 
204
                // Explicit reference_counting changes
 
205
                void increment_reference_count()
 
206
                        {
 
207
                        Py::_XINCREF(p);
 
208
                        }
 
209
 
 
210
                void decrement_reference_count()
 
211
                        {
 
212
                        // not allowed to commit suicide, however
 
213
                        if(reference_count() == 1)
 
214
                        throw RuntimeError("Object::decrement_reference_count error.");
 
215
                        Py::_XDECREF(p);
 
216
                        }
 
217
                // Would like to call this pointer() but messes up STL in SeqBase<T>
 
218
                PyObject* ptr () const
 
219
                        {
 
220
                        return p;
 
221
                        }
 
222
 
 
223
                //
 
224
                // Queries
 
225
                //
 
226
 
 
227
                // Can pyob be used in this object's constructor?
 
228
                virtual bool accepts (PyObject *pyob) const
 
229
                        {
 
230
                        return (pyob != 0);
 
231
                        }
 
232
 
 
233
                int reference_count () const
 
234
                        { // the reference count
 
235
                        return p ? p->ob_refcnt : 0;
 
236
                        }
 
237
 
 
238
                Type type () const; // the type object associated with this one
 
239
 
 
240
                String str () const; // the str() representation
 
241
 
 
242
                std::string as_string() const;
 
243
 
 
244
                String repr () const; // the repr () representation
 
245
 
 
246
                bool hasAttr (const std::string& s) const
 
247
                        {
 
248
                        return PyObject_HasAttrString (p, const_cast<char*>(s.c_str())) ? true: false;
 
249
                        }
 
250
 
 
251
                Object getAttr (const std::string& s) const
 
252
                        {
 
253
                        return Object (PyObject_GetAttrString (p, const_cast<char*>(s.c_str())), true);
 
254
                        }
 
255
 
 
256
                Object getItem (const Object& key) const
 
257
                        {
 
258
                        return Object (PyObject_GetItem(p, *key), true);
 
259
                        }
 
260
 
 
261
                long hashValue () const
 
262
                        {
 
263
                        return PyObject_Hash (p);
 
264
                        }
 
265
 
 
266
                //
 
267
                // int print (FILE* fp, int flags=Py_Print_RAW)
 
268
                //      {
 
269
                //      return PyObject_Print (p, fp, flags);
 
270
                //      }
 
271
                //
 
272
                bool is(PyObject *pother) const
 
273
                        {  // identity test
 
274
                        return p == pother;
 
275
                        }
 
276
 
 
277
                bool is(const Object& other) const
 
278
                        { // identity test
 
279
                        return p == other.p;
 
280
                        }
 
281
 
 
282
                bool isCallable () const
 
283
                        {
 
284
                        return PyCallable_Check (p) != 0;
 
285
                        }
 
286
 
 
287
                bool isDict () const
 
288
                        {
 
289
                        return Py::_Dict_Check (p);
 
290
                        }
 
291
 
 
292
                bool isList () const
 
293
                        {
 
294
                        return Py::_List_Check (p);
 
295
                        }
 
296
 
 
297
                bool isMapping () const
 
298
                        {
 
299
                        return PyMapping_Check (p) != 0;
 
300
                        }
 
301
 
 
302
                bool isNumeric () const
 
303
                        {
 
304
                        return PyNumber_Check (p) != 0;
 
305
                        }
 
306
 
 
307
                bool isSequence () const
 
308
                        {
 
309
                        return PySequence_Check (p) != 0;
 
310
                        }
 
311
 
 
312
                bool isTrue () const
 
313
                        {
 
314
                        return PyObject_IsTrue (p) != 0;
 
315
                        }
 
316
 
 
317
                bool isType (const Type& t) const;
 
318
 
 
319
                bool isTuple() const
 
320
                        {
 
321
                        return Py::_Tuple_Check(p);
 
322
                        }
 
323
 
 
324
                bool isString() const
 
325
                        {
 
326
                        return Py::_String_Check(p) || Py::_Unicode_Check(p);
 
327
                        }
 
328
 
 
329
                bool isUnicode() const
 
330
                        {
 
331
                        return Py::_Unicode_Check(p);
 
332
                        }
 
333
 
 
334
                // Commands
 
335
                void setAttr (const std::string& s, const Object& value)
 
336
                        {
 
337
                        if(PyObject_SetAttrString (p, const_cast<char*>(s.c_str()), *value) == -1)
 
338
                        throw AttributeError ("getAttr failed.");
 
339
                        }
 
340
 
 
341
                void delAttr (const std::string& s)
 
342
                        {
 
343
                        if(PyObject_DelAttrString (p, const_cast<char*>(s.c_str())) == -1)
 
344
                        throw AttributeError ("delAttr failed.");
 
345
                        }
 
346
 
 
347
                // PyObject_SetItem is too weird to be using from C++
 
348
                // so it is intentionally omitted.
 
349
 
 
350
                void delItem (const Object& key)
 
351
                        {
 
352
                        //if(PyObject_DelItem(p, *key) == -1)
 
353
                        // failed to link on Windows?
 
354
                        throw KeyError("delItem failed.");
 
355
                        }
 
356
                // Equality and comparison use PyObject_Compare
 
357
 
 
358
                bool operator==(const Object& o2) const
 
359
                        {
 
360
                        int k = PyObject_Compare (p, *o2);
 
361
                        if (PyErr_Occurred()) throw Exception();
 
362
                        return k == 0;
 
363
                        }
 
364
 
 
365
                bool operator!=(const Object& o2) const
 
366
                        {
 
367
                        int k = PyObject_Compare (p, *o2);
 
368
                        if (PyErr_Occurred()) throw Exception();
 
369
                        return k != 0;
 
370
 
 
371
                        }
 
372
 
 
373
                bool operator>=(const Object& o2) const
 
374
                        {
 
375
                        int k = PyObject_Compare (p, *o2);
 
376
                        if (PyErr_Occurred()) throw Exception();
 
377
                        return k >= 0;
 
378
                        }
 
379
 
 
380
                bool operator<=(const Object& o2) const
 
381
                        {
 
382
                        int k = PyObject_Compare (p, *o2);
 
383
                        if (PyErr_Occurred()) throw Exception();
 
384
                        return k <= 0;
 
385
                        }
 
386
 
 
387
                bool operator<(const Object& o2) const
 
388
                        {
 
389
                        int k = PyObject_Compare (p, *o2);
 
390
                        if (PyErr_Occurred()) throw Exception();
 
391
                        return k < 0;
 
392
                        }
 
393
 
 
394
                bool operator>(const Object& o2) const
 
395
                        {
 
396
                        int k = PyObject_Compare (p, *o2);
 
397
                        if (PyErr_Occurred()) throw Exception();
 
398
                        return k > 0;
 
399
                        }
 
400
                };
 
401
        // End of class Object
 
402
        inline PyObject* new_reference_to(const Object& g)
 
403
                {
 
404
                PyObject* p = g.ptr();
 
405
                Py::_XINCREF(p);
 
406
                return p;
 
407
                }
 
408
 
 
409
        // Nothing() is what an extension method returns if
 
410
        // there is no other return value.
 
411
        inline Object Nothing()
 
412
                {
 
413
                return Object(Py::_None());
 
414
                }
 
415
 
 
416
        // Python special None value
 
417
        inline Object None()
 
418
                {
 
419
                return Object(Py::_None());
 
420
                }
 
421
 
 
422
        // TMM: 31May'01 - Added the #ifndef so I can exlude iostreams.
 
423
#ifndef CXX_NO_IOSTREAMS
 
424
        std::ostream& operator<< (std::ostream& os, const Object& ob);
 
425
#endif
 
426
 
 
427
        // Class Type
 
428
        class Type: public Object
 
429
                {
 
430
        public:
 
431
                explicit Type (PyObject* pyob, bool owned = false): Object(pyob, owned)
 
432
                        {
 
433
                        validate();
 
434
                        }
 
435
 
 
436
                Type (const Object& ob): Object(*ob)
 
437
                        {
 
438
                        validate();
 
439
                        }
 
440
 
 
441
                Type(const Type& t): Object(t)
 
442
                        {
 
443
                        validate();
 
444
                        }
 
445
 
 
446
                Type& operator= (const Object& rhs)
 
447
                        {
 
448
                        return (*this = *rhs);
 
449
                        }
 
450
 
 
451
                Type& operator= (PyObject* rhsp)
 
452
                        {
 
453
                        if(ptr() == rhsp) return *this;
 
454
                        set (rhsp);
 
455
                        return *this;
 
456
                        }
 
457
                virtual bool accepts (PyObject *pyob) const
 
458
                        {
 
459
                        return pyob && Py::_Type_Check (pyob);
 
460
                        }
 
461
                };
 
462
 
 
463
 
 
464
        //
 
465
        //      Convert an owned Python pointer into a CXX Object
 
466
        //
 
467
        inline Object asObject (PyObject *p)
 
468
                {
 
469
                return Object(p, true);
 
470
                }
 
471
 
 
472
 
 
473
 
 
474
 
 
475
        // ===============================================
 
476
        // class Int
 
477
        class Int: public Object
 
478
                {
 
479
        public:
 
480
                // Constructor
 
481
                explicit Int (PyObject *pyob, bool owned = false): Object (pyob, owned)
 
482
                        {
 
483
                        validate();
 
484
                        }
 
485
 
 
486
                Int (const Int& ob): Object(*ob)
 
487
                        {
 
488
                        validate();
 
489
                        }
 
490
 
 
491
                // create from long
 
492
                explicit Int (long v = 0L): Object(PyInt_FromLong(v), true)
 
493
                        {
 
494
                        validate();
 
495
                        }
 
496
 
 
497
                // create from int
 
498
                explicit Int (int v)
 
499
                        {
 
500
                        long w = v;
 
501
                        set(PyInt_FromLong(w), true);
 
502
                        validate();
 
503
                        }
 
504
 
 
505
                // create from bool
 
506
                explicit Int (bool v)
 
507
                        {
 
508
                        long w = v ? 1 : 0;
 
509
                        set(PyInt_FromLong(w), true);
 
510
                        validate();
 
511
                        }
 
512
 
 
513
                explicit Int (const Object& ob)
 
514
                        {
 
515
                        set(PyNumber_Int(*ob), true);
 
516
                        validate();
 
517
                        }
 
518
 
 
519
                // Assignment acquires new ownership of pointer
 
520
 
 
521
                Int& operator= (const Object& rhs)
 
522
                        {
 
523
                        return (*this = *rhs);
 
524
                        }
 
525
 
 
526
                Int& operator= (PyObject* rhsp)
 
527
                        {
 
528
                        if(ptr() == rhsp) return *this;
 
529
                        set (PyNumber_Int(rhsp), true);
 
530
                        return *this;
 
531
                        }
 
532
                // Membership
 
533
                virtual bool accepts (PyObject *pyob) const
 
534
                        {
 
535
                        return pyob && Py::_Int_Check (pyob);
 
536
                        }
 
537
                // convert to long
 
538
                operator long() const
 
539
                        {
 
540
                        return PyInt_AsLong (ptr());
 
541
                        }
 
542
                // assign from an int
 
543
                Int& operator= (int v)
 
544
                        {
 
545
                        set (PyInt_FromLong (long(v)), true);
 
546
                        return *this;
 
547
                        }
 
548
                // assign from long
 
549
                Int& operator= (long v)
 
550
                        {
 
551
                        set (PyInt_FromLong (v), true);
 
552
                        return *this;
 
553
                        }
 
554
                };
 
555
 
 
556
        // ===============================================
 
557
        // class Long
 
558
        class Long: public Object
 
559
                {
 
560
        public:
 
561
                // Constructor
 
562
                explicit Long (PyObject *pyob, bool owned = false): Object (pyob, owned)
 
563
                        {
 
564
                        validate();
 
565
                        }
 
566
 
 
567
                Long (const Long& ob): Object(ob.ptr())
 
568
                        {
 
569
                        validate();
 
570
                        }
 
571
 
 
572
                // create from long
 
573
                explicit Long (long v = 0L)
 
574
                        : Object(PyLong_FromLong(v), true)
 
575
                        {
 
576
                        validate();
 
577
                        }
 
578
                // create from unsigned long
 
579
                explicit Long (unsigned long v)
 
580
                        : Object(PyLong_FromUnsignedLong(v), true)
 
581
                        {
 
582
                        validate();
 
583
                        }
 
584
                // create from int
 
585
                explicit Long (int v)
 
586
                        : Object(PyLong_FromLong(static_cast<long>(v)), true)
 
587
                        {
 
588
                        validate();
 
589
                        }
 
590
 
 
591
                // try to create from any object
 
592
                Long (const Object& ob)
 
593
                        : Object(PyNumber_Long(*ob), true)
 
594
                        {
 
595
                        validate();
 
596
                        }
 
597
 
 
598
                // Assignment acquires new ownership of pointer
 
599
 
 
600
                Long& operator= (const Object& rhs)
 
601
                        {
 
602
                        return (*this = *rhs);
 
603
                        }
 
604
 
 
605
                Long& operator= (PyObject* rhsp)
 
606
                        {
 
607
                        if(ptr() == rhsp) return *this;
 
608
                        set (PyNumber_Long(rhsp), true);
 
609
                        return *this;
 
610
                        }
 
611
                // Membership
 
612
                virtual bool accepts (PyObject *pyob) const
 
613
                        {
 
614
                        return pyob && Py::_Long_Check (pyob);
 
615
                        }
 
616
                // convert to long
 
617
                operator long() const
 
618
                        {
 
619
                        return PyLong_AsLong (ptr());
 
620
                        }
 
621
                // convert to unsigned
 
622
                operator unsigned long() const
 
623
                        {
 
624
                        return PyLong_AsUnsignedLong (ptr());
 
625
                        }
 
626
                operator double() const
 
627
                        {
 
628
                        return PyLong_AsDouble (ptr());
 
629
                        }
 
630
                // assign from an int
 
631
                Long& operator= (int v)
 
632
                        {
 
633
                        set(PyLong_FromLong (long(v)), true);
 
634
                        return *this;
 
635
                        }
 
636
                // assign from long
 
637
                Long& operator= (long v)
 
638
                        {
 
639
                        set(PyLong_FromLong (v), true);
 
640
                        return *this;
 
641
                        }
 
642
                // assign from unsigned long
 
643
                Long& operator= (unsigned long v)
 
644
                        {
 
645
                        set(PyLong_FromUnsignedLong (v), true);
 
646
                        return *this;
 
647
                        }
 
648
                };
 
649
 
 
650
        // ===============================================
 
651
        // class Float
 
652
        //
 
653
        class Float: public Object
 
654
                {
 
655
        public:
 
656
                // Constructor
 
657
                explicit Float (PyObject *pyob, bool owned = false): Object(pyob, owned)
 
658
                        {
 
659
                        validate();
 
660
                        }
 
661
 
 
662
                Float (const Float& f): Object(f)
 
663
                        {
 
664
                        validate();
 
665
                        }
 
666
 
 
667
                // make from double
 
668
                explicit Float (double v=0.0)
 
669
                        : Object(PyFloat_FromDouble (v), true)
 
670
                        {
 
671
                        validate();
 
672
                        }
 
673
 
 
674
                // try to make from any object
 
675
                Float (const Object& ob)
 
676
                        : Object(PyNumber_Float(*ob), true)
 
677
                        {
 
678
                        validate();
 
679
                        }
 
680
 
 
681
                Float& operator= (const Object& rhs)
 
682
                        {
 
683
                        return (*this = *rhs);
 
684
                        }
 
685
 
 
686
                Float& operator= (PyObject* rhsp)
 
687
                        {
 
688
                        if(ptr() == rhsp) return *this;
 
689
                        set (PyNumber_Float(rhsp), true);
 
690
                        return *this;
 
691
                        }
 
692
                // Membership
 
693
                virtual bool accepts (PyObject *pyob) const
 
694
                        {
 
695
                        return pyob && Py::_Float_Check (pyob);
 
696
                        }
 
697
                // convert to double
 
698
                operator double () const
 
699
                        {
 
700
                        return PyFloat_AsDouble (ptr());
 
701
                        }
 
702
                // assign from a double
 
703
                Float& operator= (double v)
 
704
                        {
 
705
                        set(PyFloat_FromDouble (v), true);
 
706
                        return *this;
 
707
                        }
 
708
                // assign from an int
 
709
                Float& operator= (int v)
 
710
                        {
 
711
                        set(PyFloat_FromDouble (double(v)), true);
 
712
                        return *this;
 
713
                        }
 
714
                // assign from long
 
715
                Float& operator= (long v)
 
716
                        {
 
717
                        set(PyFloat_FromDouble (double(v)), true);
 
718
                        return *this;
 
719
                        }
 
720
                // assign from an Int
 
721
                Float& operator= (const Int& iob)
 
722
                        {
 
723
                        set(PyFloat_FromDouble (double(long(iob))), true);
 
724
                        return *this;
 
725
                        }
 
726
                };
 
727
 
 
728
        // ===============================================
 
729
        // class Complex
 
730
        class Complex: public Object
 
731
                {
 
732
        public:
 
733
                // Constructor
 
734
                explicit Complex (PyObject *pyob, bool owned = false): Object(pyob, owned)
 
735
                        {
 
736
                        validate();
 
737
                        }
 
738
 
 
739
                Complex (const Complex& f): Object(f)
 
740
                        {
 
741
                        validate();
 
742
                        }
 
743
 
 
744
                // make from double
 
745
                explicit Complex (double v=0.0, double w=0.0)
 
746
                        :Object(PyComplex_FromDoubles (v, w), true)
 
747
                        {
 
748
                        validate();
 
749
                        }
 
750
 
 
751
                Complex& operator= (const Object& rhs)
 
752
                        {
 
753
                        return (*this = *rhs);
 
754
                        }
 
755
 
 
756
                Complex& operator= (PyObject* rhsp)
 
757
                        {
 
758
                        if(ptr() == rhsp) return *this;
 
759
                        set (rhsp);
 
760
                        return *this;
 
761
                        }
 
762
                // Membership
 
763
                virtual bool accepts (PyObject *pyob) const
 
764
                        {
 
765
                        return pyob && Py::_Complex_Check (pyob);
 
766
                        }
 
767
                // convert to Py_complex
 
768
                operator Py_complex () const
 
769
                        {
 
770
                        return PyComplex_AsCComplex (ptr());
 
771
                        }
 
772
                // assign from a Py_complex
 
773
                Complex& operator= (const Py_complex& v)
 
774
                        {
 
775
                        set(PyComplex_FromCComplex (v), true);
 
776
                        return *this;
 
777
                        }
 
778
                // assign from a double
 
779
                Complex& operator= (double v)
 
780
                        {
 
781
                        set(PyComplex_FromDoubles (v, 0.0), true);
 
782
                        return *this;
 
783
                        }
 
784
                // assign from an int
 
785
                Complex& operator= (int v)
 
786
                        {
 
787
                        set(PyComplex_FromDoubles (double(v), 0.0), true);
 
788
                        return *this;
 
789
                        }
 
790
                // assign from long
 
791
                Complex& operator= (long v)
 
792
                        {
 
793
                        set(PyComplex_FromDoubles (double(v), 0.0), true);
 
794
                        return *this;
 
795
                        }
 
796
                // assign from an Int
 
797
                Complex& operator= (const Int& iob)
 
798
                        {
 
799
                        set(PyComplex_FromDoubles (double(long(iob)), 0.0), true);
 
800
                        return *this;
 
801
                        }
 
802
 
 
803
                double real() const
 
804
                        {
 
805
                        return PyComplex_RealAsDouble(ptr());
 
806
                        }
 
807
 
 
808
                double imag() const
 
809
                        {
 
810
                        return PyComplex_ImagAsDouble(ptr());
 
811
                        }
 
812
                };
 
813
        // Sequences
 
814
        // Sequences are here represented as sequences of items of type T.
 
815
        // The base class SeqBase<T> represents that.
 
816
        // In basic Python T is always "Object".
 
817
 
 
818
        // seqref<T> is what you get if you get elements from a non-const SeqBase<T>.
 
819
        // Note: seqref<T> could probably be a nested class in SeqBase<T> but that might stress
 
820
        // some compilers needlessly. Simlarly for mapref later.
 
821
 
 
822
        // While this class is not intended for enduser use, it needs some public
 
823
        // constructors for the benefit of the STL.
 
824
 
 
825
        // See Scott Meyer's More Essential C++ for a description of proxies.
 
826
        // This application is even more complicated. We are doing an unusual thing
 
827
        // in having a double proxy. If we want the STL to work
 
828
        // properly we have to compromise by storing the rvalue inside. The
 
829
        // entire Object API is repeated so that things like s[i].isList() will
 
830
        // work properly.
 
831
 
 
832
        // Still, once in a while a weird compiler message may occur using expressions like x[i]
 
833
        // Changing them to Object(x[i]) helps the compiler to understand that the
 
834
        // conversion of a seqref to an Object is wanted.
 
835
 
 
836
        template<TEMPLATE_TYPENAME T>
 
837
        class seqref
 
838
                {
 
839
        protected:
 
840
                SeqBase<T>& s; // the sequence
 
841
                int offset; // item number
 
842
                T the_item; // lvalue
 
843
        public:
 
844
 
 
845
                seqref (SeqBase<T>& seq, sequence_index_type j)
 
846
                        : s(seq), offset(j), the_item (s.getItem(j))
 
847
                        {}
 
848
 
 
849
                seqref (const seqref<T>& range)
 
850
                        : s(range.s), offset(range.offset), the_item(range.the_item)
 
851
                        {}
 
852
 
 
853
                // TMM: added this seqref ctor for use with STL algorithms
 
854
                seqref (Object& obj)
 
855
                        : s(dynamic_cast< SeqBase<T>&>(obj))
 
856
                        , offset( NULL )
 
857
                        , the_item(s.getItem(offset))
 
858
                        {}
 
859
                ~seqref()
 
860
                        {}
 
861
 
 
862
                operator T() const
 
863
                        { // rvalue
 
864
                        return the_item;
 
865
                        }
 
866
 
 
867
                seqref<T>& operator=(const seqref<T>& rhs)
 
868
                        { //used as lvalue
 
869
                        the_item = rhs.the_item;
 
870
                        s.setItem(offset, the_item);
 
871
                        return *this;
 
872
                        }
 
873
 
 
874
                seqref<T>& operator=(const T& ob)
 
875
                        { // used as lvalue
 
876
                        the_item = ob;
 
877
                        s.setItem(offset, ob);
 
878
                        return *this;
 
879
                        }
 
880
 
 
881
                // forward everything else to the item
 
882
                PyObject* ptr () const
 
883
                        {
 
884
                        return the_item.ptr();
 
885
                        }
 
886
 
 
887
                int reference_count () const
 
888
                        { // the reference count
 
889
                        return the_item.reference_count();
 
890
                        }
 
891
 
 
892
                Type type () const
 
893
                        {
 
894
                        return the_item.type();
 
895
                        }
 
896
 
 
897
                String str () const;
 
898
 
 
899
                String repr () const;
 
900
 
 
901
                bool hasAttr (const std::string& attr_name) const
 
902
                        {
 
903
                        return the_item.hasAttr(attr_name);
 
904
                        }
 
905
 
 
906
                Object getAttr (const std::string& attr_name) const
 
907
                        {
 
908
                        return the_item.getAttr(attr_name);
 
909
                        }
 
910
 
 
911
                Object getItem (const Object& key) const
 
912
                        {
 
913
                        return the_item.getItem(key);
 
914
                        }
 
915
 
 
916
                long hashValue () const
 
917
                        {
 
918
                        return the_item.hashValue();
 
919
                        }
 
920
 
 
921
                bool isCallable () const
 
922
                        {
 
923
                        return the_item.isCallable();
 
924
                        }
 
925
 
 
926
                bool isDict () const
 
927
                        {
 
928
                        return the_item.isDict();
 
929
                        }
 
930
 
 
931
                bool isList () const
 
932
                        {
 
933
                        return the_item.isList();
 
934
                        }
 
935
 
 
936
                bool isMapping () const
 
937
                        {
 
938
                        return the_item.isMapping();
 
939
                        }
 
940
 
 
941
                bool isNumeric () const
 
942
                        {
 
943
                        return the_item.isNumeric();
 
944
                        }
 
945
 
 
946
                bool isSequence () const
 
947
                        {
 
948
                        return the_item.isSequence();
 
949
                        }
 
950
 
 
951
                bool isTrue () const
 
952
                        {
 
953
                        return the_item.isTrue();
 
954
                        }
 
955
 
 
956
                bool isType (const Type& t) const
 
957
                        {
 
958
                        return the_item.isType (t);
 
959
                        }
 
960
 
 
961
                bool isTuple() const
 
962
                        {
 
963
                        return the_item.isTuple();
 
964
                        }
 
965
 
 
966
                bool isString() const
 
967
                        {
 
968
                        return the_item.isString();
 
969
                        }
 
970
                // Commands
 
971
                void setAttr (const std::string& attr_name, const Object& value)
 
972
                        {
 
973
                        the_item.setAttr(attr_name, value);
 
974
                        }
 
975
 
 
976
                void delAttr (const std::string& attr_name)
 
977
                        {
 
978
                        the_item.delAttr(attr_name);
 
979
                        }
 
980
 
 
981
                void delItem (const Object& key)
 
982
                        {
 
983
                        the_item.delItem(key);
 
984
                        }
 
985
 
 
986
                bool operator==(const Object& o2) const
 
987
                        {
 
988
                        return the_item == o2;
 
989
                        }
 
990
 
 
991
                bool operator!=(const Object& o2) const
 
992
                        {
 
993
                        return the_item != o2;
 
994
                        }
 
995
 
 
996
                bool operator>=(const Object& o2) const
 
997
                        {
 
998
                        return the_item >= o2;
 
999
                        }
 
1000
 
 
1001
                bool operator<=(const Object& o2) const
 
1002
                        {
 
1003
                        return the_item <= o2;
 
1004
                        }
 
1005
 
 
1006
                bool operator<(const Object& o2) const
 
1007
                        {
 
1008
                        return the_item < o2;
 
1009
                        }
 
1010
 
 
1011
                bool operator>(const Object& o2) const
 
1012
                        {
 
1013
                        return the_item > o2;
 
1014
                        }
 
1015
                }; // end of seqref
 
1016
 
 
1017
 
 
1018
        // class SeqBase<T>
 
1019
        // ...the base class for all sequence types
 
1020
 
 
1021
        template<TEMPLATE_TYPENAME T>
 
1022
        class SeqBase: public Object
 
1023
                {
 
1024
        public:
 
1025
                // STL definitions
 
1026
                typedef size_t size_type;
 
1027
                typedef seqref<T> reference;
 
1028
                typedef T const_reference;
 
1029
                typedef seqref<T>* pointer;
 
1030
                typedef int difference_type;
 
1031
                typedef T value_type;           // TMM: 26Jun'01
 
1032
 
 
1033
                virtual size_type max_size() const
 
1034
                        {
 
1035
                        return std::string::npos; // ?
 
1036
                        }
 
1037
 
 
1038
                virtual size_type capacity() const
 
1039
                        {
 
1040
                        return size();
 
1041
                        }
 
1042
 
 
1043
                virtual void swap(SeqBase<T>& c)
 
1044
                        {
 
1045
                        SeqBase<T> temp = c;
 
1046
                        c = ptr();
 
1047
                        set(temp.ptr());
 
1048
                        }
 
1049
 
 
1050
                virtual size_type size () const
 
1051
                        {
 
1052
                        return PySequence_Length (ptr());
 
1053
                        }
 
1054
 
 
1055
                explicit SeqBase<T> ()
 
1056
                        :Object(PyTuple_New(0), true)
 
1057
                        {
 
1058
                        validate();
 
1059
                        }
 
1060
 
 
1061
                explicit SeqBase<T> (PyObject* pyob, bool owned=false)
 
1062
                        : Object(pyob, owned)
 
1063
                        {
 
1064
                        validate();
 
1065
                        }
 
1066
 
 
1067
                SeqBase<T> (const Object& ob): Object(ob)
 
1068
                        {
 
1069
                        validate();
 
1070
                        }
 
1071
 
 
1072
                // Assignment acquires new ownership of pointer
 
1073
 
 
1074
                SeqBase<T>& operator= (const Object& rhs)
 
1075
                        {
 
1076
                        return (*this = *rhs);
 
1077
                        }
 
1078
 
 
1079
                SeqBase<T>& operator= (PyObject* rhsp)
 
1080
                        {
 
1081
                        if(ptr() == rhsp) return *this;
 
1082
                        set (rhsp);
 
1083
                        return *this;
 
1084
                        }
 
1085
 
 
1086
                virtual bool accepts (PyObject *pyob) const
 
1087
                        {
 
1088
                        return pyob && PySequence_Check (pyob);
 
1089
                        }
 
1090
 
 
1091
                size_type length () const
 
1092
                        {
 
1093
                        return PySequence_Length (ptr());
 
1094
                        }
 
1095
 
 
1096
                // Element access
 
1097
                const T operator[](sequence_index_type index) const
 
1098
                        {
 
1099
                        return getItem(index);
 
1100
                        }
 
1101
 
 
1102
                seqref<T> operator[](sequence_index_type index)
 
1103
                        {
 
1104
                        return seqref<T>(*this, index);
 
1105
                        }
 
1106
 
 
1107
                virtual T getItem (sequence_index_type i) const
 
1108
                        {
 
1109
                        return T(asObject(PySequence_GetItem (ptr(), i)));
 
1110
                        }
 
1111
 
 
1112
                virtual void setItem (sequence_index_type i, const T& ob)
 
1113
                        {
 
1114
                        if (PySequence_SetItem (ptr(), i, *ob) == -1)
 
1115
                                {
 
1116
                                throw Exception();
 
1117
                                }
 
1118
                        }
 
1119
 
 
1120
                SeqBase<T> repeat (int count) const
 
1121
                        {
 
1122
                        return SeqBase<T> (PySequence_Repeat (ptr(), count), true);
 
1123
                        }
 
1124
 
 
1125
                SeqBase<T> concat (const SeqBase<T>& other) const
 
1126
                        {
 
1127
                        return SeqBase<T> (PySequence_Concat(ptr(), *other), true);
 
1128
                        }
 
1129
 
 
1130
                // more STL compatability
 
1131
                const T front () const
 
1132
                        {
 
1133
                        return getItem(0);
 
1134
                        }
 
1135
 
 
1136
                seqref<T> front()
 
1137
                        {
 
1138
                        return seqref<T>(this, 0);
 
1139
                        }
 
1140
 
 
1141
                const T back () const
 
1142
                        {
 
1143
                        return getItem(size()-1);
 
1144
                        }
 
1145
 
 
1146
                seqref<T> back()
 
1147
                        {
 
1148
                        return seqref<T>(this, size()-1);
 
1149
                        }
 
1150
 
 
1151
                void verify_length(size_type required_size) const
 
1152
                        {
 
1153
                        if (size() != required_size)
 
1154
                        throw IndexError ("Unexpected SeqBase<T> length.");
 
1155
                        }
 
1156
 
 
1157
                void verify_length(size_type min_size, size_type max_size) const
 
1158
                        {
 
1159
                        size_type n = size();
 
1160
                        if (n < min_size || n > max_size)
 
1161
                        throw IndexError ("Unexpected SeqBase<T> length.");
 
1162
                        }
 
1163
 
 
1164
                class iterator
 
1165
                        : public random_access_iterator_parent(seqref<T>)
 
1166
                        {
 
1167
                protected:
 
1168
                        friend class SeqBase<T>;
 
1169
                        SeqBase<T>* seq;
 
1170
                        int count;
 
1171
 
 
1172
                public:
 
1173
                        ~iterator ()
 
1174
                                {}
 
1175
 
 
1176
                        iterator ()
 
1177
                                : seq( 0 )
 
1178
                                , count( 0 )
 
1179
                                {}
 
1180
 
 
1181
                        iterator (SeqBase<T>* s, int where)
 
1182
                                : seq( s )
 
1183
                                , count( where )
 
1184
                                {}
 
1185
 
 
1186
                        iterator (const iterator& other)
 
1187
                                : seq( other.seq )
 
1188
                                , count( other.count )
 
1189
                                {}
 
1190
 
 
1191
                        bool eql (const iterator& other) const
 
1192
                                {
 
1193
                                return (*seq == *other.seq) && (count == other.count);
 
1194
                                }
 
1195
 
 
1196
                        bool neq (const iterator& other) const
 
1197
                                {
 
1198
                                return (*seq != *other.seq) || (count != other.count);
 
1199
                                }
 
1200
 
 
1201
                        bool lss (const iterator& other) const
 
1202
                                {
 
1203
                                return (count < other.count);
 
1204
                                }
 
1205
 
 
1206
                        bool gtr (const iterator& other) const
 
1207
                                {
 
1208
                                return (count > other.count);
 
1209
                                }
 
1210
 
 
1211
                        bool leq (const iterator& other) const
 
1212
                                {
 
1213
                                return (count <= other.count);
 
1214
                                }
 
1215
 
 
1216
                        bool geq (const iterator& other) const
 
1217
                                {
 
1218
                                return (count >= other.count);
 
1219
                                }
 
1220
 
 
1221
                        seqref<T> operator*()
 
1222
                                {
 
1223
                                return seqref<T>(*seq, count);
 
1224
                                }
 
1225
 
 
1226
                        seqref<T> operator[] (sequence_index_type i)
 
1227
                                {
 
1228
                                return seqref<T>(*seq, count + i);
 
1229
                                }
 
1230
 
 
1231
                        iterator& operator=(const iterator& other)
 
1232
                                {
 
1233
                                if (this == &other) return *this;
 
1234
                                seq = other.seq;
 
1235
                                count = other.count;
 
1236
                                return *this;
 
1237
                                }
 
1238
 
 
1239
                        iterator operator+(int n) const
 
1240
                                {
 
1241
                                return iterator(seq, count + n);
 
1242
                                }
 
1243
 
 
1244
                        iterator operator-(int n) const
 
1245
                                {
 
1246
                                return iterator(seq, count - n);
 
1247
                                }
 
1248
 
 
1249
                        iterator& operator+=(int n)
 
1250
                                {
 
1251
                                count = count + n;
 
1252
                                return *this;
 
1253
                                }
 
1254
 
 
1255
                        iterator& operator-=(int n)
 
1256
                                {
 
1257
                                count = count - n;
 
1258
                                return *this;
 
1259
                                }
 
1260
 
 
1261
                        int operator-(const iterator& other) const
 
1262
                                {
 
1263
                                if (*seq != *other.seq)
 
1264
                                throw RuntimeError ("SeqBase<T>::iterator comparison error");
 
1265
                                return count - other.count;
 
1266
                                }
 
1267
 
 
1268
                        // prefix ++
 
1269
                        iterator& operator++ ()
 
1270
                                { count++; return *this;}
 
1271
                        // postfix ++
 
1272
                        iterator operator++ (int)
 
1273
                                { return iterator(seq, count++);}
 
1274
                        // prefix --
 
1275
                        iterator& operator-- ()
 
1276
                                { count--; return *this;}
 
1277
                        // postfix --
 
1278
                        iterator operator-- (int)
 
1279
                                { return iterator(seq, count--);}
 
1280
 
 
1281
                        std::string diagnose() const
 
1282
                                {
 
1283
                                std::OSTRSTREAM oss;
 
1284
                                oss << "iterator diagnosis " << seq << ", " << count << std::ends;
 
1285
                                return std::string(oss.str());
 
1286
                                }
 
1287
                        };    // end of class SeqBase<T>::iterator
 
1288
 
 
1289
                iterator begin ()
 
1290
                        {
 
1291
                        return iterator(this, 0);
 
1292
                        }
 
1293
 
 
1294
                iterator end ()
 
1295
                        {
 
1296
                        return iterator(this, length());
 
1297
                        }
 
1298
 
 
1299
                class const_iterator
 
1300
                        : public random_access_iterator_parent(const Object)
 
1301
                        {
 
1302
                protected:
 
1303
                        friend class SeqBase<T>;
 
1304
                        const SeqBase<T>* seq;
 
1305
                        sequence_index_type count;
 
1306
 
 
1307
                public:
 
1308
                        ~const_iterator ()
 
1309
                                {}
 
1310
 
 
1311
                        const_iterator ()
 
1312
                                : seq( 0 )
 
1313
                                , count( 0 )
 
1314
                                {}
 
1315
 
 
1316
                        const_iterator (const SeqBase<T>* s, int where)
 
1317
                                : seq( s )
 
1318
                                , count( where )
 
1319
                                {}
 
1320
 
 
1321
                        const_iterator(const const_iterator& other)
 
1322
                                : seq( other.seq )
 
1323
                                , count( other.count )
 
1324
                                {}
 
1325
 
 
1326
                        const T operator*() const
 
1327
                                {
 
1328
                                return seq->getItem(count);
 
1329
                                }
 
1330
 
 
1331
                        const T operator[] (sequence_index_type i) const
 
1332
                                {
 
1333
                                return seq->getItem(count + i);
 
1334
                                }
 
1335
 
 
1336
                        const_iterator& operator=(const const_iterator& other)
 
1337
                                {
 
1338
                                if (this == &other) return *this;
 
1339
                                seq = other.seq;
 
1340
                                count = other.count;
 
1341
                                return *this;
 
1342
                                }
 
1343
 
 
1344
                        const_iterator operator+(int n) const
 
1345
                                {
 
1346
                                return const_iterator(seq, count + n);
 
1347
                                }
 
1348
 
 
1349
                        bool eql (const const_iterator& other) const
 
1350
                                {
 
1351
                                return (*seq == *other.seq) && (count == other.count);
 
1352
                                }
 
1353
 
 
1354
                        bool neq (const const_iterator& other) const
 
1355
                                {
 
1356
                                return (*seq != *other.seq) || (count != other.count);
 
1357
                                }
 
1358
 
 
1359
                        bool lss (const const_iterator& other) const
 
1360
                                {
 
1361
                                return (count < other.count);
 
1362
                                }
 
1363
 
 
1364
                        bool gtr (const const_iterator& other) const
 
1365
                                {
 
1366
                                return (count > other.count);
 
1367
                                }
 
1368
 
 
1369
                        bool leq (const const_iterator& other) const
 
1370
                                {
 
1371
                                return (count <= other.count);
 
1372
                                }
 
1373
 
 
1374
                        bool geq (const const_iterator& other) const
 
1375
                                {
 
1376
                                return (count >= other.count);
 
1377
                                }
 
1378
 
 
1379
                        const_iterator operator-(int n)
 
1380
                                {
 
1381
                                return const_iterator(seq, count - n);
 
1382
                                }
 
1383
 
 
1384
                        const_iterator& operator+=(int n)
 
1385
                                {
 
1386
                                count = count + n;
 
1387
                                return *this;
 
1388
                                }
 
1389
 
 
1390
                        const_iterator& operator-=(int n)
 
1391
                                {
 
1392
                                count = count - n;
 
1393
                                return *this;
 
1394
                                }
 
1395
 
 
1396
                        int operator-(const const_iterator& other) const
 
1397
                                {
 
1398
                                if (*seq != *other.seq)
 
1399
                                throw RuntimeError ("SeqBase<T>::const_iterator::- error");
 
1400
                                return count - other.count;
 
1401
                                }
 
1402
                        // prefix ++
 
1403
                        const_iterator& operator++ ()
 
1404
                                { count++; return *this;}
 
1405
                        // postfix ++
 
1406
                        const_iterator operator++ (int)
 
1407
                                { return const_iterator(seq, count++);}
 
1408
                        // prefix --
 
1409
                        const_iterator& operator-- ()
 
1410
                                { count--; return *this;}
 
1411
                        // postfix --
 
1412
                        const_iterator operator-- (int)
 
1413
                                { return const_iterator(seq, count--);}
 
1414
                        };    // end of class SeqBase<T>::const_iterator
 
1415
 
 
1416
                const_iterator begin () const
 
1417
                        {
 
1418
                        return const_iterator(this, 0);
 
1419
                        }
 
1420
 
 
1421
                const_iterator end () const
 
1422
                        {
 
1423
                        return const_iterator(this, length());
 
1424
                        }
 
1425
                };
 
1426
 
 
1427
        // Here's an important typedef you might miss if reading too fast...
 
1428
        typedef SeqBase<Object> Sequence;
 
1429
 
 
1430
        template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
 
1431
        template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
 
1432
        template <TEMPLATE_TYPENAME T> bool operator< (const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
 
1433
        template <TEMPLATE_TYPENAME T> bool operator> (const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
 
1434
        template <TEMPLATE_TYPENAME T> bool operator<=(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
 
1435
        template <TEMPLATE_TYPENAME T> bool operator>=(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
 
1436
 
 
1437
        template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
 
1438
        template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
 
1439
        template <TEMPLATE_TYPENAME T> bool operator< (const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
 
1440
        template <TEMPLATE_TYPENAME T> bool operator> (const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
 
1441
        template <TEMPLATE_TYPENAME T> bool operator<=(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
 
1442
        template <TEMPLATE_TYPENAME T> bool operator>=(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right); 
 
1443
 
 
1444
 
 
1445
        extern bool operator==(const Sequence::iterator& left, const Sequence::iterator& right);
 
1446
        extern bool operator!=(const Sequence::iterator& left, const Sequence::iterator& right);
 
1447
        extern bool operator< (const Sequence::iterator& left, const Sequence::iterator& right);
 
1448
        extern bool operator> (const Sequence::iterator& left, const Sequence::iterator& right);
 
1449
        extern bool operator<=(const Sequence::iterator& left, const Sequence::iterator& right);
 
1450
        extern bool operator>=(const Sequence::iterator& left, const Sequence::iterator& right);
 
1451
 
 
1452
        extern bool operator==(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
 
1453
        extern bool operator!=(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
 
1454
        extern bool operator< (const Sequence::const_iterator& left, const Sequence::const_iterator& right);
 
1455
        extern bool operator> (const Sequence::const_iterator& left, const Sequence::const_iterator& right);
 
1456
        extern bool operator<=(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
 
1457
        extern bool operator>=(const Sequence::const_iterator& left, const Sequence::const_iterator& right); 
 
1458
 
 
1459
        // ==================================================
 
1460
        // class Char
 
1461
        // Python strings return strings as individual elements.
 
1462
        // I'll try having a class Char which is a String of length 1
 
1463
        //
 
1464
        typedef std::basic_string<Py_UNICODE> unicodestring;
 
1465
        extern Py_UNICODE unicode_null_string[1];
 
1466
 
 
1467
        class Char: public Object
 
1468
                {
 
1469
        public:
 
1470
                explicit Char (PyObject *pyob, bool owned = false): Object(pyob, owned)
 
1471
                        {
 
1472
                        validate();
 
1473
                        }
 
1474
 
 
1475
                Char (const Object& ob): Object(ob)
 
1476
                        {
 
1477
                        validate();
 
1478
                        }
 
1479
 
 
1480
                Char (const std::string& v = "")
 
1481
                        :Object(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true)
 
1482
                        {
 
1483
                        validate();
 
1484
                        }
 
1485
 
 
1486
                Char (char v)
 
1487
                        : Object(PyString_FromStringAndSize (&v, 1), true)
 
1488
                        {
 
1489
                        validate();
 
1490
                        }
 
1491
 
 
1492
                Char (Py_UNICODE v)
 
1493
                        : Object(PyUnicode_FromUnicode (&v, 1), true)
 
1494
                        {
 
1495
                        validate();
 
1496
                        }
 
1497
                // Assignment acquires new ownership of pointer
 
1498
                Char& operator= (const Object& rhs)
 
1499
                        {
 
1500
                        return (*this = *rhs);
 
1501
                        }
 
1502
 
 
1503
                Char& operator= (PyObject* rhsp)
 
1504
                        {
 
1505
                        if(ptr() == rhsp) return *this;
 
1506
                        set (rhsp);
 
1507
                        return *this;
 
1508
                        }
 
1509
 
 
1510
                // Membership
 
1511
                virtual bool accepts (PyObject *pyob) const
 
1512
                        {
 
1513
                        return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob)) && PySequence_Length (pyob) == 1;
 
1514
                        }
 
1515
 
 
1516
                // Assignment from C string
 
1517
                Char& operator= (const std::string& v)
 
1518
                        {
 
1519
                        set(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true);
 
1520
                        return *this;
 
1521
                        }
 
1522
 
 
1523
                Char& operator= (char v)
 
1524
                        {
 
1525
                        set(PyString_FromStringAndSize (&v, 1), true);
 
1526
                        return *this;
 
1527
                        }
 
1528
 
 
1529
                Char& operator= (const unicodestring& v)
 
1530
                        {
 
1531
                        set(PyUnicode_FromUnicode (const_cast<Py_UNICODE*>(v.data()),1), true);
 
1532
                        return *this;
 
1533
                        }
 
1534
 
 
1535
                Char& operator= (Py_UNICODE v)
 
1536
                        {
 
1537
                        set(PyUnicode_FromUnicode (&v, 1), true);
 
1538
                        return *this;
 
1539
                        }
 
1540
 
 
1541
                // Conversion
 
1542
                operator String() const;
 
1543
 
 
1544
                operator std::string () const
 
1545
                        {
 
1546
                        return std::string(PyString_AsString (ptr()));
 
1547
                        }
 
1548
                };
 
1549
 
 
1550
        class String: public SeqBase<Char>
 
1551
                {
 
1552
        public:
 
1553
                virtual size_type capacity() const
 
1554
                        {
 
1555
                        return max_size();
 
1556
                        }
 
1557
 
 
1558
                explicit String (PyObject *pyob, bool owned = false): SeqBase<Char>(pyob, owned)
 
1559
                        {
 
1560
                        validate();
 
1561
                        }
 
1562
 
 
1563
                String (const Object& ob): SeqBase<Char>(ob)
 
1564
                        {
 
1565
                        validate();
 
1566
                        }
 
1567
 
 
1568
                String()
 
1569
                        : SeqBase<Char>( PyString_FromStringAndSize( "", 0 ), true )
 
1570
                        {
 
1571
                        validate();
 
1572
                        }
 
1573
 
 
1574
                String( const std::string& v )
 
1575
                        : SeqBase<Char>( PyString_FromStringAndSize( const_cast<char*>(v.data()),
 
1576
                                static_cast<int>( v.length() ) ), true )
 
1577
                        {
 
1578
                        validate();
 
1579
                        }
 
1580
 
 
1581
                String( const char *s, const char *encoding, const char *error="strict" )
 
1582
                        : SeqBase<Char>( PyUnicode_Decode( s, strlen( s ), encoding, error ), true )
 
1583
                        {
 
1584
                        validate();
 
1585
                        }
 
1586
 
 
1587
                String( const char *s, int len, const char *encoding, const char *error="strict" )
 
1588
                        : SeqBase<Char>( PyUnicode_Decode( s, len, encoding, error ), true )
 
1589
                        {
 
1590
                        validate();
 
1591
                        }
 
1592
 
 
1593
                String( const std::string &s, const char *encoding, const char *error="strict" )
 
1594
                        : SeqBase<Char>( PyUnicode_Decode( s.c_str(), s.length(), encoding, error ), true )
 
1595
                        {
 
1596
                        validate();
 
1597
                        }
 
1598
 
 
1599
                String( const std::string& v, std::string::size_type vsize )
 
1600
                        : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v.data()),
 
1601
                                        static_cast<int>( vsize ) ), true)
 
1602
                        {
 
1603
                        validate();
 
1604
                        }
 
1605
 
 
1606
                String( const char *v, int vsize )
 
1607
                        : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v), vsize ), true )
 
1608
                        {
 
1609
                        validate();
 
1610
                        }
 
1611
 
 
1612
                String( const char* v )
 
1613
                        : SeqBase<Char>( PyString_FromString( v ), true )
 
1614
                        {
 
1615
                        validate();
 
1616
                        }
 
1617
 
 
1618
                // Assignment acquires new ownership of pointer
 
1619
                String& operator= ( const Object& rhs )
 
1620
                        {
 
1621
                        return *this = *rhs;
 
1622
                        }
 
1623
 
 
1624
                String& operator= (PyObject* rhsp)
 
1625
                        {
 
1626
                        if( ptr() == rhsp )
 
1627
                                return *this;
 
1628
                        set (rhsp);
 
1629
                        return *this;
 
1630
                        }
 
1631
                // Membership
 
1632
                virtual bool accepts (PyObject *pyob) const
 
1633
                        {
 
1634
                        return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob));
 
1635
                        }
 
1636
 
 
1637
                // Assignment from C string
 
1638
                String& operator= (const std::string& v)
 
1639
                        {
 
1640
                        set( PyString_FromStringAndSize( const_cast<char*>( v.data() ),
 
1641
                                        static_cast<int>( v.length() ) ), true );
 
1642
                        return *this;
 
1643
                        }
 
1644
                String& operator= (const unicodestring& v)
 
1645
                        {
 
1646
                        set( PyUnicode_FromUnicode( const_cast<Py_UNICODE*>( v.data() ),
 
1647
                                        static_cast<int>( v.length() ) ), true );
 
1648
                        return *this;
 
1649
                        }
 
1650
 
 
1651
 
 
1652
                // Encode
 
1653
                String encode( const char *encoding, const char *error="strict" )
 
1654
                        {
 
1655
                        if( isUnicode() )
 
1656
                        {
 
1657
                                return String( PyUnicode_AsEncodedString( ptr(), encoding, error ) );
 
1658
                        }
 
1659
                        else
 
1660
                        {
 
1661
                                return String( PyString_AsEncodedObject( ptr(), encoding, error ) );
 
1662
                        }
 
1663
                        }
 
1664
 
 
1665
                String decode( const char *encoding, const char *error="strict" )
 
1666
                        {
 
1667
                        return Object( PyString_AsDecodedObject( ptr(), encoding, error ) );
 
1668
                        }
 
1669
 
 
1670
                // Queries
 
1671
                virtual size_type size () const
 
1672
                        {
 
1673
                        if( isUnicode() )
 
1674
                        {
 
1675
                                return static_cast<size_type>( PyUnicode_GET_SIZE (ptr()) );
 
1676
                        }
 
1677
                        else
 
1678
                        {
 
1679
                                return static_cast<size_type>( PyString_Size (ptr()) );
 
1680
                        }
 
1681
                        }
 
1682
 
 
1683
                operator std::string () const
 
1684
                        {
 
1685
                        return as_std_string();
 
1686
                        }
 
1687
 
 
1688
                std::string as_std_string() const
 
1689
                        {
 
1690
                        if( isUnicode() )
 
1691
                        {
 
1692
                                throw TypeError("cannot return std::string from Unicode object");
 
1693
                        }
 
1694
                        else
 
1695
                        {
 
1696
                                return std::string( PyString_AsString( ptr() ), static_cast<size_type>( PyString_Size( ptr() ) ) );
 
1697
                        }
 
1698
                        }
 
1699
 
 
1700
                unicodestring as_unicodestring() const
 
1701
                        {
 
1702
                        if( isUnicode() )
 
1703
                        {
 
1704
                                return unicodestring( PyUnicode_AS_UNICODE( ptr() ),
 
1705
                                        static_cast<size_type>( PyUnicode_GET_SIZE( ptr() ) ) );
 
1706
                        }
 
1707
                        else
 
1708
                        {
 
1709
                                throw TypeError("can only return unicodestring from Unicode object");
 
1710
                        }
 
1711
                        }
 
1712
                };
 
1713
 
 
1714
        // ==================================================
 
1715
        // class Tuple
 
1716
        class Tuple: public Sequence
 
1717
                {
 
1718
        public:
 
1719
                virtual void setItem (sequence_index_type offset, const Object&ob)
 
1720
                        {
 
1721
                        // note PyTuple_SetItem is a thief...
 
1722
                        if(PyTuple_SetItem (ptr(), offset, new_reference_to(ob)) == -1)
 
1723
                                {
 
1724
                                throw Exception();
 
1725
                                }
 
1726
                        }
 
1727
 
 
1728
                // Constructor
 
1729
                explicit Tuple (PyObject *pyob, bool owned = false): Sequence (pyob, owned)
 
1730
                        {
 
1731
                        validate();
 
1732
                        }
 
1733
 
 
1734
                Tuple (const Object& ob): Sequence(ob)
 
1735
                        {
 
1736
                        validate();
 
1737
                        }
 
1738
 
 
1739
                // New tuple of a given size
 
1740
                explicit Tuple (int size = 0)
 
1741
                        {
 
1742
                        set(PyTuple_New (size), true);
 
1743
                        validate ();
 
1744
                        for (sequence_index_type i=0; i < size; i++)
 
1745
                                {
 
1746
                                if(PyTuple_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
 
1747
                                        {
 
1748
                                        throw Exception();
 
1749
                                        }
 
1750
                                }
 
1751
                        }
 
1752
                // Tuple from any sequence
 
1753
                explicit Tuple (const Sequence& s)
 
1754
                        {
 
1755
                        sequence_index_type limit( sequence_index_type( s.length() ) );
 
1756
 
 
1757
                        set(PyTuple_New (limit), true);
 
1758
                        validate();
 
1759
                        
 
1760
                        for(sequence_index_type i=0; i < limit; i++)
 
1761
                                {
 
1762
                                if(PyTuple_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
 
1763
                                        {
 
1764
                                        throw Exception();
 
1765
                                        }
 
1766
                                }
 
1767
                        }
 
1768
                // Assignment acquires new ownership of pointer
 
1769
 
 
1770
                Tuple& operator= (const Object& rhs)
 
1771
                        {
 
1772
                        return (*this = *rhs);
 
1773
                        }
 
1774
 
 
1775
                Tuple& operator= (PyObject* rhsp)
 
1776
                        {
 
1777
                        if(ptr() == rhsp) return *this;
 
1778
                        set (rhsp);
 
1779
                        return *this;
 
1780
                        }
 
1781
                // Membership
 
1782
                virtual bool accepts (PyObject *pyob) const
 
1783
                        {
 
1784
                        return pyob && Py::_Tuple_Check (pyob);
 
1785
                        }
 
1786
 
 
1787
                Tuple getSlice (int i, int j) const
 
1788
                        {
 
1789
                        return Tuple (PySequence_GetSlice (ptr(), i, j), true);
 
1790
                        }
 
1791
 
 
1792
                };
 
1793
 
 
1794
        // ==================================================
 
1795
        // class List
 
1796
 
 
1797
        class List: public Sequence
 
1798
                {
 
1799
        public:
 
1800
                // Constructor
 
1801
                explicit List (PyObject *pyob, bool owned = false): Sequence(pyob, owned)
 
1802
                        {
 
1803
                        validate();
 
1804
                        }
 
1805
                List (const Object& ob): Sequence(ob)
 
1806
                        {
 
1807
                        validate();
 
1808
                        }
 
1809
                // Creation at a fixed size
 
1810
                List (int size = 0)
 
1811
                        {
 
1812
                        set(PyList_New (size), true);
 
1813
                        validate();
 
1814
                        for (sequence_index_type i=0; i < size; i++)
 
1815
                                {
 
1816
                                if(PyList_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
 
1817
                                        {
 
1818
                                        throw Exception();
 
1819
                                        }
 
1820
                                }
 
1821
                        }
 
1822
 
 
1823
                // List from a sequence
 
1824
                List (const Sequence& s): Sequence()
 
1825
                        {
 
1826
                        int n = s.length();
 
1827
                        set(PyList_New (n), true);
 
1828
                        validate();
 
1829
                        for (sequence_index_type i=0; i < n; i++)
 
1830
                                {
 
1831
                                if(PyList_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
 
1832
                                        {
 
1833
                                        throw Exception();
 
1834
                                        }
 
1835
                                }
 
1836
                        }
 
1837
 
 
1838
                virtual size_type capacity() const
 
1839
                        {
 
1840
                        return max_size();
 
1841
                        }
 
1842
                // Assignment acquires new ownership of pointer
 
1843
 
 
1844
                List& operator= (const Object& rhs)
 
1845
                        {
 
1846
                        return (*this = *rhs);
 
1847
                        }
 
1848
 
 
1849
                List& operator= (PyObject* rhsp)
 
1850
                        {
 
1851
                        if(ptr() == rhsp) return *this;
 
1852
                        set (rhsp);
 
1853
                        return *this;
 
1854
                        }
 
1855
                // Membership
 
1856
                virtual bool accepts (PyObject *pyob) const
 
1857
                        {
 
1858
                        return pyob && Py::_List_Check (pyob);
 
1859
                        }
 
1860
 
 
1861
                List getSlice (int i, int j) const
 
1862
                        {
 
1863
                        return List (PyList_GetSlice (ptr(), i, j), true);
 
1864
                        }
 
1865
 
 
1866
                void setSlice (int i, int j, const Object& v)
 
1867
                        {
 
1868
                        if(PyList_SetSlice (ptr(), i, j, *v) == -1)
 
1869
                                {
 
1870
                                throw Exception();
 
1871
                                }
 
1872
                        }
 
1873
 
 
1874
                void append (const Object& ob)
 
1875
                        {
 
1876
                        if(PyList_Append (ptr(), *ob) == -1)
 
1877
                                {
 
1878
                                throw Exception();
 
1879
                                }
 
1880
                        }
 
1881
 
 
1882
                void insert (int i, const Object& ob)
 
1883
                        {
 
1884
                        if(PyList_Insert (ptr(), i, *ob) == -1)
 
1885
                                {
 
1886
                                throw Exception();
 
1887
                                }
 
1888
                        }
 
1889
 
 
1890
                void sort ()
 
1891
                        {
 
1892
                        if(PyList_Sort(ptr()) == -1)
 
1893
                                {
 
1894
                                throw Exception();
 
1895
                                }
 
1896
                        }
 
1897
 
 
1898
                void reverse ()
 
1899
                        {
 
1900
                        if(PyList_Reverse(ptr()) == -1)
 
1901
                                {
 
1902
                                throw Exception();
 
1903
                                }
 
1904
                        }
 
1905
                };
 
1906
 
 
1907
 
 
1908
        // Mappings
 
1909
        // ==================================================
 
1910
        template<TEMPLATE_TYPENAME T>
 
1911
        class mapref
 
1912
                {
 
1913
        protected:
 
1914
                MapBase<T>& s; // the map
 
1915
                Object key; // item key
 
1916
                T the_item;
 
1917
 
 
1918
        public:
 
1919
                mapref<T> (MapBase<T>& map, const std::string& k)
 
1920
                        : s(map), the_item()
 
1921
                        {
 
1922
                        key = String(k);
 
1923
                        if(map.hasKey(key)) the_item = map.getItem(key);
 
1924
                        };
 
1925
 
 
1926
                mapref<T> (MapBase<T>& map, const Object& k)
 
1927
                        : s(map), key(k), the_item()
 
1928
                        {
 
1929
                        if(map.hasKey(key)) the_item = map.getItem(key);
 
1930
                        };
 
1931
 
 
1932
                ~mapref<T>()
 
1933
                        {}
 
1934
 
 
1935
                // MapBase<T> stuff
 
1936
                // lvalue
 
1937
                mapref<T>& operator=(const mapref<T>& other)
 
1938
                        {
 
1939
                        if(this == &other) return *this;
 
1940
                        the_item = other.the_item;
 
1941
                        s.setItem(key, other.the_item);
 
1942
                        return *this;
 
1943
                        };
 
1944
 
 
1945
                mapref<T>& operator= (const T& ob)
 
1946
                        {
 
1947
                        the_item = ob;
 
1948
                        s.setItem (key, ob);
 
1949
                        return *this;
 
1950
                        }
 
1951
 
 
1952
                // rvalue
 
1953
                operator T() const
 
1954
                        {
 
1955
                        return the_item;
 
1956
                        }
 
1957
 
 
1958
                // forward everything else to the_item
 
1959
                PyObject* ptr () const
 
1960
                        {
 
1961
                        return the_item.ptr();
 
1962
                        }
 
1963
 
 
1964
                int reference_count () const
 
1965
                        { // the mapref count
 
1966
                        return the_item.reference_count();
 
1967
                        }
 
1968
 
 
1969
                Type type () const
 
1970
                        {
 
1971
                        return the_item.type();
 
1972
                        }
 
1973
 
 
1974
                String str () const
 
1975
                        {
 
1976
                        return the_item.str();
 
1977
                        }
 
1978
 
 
1979
                String repr () const
 
1980
                        {
 
1981
                        return the_item.repr();
 
1982
                        }
 
1983
 
 
1984
                bool hasAttr (const std::string& attr_name) const
 
1985
                        {
 
1986
                        return the_item.hasAttr(attr_name);
 
1987
                        }
 
1988
 
 
1989
                Object getAttr (const std::string& attr_name) const
 
1990
                        {
 
1991
                        return the_item.getAttr(attr_name);
 
1992
                        }
 
1993
 
 
1994
                Object getItem (const Object& k) const
 
1995
                        {
 
1996
                        return the_item.getItem(k);
 
1997
                        }
 
1998
 
 
1999
                long hashValue () const
 
2000
                        {
 
2001
                        return the_item.hashValue();
 
2002
                        }
 
2003
 
 
2004
                bool isCallable () const
 
2005
                        {
 
2006
                        return the_item.isCallable();
 
2007
                        }
 
2008
 
 
2009
                bool isList () const
 
2010
                        {
 
2011
                        return the_item.isList();
 
2012
                        }
 
2013
 
 
2014
                bool isMapping () const
 
2015
                        {
 
2016
                        return the_item.isMapping();
 
2017
                        }
 
2018
 
 
2019
                bool isNumeric () const
 
2020
                        {
 
2021
                        return the_item.isNumeric();
 
2022
                        }
 
2023
 
 
2024
                bool isSequence () const
 
2025
                        {
 
2026
                        return the_item.isSequence();
 
2027
                        }
 
2028
 
 
2029
                bool isTrue () const
 
2030
                        {
 
2031
                        return the_item.isTrue();
 
2032
                        }
 
2033
 
 
2034
                bool isType (const Type& t) const
 
2035
                        {
 
2036
                        return the_item.isType (t);
 
2037
                        }
 
2038
 
 
2039
                bool isTuple() const
 
2040
                        {
 
2041
                        return the_item.isTuple();
 
2042
                        }
 
2043
 
 
2044
                bool isString() const
 
2045
                        {
 
2046
                        return the_item.isString();
 
2047
                        }
 
2048
 
 
2049
                // Commands
 
2050
                void setAttr (const std::string& attr_name, const Object& value)
 
2051
                        {
 
2052
                        the_item.setAttr(attr_name, value);
 
2053
                        }
 
2054
 
 
2055
                void delAttr (const std::string& attr_name)
 
2056
                        {
 
2057
                        the_item.delAttr(attr_name);
 
2058
                        }
 
2059
 
 
2060
                void delItem (const Object& k)
 
2061
                        {
 
2062
                        the_item.delItem(k);
 
2063
                        }
 
2064
                }; // end of mapref
 
2065
 
 
2066
        // TMM: now for mapref<T>
 
2067
        template< class T >
 
2068
        bool operator==(const mapref<T>& left, const mapref<T>& right)
 
2069
                {
 
2070
                return true;    // NOT completed.
 
2071
                }
 
2072
 
 
2073
        template< class T >
 
2074
        bool operator!=(const mapref<T>& left, const mapref<T>& right)
 
2075
                {
 
2076
                return true;    // not completed.
 
2077
                }
 
2078
 
 
2079
        template<TEMPLATE_TYPENAME T>
 
2080
        class MapBase: public Object
 
2081
                {
 
2082
        protected:
 
2083
                explicit MapBase<T>()
 
2084
                        {}
 
2085
        public:
 
2086
                // reference: proxy class for implementing []
 
2087
                // TMM: 26Jun'01 - the types
 
2088
                // If you assume that Python mapping is a hash_map...
 
2089
                // hash_map::value_type is not assignable, but
 
2090
                // (*it).second = data must be a valid expression
 
2091
                typedef size_t size_type;
 
2092
                typedef Object key_type;
 
2093
                typedef mapref<T> data_type;
 
2094
                typedef std::pair< const T, T > value_type;
 
2095
                typedef std::pair< const T, mapref<T> > reference;
 
2096
                typedef const std::pair< const T, const T > const_reference;
 
2097
                typedef std::pair< const T, mapref<T> > pointer;
 
2098
 
 
2099
                // Constructor
 
2100
                explicit MapBase<T> (PyObject *pyob, bool owned = false): Object(pyob, owned)
 
2101
                        {
 
2102
                        validate();
 
2103
                        }
 
2104
 
 
2105
                // TMM: 02Jul'01 - changed MapBase<T> to Object in next line
 
2106
                MapBase<T> (const Object& ob): Object(ob)
 
2107
                        {
 
2108
                        validate();
 
2109
                        }
 
2110
 
 
2111
                // Assignment acquires new ownership of pointer
 
2112
                MapBase<T>& operator= (const Object& rhs)
 
2113
                        {
 
2114
                        return (*this = *rhs);
 
2115
                        }
 
2116
 
 
2117
                MapBase<T>& operator= (PyObject* rhsp)
 
2118
                        {
 
2119
                        if(ptr() == rhsp) return *this;
 
2120
                        set (rhsp);
 
2121
                        return *this;
 
2122
                        }
 
2123
                // Membership
 
2124
                virtual bool accepts (PyObject *pyob) const
 
2125
                        {
 
2126
                        return pyob && PyMapping_Check(pyob);
 
2127
                        }
 
2128
 
 
2129
                // Clear -- PyMapping Clear is missing
 
2130
                //
 
2131
 
 
2132
                void clear ()
 
2133
                        {
 
2134
                        List k = keys();
 
2135
                        for(List::iterator i = k.begin(); i != k.end(); i++)
 
2136
                                {
 
2137
                                delItem(*i);
 
2138
                                }
 
2139
                        }
 
2140
 
 
2141
                virtual size_type size() const
 
2142
                        {
 
2143
                        return PyMapping_Length (ptr());
 
2144
                        }
 
2145
 
 
2146
                // Element Access
 
2147
                T operator[](const std::string& key) const
 
2148
                        {
 
2149
                        return getItem(key);
 
2150
                        }
 
2151
 
 
2152
                T operator[](const Object& key) const
 
2153
                        {
 
2154
                        return getItem(key);
 
2155
                        }
 
2156
 
 
2157
                mapref<T> operator[](const std::string& key)
 
2158
                        {
 
2159
                        return mapref<T>(*this, key);
 
2160
                        }
 
2161
 
 
2162
                mapref<T> operator[](const Object& key)
 
2163
                        {
 
2164
                        return mapref<T>(*this, key);
 
2165
                        }
 
2166
 
 
2167
                int length () const
 
2168
                        {
 
2169
                        return PyMapping_Length (ptr());
 
2170
                        }
 
2171
 
 
2172
                bool hasKey (const std::string& s) const
 
2173
                        {
 
2174
                        return PyMapping_HasKeyString (ptr(),const_cast<char*>(s.c_str())) != 0;
 
2175
                        }
 
2176
 
 
2177
                bool hasKey (const Object& s) const
 
2178
                        {
 
2179
                        return PyMapping_HasKey (ptr(), s.ptr()) != 0;
 
2180
                        }
 
2181
 
 
2182
                T getItem (const std::string& s) const
 
2183
                        {
 
2184
                        return T(
 
2185
                        asObject(PyMapping_GetItemString (ptr(),const_cast<char*>(s.c_str())))
 
2186
                        );
 
2187
                        }
 
2188
 
 
2189
                T getItem (const Object& s) const
 
2190
                        {
 
2191
                        return T(
 
2192
                        asObject(PyObject_GetItem (ptr(), s.ptr()))
 
2193
                        );
 
2194
                        }
 
2195
 
 
2196
                virtual void setItem (const char *s, const Object& ob)
 
2197
                        {
 
2198
                        if (PyMapping_SetItemString (ptr(), const_cast<char*>(s), *ob) == -1)
 
2199
                                {
 
2200
                                throw Exception();
 
2201
                                }
 
2202
                        }
 
2203
 
 
2204
                virtual void setItem (const std::string& s, const Object& ob)
 
2205
                        {
 
2206
                        if (PyMapping_SetItemString (ptr(), const_cast<char*>(s.c_str()), *ob) == -1)
 
2207
                                {
 
2208
                                throw Exception();
 
2209
                                }
 
2210
                        }
 
2211
 
 
2212
                virtual void setItem (const Object& s, const Object& ob)
 
2213
                        {
 
2214
                        if (PyObject_SetItem (ptr(), s.ptr(), ob.ptr()) == -1)
 
2215
                                {
 
2216
                                throw Exception();
 
2217
                                }
 
2218
                        }
 
2219
 
 
2220
                void delItem (const std::string& s)
 
2221
                        {
 
2222
                        if (PyMapping_DelItemString (ptr(), const_cast<char*>(s.c_str())) == -1)
 
2223
                                {
 
2224
                                throw Exception();
 
2225
                                }
 
2226
                        }
 
2227
 
 
2228
                void delItem (const Object& s)
 
2229
                        {
 
2230
                        if (PyMapping_DelItem (ptr(), *s) == -1)
 
2231
                                {
 
2232
                                throw Exception();
 
2233
                                }
 
2234
                        }
 
2235
                // Queries
 
2236
                List keys () const
 
2237
                        {
 
2238
                        return List(PyMapping_Keys(ptr()), true);
 
2239
                        }
 
2240
 
 
2241
                List values () const
 
2242
                        { // each returned item is a (key, value) pair
 
2243
                        return List(PyMapping_Values(ptr()), true);
 
2244
                        }
 
2245
 
 
2246
                List items () const
 
2247
                        {
 
2248
                        return List(PyMapping_Items(ptr()), true);
 
2249
                        }
 
2250
 
 
2251
                // iterators for MapBase<T>
 
2252
                // Added by TMM: 2Jul'01 - NOT COMPLETED
 
2253
                // There is still a bug.  I decided to stop, before fixing the bug, because
 
2254
                // this can't be halfway efficient until Python gets built-in iterators.
 
2255
                // My current soln is to iterate over the map by getting a copy of its keys
 
2256
                // and iterating over that.  Not a good solution.
 
2257
 
 
2258
                // The iterator holds a MapBase<T>* rather than a MapBase<T> because that's
 
2259
                // how the sequence iterator is implemented and it works.  But it does seem
 
2260
                // odd to me - we are iterating over the map object, not the reference.
 
2261
 
 
2262
#if 0   // here is the test code with which I found the (still existing) bug
 
2263
                typedef cxx::Dict       d_t;
 
2264
                d_t     d;
 
2265
                cxx::String     s1("blah");
 
2266
                cxx::String     s2("gorf");
 
2267
                d[ "one" ] = s1;
 
2268
                d[ "two" ] = s1;
 
2269
                d[ "three" ] = s2;
 
2270
                d[ "four" ] = s2;
 
2271
 
 
2272
                d_t::iterator   it;
 
2273
                it = d.begin();         // this (using the assignment operator) is causing
 
2274
                // a problem; if I just use the copy ctor it works fine.
 
2275
                for( ; it != d.end(); ++it )
 
2276
                        {
 
2277
                        d_t::value_type vt( *it );
 
2278
                        cxx::String rs = vt.second.repr();
 
2279
                        std::string ls = rs.operator std::string();
 
2280
                        fprintf( stderr, "%s\n", ls );
 
2281
                        }
 
2282
#endif // 0
 
2283
 
 
2284
                class iterator
 
2285
                        {
 
2286
                        // : public forward_iterator_parent( std::pair<const T,T> ) {
 
2287
                protected:
 
2288
                        typedef std::forward_iterator_tag iterator_category;
 
2289
                        typedef std::pair< const T, T > value_type;
 
2290
                        typedef int difference_type;
 
2291
                        typedef std::pair< const T, mapref<T> > pointer;
 
2292
                        typedef std::pair< const T, mapref<T> > reference;
 
2293
 
 
2294
                        friend class MapBase<T>;
 
2295
                        //
 
2296
                        MapBase<T>* map;
 
2297
                        List    keys;                   // for iterating over the map
 
2298
                        List::iterator  pos;            // index into the keys
 
2299
 
 
2300
                public:
 
2301
                        ~iterator ()
 
2302
                                {}
 
2303
 
 
2304
                        iterator ()
 
2305
                                : map( 0 )
 
2306
                                , keys()
 
2307
                                , pos()
 
2308
                                {}
 
2309
 
 
2310
                        iterator (MapBase<T>* m, bool end = false )
 
2311
                                : map( m )
 
2312
                                , keys( m->keys() )
 
2313
                                , pos( end ? keys.end() : keys.begin() )
 
2314
                                {}
 
2315
 
 
2316
                        iterator (const iterator& other)
 
2317
                                : map( other.map )
 
2318
                                , keys( other.keys )
 
2319
                                , pos( other.pos )
 
2320
                                {}
 
2321
 
 
2322
                        reference operator*()
 
2323
                                {
 
2324
                                Object key = *pos;
 
2325
                                return std::make_pair(key, mapref<T>(*map,key));
 
2326
                                }
 
2327
 
 
2328
                        iterator& operator=(const iterator& other)
 
2329
                                {
 
2330
                                if (this == &other)
 
2331
                                        return *this;
 
2332
                                map = other.map;
 
2333
                                keys = other.keys;
 
2334
                                pos = other.pos;
 
2335
                                return *this;
 
2336
                                }
 
2337
 
 
2338
                        bool eql(const iterator& right) const
 
2339
                                {
 
2340
                                return *map == *right.map && pos == right.pos;
 
2341
                                }
 
2342
                        bool neq( const iterator& right ) const
 
2343
                                {
 
2344
                                return *map != *right.map || pos != right.pos;
 
2345
                                }
 
2346
 
 
2347
                        // pointer operator->() {
 
2348
                        //    return ;
 
2349
                        // }
 
2350
 
 
2351
                        // prefix ++
 
2352
                        iterator& operator++ ()
 
2353
                                { pos++; return *this;}
 
2354
                        // postfix ++
 
2355
                        iterator operator++ (int)
 
2356
                                { return iterator(map, keys, pos++);}
 
2357
                        // prefix --
 
2358
                        iterator& operator-- ()
 
2359
                                { pos--; return *this;}
 
2360
                        // postfix --
 
2361
                        iterator operator-- (int)
 
2362
                                { return iterator(map, keys, pos--);}
 
2363
 
 
2364
                        std::string diagnose() const
 
2365
                                {
 
2366
                                std::OSTRSTREAM oss;
 
2367
                                oss << "iterator diagnosis " << map << ", " << pos << std::ends;
 
2368
                                return std::string(oss.str());
 
2369
                                }
 
2370
                        };    // end of class MapBase<T>::iterator
 
2371
 
 
2372
                iterator begin ()
 
2373
                        {
 
2374
                        return iterator(this);
 
2375
                        }
 
2376
 
 
2377
                iterator end ()
 
2378
                        {
 
2379
                        return iterator(this, true);
 
2380
                        }
 
2381
 
 
2382
                class const_iterator
 
2383
                        {
 
2384
                protected:
 
2385
                        typedef std::forward_iterator_tag iterator_category;
 
2386
                        typedef const std::pair< const T, T > value_type;
 
2387
                        typedef int difference_type;
 
2388
                        typedef const std::pair< const T, T > pointer;
 
2389
                        typedef const std::pair< const T, T > reference;
 
2390
 
 
2391
                        friend class MapBase<T>;
 
2392
                        const MapBase<T>* map;
 
2393
                        List    keys;   // for iterating over the map
 
2394
                        List::iterator  pos;            // index into the keys
 
2395
 
 
2396
                public:
 
2397
                        ~const_iterator ()
 
2398
                                {}
 
2399
 
 
2400
                        const_iterator ()
 
2401
                                : map( 0 )
 
2402
                                , keys()
 
2403
                                , pos()
 
2404
                                {}
 
2405
 
 
2406
                        const_iterator (const MapBase<T>* m, List k, List::iterator p )
 
2407
                                : map( m )
 
2408
                                , keys( k )
 
2409
                                , pos( p )
 
2410
                                {}
 
2411
 
 
2412
                        const_iterator(const const_iterator& other)
 
2413
                                : map( other.map )
 
2414
                                , keys( other.keys )
 
2415
                                , pos( other.pos )
 
2416
                                {}
 
2417
 
 
2418
                        bool eql(const const_iterator& right) const
 
2419
                                {
 
2420
                                return *map == *right.map && pos == right.pos;
 
2421
                                }
 
2422
                        bool neq( const const_iterator& right ) const
 
2423
                                {
 
2424
                                return *map != *right.map || pos != right.pos;
 
2425
                                }
 
2426
 
 
2427
 
 
2428
                        //                      const_reference operator*() {
 
2429
                        //                              Object key = *pos;
 
2430
                        //                              return std::make_pair( key, map->[key] );
 
2431
                        // GCC < 3 barfes on this line at the '['.
 
2432
                        //             }
 
2433
 
 
2434
                        const_iterator& operator=(const const_iterator& other)
 
2435
                                {
 
2436
                                if (this == &other) return *this;
 
2437
                                map = other.map;
 
2438
                                keys = other.keys;
 
2439
                                pos = other.pos;
 
2440
                                return *this;
 
2441
                                }
 
2442
 
 
2443
                        // prefix ++
 
2444
                        const_iterator& operator++ ()
 
2445
                                { pos++; return *this;}
 
2446
                        // postfix ++
 
2447
                        const_iterator operator++ (int)
 
2448
                                { return const_iterator(map, keys, pos++);}
 
2449
                        // prefix --
 
2450
                        const_iterator& operator-- ()
 
2451
                                { pos--; return *this;}
 
2452
                        // postfix --
 
2453
                        const_iterator operator-- (int)
 
2454
                                { return const_iterator(map, keys, pos--);}
 
2455
                        };    // end of class MapBase<T>::const_iterator
 
2456
 
 
2457
                const_iterator begin () const
 
2458
                        {
 
2459
                        return const_iterator(this, 0);
 
2460
                        }
 
2461
 
 
2462
                const_iterator end () const
 
2463
                        {
 
2464
                        return const_iterator(this, length());
 
2465
                        }
 
2466
 
 
2467
                };      // end of MapBase<T>
 
2468
 
 
2469
        typedef MapBase<Object> Mapping;
 
2470
 
 
2471
        template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME MapBase<T>::iterator& left, const EXPLICIT_TYPENAME MapBase<T>::iterator& right);
 
2472
        template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME MapBase<T>::iterator& left, const EXPLICIT_TYPENAME MapBase<T>::iterator& right);
 
2473
        template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME MapBase<T>::const_iterator& left, const EXPLICIT_TYPENAME MapBase<T>::const_iterator& right);
 
2474
        template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME MapBase<T>::const_iterator& left, const EXPLICIT_TYPENAME MapBase<T>::const_iterator& right);
 
2475
 
 
2476
        extern bool operator==(const Mapping::iterator& left, const Mapping::iterator& right);
 
2477
        extern bool operator!=(const Mapping::iterator& left, const Mapping::iterator& right);
 
2478
        extern bool operator==(const Mapping::const_iterator& left, const Mapping::const_iterator& right);
 
2479
        extern bool operator!=(const Mapping::const_iterator& left, const Mapping::const_iterator& right);
 
2480
 
 
2481
 
 
2482
        // ==================================================
 
2483
        // class Dict
 
2484
        class Dict: public Mapping
 
2485
                {
 
2486
        public:
 
2487
                // Constructor
 
2488
                explicit Dict (PyObject *pyob, bool owned=false): Mapping (pyob, owned)
 
2489
                        {
 
2490
                        validate();
 
2491
                        }
 
2492
                Dict (const Dict& ob): Mapping(ob)
 
2493
                        {
 
2494
                        validate();
 
2495
                        }
 
2496
                // Creation
 
2497
                Dict ()
 
2498
                        {
 
2499
                        set(PyDict_New (), true);
 
2500
                        validate();
 
2501
                        }
 
2502
                // Assignment acquires new ownership of pointer
 
2503
 
 
2504
                Dict& operator= (const Object& rhs)
 
2505
                        {
 
2506
                        return (*this = *rhs);
 
2507
                        }
 
2508
 
 
2509
                Dict& operator= (PyObject* rhsp)
 
2510
                        {
 
2511
                        if(ptr() == rhsp) return *this;
 
2512
                        set(rhsp);
 
2513
                        return *this;
 
2514
                        }
 
2515
                // Membership
 
2516
                virtual bool accepts (PyObject *pyob) const
 
2517
                        {
 
2518
                        return pyob && Py::_Dict_Check (pyob);
 
2519
                        }
 
2520
                };
 
2521
 
 
2522
        class Callable: public Object
 
2523
                {
 
2524
        public:
 
2525
                // Constructor
 
2526
                explicit Callable (): Object()  {}
 
2527
                explicit Callable (PyObject *pyob, bool owned = false): Object (pyob, owned)
 
2528
                        {
 
2529
                        validate();
 
2530
                        }
 
2531
 
 
2532
                Callable (const Object& ob): Object(ob)
 
2533
                        {
 
2534
                        validate();
 
2535
                        }
 
2536
 
 
2537
                // Assignment acquires new ownership of pointer
 
2538
 
 
2539
                Callable& operator= (const Object& rhs)
 
2540
                        {
 
2541
                        return (*this = *rhs);
 
2542
                        }
 
2543
 
 
2544
                Callable& operator= (PyObject* rhsp)
 
2545
                        {
 
2546
                        if(ptr() == rhsp) return *this;
 
2547
                        set (rhsp);
 
2548
                        return *this;
 
2549
                        }
 
2550
 
 
2551
                // Membership
 
2552
                virtual bool accepts (PyObject *pyob) const
 
2553
                        {
 
2554
                        return pyob && PyCallable_Check (pyob);
 
2555
                        }
 
2556
 
 
2557
                // Call
 
2558
                Object apply(const Tuple& args) const
 
2559
                        {
 
2560
                        return asObject(PyObject_CallObject(ptr(), args.ptr()));
 
2561
                        }
 
2562
 
 
2563
                // Call with keywords
 
2564
                Object apply(const Tuple& args, const Dict& kw) const
 
2565
                        {
 
2566
                        return asObject( PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ) );
 
2567
                        }
 
2568
 
 
2569
                Object apply(PyObject* pargs = 0) const
 
2570
                        {
 
2571
                        return apply (Tuple(pargs));
 
2572
                        }
 
2573
                };
 
2574
 
 
2575
        class Module: public Object
 
2576
                {
 
2577
        public:
 
2578
                explicit Module (PyObject* pyob, bool owned = false): Object (pyob, owned)
 
2579
                        {
 
2580
                        validate();
 
2581
                        }
 
2582
 
 
2583
                // Construct from module name
 
2584
                explicit Module (const std::string&s): Object()
 
2585
                        {
 
2586
                        PyObject *m = PyImport_AddModule( const_cast<char *>(s.c_str()) );
 
2587
                        set( m, false );
 
2588
                        validate ();
 
2589
                        }
 
2590
 
 
2591
                // Copy constructor acquires new ownership of pointer
 
2592
                Module (const Module& ob): Object(*ob)
 
2593
                        {
 
2594
                        validate();
 
2595
                        }
 
2596
 
 
2597
                Module& operator= (const Object& rhs)
 
2598
                        {
 
2599
                        return (*this = *rhs);
 
2600
                        }
 
2601
 
 
2602
                Module& operator= (PyObject* rhsp)
 
2603
                        {
 
2604
                        if(ptr() == rhsp) return *this;
 
2605
                        set(rhsp);
 
2606
                        return *this;
 
2607
                        }
 
2608
 
 
2609
                Dict getDict()
 
2610
                        {
 
2611
                        return Dict(PyModule_GetDict(ptr()));
 
2612
                        // Caution -- PyModule_GetDict returns borrowed reference!
 
2613
                        }
 
2614
                };
 
2615
 
 
2616
        // Numeric interface
 
2617
        inline Object operator+ (const Object& a)
 
2618
                {
 
2619
                return asObject(PyNumber_Positive(*a));
 
2620
                }
 
2621
        inline Object operator- (const Object& a)
 
2622
                {
 
2623
                return asObject(PyNumber_Negative(*a));
 
2624
                }
 
2625
 
 
2626
        inline Object abs(const Object& a)
 
2627
                {
 
2628
                return asObject(PyNumber_Absolute(*a));
 
2629
                }
 
2630
 
 
2631
        inline std::pair<Object,Object> coerce(const Object& a, const Object& b)
 
2632
                {
 
2633
                PyObject *p1, *p2;
 
2634
                p1 = *a;
 
2635
                p2 = *b;
 
2636
                if(PyNumber_Coerce(&p1,&p2) == -1)
 
2637
                        {
 
2638
                        throw Exception();
 
2639
                        }
 
2640
                return std::pair<Object,Object>(asObject(p1), asObject(p2));
 
2641
                }
 
2642
 
 
2643
        inline Object operator+ (const Object& a, const Object& b)
 
2644
                {
 
2645
                return asObject(PyNumber_Add(*a, *b));
 
2646
                }
 
2647
        inline Object operator+ (const Object& a, int j)
 
2648
                {
 
2649
                return asObject(PyNumber_Add(*a, *Int(j)));
 
2650
                }
 
2651
        inline Object operator+ (const Object& a, double v)
 
2652
                {
 
2653
                return asObject(PyNumber_Add(*a, *Float(v)));
 
2654
                }
 
2655
        inline Object operator+ (int j, const Object& b)
 
2656
                {
 
2657
                return asObject(PyNumber_Add(*Int(j), *b));
 
2658
                }
 
2659
        inline Object operator+ (double v, const Object& b)
 
2660
                {
 
2661
                return asObject(PyNumber_Add(*Float(v), *b));
 
2662
                }
 
2663
 
 
2664
        inline Object operator- (const Object& a, const Object& b)
 
2665
                {
 
2666
                return asObject(PyNumber_Subtract(*a, *b));
 
2667
                }
 
2668
        inline Object operator- (const Object& a, int j)
 
2669
                {
 
2670
                return asObject(PyNumber_Subtract(*a, *Int(j)));
 
2671
                }
 
2672
        inline Object operator- (const Object& a, double v)
 
2673
                {
 
2674
                return asObject(PyNumber_Subtract(*a, *Float(v)));
 
2675
                }
 
2676
        inline Object operator- (int j, const Object& b)
 
2677
                {
 
2678
                return asObject(PyNumber_Subtract(*Int(j), *b));
 
2679
                }
 
2680
        inline Object operator- (double v, const Object& b)
 
2681
                {
 
2682
                return asObject(PyNumber_Subtract(*Float(v), *b));
 
2683
                }
 
2684
 
 
2685
        inline Object operator* (const Object& a, const Object& b)
 
2686
                {
 
2687
                return asObject(PyNumber_Multiply(*a, *b));
 
2688
                }
 
2689
        inline Object operator* (const Object& a, int j)
 
2690
                {
 
2691
                return asObject(PyNumber_Multiply(*a, *Int(j)));
 
2692
                }
 
2693
        inline Object operator* (const Object& a, double v)
 
2694
                {
 
2695
                return asObject(PyNumber_Multiply(*a, *Float(v)));
 
2696
                }
 
2697
        inline Object operator* (int j, const Object& b)
 
2698
                {
 
2699
                return asObject(PyNumber_Multiply(*Int(j), *b));
 
2700
                }
 
2701
        inline Object operator* (double v, const Object& b)
 
2702
                {
 
2703
                return asObject(PyNumber_Multiply(*Float(v), *b));
 
2704
                }
 
2705
 
 
2706
        inline Object operator/ (const Object& a, const Object& b)
 
2707
                {
 
2708
                return asObject(PyNumber_Divide(*a, *b));
 
2709
                }
 
2710
        inline Object operator/ (const Object& a, int j)
 
2711
                {
 
2712
                return asObject(PyNumber_Divide(*a, *Int(j)));
 
2713
                }
 
2714
        inline Object operator/ (const Object& a, double v)
 
2715
                {
 
2716
                return asObject(PyNumber_Divide(*a, *Float(v)));
 
2717
                }
 
2718
        inline Object operator/ (int j, const Object& b)
 
2719
                {
 
2720
                return asObject(PyNumber_Divide(*Int(j), *b));
 
2721
                }
 
2722
        inline Object operator/ (double v, const Object& b)
 
2723
                {
 
2724
                return asObject(PyNumber_Divide(*Float(v), *b));
 
2725
                }
 
2726
 
 
2727
        inline Object operator% (const Object& a, const Object& b)
 
2728
                {
 
2729
                return asObject(PyNumber_Remainder(*a, *b));
 
2730
                }
 
2731
        inline Object operator% (const Object& a, int j)
 
2732
                {
 
2733
                return asObject(PyNumber_Remainder(*a, *Int(j)));
 
2734
                }
 
2735
        inline Object operator% (const Object& a, double v)
 
2736
                {
 
2737
                return asObject(PyNumber_Remainder(*a, *Float(v)));
 
2738
                }
 
2739
        inline Object operator% (int j, const Object& b)
 
2740
                {
 
2741
                return asObject(PyNumber_Remainder(*Int(j), *b));
 
2742
                }
 
2743
        inline Object operator% (double v, const Object& b)
 
2744
                {
 
2745
                return asObject(PyNumber_Remainder(*Float(v), *b));
 
2746
                }
 
2747
 
 
2748
        inline Object type(const Exception&) // return the type of the error
 
2749
                {
 
2750
                PyObject *ptype, *pvalue, *ptrace;
 
2751
                PyErr_Fetch(&ptype, &pvalue, &ptrace);
 
2752
                Object result(pvalue);
 
2753
                PyErr_Restore(ptype, pvalue, ptrace);
 
2754
                return result;
 
2755
                }
 
2756
 
 
2757
        inline Object value(const Exception&) // return the value of the error
 
2758
                {
 
2759
                PyObject *ptype, *pvalue, *ptrace;
 
2760
                PyErr_Fetch(&ptype, &pvalue, &ptrace);
 
2761
                Object result;
 
2762
                if(pvalue) result = pvalue;
 
2763
                PyErr_Restore(ptype, pvalue, ptrace);
 
2764
                return result;
 
2765
                }
 
2766
 
 
2767
        inline Object trace(const Exception&) // return the traceback of the error
 
2768
                {
 
2769
                PyObject *ptype, *pvalue, *ptrace;
 
2770
                PyErr_Fetch(&ptype, &pvalue, &ptrace);
 
2771
                Object result;
 
2772
                if(ptrace) result = pvalue;
 
2773
                PyErr_Restore(ptype, pvalue, ptrace);
 
2774
                return result;
 
2775
                }
 
2776
 
 
2777
 
 
2778
 
 
2779
template<TEMPLATE_TYPENAME T>
 
2780
String seqref<T>::str () const
 
2781
                        {
 
2782
                        return the_item.str();
 
2783
                        }
 
2784
 
 
2785
template<TEMPLATE_TYPENAME T>
 
2786
String seqref<T>::repr () const
 
2787
                        {
 
2788
                        return the_item.repr();
 
2789
                        }
 
2790
 
 
2791
 
 
2792
        } // namespace Py
 
2793
#endif  // __CXX_Objects__h