6#ifndef __CXX_Objects__h
7#define __CXX_Objects__h
10#if defined(_XOPEN_SOURCE)
28 typedef int sequence_index_type;
33 template<
typename T>
class SeqBase;
36 template<
typename T>
class MapBase;
39 inline PyObject* new_reference_to(PyObject* p)
47 inline PyObject* Null()
49 return (
static_cast<PyObject*
>(0));
127 void set (PyObject* pyob,
bool owned =
false)
155#if defined( _CPPRTTI )
156 std::string s(
"Error creating object of type ");
157 s += (
typeid (*this)).
name();
160 throw TypeError (
"CXX: type error.");
167 explicit Object (PyObject* pyob=Py::_None(),
bool owned =
false): p (pyob)
177 Object (
const Object& ob): p(ob.p)
184 Object& operator= (
const Object& rhs)
190 Object& operator= (PyObject* rhsp)
192 if(ptr() == rhsp)
return *
this;
204 PyObject* operator* ()
const
210 void increment_reference_count()
215 void decrement_reference_count()
218 if(reference_count() == 1)
219 throw RuntimeError(
"Object::decrement_reference_count error.");
223 PyObject* ptr ()
const
233 virtual bool accepts (PyObject *pyob)
const
238 int reference_count ()
const
240 return p ? p->ob_refcnt : 0;
247 std::string as_string()
const;
253 bool hasAttr (
const std::string& s)
const
255 return PyObject_HasAttrString (p,
const_cast<char*
>(s.c_str())) ?
true:
false;
258 Object getAttr (
const std::string& s)
const
260 return Object (PyObject_GetAttrString (p,
const_cast<char*
>(s.c_str())),
true);
263 Object getItem (
const Object& key)
const
265 return Object (PyObject_GetItem(p, *key),
true);
268 long hashValue ()
const
270 return PyObject_Hash (p);
279 bool is(PyObject *pother)
const
284 bool is(
const Object& other)
const
289 bool isCallable ()
const
291 return PyCallable_Check (p) != 0;
294 bool isInstance ()
const
296 return PyInstance_Check (p) != 0;
301 return Py::_Dict_Check (p);
306 return Py::_List_Check (p);
309 bool isMapping ()
const
311 return PyMapping_Check (p) != 0;
314 bool isNumeric ()
const
316 return PyNumber_Check (p) != 0;
319 bool isSequence ()
const
321 return PySequence_Check (p) != 0;
326 return PyObject_IsTrue (p) != 0;
329 bool isType (
const Type& t)
const;
333 return Py::_Tuple_Check(p);
336 bool isString()
const
338 return Py::_String_Check(p) || Py::_Unicode_Check(p);
341 bool isUnicode()
const
343 return Py::_Unicode_Check(p);
347 void setAttr (
const std::string& s,
const Object& value)
349 if(PyObject_SetAttrString (p,
const_cast<char*
>(s.c_str()), *value) == -1)
350 throw AttributeError (
"getAttr failed.");
353 void delAttr (
const std::string& s)
355 if(PyObject_DelAttrString (p,
const_cast<char*
>(s.c_str())) == -1)
356 throw AttributeError (
"delAttr failed.");
362 void delItem (
const Object& )
366 throw KeyError(
"delItem failed.");
370 bool operator==(
const Object& o2)
const
372 int k = PyObject_Compare (p, *o2);
373 if (PyErr_Occurred())
throw Exception();
377 bool operator!=(
const Object& o2)
const
379 int k = PyObject_Compare (p, *o2);
380 if (PyErr_Occurred())
throw Exception();
385 bool operator>=(
const Object& o2)
const
387 int k = PyObject_Compare (p, *o2);
388 if (PyErr_Occurred())
throw Exception();
392 bool operator<=(
const Object& o2)
const
394 int k = PyObject_Compare (p, *o2);
395 if (PyErr_Occurred())
throw Exception();
399 bool operator<(
const Object& o2)
const
401 int k = PyObject_Compare (p, *o2);
402 if (PyErr_Occurred())
throw Exception();
406 bool operator>(
const Object& o2)
const
408 int k = PyObject_Compare (p, *o2);
409 if (PyErr_Occurred())
throw Exception();
414 inline PyObject* new_reference_to(
const Object& g)
416 PyObject* p = g.ptr();
423 inline Object Nothing()
425 return Object(Py::_None());
431 return Object(Py::_None());
435#ifndef CXX_NO_IOSTREAMS
436 std::ostream& operator<< (std::ostream& os,
const Object& ob);
440 class Type:
public Object
443 explicit Type (PyObject* pyob,
bool owned =
false): Object(pyob, owned)
448 Type (
const Object& ob): Object(*ob)
453 Type(
const Type& t): Object(t)
458 Type& operator= (
const Object& rhs)
460 return (*
this = *rhs);
463 Type& operator= (PyObject* rhsp)
465 if(ptr() == rhsp)
return *
this;
469 virtual bool accepts (PyObject *pyob)
const
471 return pyob && Py::_Type_Check (pyob);
479 inline Object asObject (PyObject *p)
481 return Object(p,
true);
489 class Int:
public Object
493 explicit Int (PyObject *pyob,
bool owned =
false): Object (pyob, owned)
498 Int (
const Int& ob): Object(*ob)
504 explicit Int (
long v = 0L): Object(PyInt_FromLong(v), true)
513 set(PyInt_FromLong(w),
true);
517 Int (
const Object& ob)
519 set(PyNumber_Int(*ob),
true);
525 Int& operator= (
const Object& rhs)
527 return (*
this = *rhs);
530 Int& operator= (PyObject* rhsp)
532 if(ptr() == rhsp)
return *
this;
533 set (PyNumber_Int(rhsp),
true);
537 virtual bool accepts (PyObject *pyob)
const
539 return pyob && Py::_Int_Check (pyob);
542 operator long()
const
544 return PyInt_AsLong (ptr());
547 Int& operator= (
int v)
549 set (PyInt_FromLong (
long(v)),
true);
553 Int& operator= (
long v)
555 set (PyInt_FromLong (v),
true);
562 class Long:
public Object
566 explicit Long (PyObject *pyob,
bool owned =
false): Object (pyob, owned)
571 Long (
const Long& ob): Object(ob.ptr())
577 explicit Long (
long v = 0L)
578 : Object(PyLong_FromLong(v), true)
583 explicit Long (
int v)
584 : Object(PyLong_FromLong(static_cast<long>(v)), true)
590 explicit Long (
unsigned long v)
591 : Object(PyLong_FromUnsignedLong(v), true)
597 Long (
const Object& ob)
598 : Object(PyNumber_Long(*ob), true)
605 Long& operator= (
const Object& rhs)
607 return (*
this = *rhs);
610 Long& operator= (PyObject* rhsp)
612 if(ptr() == rhsp)
return *
this;
613 set (PyNumber_Long(rhsp),
true);
617 virtual bool accepts (PyObject *pyob)
const
619 return pyob && Py::_Long_Check (pyob);
622 operator long()
const
624 return PyLong_AsLong (ptr());
626 operator double()
const
628 return PyLong_AsDouble (ptr());
630 operator unsigned long()
const
632 return PyLong_AsUnsignedLong (ptr());
635 Long& operator= (
int v)
637 set(PyLong_FromLong (
long(v)),
true);
641 Long& operator= (
long v)
643 set(PyLong_FromLong (v),
true);
647 Long& operator= (
unsigned long v)
649 set(PyLong_FromUnsignedLong (v),
true);
657 class Float:
public Object
661 explicit Float (PyObject *pyob,
bool owned =
false): Object(pyob, owned)
666 Float (
const Float& f): Object(f)
672 explicit Float (
double v=0.0)
673 : Object(PyFloat_FromDouble (v), true)
679 Float (
const Object& ob)
680 : Object(PyNumber_Float(*ob), true)
685 Float& operator= (
const Object& rhs)
687 return (*
this = *rhs);
690 Float& operator= (PyObject* rhsp)
692 if(ptr() == rhsp)
return *
this;
693 set (PyNumber_Float(rhsp),
true);
697 virtual bool accepts (PyObject *pyob)
const
699 return pyob && Py::_Float_Check (pyob);
702 operator double ()
const
704 return PyFloat_AsDouble (ptr());
707 Float& operator= (
double v)
709 set(PyFloat_FromDouble (v),
true);
713 Float& operator= (
int v)
715 set(PyFloat_FromDouble (
double(v)),
true);
719 Float& operator= (
long v)
721 set(PyFloat_FromDouble (
double(v)),
true);
725 Float& operator= (
const Int& iob)
727 set(PyFloat_FromDouble (
double(
long(iob))),
true);
734 class Complex:
public Object
738 explicit Complex (PyObject *pyob,
bool owned =
false): Object(pyob, owned)
743 Complex (
const Complex& f): Object(f)
749 explicit Complex (
double v=0.0,
double w=0.0)
750 :Object(PyComplex_FromDoubles (v, w), true)
755 Complex& operator= (
const Object& rhs)
757 return (*
this = *rhs);
760 Complex& operator= (PyObject* rhsp)
762 if(ptr() == rhsp)
return *
this;
767 virtual bool accepts (PyObject *pyob)
const
769 return pyob && Py::_Complex_Check (pyob);
772 operator Py_complex ()
const
774 return PyComplex_AsCComplex (ptr());
777 Complex& operator= (
const Py_complex& v)
779 set(PyComplex_FromCComplex (v),
true);
783 Complex& operator= (
double v)
785 set(PyComplex_FromDoubles (v, 0.0),
true);
789 Complex& operator= (
int v)
791 set(PyComplex_FromDoubles (
double(v), 0.0),
true);
795 Complex& operator= (
long v)
797 set(PyComplex_FromDoubles (
double(v), 0.0),
true);
801 Complex& operator= (
const Int& iob)
803 set(PyComplex_FromDoubles (
double(
long(iob)), 0.0),
true);
809 return PyComplex_RealAsDouble(ptr());
814 return PyComplex_ImagAsDouble(ptr());
849 seqref (SeqBase<T>& seq, sequence_index_type j)
850 : s(seq), offset(j), the_item (s.getItem(j))
853 seqref (
const seqref<T>& range)
854 : s(range.s), offset(range.offset), the_item(range.the_item)
859 : s(dynamic_cast< SeqBase<T>&>(obj))
861 , the_item(s.getItem(offset))
871 seqref<T>& operator=(
const seqref<T>& rhs)
873 the_item = rhs.the_item;
874 s.setItem(offset, the_item);
878 seqref<T>& operator=(
const T& ob)
881 s.setItem(offset, ob);
886 PyObject* ptr ()
const
888 return the_item.ptr();
891 int reference_count ()
const
893 return the_item.reference_count();
898 return the_item.type();
905 bool hasAttr (
const std::string& attr_name)
const
907 return the_item.hasAttr(attr_name);
910 Object getAttr (
const std::string& attr_name)
const
912 return the_item.getAttr(attr_name);
915 Object getItem (
const Object& key)
const
917 return the_item.getItem(key);
920 long hashValue ()
const
922 return the_item.hashValue();
925 bool isCallable ()
const
927 return the_item.isCallable();
930 bool isInstance ()
const
932 return the_item.isInstance();
937 return the_item.isDict();
942 return the_item.isList();
945 bool isMapping ()
const
947 return the_item.isMapping();
950 bool isNumeric ()
const
952 return the_item.isNumeric();
955 bool isSequence ()
const
957 return the_item.isSequence();
962 return the_item.isTrue();
965 bool isType (
const Type& t)
const
967 return the_item.isType (t);
972 return the_item.isTuple();
975 bool isString()
const
977 return the_item.isString();
980 void setAttr (
const std::string& attr_name,
const Object& value)
982 the_item.setAttr(attr_name, value);
985 void delAttr (
const std::string& attr_name)
987 the_item.delAttr(attr_name);
990 void delItem (
const Object& key)
992 the_item.delItem(key);
995 bool operator==(
const Object& o2)
const
997 return the_item == o2;
1000 bool operator!=(
const Object& o2)
const
1002 return the_item != o2;
1005 bool operator>=(
const Object& o2)
const
1007 return the_item >= o2;
1010 bool operator<=(
const Object& o2)
const
1012 return the_item <= o2;
1015 bool operator<(
const Object& o2)
const
1017 return the_item < o2;
1020 bool operator>(
const Object& o2)
const
1022 return the_item > o2;
1030 template<
typename T>
1031 class SeqBase:
public Object
1035 typedef size_t size_type;
1036 typedef seqref<T> reference;
1037 typedef T const_reference;
1038 typedef seqref<T>* pointer;
1039 typedef int difference_type;
1040 typedef T value_type;
1042 virtual size_type max_size()
const
1044 return std::string::npos;
1047 virtual size_type capacity()
const
1052 virtual void swap(SeqBase<T>& c)
1054 SeqBase<T> temp = c;
1059 virtual size_type size ()
const
1061 return PySequence_Length (ptr());
1064 explicit SeqBase<T> ()
1065 :Object(PyTuple_New(0), true)
1070 explicit SeqBase<T> (PyObject* pyob,
bool owned=
false)
1071 : Object(pyob, owned)
1076 SeqBase<T> (
const Object& ob): Object(ob)
1083 SeqBase<T>& operator= (
const Object& rhs)
1085 return (*
this = *rhs);
1088 SeqBase<T>& operator= (PyObject* rhsp)
1090 if(ptr() == rhsp)
return *
this;
1095 virtual bool accepts (PyObject *pyob)
const
1097 return pyob && PySequence_Check (pyob);
1100 size_type length ()
const
1102 return PySequence_Length (ptr());
1106 const T operator[](sequence_index_type index)
const
1108 return getItem(index);
1111 seqref<T> operator[](sequence_index_type index)
1113 return seqref<T>(*
this, index);
1116 virtual T getItem (sequence_index_type i)
const
1118 return T(asObject(PySequence_GetItem (ptr(), i)));
1121 virtual void setItem (sequence_index_type i,
const T& ob)
1123 if (PySequence_SetItem (ptr(), i, *ob) == -1)
1129 SeqBase<T> repeat (
int count)
const
1131 return SeqBase<T> (PySequence_Repeat (ptr(), count),
true);
1134 SeqBase<T> concat (
const SeqBase<T>& other)
const
1136 return SeqBase<T> (PySequence_Concat(ptr(), *other),
true);
1140 const T front ()
const
1147 return seqref<T>(
this, 0);
1150 const T back ()
const
1152 return getItem(size()-1);
1157 return seqref<T>(
this, size()-1);
1160 void verify_length(size_type required_size)
const
1162 if (size() != required_size)
1163 throw IndexError (
"Unexpected SeqBase<T> length.");
1166 void verify_length(size_type min_size, size_type max_size)
const
1168 size_type n = size();
1169 if (n < min_size || n > max_size)
1170 throw IndexError (
"Unexpected SeqBase<T> length.");
1174 :
public random_access_iterator_parent(seqref<T>)
1177 friend class SeqBase<T>;
1190 iterator (SeqBase<T>* s,
int where)
1195 iterator (
const iterator& other)
1197 , count( other.count )
1200 bool eql (
const iterator& other)
const
1202 return (*seq == *other.seq) && (count == other.count);
1205 bool neq (
const iterator& other)
const
1207 return (*seq != *other.seq) || (count != other.count);
1210 bool lss (
const iterator& other)
const
1212 return (count < other.count);
1215 bool gtr (
const iterator& other)
const
1217 return (count > other.count);
1220 bool leq (
const iterator& other)
const
1222 return (count <= other.count);
1225 bool geq (
const iterator& other)
const
1227 return (count >= other.count);
1230 seqref<T> operator*()
1232 return seqref<T>(*seq, count);
1235 seqref<T> operator[] (sequence_index_type i)
1237 return seqref<T>(*seq, count + i);
1240 iterator& operator=(
const iterator& other)
1242 if (
this == &other)
return *
this;
1244 count = other.count;
1248 iterator operator+(
int n)
const
1250 return iterator(seq, count + n);
1253 iterator operator-(
int n)
const
1255 return iterator(seq, count - n);
1258 iterator& operator+=(
int n)
1264 iterator& operator-=(
int n)
1270 int operator-(
const iterator& other)
const
1272 if (*seq != *other.seq)
1273 throw RuntimeError (
"SeqBase<T>::iterator comparison error");
1274 return count - other.count;
1278 iterator& operator++ ()
1279 { count++;
return *
this;}
1281 iterator operator++ (
int)
1282 {
return iterator(seq, count++);}
1284 iterator& operator-- ()
1285 { count--;
return *
this;}
1287 iterator operator-- (
int)
1288 {
return iterator(seq, count--);}
1290 std::string diagnose()
const
1292 std::ostringstream oss;
1293 oss <<
"iterator diagnosis " << seq <<
", " << count << std::ends;
1294 return std::string(oss.str());
1300 return iterator(
this, 0);
1305 return iterator(
this, length());
1308 class const_iterator
1309 :
public random_access_iterator_parent(const Object)
1312 friend class SeqBase<T>;
1313 const SeqBase<T>* seq;
1314 sequence_index_type count;
1325 const_iterator (
const SeqBase<T>* s,
int where)
1330 const_iterator(
const const_iterator& other)
1332 , count( other.count )
1335 const T operator*()
const
1337 return seq->getItem(count);
1340 const T operator[] (sequence_index_type i)
const
1342 return seq->getItem(count + i);
1345 const_iterator& operator=(
const const_iterator& other)
1347 if (
this == &other)
return *
this;
1349 count = other.count;
1353 const_iterator operator+(
int n)
const
1355 return const_iterator(seq, count + n);
1358 bool eql (
const const_iterator& other)
const
1360 return (*seq == *other.seq) && (count == other.count);
1363 bool neq (
const const_iterator& other)
const
1365 return (*seq != *other.seq) || (count != other.count);
1368 bool lss (
const const_iterator& other)
const
1370 return (count < other.count);
1373 bool gtr (
const const_iterator& other)
const
1375 return (count > other.count);
1378 bool leq (
const const_iterator& other)
const
1380 return (count <= other.count);
1383 bool geq (
const const_iterator& other)
const
1385 return (count >= other.count);
1388 const_iterator operator-(
int n)
1390 return const_iterator(seq, count - n);
1393 const_iterator& operator+=(
int n)
1399 const_iterator& operator-=(
int n)
1405 int operator-(
const const_iterator& other)
const
1407 if (*seq != *other.seq)
1408 throw RuntimeError (
"SeqBase<T>::const_iterator::- error");
1409 return count - other.count;
1412 const_iterator& operator++ ()
1413 { count++;
return *
this;}
1415 const_iterator operator++ (
int)
1416 {
return const_iterator(seq, count++);}
1418 const_iterator& operator-- ()
1419 { count--;
return *
this;}
1421 const_iterator operator-- (
int)
1422 {
return const_iterator(seq, count--);}
1425 const_iterator begin ()
const
1427 return const_iterator(
this, 0);
1430 const_iterator end ()
const
1432 return const_iterator(
this, length());
1437 typedef SeqBase<Object> Sequence;
1439 template <
typename T>
bool operator==(
const typename SeqBase<T>::iterator& left,
const typename SeqBase<T>::iterator& right);
1440 template <
typename T>
bool operator!=(
const typename SeqBase<T>::iterator& left,
const typename SeqBase<T>::iterator& right);
1441 template <
typename T>
bool operator< (
const typename SeqBase<T>::iterator& left,
const typename SeqBase<T>::iterator& right);
1442 template <
typename T>
bool operator> (
const typename SeqBase<T>::iterator& left,
const typename SeqBase<T>::iterator& right);
1443 template <
typename T>
bool operator<=(
const typename SeqBase<T>::iterator& left,
const typename SeqBase<T>::iterator& right);
1444 template <
typename T>
bool operator>=(
const typename SeqBase<T>::iterator& left,
const typename SeqBase<T>::iterator& right);
1446 template <
typename T>
bool operator==(
const typename SeqBase<T>::const_iterator& left,
const typename SeqBase<T>::const_iterator& right);
1447 template <
typename T>
bool operator!=(
const typename SeqBase<T>::const_iterator& left,
const typename SeqBase<T>::const_iterator& right);
1448 template <
typename T>
bool operator< (
const typename SeqBase<T>::const_iterator& left,
const typename SeqBase<T>::const_iterator& right);
1449 template <
typename T>
bool operator> (
const typename SeqBase<T>::const_iterator& left,
const typename SeqBase<T>::const_iterator& right);
1450 template <
typename T>
bool operator<=(
const typename SeqBase<T>::const_iterator& left,
const typename SeqBase<T>::const_iterator& right);
1451 template <
typename T>
bool operator>=(
const typename SeqBase<T>::const_iterator& left,
const typename SeqBase<T>::const_iterator& right);
1454 extern bool operator==(
const Sequence::iterator& left,
const Sequence::iterator& right);
1455 extern bool operator!=(
const Sequence::iterator& left,
const Sequence::iterator& right);
1456 extern bool operator< (
const Sequence::iterator& left,
const Sequence::iterator& right);
1457 extern bool operator> (
const Sequence::iterator& left,
const Sequence::iterator& right);
1458 extern bool operator<=(
const Sequence::iterator& left,
const Sequence::iterator& right);
1459 extern bool operator>=(
const Sequence::iterator& left,
const Sequence::iterator& right);
1461 extern bool operator==(
const Sequence::const_iterator& left,
const Sequence::const_iterator& right);
1462 extern bool operator!=(
const Sequence::const_iterator& left,
const Sequence::const_iterator& right);
1463 extern bool operator< (
const Sequence::const_iterator& left,
const Sequence::const_iterator& right);
1464 extern bool operator> (
const Sequence::const_iterator& left,
const Sequence::const_iterator& right);
1465 extern bool operator<=(
const Sequence::const_iterator& left,
const Sequence::const_iterator& right);
1466 extern bool operator>=(
const Sequence::const_iterator& left,
const Sequence::const_iterator& right);
1473 typedef std::basic_string<Py_UNICODE> unicodestring;
1474 extern Py_UNICODE unicode_null_string[1];
1476 class Char:
public Object
1479 explicit Char (PyObject *pyob,
bool owned =
false): Object(pyob, owned)
1484 Char (
const Object& ob): Object(ob)
1489 Char (
const std::string& v =
"")
1490 :Object(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true)
1496 : Object(PyString_FromStringAndSize (&v, 1), true)
1502 : Object(PyUnicode_FromUnicode (&v, 1), true)
1507 Char& operator= (
const Object& rhs)
1509 return (*
this = *rhs);
1512 Char& operator= (PyObject* rhsp)
1514 if(ptr() == rhsp)
return *
this;
1520 virtual bool accepts (PyObject *pyob)
const
1522 return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob)) && PySequence_Length (pyob) == 1;
1526 Char& operator= (
const std::string& v)
1528 set(PyString_FromStringAndSize (
const_cast<char*
>(v.c_str()),1),
true);
1532 Char& operator= (
char v)
1534 set(PyString_FromStringAndSize (&v, 1),
true);
1538 Char& operator= (
const unicodestring& v)
1540 set(PyUnicode_FromUnicode (
const_cast<Py_UNICODE*
>(v.data()),1),
true);
1544 Char& operator= (Py_UNICODE v)
1546 set(PyUnicode_FromUnicode (&v, 1),
true);
1553 operator std::string ()
const
1555 return std::string(PyString_AsString (ptr()));
1559 class String:
public SeqBase<Char>
1562 virtual size_type capacity()
const
1567 explicit String (PyObject *pyob,
bool owned =
false): SeqBase<Char>(pyob, owned)
1572 String (
const Object& ob): SeqBase<Char>(ob)
1578 : SeqBase<Char>( PyString_FromStringAndSize(
"", 0 ), true )
1583 String(
const std::string& v )
1584 : SeqBase<Char>( PyString_FromStringAndSize( const_cast<char*>(v.data()),
1585 static_cast<int>( v.length() ) ), true )
1590 String(
const char *s,
const char *encoding,
const char *error=
"strict" )
1591 : SeqBase<Char>( PyUnicode_Decode( s, strlen( s ), encoding, error ), true )
1596 String(
const char *s,
int len,
const char *encoding,
const char *error=
"strict" )
1597 : SeqBase<Char>( PyUnicode_Decode( s, len, encoding, error ), true )
1602 String(
const std::string &s,
const char *encoding,
const char *error=
"strict" )
1603 : SeqBase<Char>( PyUnicode_Decode( s.c_str(), s.length(), encoding, error ), true )
1608 String(
const std::string& v, std::string::size_type vsize )
1609 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v.data()),
1610 static_cast<int>( vsize ) ), true)
1615 String(
const char *v,
int vsize )
1616 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v), vsize ), true )
1621 String(
const char* v )
1622 : SeqBase<Char>( PyString_FromString( v ), true )
1628 String& operator= (
const Object& rhs )
1630 return *
this = *rhs;
1633 String& operator= (PyObject* rhsp)
1641 virtual bool accepts (PyObject *pyob)
const
1643 return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob));
1647 String& operator= (
const std::string& v)
1649 set( PyString_FromStringAndSize(
const_cast<char*
>( v.data() ),
1650 static_cast<int>( v.length() ) ),
true );
1653 String& operator= (
const unicodestring& v)
1655 set( PyUnicode_FromUnicode(
const_cast<Py_UNICODE*
>( v.data() ),
1656 static_cast<int>( v.length() ) ),
true );
1662 String encode(
const char *encoding,
const char *error=
"strict" )
1666 return String( PyUnicode_AsEncodedString( ptr(), encoding, error ) );
1670 return String( PyString_AsEncodedObject( ptr(), encoding, error ) );
1674 String decode(
const char *encoding,
const char *error=
"strict" )
1676 return Object( PyString_AsDecodedObject( ptr(), encoding, error ) );
1680 virtual size_type size ()
const
1684 return static_cast<size_type
>( PyUnicode_GET_SIZE (ptr()) );
1688 return static_cast<size_type
>( PyString_Size (ptr()) );
1692 operator std::string ()
const
1694 return as_std_string();
1697 std::string as_std_string()
const
1701 throw TypeError(
"cannot return std::string from Unicode object");
1705 return std::string( PyString_AsString( ptr() ),
static_cast<size_type
>( PyString_Size( ptr() ) ) );
1709 unicodestring as_unicodestring()
const
1713 return unicodestring( PyUnicode_AS_UNICODE( ptr() ),
1714 static_cast<size_type
>( PyUnicode_GET_SIZE( ptr() ) ) );
1718 throw TypeError(
"can only return unicodestring from Unicode object");
1725 class Tuple:
public Sequence
1728 virtual void setItem (sequence_index_type offset,
const Object&ob)
1731 if(PyTuple_SetItem (ptr(), offset, new_reference_to(ob)) == -1)
1738 explicit Tuple (PyObject *pyob,
bool owned =
false): Sequence (pyob, owned)
1743 Tuple (
const Object& ob): Sequence(ob)
1749 explicit Tuple (
int size = 0)
1751 set(PyTuple_New (size),
true);
1753 for (sequence_index_type i=0; i < size; i++)
1755 if(PyTuple_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
1762 explicit Tuple (
const Sequence& s)
1764 sequence_index_type limit( sequence_index_type( s.length() ) );
1766 set(PyTuple_New (limit),
true);
1769 for(sequence_index_type i=0; i < limit; i++)
1771 if(PyTuple_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
1779 Tuple& operator= (
const Object& rhs)
1781 return (*
this = *rhs);
1784 Tuple& operator= (PyObject* rhsp)
1786 if(ptr() == rhsp)
return *
this;
1791 virtual bool accepts (PyObject *pyob)
const
1793 return pyob && Py::_Tuple_Check (pyob);
1796 Tuple getSlice (
int i,
int j)
const
1798 return Tuple (PySequence_GetSlice (ptr(), i, j),
true);
1806 class List:
public Sequence
1810 explicit List (PyObject *pyob,
bool owned =
false): Sequence(pyob, owned)
1814 List (
const Object& ob): Sequence(ob)
1821 set(PyList_New (size),
true);
1823 for (sequence_index_type i=0; i < size; i++)
1825 if(PyList_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
1833 List (
const Sequence& s): Sequence()
1836 set(PyList_New (n),
true);
1838 for (sequence_index_type i=0; i < n; i++)
1840 if(PyList_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
1847 virtual size_type capacity()
const
1853 List& operator= (
const Object& rhs)
1855 return (*
this = *rhs);
1858 List& operator= (PyObject* rhsp)
1860 if(ptr() == rhsp)
return *
this;
1865 virtual bool accepts (PyObject *pyob)
const
1867 return pyob && Py::_List_Check (pyob);
1870 List getSlice (
int i,
int j)
const
1872 return List (PyList_GetSlice (ptr(), i, j),
true);
1875 void setSlice (
int i,
int j,
const Object& v)
1877 if(PyList_SetSlice (ptr(), i, j, *v) == -1)
1883 void append (
const Object& ob)
1885 if(PyList_Append (ptr(), *ob) == -1)
1891 void insert (
int i,
const Object& ob)
1893 if(PyList_Insert (ptr(), i, *ob) == -1)
1901 if(PyList_Sort(ptr()) == -1)
1909 if(PyList_Reverse(ptr()) == -1)
1919 template<
typename T>
1928 mapref<T> (MapBase<T>& map,
const std::string& k)
1929 : s(map), the_item()
1932 if(map.hasKey(key)) the_item = map.getItem(key);
1935 mapref<T> (MapBase<T>& map,
const Object& k)
1936 : s(map), key(k), the_item()
1938 if(map.hasKey(key)) the_item = map.getItem(key);
1946 mapref<T>& operator=(
const mapref<T>& other)
1948 if(
this == &other)
return *
this;
1949 the_item = other.the_item;
1950 s.setItem(key, other.the_item);
1954 mapref<T>& operator= (
const T& ob)
1957 s.setItem (key, ob);
1968 PyObject* ptr ()
const
1970 return the_item.ptr();
1973 int reference_count ()
const
1975 return the_item.reference_count();
1980 return the_item.type();
1985 return the_item.str();
1990 return the_item.repr();
1993 bool hasAttr (
const std::string& attr_name)
const
1995 return the_item.hasAttr(attr_name);
1998 Object getAttr (
const std::string& attr_name)
const
2000 return the_item.getAttr(attr_name);
2003 Object getItem (
const Object& k)
const
2005 return the_item.getItem(k);
2008 long hashValue ()
const
2010 return the_item.hashValue();
2013 bool isCallable ()
const
2015 return the_item.isCallable();
2018 bool isList ()
const
2020 return the_item.isList();
2023 bool isMapping ()
const
2025 return the_item.isMapping();
2028 bool isNumeric ()
const
2030 return the_item.isNumeric();
2033 bool isSequence ()
const
2035 return the_item.isSequence();
2038 bool isTrue ()
const
2040 return the_item.isTrue();
2043 bool isType (
const Type& t)
const
2045 return the_item.isType (t);
2048 bool isTuple()
const
2050 return the_item.isTuple();
2053 bool isString()
const
2055 return the_item.isString();
2059 void setAttr (
const std::string& attr_name,
const Object& value)
2061 the_item.setAttr(attr_name, value);
2064 void delAttr (
const std::string& attr_name)
2066 the_item.delAttr(attr_name);
2069 void delItem (
const Object& k)
2071 the_item.delItem(k);
2077 bool operator==(
const mapref<T>& left,
const mapref<T>& right)
2083 bool operator!=(
const mapref<T>& left,
const mapref<T>& right)
2088 template<
typename T>
2089 class MapBase:
public Object
2092 explicit MapBase<T>()
2100 typedef size_t size_type;
2101 typedef Object key_type;
2102 typedef mapref<T> data_type;
2103 typedef std::pair< const T, T > value_type;
2104 typedef std::pair< const T, mapref<T> > reference;
2105 typedef const std::pair< const T, const T > const_reference;
2106 typedef std::pair< const T, mapref<T> > pointer;
2109 explicit MapBase<T> (PyObject *pyob,
bool owned =
false): Object(pyob, owned)
2115 MapBase<T> (
const Object& ob): Object(ob)
2121 MapBase<T>& operator= (
const Object& rhs)
2123 return (*
this = *rhs);
2126 MapBase<T>& operator= (PyObject* rhsp)
2128 if(ptr() == rhsp)
return *
this;
2133 virtual bool accepts (PyObject *pyob)
const
2135 return pyob && PyMapping_Check(pyob);
2144 for(List::iterator i = k.begin(); i != k.end(); i++)
2150 virtual size_type size()
const
2152 return PyMapping_Length (ptr());
2156 T operator[](
const std::string& key)
const
2158 return getItem(key);
2161 T operator[](
const Object& key)
const
2163 return getItem(key);
2166 mapref<T> operator[](
const std::string& key)
2168 return mapref<T>(*
this, key);
2171 mapref<T> operator[](
const Object& key)
2173 return mapref<T>(*
this, key);
2178 return PyMapping_Length (ptr());
2181 bool hasKey (
const std::string& s)
const
2183 return PyMapping_HasKeyString (ptr(),
const_cast<char*
>(s.c_str())) != 0;
2186 bool hasKey (
const Object& s)
const
2188 return PyMapping_HasKey (ptr(), s.ptr()) != 0;
2191 T getItem (
const std::string& s)
const
2194 asObject(PyMapping_GetItemString (ptr(),
const_cast<char*
>(s.c_str())))
2198 T getItem (
const Object& s)
const
2201 asObject(PyObject_GetItem (ptr(), s.ptr()))
2205 virtual void setItem (
const char *s,
const Object& ob)
2207 if (PyMapping_SetItemString (ptr(),
const_cast<char*
>(s), *ob) == -1)
2213 virtual void setItem (
const std::string& s,
const Object& ob)
2215 if (PyMapping_SetItemString (ptr(),
const_cast<char*
>(s.c_str()), *ob) == -1)
2221 virtual void setItem (
const Object& s,
const Object& ob)
2223 if (PyObject_SetItem (ptr(), s.ptr(), ob.ptr()) == -1)
2229 void delItem (
const std::string& s)
2231 if (PyMapping_DelItemString (ptr(),
const_cast<char*
>(s.c_str())) == -1)
2237 void delItem (
const Object& s)
2239 if (PyMapping_DelItem (ptr(), *s) == -1)
2247 return List(PyMapping_Keys(ptr()),
true);
2250 List values ()
const
2252 return List(PyMapping_Values(ptr()),
true);
2257 return List(PyMapping_Items(ptr()),
true);
2272 typedef cxx::Dict d_t;
2274 cxx::String s1(
"blah");
2275 cxx::String s2(
"gorf");
2284 for( ; it != d.end(); ++it )
2286 d_t::value_type vt( *it );
2287 cxx::String rs = vt.second.repr();
2288 std::string ls = rs.operator std::string();
2289 fprintf( stderr,
"%s\n", ls );
2297 typedef std::forward_iterator_tag iterator_category;
2298 typedef std::pair< const T, T > value_type;
2299 typedef int difference_type;
2300 typedef std::pair< const T, mapref<T> > pointer;
2301 typedef std::pair< const T, mapref<T> > reference;
2303 friend class MapBase<T>;
2319 iterator (MapBase<T>* m,
bool end =
false )
2322 , pos( end ? keys.end() : keys.begin() )
2325 iterator (
const iterator& other)
2327 , keys( other.keys )
2331 reference operator*()
2334 return std::make_pair(key, mapref<T>(*map,key));
2337 iterator& operator=(
const iterator& other)
2347 bool eql(
const iterator& right)
const
2349 return *map == *right.map && pos == right.pos;
2351 bool neq(
const iterator& right )
const
2353 return *map != *right.map || pos != right.pos;
2361 iterator& operator++ ()
2362 { pos++;
return *
this;}
2364 iterator operator++ (
int)
2365 {
return iterator(map, keys, pos++);}
2367 iterator& operator-- ()
2368 { pos--;
return *
this;}
2370 iterator operator-- (
int)
2371 {
return iterator(map, keys, pos--);}
2373 std::string diagnose()
const
2375 std::ostringstream oss;
2376 oss <<
"iterator diagnosis " << map <<
", " << pos << std::ends;
2377 return std::string(oss.str());
2383 return iterator(
this);
2388 return iterator(
this,
true);
2391 class const_iterator
2394 typedef std::forward_iterator_tag iterator_category;
2395 typedef const std::pair< const T, T > value_type;
2396 typedef int difference_type;
2397 typedef const std::pair< const T, T > pointer;
2398 typedef const std::pair< const T, T > reference;
2400 friend class MapBase<T>;
2401 const MapBase<T>* map;
2415 const_iterator (
const MapBase<T>* m, List k, List::iterator p )
2421 const_iterator(
const const_iterator& other)
2423 , keys( other.keys )
2427 bool eql(
const const_iterator& right)
const
2429 return *map == *right.map && pos == right.pos;
2431 bool neq(
const const_iterator& right )
const
2433 return *map != *right.map || pos != right.pos;
2443 const_iterator& operator=(
const const_iterator& other)
2445 if (
this == &other)
return *
this;
2453 const_iterator& operator++ ()
2454 { pos++;
return *
this;}
2456 const_iterator operator++ (
int)
2457 {
return const_iterator(map, keys, pos++);}
2459 const_iterator& operator-- ()
2460 { pos--;
return *
this;}
2462 const_iterator operator-- (
int)
2463 {
return const_iterator(map, keys, pos--);}
2466 const_iterator begin ()
const
2468 return const_iterator(
this, 0);
2471 const_iterator end ()
const
2473 return const_iterator(
this, length());
2478 typedef MapBase<Object> Mapping;
2480 template <
typename T>
bool operator==(
const typename MapBase<T>::iterator& left,
const typename MapBase<T>::iterator& right);
2481 template <
typename T>
bool operator!=(
const typename MapBase<T>::iterator& left,
const typename MapBase<T>::iterator& right);
2482 template <
typename T>
bool operator==(
const typename MapBase<T>::const_iterator& left,
const typename MapBase<T>::const_iterator& right);
2483 template <
typename T>
bool operator!=(
const typename MapBase<T>::const_iterator& left,
const typename MapBase<T>::const_iterator& right);
2485 extern bool operator==(
const Mapping::iterator& left,
const Mapping::iterator& right);
2486 extern bool operator!=(
const Mapping::iterator& left,
const Mapping::iterator& right);
2487 extern bool operator==(
const Mapping::const_iterator& left,
const Mapping::const_iterator& right);
2488 extern bool operator!=(
const Mapping::const_iterator& left,
const Mapping::const_iterator& right);
2493 class Dict:
public Mapping
2497 explicit Dict (PyObject *pyob,
bool owned=
false): Mapping (pyob, owned)
2501 Dict (
const Dict& ob): Mapping(ob)
2508 set(PyDict_New (),
true);
2513 Dict& operator= (
const Object& rhs)
2515 return (*
this = *rhs);
2518 Dict& operator= (PyObject* rhsp)
2520 if(ptr() == rhsp)
return *
this;
2525 virtual bool accepts (PyObject *pyob)
const
2527 return pyob && Py::_Dict_Check (pyob);
2531 class Callable:
public Object
2534 explicit Callable (): Object()
2538 explicit Callable (PyObject *pyob,
bool owned =
false): Object (pyob, owned)
2543 Callable (
const Object& ob): Object(ob)
2550 Callable& operator= (
const Object& rhs)
2552 return (*
this = *rhs);
2555 Callable& operator= (PyObject* rhsp)
2557 if(ptr() == rhsp)
return *
this;
2563 virtual bool accepts (PyObject *pyob)
const
2565 return pyob && PyCallable_Check (pyob);
2569 Object apply(
const Tuple& args)
const
2571 return asObject(PyObject_CallObject(ptr(), args.ptr()));
2575 Object apply(
const Tuple& args,
const Dict& kw)
const
2577 return asObject(PyObject_CallObject(ptr(), args.ptr(), kw.ptr()));
2580 Object apply(PyObject* pargs = 0)
const
2582 return apply (Tuple(pargs));
2586 class Module:
public Object
2589 explicit Module (PyObject* pyob,
bool owned =
false): Object (pyob, owned)
2595 explicit Module (
const std::string&s): Object()
2597 PyObject *m = PyImport_AddModule(
const_cast<char *
>(s.c_str()) );
2603 Module (
const Module& ob): Object(*ob)
2608 Module& operator= (
const Object& rhs)
2610 return (*
this = *rhs);
2613 Module& operator= (PyObject* rhsp)
2615 if(ptr() == rhsp)
return *
this;
2622 return Dict(PyModule_GetDict(ptr()));
2628 inline Object operator+ (
const Object& a)
2630 return asObject(PyNumber_Positive(*a));
2632 inline Object operator- (
const Object& a)
2634 return asObject(PyNumber_Negative(*a));
2637 inline Object abs(
const Object& a)
2639 return asObject(PyNumber_Absolute(*a));
2642 inline std::pair<Object,Object> coerce(
const Object& a,
const Object& b)
2647 if(PyNumber_Coerce(&p1,&p2) == -1)
2651 return std::pair<Object,Object>(asObject(p1), asObject(p2));
2654 inline Object operator+ (
const Object& a,
const Object& b)
2656 return asObject(PyNumber_Add(*a, *b));
2658 inline Object operator+ (
const Object& a,
int j)
2660 return asObject(PyNumber_Add(*a, *Int(j)));
2662 inline Object operator+ (
const Object& a,
double v)
2664 return asObject(PyNumber_Add(*a, *Float(v)));
2666 inline Object operator+ (
int j,
const Object& b)
2668 return asObject(PyNumber_Add(*Int(j), *b));
2670 inline Object operator+ (
double v,
const Object& b)
2672 return asObject(PyNumber_Add(*Float(v), *b));
2675 inline Object operator- (
const Object& a,
const Object& b)
2677 return asObject(PyNumber_Subtract(*a, *b));
2679 inline Object operator- (
const Object& a,
int j)
2681 return asObject(PyNumber_Subtract(*a, *Int(j)));
2683 inline Object operator- (
const Object& a,
double v)
2685 return asObject(PyNumber_Subtract(*a, *Float(v)));
2687 inline Object operator- (
int j,
const Object& b)
2689 return asObject(PyNumber_Subtract(*Int(j), *b));
2691 inline Object operator- (
double v,
const Object& b)
2693 return asObject(PyNumber_Subtract(*Float(v), *b));
2696 inline Object operator* (
const Object& a,
const Object& b)
2698 return asObject(PyNumber_Multiply(*a, *b));
2700 inline Object operator* (
const Object& a,
int j)
2702 return asObject(PyNumber_Multiply(*a, *Int(j)));
2704 inline Object operator* (
const Object& a,
double v)
2706 return asObject(PyNumber_Multiply(*a, *Float(v)));
2708 inline Object operator* (
int j,
const Object& b)
2710 return asObject(PyNumber_Multiply(*Int(j), *b));
2712 inline Object operator* (
double v,
const Object& b)
2714 return asObject(PyNumber_Multiply(*Float(v), *b));
2717 inline Object operator/ (
const Object& a,
const Object& b)
2719 return asObject(PyNumber_Divide(*a, *b));
2721 inline Object operator/ (
const Object& a,
int j)
2723 return asObject(PyNumber_Divide(*a, *Int(j)));
2725 inline Object operator/ (
const Object& a,
double v)
2727 return asObject(PyNumber_Divide(*a, *Float(v)));
2729 inline Object operator/ (
int j,
const Object& b)
2731 return asObject(PyNumber_Divide(*Int(j), *b));
2733 inline Object operator/ (
double v,
const Object& b)
2735 return asObject(PyNumber_Divide(*Float(v), *b));
2738 inline Object operator% (
const Object& a,
const Object& b)
2740 return asObject(PyNumber_Remainder(*a, *b));
2742 inline Object operator% (
const Object& a,
int j)
2744 return asObject(PyNumber_Remainder(*a, *Int(j)));
2746 inline Object operator% (
const Object& a,
double v)
2748 return asObject(PyNumber_Remainder(*a, *Float(v)));
2750 inline Object operator% (
int j,
const Object& b)
2752 return asObject(PyNumber_Remainder(*Int(j), *b));
2754 inline Object operator% (
double v,
const Object& b)
2756 return asObject(PyNumber_Remainder(*Float(v), *b));
2759 inline Object type(
const Exception&)
2761 PyObject *ptype, *pvalue, *ptrace;
2762 PyErr_Fetch(&ptype, &pvalue, &ptrace);
2763 Object result(ptype);
2764 PyErr_Restore(ptype, pvalue, ptrace);
2768 inline Object value(
const Exception&)
2770 PyObject *ptype, *pvalue, *ptrace;
2771 PyErr_Fetch(&ptype, &pvalue, &ptrace);
2773 if(pvalue) result = pvalue;
2774 PyErr_Restore(ptype, pvalue, ptrace);
2778 inline Object trace(
const Exception&)
2780 PyObject *ptype, *pvalue, *ptrace;
2781 PyErr_Fetch(&ptype, &pvalue, &ptrace);
2783 if(ptrace) result = ptrace;
2784 PyErr_Restore(ptype, pvalue, ptrace);
2791String seqref<T>::str ()
const
2793 return the_item.str();
2797String seqref<T>::repr ()
const
2799 return the_item.repr();
const char * name(StdAction id)
@ List
TQValueList<TQVariant>.