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
//---------------------------------------------------------------------------//
6
#ifndef __CXX_Objects__h
7
#define __CXX_Objects__h
10
#include "CXX/Version.hxx"
11
#include "CXX/Config.hxx"
12
#include "CXX/Exception.hxx"
24
typedef int sequence_index_type; // type of an index into a sequence
26
// Forward declarations
29
template<TEMPLATE_TYPENAME T> class SeqBase;
31
template<TEMPLATE_TYPENAME T> class MapBase;
33
// new_reference_to also overloaded below on Object
34
inline PyObject* new_reference_to(PyObject* p)
40
// returning Null() from an extension method triggers a
42
inline PyObject* Null()
44
return (static_cast<PyObject*>(0));
47
//===========================================================================//
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.
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.
58
// (1) Write a routine int MyType_Check (PyObject *) modeled after PyInt_Check,
59
// PyFloat_Check, etc.
61
// (2) Add method accepts:
62
// virtual bool accepts (PyObject *pyob) const {
63
// return pyob && MyType_Check (pyob);
66
// (3) Include the following constructor and copy constructor
69
explicit MyType (PyObject *pyob): Object(pyob) {
73
MyType(const Object& other): Object(other.ptr()) {
78
// Alernate version for the constructor to allow for construction from owned pointers:
80
explicit MyType (PyObject *pyob): Object(pyob) {
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.
89
// (4) Each class needs at least these two assignment operators:
91
MyType& operator= (const Object& rhs) {
92
return (*this = *rhs);
95
Mytype& operator= (PyObject* rhsp) {
96
if(ptr() == rhsp) return *this;
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.
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.
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
122
void set (PyObject* pyob, bool owned = false)
141
// release pointer if not the right type
146
{ // Error message already set
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();
155
throw TypeError ("CXX: type error.");
161
// Constructor acquires new ownership of pointer unless explicitly told not to.
162
explicit Object (PyObject* pyob=Py::_None(), bool owned = false): p (pyob)
171
// Copy constructor acquires new ownership of pointer
172
Object (const Object& ob): p(ob.p)
178
// Assignment acquires new ownership of pointer
179
Object& operator= (const Object& rhs)
185
Object& operator= (PyObject* rhsp)
187
if(ptr() == rhsp) return *this;
198
// Loaning the pointer to others, retain ownership
199
PyObject* operator* () const
204
// Explicit reference_counting changes
205
void increment_reference_count()
210
void decrement_reference_count()
212
// not allowed to commit suicide, however
213
if(reference_count() == 1)
214
throw RuntimeError("Object::decrement_reference_count error.");
217
// Would like to call this pointer() but messes up STL in SeqBase<T>
218
PyObject* ptr () const
227
// Can pyob be used in this object's constructor?
228
virtual bool accepts (PyObject *pyob) const
233
int reference_count () const
234
{ // the reference count
235
return p ? p->ob_refcnt : 0;
238
Type type () const; // the type object associated with this one
240
String str () const; // the str() representation
242
std::string as_string() const;
244
String repr () const; // the repr () representation
246
bool hasAttr (const std::string& s) const
248
return PyObject_HasAttrString (p, const_cast<char*>(s.c_str())) ? true: false;
251
Object getAttr (const std::string& s) const
253
return Object (PyObject_GetAttrString (p, const_cast<char*>(s.c_str())), true);
256
Object getItem (const Object& key) const
258
return Object (PyObject_GetItem(p, *key), true);
261
long hashValue () const
263
return PyObject_Hash (p);
267
// int print (FILE* fp, int flags=Py_Print_RAW)
269
// return PyObject_Print (p, fp, flags);
272
bool is(PyObject *pother) const
277
bool is(const Object& other) const
282
bool isCallable () const
284
return PyCallable_Check (p) != 0;
289
return Py::_Dict_Check (p);
294
return Py::_List_Check (p);
297
bool isMapping () const
299
return PyMapping_Check (p) != 0;
302
bool isNumeric () const
304
return PyNumber_Check (p) != 0;
307
bool isSequence () const
309
return PySequence_Check (p) != 0;
314
return PyObject_IsTrue (p) != 0;
317
bool isType (const Type& t) const;
321
return Py::_Tuple_Check(p);
324
bool isString() const
326
return Py::_String_Check(p) || Py::_Unicode_Check(p);
329
bool isUnicode() const
331
return Py::_Unicode_Check(p);
335
void setAttr (const std::string& s, const Object& value)
337
if(PyObject_SetAttrString (p, const_cast<char*>(s.c_str()), *value) == -1)
338
throw AttributeError ("getAttr failed.");
341
void delAttr (const std::string& s)
343
if(PyObject_DelAttrString (p, const_cast<char*>(s.c_str())) == -1)
344
throw AttributeError ("delAttr failed.");
347
// PyObject_SetItem is too weird to be using from C++
348
// so it is intentionally omitted.
350
void delItem (const Object& key)
352
//if(PyObject_DelItem(p, *key) == -1)
353
// failed to link on Windows?
354
throw KeyError("delItem failed.");
356
// Equality and comparison use PyObject_Compare
358
bool operator==(const Object& o2) const
360
int k = PyObject_Compare (p, *o2);
361
if (PyErr_Occurred()) throw Exception();
365
bool operator!=(const Object& o2) const
367
int k = PyObject_Compare (p, *o2);
368
if (PyErr_Occurred()) throw Exception();
373
bool operator>=(const Object& o2) const
375
int k = PyObject_Compare (p, *o2);
376
if (PyErr_Occurred()) throw Exception();
380
bool operator<=(const Object& o2) const
382
int k = PyObject_Compare (p, *o2);
383
if (PyErr_Occurred()) throw Exception();
387
bool operator<(const Object& o2) const
389
int k = PyObject_Compare (p, *o2);
390
if (PyErr_Occurred()) throw Exception();
394
bool operator>(const Object& o2) const
396
int k = PyObject_Compare (p, *o2);
397
if (PyErr_Occurred()) throw Exception();
401
// End of class Object
402
inline PyObject* new_reference_to(const Object& g)
404
PyObject* p = g.ptr();
409
// Nothing() is what an extension method returns if
410
// there is no other return value.
411
inline Object Nothing()
413
return Object(Py::_None());
416
// Python special None value
419
return Object(Py::_None());
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);
428
class Type: public Object
431
explicit Type (PyObject* pyob, bool owned = false): Object(pyob, owned)
436
Type (const Object& ob): Object(*ob)
441
Type(const Type& t): Object(t)
446
Type& operator= (const Object& rhs)
448
return (*this = *rhs);
451
Type& operator= (PyObject* rhsp)
453
if(ptr() == rhsp) return *this;
457
virtual bool accepts (PyObject *pyob) const
459
return pyob && Py::_Type_Check (pyob);
465
// Convert an owned Python pointer into a CXX Object
467
inline Object asObject (PyObject *p)
469
return Object(p, true);
475
// ===============================================
477
class Int: public Object
481
explicit Int (PyObject *pyob, bool owned = false): Object (pyob, owned)
486
Int (const Int& ob): Object(*ob)
492
explicit Int (long v = 0L): Object(PyInt_FromLong(v), true)
501
set(PyInt_FromLong(w), true);
506
explicit Int (bool v)
509
set(PyInt_FromLong(w), true);
513
explicit Int (const Object& ob)
515
set(PyNumber_Int(*ob), true);
519
// Assignment acquires new ownership of pointer
521
Int& operator= (const Object& rhs)
523
return (*this = *rhs);
526
Int& operator= (PyObject* rhsp)
528
if(ptr() == rhsp) return *this;
529
set (PyNumber_Int(rhsp), true);
533
virtual bool accepts (PyObject *pyob) const
535
return pyob && Py::_Int_Check (pyob);
538
operator long() const
540
return PyInt_AsLong (ptr());
542
// assign from an int
543
Int& operator= (int v)
545
set (PyInt_FromLong (long(v)), true);
549
Int& operator= (long v)
551
set (PyInt_FromLong (v), true);
556
// ===============================================
558
class Long: public Object
562
explicit Long (PyObject *pyob, bool owned = false): Object (pyob, owned)
567
Long (const Long& ob): Object(ob.ptr())
573
explicit Long (long v = 0L)
574
: Object(PyLong_FromLong(v), true)
578
// create from unsigned long
579
explicit Long (unsigned long v)
580
: Object(PyLong_FromUnsignedLong(v), true)
585
explicit Long (int v)
586
: Object(PyLong_FromLong(static_cast<long>(v)), true)
591
// try to create from any object
592
Long (const Object& ob)
593
: Object(PyNumber_Long(*ob), true)
598
// Assignment acquires new ownership of pointer
600
Long& operator= (const Object& rhs)
602
return (*this = *rhs);
605
Long& operator= (PyObject* rhsp)
607
if(ptr() == rhsp) return *this;
608
set (PyNumber_Long(rhsp), true);
612
virtual bool accepts (PyObject *pyob) const
614
return pyob && Py::_Long_Check (pyob);
617
operator long() const
619
return PyLong_AsLong (ptr());
621
// convert to unsigned
622
operator unsigned long() const
624
return PyLong_AsUnsignedLong (ptr());
626
operator double() const
628
return PyLong_AsDouble (ptr());
630
// assign from an int
631
Long& operator= (int v)
633
set(PyLong_FromLong (long(v)), true);
637
Long& operator= (long v)
639
set(PyLong_FromLong (v), true);
642
// assign from unsigned long
643
Long& operator= (unsigned long v)
645
set(PyLong_FromUnsignedLong (v), true);
650
// ===============================================
653
class Float: public Object
657
explicit Float (PyObject *pyob, bool owned = false): Object(pyob, owned)
662
Float (const Float& f): Object(f)
668
explicit Float (double v=0.0)
669
: Object(PyFloat_FromDouble (v), true)
674
// try to make from any object
675
Float (const Object& ob)
676
: Object(PyNumber_Float(*ob), true)
681
Float& operator= (const Object& rhs)
683
return (*this = *rhs);
686
Float& operator= (PyObject* rhsp)
688
if(ptr() == rhsp) return *this;
689
set (PyNumber_Float(rhsp), true);
693
virtual bool accepts (PyObject *pyob) const
695
return pyob && Py::_Float_Check (pyob);
698
operator double () const
700
return PyFloat_AsDouble (ptr());
702
// assign from a double
703
Float& operator= (double v)
705
set(PyFloat_FromDouble (v), true);
708
// assign from an int
709
Float& operator= (int v)
711
set(PyFloat_FromDouble (double(v)), true);
715
Float& operator= (long v)
717
set(PyFloat_FromDouble (double(v)), true);
720
// assign from an Int
721
Float& operator= (const Int& iob)
723
set(PyFloat_FromDouble (double(long(iob))), true);
728
// ===============================================
730
class Complex: public Object
734
explicit Complex (PyObject *pyob, bool owned = false): Object(pyob, owned)
739
Complex (const Complex& f): Object(f)
745
explicit Complex (double v=0.0, double w=0.0)
746
:Object(PyComplex_FromDoubles (v, w), true)
751
Complex& operator= (const Object& rhs)
753
return (*this = *rhs);
756
Complex& operator= (PyObject* rhsp)
758
if(ptr() == rhsp) return *this;
763
virtual bool accepts (PyObject *pyob) const
765
return pyob && Py::_Complex_Check (pyob);
767
// convert to Py_complex
768
operator Py_complex () const
770
return PyComplex_AsCComplex (ptr());
772
// assign from a Py_complex
773
Complex& operator= (const Py_complex& v)
775
set(PyComplex_FromCComplex (v), true);
778
// assign from a double
779
Complex& operator= (double v)
781
set(PyComplex_FromDoubles (v, 0.0), true);
784
// assign from an int
785
Complex& operator= (int v)
787
set(PyComplex_FromDoubles (double(v), 0.0), true);
791
Complex& operator= (long v)
793
set(PyComplex_FromDoubles (double(v), 0.0), true);
796
// assign from an Int
797
Complex& operator= (const Int& iob)
799
set(PyComplex_FromDoubles (double(long(iob)), 0.0), true);
805
return PyComplex_RealAsDouble(ptr());
810
return PyComplex_ImagAsDouble(ptr());
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".
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.
822
// While this class is not intended for enduser use, it needs some public
823
// constructors for the benefit of the STL.
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
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.
836
template<TEMPLATE_TYPENAME T>
840
SeqBase<T>& s; // the sequence
841
int offset; // item number
842
T the_item; // lvalue
845
seqref (SeqBase<T>& seq, sequence_index_type j)
846
: s(seq), offset(j), the_item (s.getItem(j))
849
seqref (const seqref<T>& range)
850
: s(range.s), offset(range.offset), the_item(range.the_item)
853
// TMM: added this seqref ctor for use with STL algorithms
855
: s(dynamic_cast< SeqBase<T>&>(obj))
857
, the_item(s.getItem(offset))
867
seqref<T>& operator=(const seqref<T>& rhs)
869
the_item = rhs.the_item;
870
s.setItem(offset, the_item);
874
seqref<T>& operator=(const T& ob)
877
s.setItem(offset, ob);
881
// forward everything else to the item
882
PyObject* ptr () const
884
return the_item.ptr();
887
int reference_count () const
888
{ // the reference count
889
return the_item.reference_count();
894
return the_item.type();
899
String repr () const;
901
bool hasAttr (const std::string& attr_name) const
903
return the_item.hasAttr(attr_name);
906
Object getAttr (const std::string& attr_name) const
908
return the_item.getAttr(attr_name);
911
Object getItem (const Object& key) const
913
return the_item.getItem(key);
916
long hashValue () const
918
return the_item.hashValue();
921
bool isCallable () const
923
return the_item.isCallable();
928
return the_item.isDict();
933
return the_item.isList();
936
bool isMapping () const
938
return the_item.isMapping();
941
bool isNumeric () const
943
return the_item.isNumeric();
946
bool isSequence () const
948
return the_item.isSequence();
953
return the_item.isTrue();
956
bool isType (const Type& t) const
958
return the_item.isType (t);
963
return the_item.isTuple();
966
bool isString() const
968
return the_item.isString();
971
void setAttr (const std::string& attr_name, const Object& value)
973
the_item.setAttr(attr_name, value);
976
void delAttr (const std::string& attr_name)
978
the_item.delAttr(attr_name);
981
void delItem (const Object& key)
983
the_item.delItem(key);
986
bool operator==(const Object& o2) const
988
return the_item == o2;
991
bool operator!=(const Object& o2) const
993
return the_item != o2;
996
bool operator>=(const Object& o2) const
998
return the_item >= o2;
1001
bool operator<=(const Object& o2) const
1003
return the_item <= o2;
1006
bool operator<(const Object& o2) const
1008
return the_item < o2;
1011
bool operator>(const Object& o2) const
1013
return the_item > o2;
1019
// ...the base class for all sequence types
1021
template<TEMPLATE_TYPENAME T>
1022
class SeqBase: public Object
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
1033
virtual size_type max_size() const
1035
return std::string::npos; // ?
1038
virtual size_type capacity() const
1043
virtual void swap(SeqBase<T>& c)
1045
SeqBase<T> temp = c;
1050
virtual size_type size () const
1052
return PySequence_Length (ptr());
1055
explicit SeqBase<T> ()
1056
:Object(PyTuple_New(0), true)
1061
explicit SeqBase<T> (PyObject* pyob, bool owned=false)
1062
: Object(pyob, owned)
1067
SeqBase<T> (const Object& ob): Object(ob)
1072
// Assignment acquires new ownership of pointer
1074
SeqBase<T>& operator= (const Object& rhs)
1076
return (*this = *rhs);
1079
SeqBase<T>& operator= (PyObject* rhsp)
1081
if(ptr() == rhsp) return *this;
1086
virtual bool accepts (PyObject *pyob) const
1088
return pyob && PySequence_Check (pyob);
1091
size_type length () const
1093
return PySequence_Length (ptr());
1097
const T operator[](sequence_index_type index) const
1099
return getItem(index);
1102
seqref<T> operator[](sequence_index_type index)
1104
return seqref<T>(*this, index);
1107
virtual T getItem (sequence_index_type i) const
1109
return T(asObject(PySequence_GetItem (ptr(), i)));
1112
virtual void setItem (sequence_index_type i, const T& ob)
1114
if (PySequence_SetItem (ptr(), i, *ob) == -1)
1120
SeqBase<T> repeat (int count) const
1122
return SeqBase<T> (PySequence_Repeat (ptr(), count), true);
1125
SeqBase<T> concat (const SeqBase<T>& other) const
1127
return SeqBase<T> (PySequence_Concat(ptr(), *other), true);
1130
// more STL compatability
1131
const T front () const
1138
return seqref<T>(this, 0);
1141
const T back () const
1143
return getItem(size()-1);
1148
return seqref<T>(this, size()-1);
1151
void verify_length(size_type required_size) const
1153
if (size() != required_size)
1154
throw IndexError ("Unexpected SeqBase<T> length.");
1157
void verify_length(size_type min_size, size_type max_size) const
1159
size_type n = size();
1160
if (n < min_size || n > max_size)
1161
throw IndexError ("Unexpected SeqBase<T> length.");
1165
: public random_access_iterator_parent(seqref<T>)
1168
friend class SeqBase<T>;
1181
iterator (SeqBase<T>* s, int where)
1186
iterator (const iterator& other)
1188
, count( other.count )
1191
bool eql (const iterator& other) const
1193
return (*seq == *other.seq) && (count == other.count);
1196
bool neq (const iterator& other) const
1198
return (*seq != *other.seq) || (count != other.count);
1201
bool lss (const iterator& other) const
1203
return (count < other.count);
1206
bool gtr (const iterator& other) const
1208
return (count > other.count);
1211
bool leq (const iterator& other) const
1213
return (count <= other.count);
1216
bool geq (const iterator& other) const
1218
return (count >= other.count);
1221
seqref<T> operator*()
1223
return seqref<T>(*seq, count);
1226
seqref<T> operator[] (sequence_index_type i)
1228
return seqref<T>(*seq, count + i);
1231
iterator& operator=(const iterator& other)
1233
if (this == &other) return *this;
1235
count = other.count;
1239
iterator operator+(int n) const
1241
return iterator(seq, count + n);
1244
iterator operator-(int n) const
1246
return iterator(seq, count - n);
1249
iterator& operator+=(int n)
1255
iterator& operator-=(int n)
1261
int operator-(const iterator& other) const
1263
if (*seq != *other.seq)
1264
throw RuntimeError ("SeqBase<T>::iterator comparison error");
1265
return count - other.count;
1269
iterator& operator++ ()
1270
{ count++; return *this;}
1272
iterator operator++ (int)
1273
{ return iterator(seq, count++);}
1275
iterator& operator-- ()
1276
{ count--; return *this;}
1278
iterator operator-- (int)
1279
{ return iterator(seq, count--);}
1281
std::string diagnose() const
1283
std::OSTRSTREAM oss;
1284
oss << "iterator diagnosis " << seq << ", " << count << std::ends;
1285
return std::string(oss.str());
1287
}; // end of class SeqBase<T>::iterator
1291
return iterator(this, 0);
1296
return iterator(this, length());
1299
class const_iterator
1300
: public random_access_iterator_parent(const Object)
1303
friend class SeqBase<T>;
1304
const SeqBase<T>* seq;
1305
sequence_index_type count;
1316
const_iterator (const SeqBase<T>* s, int where)
1321
const_iterator(const const_iterator& other)
1323
, count( other.count )
1326
const T operator*() const
1328
return seq->getItem(count);
1331
const T operator[] (sequence_index_type i) const
1333
return seq->getItem(count + i);
1336
const_iterator& operator=(const const_iterator& other)
1338
if (this == &other) return *this;
1340
count = other.count;
1344
const_iterator operator+(int n) const
1346
return const_iterator(seq, count + n);
1349
bool eql (const const_iterator& other) const
1351
return (*seq == *other.seq) && (count == other.count);
1354
bool neq (const const_iterator& other) const
1356
return (*seq != *other.seq) || (count != other.count);
1359
bool lss (const const_iterator& other) const
1361
return (count < other.count);
1364
bool gtr (const const_iterator& other) const
1366
return (count > other.count);
1369
bool leq (const const_iterator& other) const
1371
return (count <= other.count);
1374
bool geq (const const_iterator& other) const
1376
return (count >= other.count);
1379
const_iterator operator-(int n)
1381
return const_iterator(seq, count - n);
1384
const_iterator& operator+=(int n)
1390
const_iterator& operator-=(int n)
1396
int operator-(const const_iterator& other) const
1398
if (*seq != *other.seq)
1399
throw RuntimeError ("SeqBase<T>::const_iterator::- error");
1400
return count - other.count;
1403
const_iterator& operator++ ()
1404
{ count++; return *this;}
1406
const_iterator operator++ (int)
1407
{ return const_iterator(seq, count++);}
1409
const_iterator& operator-- ()
1410
{ count--; return *this;}
1412
const_iterator operator-- (int)
1413
{ return const_iterator(seq, count--);}
1414
}; // end of class SeqBase<T>::const_iterator
1416
const_iterator begin () const
1418
return const_iterator(this, 0);
1421
const_iterator end () const
1423
return const_iterator(this, length());
1427
// Here's an important typedef you might miss if reading too fast...
1428
typedef SeqBase<Object> Sequence;
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);
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);
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);
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);
1459
// ==================================================
1461
// Python strings return strings as individual elements.
1462
// I'll try having a class Char which is a String of length 1
1464
typedef std::basic_string<Py_UNICODE> unicodestring;
1465
extern Py_UNICODE unicode_null_string[1];
1467
class Char: public Object
1470
explicit Char (PyObject *pyob, bool owned = false): Object(pyob, owned)
1475
Char (const Object& ob): Object(ob)
1480
Char (const std::string& v = "")
1481
:Object(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true)
1487
: Object(PyString_FromStringAndSize (&v, 1), true)
1493
: Object(PyUnicode_FromUnicode (&v, 1), true)
1497
// Assignment acquires new ownership of pointer
1498
Char& operator= (const Object& rhs)
1500
return (*this = *rhs);
1503
Char& operator= (PyObject* rhsp)
1505
if(ptr() == rhsp) return *this;
1511
virtual bool accepts (PyObject *pyob) const
1513
return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob)) && PySequence_Length (pyob) == 1;
1516
// Assignment from C string
1517
Char& operator= (const std::string& v)
1519
set(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true);
1523
Char& operator= (char v)
1525
set(PyString_FromStringAndSize (&v, 1), true);
1529
Char& operator= (const unicodestring& v)
1531
set(PyUnicode_FromUnicode (const_cast<Py_UNICODE*>(v.data()),1), true);
1535
Char& operator= (Py_UNICODE v)
1537
set(PyUnicode_FromUnicode (&v, 1), true);
1542
operator String() const;
1544
operator std::string () const
1546
return std::string(PyString_AsString (ptr()));
1550
class String: public SeqBase<Char>
1553
virtual size_type capacity() const
1558
explicit String (PyObject *pyob, bool owned = false): SeqBase<Char>(pyob, owned)
1563
String (const Object& ob): SeqBase<Char>(ob)
1569
: SeqBase<Char>( PyString_FromStringAndSize( "", 0 ), true )
1574
String( const std::string& v )
1575
: SeqBase<Char>( PyString_FromStringAndSize( const_cast<char*>(v.data()),
1576
static_cast<int>( v.length() ) ), true )
1581
String( const char *s, const char *encoding, const char *error="strict" )
1582
: SeqBase<Char>( PyUnicode_Decode( s, strlen( s ), encoding, error ), true )
1587
String( const char *s, int len, const char *encoding, const char *error="strict" )
1588
: SeqBase<Char>( PyUnicode_Decode( s, len, encoding, error ), true )
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 )
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)
1606
String( const char *v, int vsize )
1607
: SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v), vsize ), true )
1612
String( const char* v )
1613
: SeqBase<Char>( PyString_FromString( v ), true )
1618
// Assignment acquires new ownership of pointer
1619
String& operator= ( const Object& rhs )
1621
return *this = *rhs;
1624
String& operator= (PyObject* rhsp)
1632
virtual bool accepts (PyObject *pyob) const
1634
return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob));
1637
// Assignment from C string
1638
String& operator= (const std::string& v)
1640
set( PyString_FromStringAndSize( const_cast<char*>( v.data() ),
1641
static_cast<int>( v.length() ) ), true );
1644
String& operator= (const unicodestring& v)
1646
set( PyUnicode_FromUnicode( const_cast<Py_UNICODE*>( v.data() ),
1647
static_cast<int>( v.length() ) ), true );
1653
String encode( const char *encoding, const char *error="strict" )
1657
return String( PyUnicode_AsEncodedString( ptr(), encoding, error ) );
1661
return String( PyString_AsEncodedObject( ptr(), encoding, error ) );
1665
String decode( const char *encoding, const char *error="strict" )
1667
return Object( PyString_AsDecodedObject( ptr(), encoding, error ) );
1671
virtual size_type size () const
1675
return static_cast<size_type>( PyUnicode_GET_SIZE (ptr()) );
1679
return static_cast<size_type>( PyString_Size (ptr()) );
1683
operator std::string () const
1685
return as_std_string();
1688
std::string as_std_string() const
1692
throw TypeError("cannot return std::string from Unicode object");
1696
return std::string( PyString_AsString( ptr() ), static_cast<size_type>( PyString_Size( ptr() ) ) );
1700
unicodestring as_unicodestring() const
1704
return unicodestring( PyUnicode_AS_UNICODE( ptr() ),
1705
static_cast<size_type>( PyUnicode_GET_SIZE( ptr() ) ) );
1709
throw TypeError("can only return unicodestring from Unicode object");
1714
// ==================================================
1716
class Tuple: public Sequence
1719
virtual void setItem (sequence_index_type offset, const Object&ob)
1721
// note PyTuple_SetItem is a thief...
1722
if(PyTuple_SetItem (ptr(), offset, new_reference_to(ob)) == -1)
1729
explicit Tuple (PyObject *pyob, bool owned = false): Sequence (pyob, owned)
1734
Tuple (const Object& ob): Sequence(ob)
1739
// New tuple of a given size
1740
explicit Tuple (int size = 0)
1742
set(PyTuple_New (size), true);
1744
for (sequence_index_type i=0; i < size; i++)
1746
if(PyTuple_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
1752
// Tuple from any sequence
1753
explicit Tuple (const Sequence& s)
1755
sequence_index_type limit( sequence_index_type( s.length() ) );
1757
set(PyTuple_New (limit), true);
1760
for(sequence_index_type i=0; i < limit; i++)
1762
if(PyTuple_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
1768
// Assignment acquires new ownership of pointer
1770
Tuple& operator= (const Object& rhs)
1772
return (*this = *rhs);
1775
Tuple& operator= (PyObject* rhsp)
1777
if(ptr() == rhsp) return *this;
1782
virtual bool accepts (PyObject *pyob) const
1784
return pyob && Py::_Tuple_Check (pyob);
1787
Tuple getSlice (int i, int j) const
1789
return Tuple (PySequence_GetSlice (ptr(), i, j), true);
1794
// ==================================================
1797
class List: public Sequence
1801
explicit List (PyObject *pyob, bool owned = false): Sequence(pyob, owned)
1805
List (const Object& ob): Sequence(ob)
1809
// Creation at a fixed size
1812
set(PyList_New (size), true);
1814
for (sequence_index_type i=0; i < size; i++)
1816
if(PyList_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
1823
// List from a sequence
1824
List (const Sequence& s): Sequence()
1827
set(PyList_New (n), true);
1829
for (sequence_index_type i=0; i < n; i++)
1831
if(PyList_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
1838
virtual size_type capacity() const
1842
// Assignment acquires new ownership of pointer
1844
List& operator= (const Object& rhs)
1846
return (*this = *rhs);
1849
List& operator= (PyObject* rhsp)
1851
if(ptr() == rhsp) return *this;
1856
virtual bool accepts (PyObject *pyob) const
1858
return pyob && Py::_List_Check (pyob);
1861
List getSlice (int i, int j) const
1863
return List (PyList_GetSlice (ptr(), i, j), true);
1866
void setSlice (int i, int j, const Object& v)
1868
if(PyList_SetSlice (ptr(), i, j, *v) == -1)
1874
void append (const Object& ob)
1876
if(PyList_Append (ptr(), *ob) == -1)
1882
void insert (int i, const Object& ob)
1884
if(PyList_Insert (ptr(), i, *ob) == -1)
1892
if(PyList_Sort(ptr()) == -1)
1900
if(PyList_Reverse(ptr()) == -1)
1909
// ==================================================
1910
template<TEMPLATE_TYPENAME T>
1914
MapBase<T>& s; // the map
1915
Object key; // item key
1919
mapref<T> (MapBase<T>& map, const std::string& k)
1920
: s(map), the_item()
1923
if(map.hasKey(key)) the_item = map.getItem(key);
1926
mapref<T> (MapBase<T>& map, const Object& k)
1927
: s(map), key(k), the_item()
1929
if(map.hasKey(key)) the_item = map.getItem(key);
1937
mapref<T>& operator=(const mapref<T>& other)
1939
if(this == &other) return *this;
1940
the_item = other.the_item;
1941
s.setItem(key, other.the_item);
1945
mapref<T>& operator= (const T& ob)
1948
s.setItem (key, ob);
1958
// forward everything else to the_item
1959
PyObject* ptr () const
1961
return the_item.ptr();
1964
int reference_count () const
1965
{ // the mapref count
1966
return the_item.reference_count();
1971
return the_item.type();
1976
return the_item.str();
1979
String repr () const
1981
return the_item.repr();
1984
bool hasAttr (const std::string& attr_name) const
1986
return the_item.hasAttr(attr_name);
1989
Object getAttr (const std::string& attr_name) const
1991
return the_item.getAttr(attr_name);
1994
Object getItem (const Object& k) const
1996
return the_item.getItem(k);
1999
long hashValue () const
2001
return the_item.hashValue();
2004
bool isCallable () const
2006
return the_item.isCallable();
2009
bool isList () const
2011
return the_item.isList();
2014
bool isMapping () const
2016
return the_item.isMapping();
2019
bool isNumeric () const
2021
return the_item.isNumeric();
2024
bool isSequence () const
2026
return the_item.isSequence();
2029
bool isTrue () const
2031
return the_item.isTrue();
2034
bool isType (const Type& t) const
2036
return the_item.isType (t);
2039
bool isTuple() const
2041
return the_item.isTuple();
2044
bool isString() const
2046
return the_item.isString();
2050
void setAttr (const std::string& attr_name, const Object& value)
2052
the_item.setAttr(attr_name, value);
2055
void delAttr (const std::string& attr_name)
2057
the_item.delAttr(attr_name);
2060
void delItem (const Object& k)
2062
the_item.delItem(k);
2066
// TMM: now for mapref<T>
2068
bool operator==(const mapref<T>& left, const mapref<T>& right)
2070
return true; // NOT completed.
2074
bool operator!=(const mapref<T>& left, const mapref<T>& right)
2076
return true; // not completed.
2079
template<TEMPLATE_TYPENAME T>
2080
class MapBase: public Object
2083
explicit MapBase<T>()
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;
2100
explicit MapBase<T> (PyObject *pyob, bool owned = false): Object(pyob, owned)
2105
// TMM: 02Jul'01 - changed MapBase<T> to Object in next line
2106
MapBase<T> (const Object& ob): Object(ob)
2111
// Assignment acquires new ownership of pointer
2112
MapBase<T>& operator= (const Object& rhs)
2114
return (*this = *rhs);
2117
MapBase<T>& operator= (PyObject* rhsp)
2119
if(ptr() == rhsp) return *this;
2124
virtual bool accepts (PyObject *pyob) const
2126
return pyob && PyMapping_Check(pyob);
2129
// Clear -- PyMapping Clear is missing
2135
for(List::iterator i = k.begin(); i != k.end(); i++)
2141
virtual size_type size() const
2143
return PyMapping_Length (ptr());
2147
T operator[](const std::string& key) const
2149
return getItem(key);
2152
T operator[](const Object& key) const
2154
return getItem(key);
2157
mapref<T> operator[](const std::string& key)
2159
return mapref<T>(*this, key);
2162
mapref<T> operator[](const Object& key)
2164
return mapref<T>(*this, key);
2169
return PyMapping_Length (ptr());
2172
bool hasKey (const std::string& s) const
2174
return PyMapping_HasKeyString (ptr(),const_cast<char*>(s.c_str())) != 0;
2177
bool hasKey (const Object& s) const
2179
return PyMapping_HasKey (ptr(), s.ptr()) != 0;
2182
T getItem (const std::string& s) const
2185
asObject(PyMapping_GetItemString (ptr(),const_cast<char*>(s.c_str())))
2189
T getItem (const Object& s) const
2192
asObject(PyObject_GetItem (ptr(), s.ptr()))
2196
virtual void setItem (const char *s, const Object& ob)
2198
if (PyMapping_SetItemString (ptr(), const_cast<char*>(s), *ob) == -1)
2204
virtual void setItem (const std::string& s, const Object& ob)
2206
if (PyMapping_SetItemString (ptr(), const_cast<char*>(s.c_str()), *ob) == -1)
2212
virtual void setItem (const Object& s, const Object& ob)
2214
if (PyObject_SetItem (ptr(), s.ptr(), ob.ptr()) == -1)
2220
void delItem (const std::string& s)
2222
if (PyMapping_DelItemString (ptr(), const_cast<char*>(s.c_str())) == -1)
2228
void delItem (const Object& s)
2230
if (PyMapping_DelItem (ptr(), *s) == -1)
2238
return List(PyMapping_Keys(ptr()), true);
2241
List values () const
2242
{ // each returned item is a (key, value) pair
2243
return List(PyMapping_Values(ptr()), true);
2248
return List(PyMapping_Items(ptr()), true);
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.
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.
2262
#if 0 // here is the test code with which I found the (still existing) bug
2263
typedef cxx::Dict d_t;
2265
cxx::String s1("blah");
2266
cxx::String s2("gorf");
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 )
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 );
2286
// : public forward_iterator_parent( std::pair<const T,T> ) {
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;
2294
friend class MapBase<T>;
2297
List keys; // for iterating over the map
2298
List::iterator pos; // index into the keys
2310
iterator (MapBase<T>* m, bool end = false )
2313
, pos( end ? keys.end() : keys.begin() )
2316
iterator (const iterator& other)
2318
, keys( other.keys )
2322
reference operator*()
2325
return std::make_pair(key, mapref<T>(*map,key));
2328
iterator& operator=(const iterator& other)
2338
bool eql(const iterator& right) const
2340
return *map == *right.map && pos == right.pos;
2342
bool neq( const iterator& right ) const
2344
return *map != *right.map || pos != right.pos;
2347
// pointer operator->() {
2352
iterator& operator++ ()
2353
{ pos++; return *this;}
2355
iterator operator++ (int)
2356
{ return iterator(map, keys, pos++);}
2358
iterator& operator-- ()
2359
{ pos--; return *this;}
2361
iterator operator-- (int)
2362
{ return iterator(map, keys, pos--);}
2364
std::string diagnose() const
2366
std::OSTRSTREAM oss;
2367
oss << "iterator diagnosis " << map << ", " << pos << std::ends;
2368
return std::string(oss.str());
2370
}; // end of class MapBase<T>::iterator
2374
return iterator(this);
2379
return iterator(this, true);
2382
class const_iterator
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;
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
2406
const_iterator (const MapBase<T>* m, List k, List::iterator p )
2412
const_iterator(const const_iterator& other)
2414
, keys( other.keys )
2418
bool eql(const const_iterator& right) const
2420
return *map == *right.map && pos == right.pos;
2422
bool neq( const const_iterator& right ) const
2424
return *map != *right.map || pos != right.pos;
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 '['.
2434
const_iterator& operator=(const const_iterator& other)
2436
if (this == &other) return *this;
2444
const_iterator& operator++ ()
2445
{ pos++; return *this;}
2447
const_iterator operator++ (int)
2448
{ return const_iterator(map, keys, pos++);}
2450
const_iterator& operator-- ()
2451
{ pos--; return *this;}
2453
const_iterator operator-- (int)
2454
{ return const_iterator(map, keys, pos--);}
2455
}; // end of class MapBase<T>::const_iterator
2457
const_iterator begin () const
2459
return const_iterator(this, 0);
2462
const_iterator end () const
2464
return const_iterator(this, length());
2467
}; // end of MapBase<T>
2469
typedef MapBase<Object> Mapping;
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);
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);
2482
// ==================================================
2484
class Dict: public Mapping
2488
explicit Dict (PyObject *pyob, bool owned=false): Mapping (pyob, owned)
2492
Dict (const Dict& ob): Mapping(ob)
2499
set(PyDict_New (), true);
2502
// Assignment acquires new ownership of pointer
2504
Dict& operator= (const Object& rhs)
2506
return (*this = *rhs);
2509
Dict& operator= (PyObject* rhsp)
2511
if(ptr() == rhsp) return *this;
2516
virtual bool accepts (PyObject *pyob) const
2518
return pyob && Py::_Dict_Check (pyob);
2522
class Callable: public Object
2526
explicit Callable (): Object() {}
2527
explicit Callable (PyObject *pyob, bool owned = false): Object (pyob, owned)
2532
Callable (const Object& ob): Object(ob)
2537
// Assignment acquires new ownership of pointer
2539
Callable& operator= (const Object& rhs)
2541
return (*this = *rhs);
2544
Callable& operator= (PyObject* rhsp)
2546
if(ptr() == rhsp) return *this;
2552
virtual bool accepts (PyObject *pyob) const
2554
return pyob && PyCallable_Check (pyob);
2558
Object apply(const Tuple& args) const
2560
return asObject(PyObject_CallObject(ptr(), args.ptr()));
2563
// Call with keywords
2564
Object apply(const Tuple& args, const Dict& kw) const
2566
return asObject( PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ) );
2569
Object apply(PyObject* pargs = 0) const
2571
return apply (Tuple(pargs));
2575
class Module: public Object
2578
explicit Module (PyObject* pyob, bool owned = false): Object (pyob, owned)
2583
// Construct from module name
2584
explicit Module (const std::string&s): Object()
2586
PyObject *m = PyImport_AddModule( const_cast<char *>(s.c_str()) );
2591
// Copy constructor acquires new ownership of pointer
2592
Module (const Module& ob): Object(*ob)
2597
Module& operator= (const Object& rhs)
2599
return (*this = *rhs);
2602
Module& operator= (PyObject* rhsp)
2604
if(ptr() == rhsp) return *this;
2611
return Dict(PyModule_GetDict(ptr()));
2612
// Caution -- PyModule_GetDict returns borrowed reference!
2616
// Numeric interface
2617
inline Object operator+ (const Object& a)
2619
return asObject(PyNumber_Positive(*a));
2621
inline Object operator- (const Object& a)
2623
return asObject(PyNumber_Negative(*a));
2626
inline Object abs(const Object& a)
2628
return asObject(PyNumber_Absolute(*a));
2631
inline std::pair<Object,Object> coerce(const Object& a, const Object& b)
2636
if(PyNumber_Coerce(&p1,&p2) == -1)
2640
return std::pair<Object,Object>(asObject(p1), asObject(p2));
2643
inline Object operator+ (const Object& a, const Object& b)
2645
return asObject(PyNumber_Add(*a, *b));
2647
inline Object operator+ (const Object& a, int j)
2649
return asObject(PyNumber_Add(*a, *Int(j)));
2651
inline Object operator+ (const Object& a, double v)
2653
return asObject(PyNumber_Add(*a, *Float(v)));
2655
inline Object operator+ (int j, const Object& b)
2657
return asObject(PyNumber_Add(*Int(j), *b));
2659
inline Object operator+ (double v, const Object& b)
2661
return asObject(PyNumber_Add(*Float(v), *b));
2664
inline Object operator- (const Object& a, const Object& b)
2666
return asObject(PyNumber_Subtract(*a, *b));
2668
inline Object operator- (const Object& a, int j)
2670
return asObject(PyNumber_Subtract(*a, *Int(j)));
2672
inline Object operator- (const Object& a, double v)
2674
return asObject(PyNumber_Subtract(*a, *Float(v)));
2676
inline Object operator- (int j, const Object& b)
2678
return asObject(PyNumber_Subtract(*Int(j), *b));
2680
inline Object operator- (double v, const Object& b)
2682
return asObject(PyNumber_Subtract(*Float(v), *b));
2685
inline Object operator* (const Object& a, const Object& b)
2687
return asObject(PyNumber_Multiply(*a, *b));
2689
inline Object operator* (const Object& a, int j)
2691
return asObject(PyNumber_Multiply(*a, *Int(j)));
2693
inline Object operator* (const Object& a, double v)
2695
return asObject(PyNumber_Multiply(*a, *Float(v)));
2697
inline Object operator* (int j, const Object& b)
2699
return asObject(PyNumber_Multiply(*Int(j), *b));
2701
inline Object operator* (double v, const Object& b)
2703
return asObject(PyNumber_Multiply(*Float(v), *b));
2706
inline Object operator/ (const Object& a, const Object& b)
2708
return asObject(PyNumber_Divide(*a, *b));
2710
inline Object operator/ (const Object& a, int j)
2712
return asObject(PyNumber_Divide(*a, *Int(j)));
2714
inline Object operator/ (const Object& a, double v)
2716
return asObject(PyNumber_Divide(*a, *Float(v)));
2718
inline Object operator/ (int j, const Object& b)
2720
return asObject(PyNumber_Divide(*Int(j), *b));
2722
inline Object operator/ (double v, const Object& b)
2724
return asObject(PyNumber_Divide(*Float(v), *b));
2727
inline Object operator% (const Object& a, const Object& b)
2729
return asObject(PyNumber_Remainder(*a, *b));
2731
inline Object operator% (const Object& a, int j)
2733
return asObject(PyNumber_Remainder(*a, *Int(j)));
2735
inline Object operator% (const Object& a, double v)
2737
return asObject(PyNumber_Remainder(*a, *Float(v)));
2739
inline Object operator% (int j, const Object& b)
2741
return asObject(PyNumber_Remainder(*Int(j), *b));
2743
inline Object operator% (double v, const Object& b)
2745
return asObject(PyNumber_Remainder(*Float(v), *b));
2748
inline Object type(const Exception&) // return the type of the error
2750
PyObject *ptype, *pvalue, *ptrace;
2751
PyErr_Fetch(&ptype, &pvalue, &ptrace);
2752
Object result(pvalue);
2753
PyErr_Restore(ptype, pvalue, ptrace);
2757
inline Object value(const Exception&) // return the value of the error
2759
PyObject *ptype, *pvalue, *ptrace;
2760
PyErr_Fetch(&ptype, &pvalue, &ptrace);
2762
if(pvalue) result = pvalue;
2763
PyErr_Restore(ptype, pvalue, ptrace);
2767
inline Object trace(const Exception&) // return the traceback of the error
2769
PyObject *ptype, *pvalue, *ptrace;
2770
PyErr_Fetch(&ptype, &pvalue, &ptrace);
2772
if(ptrace) result = pvalue;
2773
PyErr_Restore(ptype, pvalue, ptrace);
2779
template<TEMPLATE_TYPENAME T>
2780
String seqref<T>::str () const
2782
return the_item.str();
2785
template<TEMPLATE_TYPENAME T>
2786
String seqref<T>::repr () const
2788
return the_item.repr();
2793
#endif // __CXX_Objects__h