00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef __CXX_Objects__h
00039 #define __CXX_Objects__h
00040
00041 #include "CXX/WrapPython.h"
00042 #include "CXX/Version.hxx"
00043 #include "CXX/Config.hxx"
00044 #include "CXX/Exception.hxx"
00045
00046 #include <iostream>
00047 #include STR_STREAM
00048 #include <string>
00049 #include <iterator>
00050 #include <utility>
00051 #include <typeinfo>
00052
00053 namespace Py
00054 {
00055 typedef int sequence_index_type;
00056
00057
00058 class Object;
00059 class Type;
00060 template<TEMPLATE_TYPENAME T> class SeqBase;
00061 class String;
00062 class List;
00063 template<TEMPLATE_TYPENAME T> class MapBase;
00064 class Tuple;
00065 class Dict;
00066
00067
00068 inline PyObject* new_reference_to(PyObject* p)
00069 {
00070 Py::_XINCREF(p);
00071 return p;
00072 }
00073
00074
00075
00076 inline PyObject* Null()
00077 {
00078 return (static_cast<PyObject*>(0));
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 class PYCXX_EXPORT Object
00145 {
00146 private:
00147
00148
00149
00150
00151
00152 PyObject* p;
00153
00154 protected:
00155
00156 void set (PyObject* pyob, bool owned = false)
00157 {
00158 release();
00159 p = pyob;
00160 if (!owned)
00161 {
00162 Py::_XINCREF (p);
00163 }
00164 validate();
00165 }
00166
00167 void release ()
00168 {
00169 Py::_XDECREF (p);
00170 p = 0;
00171 }
00172
00173 void validate();
00174
00175 public:
00176
00177 explicit Object (PyObject* pyob=Py::_None(), bool owned = false): p (pyob)
00178 {
00179 if(!owned)
00180 {
00181 Py::_XINCREF (p);
00182 }
00183 validate();
00184 }
00185
00186
00187 Object (const Object& ob): p(ob.p)
00188 {
00189 Py::_XINCREF (p);
00190 validate();
00191 }
00192
00193
00194 Object& operator= (const Object& rhs)
00195 {
00196 set(rhs.p);
00197 return *this;
00198 }
00199
00200 Object& operator= (PyObject* rhsp)
00201 {
00202 if(ptr() == rhsp) return *this;
00203 set (rhsp);
00204 return *this;
00205 }
00206
00207
00208 virtual ~Object ()
00209 {
00210 release ();
00211 }
00212
00213
00214 PyObject* operator* () const
00215 {
00216 return p;
00217 }
00218
00219
00220 void increment_reference_count()
00221 {
00222 Py::_XINCREF(p);
00223 }
00224
00225 void decrement_reference_count()
00226 {
00227
00228 if(reference_count() == 1)
00229 throw RuntimeError("Object::decrement_reference_count error.");
00230 Py::_XDECREF(p);
00231 }
00232
00233 PyObject* ptr () const
00234 {
00235 return p;
00236 }
00237
00238
00239
00240
00241
00242
00243 virtual bool accepts (PyObject *pyob) const
00244 {
00245 return (pyob != 0);
00246 }
00247
00248 Py_ssize_t reference_count () const
00249 {
00250 return p ? p->ob_refcnt : 0;
00251 }
00252
00253 Type type () const;
00254
00255 String str () const;
00256
00257 std::string as_string() const;
00258
00259 String repr () const;
00260
00261 List dir () const;
00262
00263 bool hasAttr (const std::string& s) const
00264 {
00265 return PyObject_HasAttrString (p, const_cast<char*>(s.c_str())) ? true: false;
00266 }
00267
00268 Object getAttr (const std::string& s) const
00269 {
00270 return Object (PyObject_GetAttrString (p, const_cast<char*>(s.c_str())), true);
00271 }
00272
00273 Object callMemberFunction( const std::string &function_name ) const;
00274 Object callMemberFunction( const std::string &function_name, const Tuple &args ) const;
00275 Object callMemberFunction( const std::string &function_name, const Tuple &args, const Dict &kw ) const;
00276
00277 Object getItem (const Object& key) const
00278 {
00279 return Object (PyObject_GetItem(p, *key), true);
00280 }
00281
00282 long hashValue () const
00283 {
00284 return PyObject_Hash (p);
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 bool is(PyObject *pother) const
00294 {
00295 return p == pother;
00296 }
00297
00298 bool is(const Object& other) const
00299 {
00300 return p == other.p;
00301 }
00302
00303 bool isNone() const
00304 {
00305 return p == _None();
00306 }
00307
00308 bool isCallable () const
00309 {
00310 return PyCallable_Check (p) != 0;
00311 }
00312
00313 bool isInstance () const
00314 {
00315 return PyInstance_Check (p) != 0;
00316 }
00317
00318 bool isDict () const
00319 {
00320 return Py::_Dict_Check (p);
00321 }
00322
00323 bool isList () const
00324 {
00325 return Py::_List_Check (p);
00326 }
00327
00328 bool isMapping () const
00329 {
00330 return PyMapping_Check (p) != 0;
00331 }
00332
00333 bool isNumeric () const
00334 {
00335 return PyNumber_Check (p) != 0;
00336 }
00337
00338 bool isSequence () const
00339 {
00340 return PySequence_Check (p) != 0;
00341 }
00342
00343 bool isTrue () const
00344 {
00345 return PyObject_IsTrue (p) != 0;
00346 }
00347
00348 bool isType (const Type& t) const;
00349
00350 bool isTuple() const
00351 {
00352 return Py::_Tuple_Check(p);
00353 }
00354
00355 bool isString() const
00356 {
00357 return Py::_String_Check(p) || Py::_Unicode_Check(p);
00358 }
00359
00360 bool isUnicode() const
00361 {
00362 return Py::_Unicode_Check( p );
00363 }
00364
00365 bool isBoolean() const
00366 {
00367 return Py::_Boolean_Check( p );
00368 }
00369
00370
00371 void setAttr (const std::string& s, const Object& value)
00372 {
00373 if(PyObject_SetAttrString (p, const_cast<char*>(s.c_str()), *value) == -1)
00374 throw AttributeError ("getAttr failed.");
00375 }
00376
00377 void delAttr (const std::string& s)
00378 {
00379 if(PyObject_DelAttrString (p, const_cast<char*>(s.c_str())) == -1)
00380 throw AttributeError ("delAttr failed.");
00381 }
00382
00383
00384
00385
00386 void delItem (const Object& key)
00387 {
00388
00389
00390 throw KeyError("delItem failed.");
00391 }
00392
00393
00394
00395 bool operator==(const Object& o2) const
00396 {
00397 int k = PyObject_RichCompareBool (p, *o2, Py_EQ);
00398 if (PyErr_Occurred()) throw Exception();
00399 return k != 0;
00400 }
00401
00402 bool operator!=(const Object& o2) const
00403 {
00404 int k = PyObject_RichCompareBool (p, *o2, Py_NE);
00405 if (PyErr_Occurred()) throw Exception();
00406 return k != 0;
00407
00408 }
00409
00410 bool operator>=(const Object& o2) const
00411 {
00412 int k = PyObject_RichCompareBool (p, *o2, Py_GE);
00413 if (PyErr_Occurred()) throw Exception();
00414 return k != 0;
00415 }
00416
00417 bool operator<=(const Object& o2) const
00418 {
00419 int k = PyObject_RichCompareBool (p, *o2, Py_LE);
00420 if (PyErr_Occurred()) throw Exception();
00421 return k != 0;
00422 }
00423
00424 bool operator<(const Object& o2) const
00425 {
00426 int k = PyObject_RichCompareBool (p, *o2, Py_LT);
00427 if (PyErr_Occurred()) throw Exception();
00428 return k != 0;
00429 }
00430
00431 bool operator>(const Object& o2) const
00432 {
00433 int k = PyObject_RichCompareBool (p, *o2, Py_GT);
00434 if (PyErr_Occurred()) throw Exception();
00435 return k != 0;
00436 }
00437 };
00438
00439 inline PyObject* new_reference_to(const Object& g)
00440 {
00441 PyObject* p = g.ptr();
00442 Py::_XINCREF(p);
00443 return p;
00444 }
00445
00446
00447
00448 inline Object Nothing()
00449 {
00450 return Object(Py::_None());
00451 }
00452
00453
00454 inline Object None()
00455 {
00456 return Object(Py::_None());
00457 }
00458
00459
00460 inline Object False()
00461 {
00462 return Object(Py::_False());
00463 }
00464
00465 inline Object True()
00466 {
00467 return Object(Py::_True());
00468 }
00469
00470
00471 #ifndef CXX_NO_IOSTREAMS
00472 PYCXX_EXPORT std::ostream& operator<< (std::ostream& os, const Object& ob);
00473 #endif
00474
00475
00476 class PYCXX_EXPORT Type: public Object
00477 {
00478 public:
00479 explicit Type (PyObject* pyob, bool owned = false): Object(pyob, owned)
00480 {
00481 validate();
00482 }
00483
00484 Type (const Object& ob): Object(*ob)
00485 {
00486 validate();
00487 }
00488
00489 Type(const Type& t): Object(t)
00490 {
00491 validate();
00492 }
00493
00494 Type& operator= (const Object& rhs)
00495 {
00496 return (*this = *rhs);
00497 }
00498
00499 Type& operator= (PyObject* rhsp)
00500 {
00501 if(ptr() == rhsp) return *this;
00502 set (rhsp);
00503 return *this;
00504 }
00505 virtual bool accepts (PyObject *pyob) const
00506 {
00507 return pyob && Py::_Type_Check (pyob);
00508 }
00509 };
00510
00511
00512
00513
00514
00515 inline Object asObject (PyObject *p)
00516 {
00517 return Object(p, true);
00518 }
00519
00520
00521
00522 class PYCXX_EXPORT Boolean: public Object
00523 {
00524 public:
00525
00526 Boolean (PyObject *pyob, bool owned = false)
00527 : Object (pyob, owned)
00528 {
00529 validate();
00530 }
00531
00532 Boolean (const Boolean& ob): Object(*ob)
00533 {
00534 validate();
00535 }
00536
00537
00538 Boolean (bool v=false)
00539 {
00540 set(PyBool_FromLong(v ? 1 : 0), true);
00541 validate();
00542 }
00543
00544 explicit Boolean (const Object& ob)
00545 : Object( *ob )
00546 {
00547 validate();
00548 }
00549
00550
00551
00552 Boolean& operator= (const Object& rhs)
00553 {
00554 return (*this = *rhs);
00555 }
00556
00557 Boolean& operator= (PyObject* rhsp)
00558 {
00559 if(ptr() == rhsp) return *this;
00560 set (rhsp);
00561 return *this;
00562 }
00563
00564
00565 virtual bool accepts (PyObject *pyob) const
00566 {
00567 return pyob && PyObject_IsTrue(pyob) != -1;
00568 }
00569
00570
00571 operator bool() const
00572 {
00573 return PyObject_IsTrue (ptr()) != 0;
00574 }
00575
00576 Boolean& operator= (bool v)
00577 {
00578 set (PyBool_FromLong (v ? 1 : 0), true);
00579 return *this;
00580 }
00581 };
00582
00583
00584
00585 class PYCXX_EXPORT Int: public Object
00586 {
00587 public:
00588
00589 Int (PyObject *pyob, bool owned = false): Object (pyob, owned)
00590 {
00591 validate();
00592 }
00593
00594 Int (const Int& ob): Object(*ob)
00595 {
00596 validate();
00597 }
00598
00599
00600 Int (long v = 0L): Object(PyInt_FromLong(v), true)
00601 {
00602 validate();
00603 }
00604
00605
00606 Int (int v)
00607 {
00608 long w = v;
00609 set(PyInt_FromLong(w), true);
00610 validate();
00611 }
00612
00613
00614 Int (bool v)
00615 {
00616 long w = v ? 1 : 0;
00617 set(PyInt_FromLong(w), true);
00618 validate();
00619 }
00620
00621 explicit Int (const Object& ob)
00622 {
00623 set(PyNumber_Int(*ob), true);
00624 validate();
00625 }
00626
00627
00628
00629 Int& operator= (const Object& rhs)
00630 {
00631 return (*this = *rhs);
00632 }
00633
00634 Int& operator= (PyObject* rhsp)
00635 {
00636 if(ptr() == rhsp) return *this;
00637 set (PyNumber_Int(rhsp), true);
00638 return *this;
00639 }
00640
00641
00642 virtual bool accepts (PyObject *pyob) const
00643 {
00644 return pyob && Py::_Int_Check (pyob);
00645 }
00646
00647
00648 operator long() const
00649 {
00650 return PyInt_AsLong (ptr());
00651 }
00652
00653 #ifdef HAVE_LONG_LONG
00654
00655 PY_LONG_LONG asLongLong() const
00656 {
00657 return PyLong_AsLongLong (ptr());
00658 }
00659
00660 unsigned PY_LONG_LONG asUnsignedLongLong() const
00661 {
00662 return PyLong_AsUnsignedLongLong (ptr());
00663 }
00664 #endif
00665
00666
00667 Int& operator= (int v)
00668 {
00669 set (PyInt_FromLong (long(v)), true);
00670 return *this;
00671 }
00672
00673
00674 Int& operator= (long v)
00675 {
00676 set (PyInt_FromLong (v), true);
00677 return *this;
00678 }
00679
00680 #ifdef HAVE_LONG_LONG
00681
00682 Int& operator= (PY_LONG_LONG v)
00683 {
00684 set (PyLong_FromLongLong (v), true);
00685 return *this;
00686 }
00687
00688 Int& operator= (unsigned PY_LONG_LONG v)
00689 {
00690 set (PyLong_FromUnsignedLongLong (v), true);
00691 return *this;
00692 }
00693 #endif
00694 };
00695
00696
00697
00698 class PYCXX_EXPORT Long: public Object
00699 {
00700 public:
00701
00702 explicit Long (PyObject *pyob, bool owned = false): Object (pyob, owned)
00703 {
00704 validate();
00705 }
00706
00707 Long (const Long& ob): Object(ob.ptr())
00708 {
00709 validate();
00710 }
00711
00712
00713 explicit Long (long v = 0L)
00714 : Object(PyLong_FromLong(v), true)
00715 {
00716 validate();
00717 }
00718
00719 explicit Long (unsigned long v)
00720 : Object(PyLong_FromUnsignedLong(v), true)
00721 {
00722 validate();
00723 }
00724
00725 explicit Long (int v)
00726 : Object(PyLong_FromLong(static_cast<long>(v)), true)
00727 {
00728 validate();
00729 }
00730
00731
00732 Long (const Object& ob)
00733 : Object(PyNumber_Long(*ob), true)
00734 {
00735 validate();
00736 }
00737
00738
00739
00740 Long& operator= (const Object& rhs)
00741 {
00742 return (*this = *rhs);
00743 }
00744
00745 Long& operator= (PyObject* rhsp)
00746 {
00747 if(ptr() == rhsp) return *this;
00748 set (PyNumber_Long(rhsp), true);
00749 return *this;
00750 }
00751
00752 virtual bool accepts (PyObject *pyob) const
00753 {
00754 return pyob && Py::_Long_Check (pyob);
00755 }
00756
00757
00758 long as_long() const
00759 {
00760 return PyLong_AsLong( ptr() );
00761 }
00762
00763
00764 operator long() const
00765 {
00766 return as_long();
00767 }
00768
00769
00770 operator unsigned long() const
00771 {
00772 return PyLong_AsUnsignedLong (ptr());
00773 }
00774 operator double() const
00775 {
00776 return PyLong_AsDouble (ptr());
00777 }
00778
00779 Long& operator= (int v)
00780 {
00781 set(PyLong_FromLong (long(v)), true);
00782 return *this;
00783 }
00784
00785 Long& operator= (long v)
00786 {
00787 set(PyLong_FromLong (v), true);
00788 return *this;
00789 }
00790
00791 Long& operator= (unsigned long v)
00792 {
00793 set(PyLong_FromUnsignedLong (v), true);
00794 return *this;
00795 }
00796 };
00797
00798 #ifdef HAVE_LONG_LONG
00799
00800
00801 class PYCXX_EXPORT LongLong: public Object
00802 {
00803 public:
00804
00805 explicit LongLong (PyObject *pyob, bool owned = false): Object (pyob, owned)
00806 {
00807 validate();
00808 }
00809
00810 LongLong (const LongLong& ob): Object(ob.ptr())
00811 {
00812 validate();
00813 }
00814
00815 explicit LongLong (PY_LONG_LONG v = 0L)
00816 : Object(PyLong_FromLongLong(v), true)
00817 {
00818 validate();
00819 }
00820
00821 explicit LongLong (unsigned PY_LONG_LONG v)
00822 : Object(PyLong_FromUnsignedLongLong(v), true)
00823 {
00824 validate();
00825 }
00826
00827 explicit LongLong (long v)
00828 : Object(PyLong_FromLongLong(v), true)
00829 {
00830 validate();
00831 }
00832
00833 explicit LongLong (unsigned long v)
00834 : Object(PyLong_FromUnsignedLongLong(v), true)
00835 {
00836 validate();
00837 }
00838
00839 explicit LongLong (int v)
00840 : Object(PyLong_FromLongLong(static_cast<PY_LONG_LONG>(v)), true)
00841 {
00842 validate();
00843 }
00844
00845
00846 LongLong (const Object& ob)
00847 : Object(PyNumber_Long(*ob), true)
00848 {
00849 validate();
00850 }
00851
00852
00853
00854 LongLong& operator= (const Object& rhs)
00855 {
00856 return (*this = *rhs);
00857 }
00858
00859 LongLong& operator= (PyObject* rhsp)
00860 {
00861 if(ptr() == rhsp) return *this;
00862 set (PyNumber_Long(rhsp), true);
00863 return *this;
00864 }
00865
00866 virtual bool accepts (PyObject *pyob) const
00867 {
00868 return pyob && Py::_Long_Check (pyob);
00869 }
00870
00871 operator PY_LONG_LONG() const
00872 {
00873 return PyLong_AsLongLong (ptr());
00874 }
00875
00876 operator unsigned PY_LONG_LONG() const
00877 {
00878 return PyLong_AsUnsignedLongLong (ptr());
00879 }
00880
00881 operator long() const
00882 {
00883 return PyLong_AsLong (ptr());
00884 }
00885
00886 operator unsigned long() const
00887 {
00888 return PyLong_AsUnsignedLong (ptr());
00889 }
00890 operator double() const
00891 {
00892 return PyLong_AsDouble (ptr());
00893 }
00894
00895 LongLong& operator= (int v)
00896 {
00897 set(PyLong_FromLongLong (long(v)), true);
00898 return *this;
00899 }
00900
00901 LongLong& operator= (PY_LONG_LONG v)
00902 {
00903 set(PyLong_FromLongLong (v), true);
00904 return *this;
00905 }
00906
00907 LongLong& operator= (unsigned PY_LONG_LONG v)
00908 {
00909 set(PyLong_FromUnsignedLongLong (v), true);
00910 return *this;
00911 }
00912
00913 LongLong& operator= (long v)
00914 {
00915 set(PyLong_FromLongLong (v), true);
00916 return *this;
00917 }
00918
00919 LongLong& operator= (unsigned long v)
00920 {
00921 set(PyLong_FromUnsignedLongLong (v), true);
00922 return *this;
00923 }
00924 };
00925 #endif
00926
00927
00928
00929
00930 class PYCXX_EXPORT Float: public Object
00931 {
00932 public:
00933
00934 explicit Float (PyObject *pyob, bool owned = false): Object(pyob, owned)
00935 {
00936 validate();
00937 }
00938
00939 Float (const Float& f): Object(f)
00940 {
00941 validate();
00942 }
00943
00944
00945 explicit Float (double v=0.0)
00946 : Object(PyFloat_FromDouble (v), true)
00947 {
00948 validate();
00949 }
00950
00951
00952 Float (const Object& ob)
00953 : Object(PyNumber_Float(*ob), true)
00954 {
00955 validate();
00956 }
00957
00958 Float& operator= (const Object& rhs)
00959 {
00960 return (*this = *rhs);
00961 }
00962
00963 Float& operator= (PyObject* rhsp)
00964 {
00965 if(ptr() == rhsp) return *this;
00966 set (PyNumber_Float(rhsp), true);
00967 return *this;
00968 }
00969
00970 virtual bool accepts (PyObject *pyob) const
00971 {
00972 return pyob && Py::_Float_Check (pyob);
00973 }
00974
00975 operator double () const
00976 {
00977 return PyFloat_AsDouble (ptr());
00978 }
00979
00980 Float& operator= (double v)
00981 {
00982 set(PyFloat_FromDouble (v), true);
00983 return *this;
00984 }
00985
00986 Float& operator= (int v)
00987 {
00988 set(PyFloat_FromDouble (double(v)), true);
00989 return *this;
00990 }
00991
00992 Float& operator= (long v)
00993 {
00994 set(PyFloat_FromDouble (double(v)), true);
00995 return *this;
00996 }
00997
00998 Float& operator= (const Int& iob)
00999 {
01000 set(PyFloat_FromDouble (double(long(iob))), true);
01001 return *this;
01002 }
01003 };
01004
01005
01006
01007 class PYCXX_EXPORT Complex: public Object
01008 {
01009 public:
01010
01011 explicit Complex (PyObject *pyob, bool owned = false): Object(pyob, owned)
01012 {
01013 validate();
01014 }
01015
01016 Complex (const Complex& f): Object(f)
01017 {
01018 validate();
01019 }
01020
01021
01022 explicit Complex (double v=0.0, double w=0.0)
01023 :Object(PyComplex_FromDoubles (v, w), true)
01024 {
01025 validate();
01026 }
01027
01028 Complex& operator= (const Object& rhs)
01029 {
01030 return (*this = *rhs);
01031 }
01032
01033 Complex& operator= (PyObject* rhsp)
01034 {
01035 if(ptr() == rhsp) return *this;
01036 set (rhsp);
01037 return *this;
01038 }
01039
01040 virtual bool accepts (PyObject *pyob) const
01041 {
01042 return pyob && Py::_Complex_Check (pyob);
01043 }
01044
01045 operator Py_complex () const
01046 {
01047 return PyComplex_AsCComplex (ptr());
01048 }
01049
01050 Complex& operator= (const Py_complex& v)
01051 {
01052 set(PyComplex_FromCComplex (v), true);
01053 return *this;
01054 }
01055
01056 Complex& operator= (double v)
01057 {
01058 set(PyComplex_FromDoubles (v, 0.0), true);
01059 return *this;
01060 }
01061
01062 Complex& operator= (int v)
01063 {
01064 set(PyComplex_FromDoubles (double(v), 0.0), true);
01065 return *this;
01066 }
01067
01068 Complex& operator= (long v)
01069 {
01070 set(PyComplex_FromDoubles (double(v), 0.0), true);
01071 return *this;
01072 }
01073
01074 Complex& operator= (const Int& iob)
01075 {
01076 set(PyComplex_FromDoubles (double(long(iob)), 0.0), true);
01077 return *this;
01078 }
01079
01080 double real() const
01081 {
01082 return PyComplex_RealAsDouble(ptr());
01083 }
01084
01085 double imag() const
01086 {
01087 return PyComplex_ImagAsDouble(ptr());
01088 }
01089 };
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113 template<TEMPLATE_TYPENAME T>
01114 class seqref
01115 {
01116 protected:
01117 SeqBase<T>& s;
01118 int offset;
01119 T the_item;
01120 public:
01121
01122 seqref (SeqBase<T>& seq, sequence_index_type j)
01123 : s(seq), offset(j), the_item (s.getItem(j))
01124 {}
01125
01126 seqref (const seqref<T>& range)
01127 : s(range.s), offset(range.offset), the_item(range.the_item)
01128 {}
01129
01130
01131 seqref (Object& obj)
01132 : s(dynamic_cast< SeqBase<T>&>(obj))
01133 , offset( NULL )
01134 , the_item(s.getItem(offset))
01135 {}
01136 ~seqref()
01137 {}
01138
01139 operator T() const
01140 {
01141 return the_item;
01142 }
01143
01144 seqref<T>& operator=(const seqref<T>& rhs)
01145 {
01146 the_item = rhs.the_item;
01147 s.setItem(offset, the_item);
01148 return *this;
01149 }
01150
01151 seqref<T>& operator=(const T& ob)
01152 {
01153 the_item = ob;
01154 s.setItem(offset, ob);
01155 return *this;
01156 }
01157
01158
01159 PyObject* ptr () const
01160 {
01161 return the_item.ptr();
01162 }
01163
01164 int reference_count () const
01165 {
01166 return the_item.reference_count();
01167 }
01168
01169 Type type () const
01170 {
01171 return the_item.type();
01172 }
01173
01174 String str () const;
01175
01176 String repr () const;
01177
01178 bool hasAttr (const std::string& attr_name) const
01179 {
01180 return the_item.hasAttr(attr_name);
01181 }
01182
01183 Object getAttr (const std::string& attr_name) const
01184 {
01185 return the_item.getAttr(attr_name);
01186 }
01187
01188 Object getItem (const Object& key) const
01189 {
01190 return the_item.getItem(key);
01191 }
01192
01193 long hashValue () const
01194 {
01195 return the_item.hashValue();
01196 }
01197
01198 bool isCallable () const
01199 {
01200 return the_item.isCallable();
01201 }
01202
01203 bool isInstance () const
01204 {
01205 return the_item.isInstance();
01206 }
01207
01208 bool isDict () const
01209 {
01210 return the_item.isDict();
01211 }
01212
01213 bool isList () const
01214 {
01215 return the_item.isList();
01216 }
01217
01218 bool isMapping () const
01219 {
01220 return the_item.isMapping();
01221 }
01222
01223 bool isNumeric () const
01224 {
01225 return the_item.isNumeric();
01226 }
01227
01228 bool isSequence () const
01229 {
01230 return the_item.isSequence();
01231 }
01232
01233 bool isTrue () const
01234 {
01235 return the_item.isTrue();
01236 }
01237
01238 bool isType (const Type& t) const
01239 {
01240 return the_item.isType (t);
01241 }
01242
01243 bool isTuple() const
01244 {
01245 return the_item.isTuple();
01246 }
01247
01248 bool isString() const
01249 {
01250 return the_item.isString();
01251 }
01252
01253 void setAttr (const std::string& attr_name, const Object& value)
01254 {
01255 the_item.setAttr(attr_name, value);
01256 }
01257
01258 void delAttr (const std::string& attr_name)
01259 {
01260 the_item.delAttr(attr_name);
01261 }
01262
01263 void delItem (const Object& key)
01264 {
01265 the_item.delItem(key);
01266 }
01267
01268 bool operator==(const Object& o2) const
01269 {
01270 return the_item == o2;
01271 }
01272
01273 bool operator!=(const Object& o2) const
01274 {
01275 return the_item != o2;
01276 }
01277
01278 bool operator>=(const Object& o2) const
01279 {
01280 return the_item >= o2;
01281 }
01282
01283 bool operator<=(const Object& o2) const
01284 {
01285 return the_item <= o2;
01286 }
01287
01288 bool operator<(const Object& o2) const
01289 {
01290 return the_item < o2;
01291 }
01292
01293 bool operator>(const Object& o2) const
01294 {
01295 return the_item > o2;
01296 }
01297 };
01298
01299
01300
01301
01302
01303 template<TEMPLATE_TYPENAME T>
01304 class SeqBase: public Object
01305 {
01306 public:
01307
01308 typedef size_t size_type;
01309 typedef seqref<T> reference;
01310 typedef T const_reference;
01311 typedef seqref<T>* pointer;
01312 typedef int difference_type;
01313 typedef T value_type;
01314
01315 virtual size_type max_size() const
01316 {
01317 return std::string::npos;
01318 }
01319
01320 virtual size_type capacity() const
01321 {
01322 return size();
01323 }
01324
01325 virtual void swap(SeqBase<T>& c)
01326 {
01327 SeqBase<T> temp = c;
01328 c = ptr();
01329 set(temp.ptr());
01330 }
01331
01332 virtual size_type size () const
01333 {
01334 return PySequence_Length (ptr());
01335 }
01336
01337 explicit SeqBase<T> ()
01338 :Object(PyTuple_New(0), true)
01339 {
01340 validate();
01341 }
01342
01343 explicit SeqBase<T> (PyObject* pyob, bool owned=false)
01344 : Object(pyob, owned)
01345 {
01346 validate();
01347 }
01348
01349 SeqBase<T> (const Object& ob): Object(ob)
01350 {
01351 validate();
01352 }
01353
01354
01355
01356 SeqBase<T>& operator= (const Object& rhs)
01357 {
01358 return (*this = *rhs);
01359 }
01360
01361 SeqBase<T>& operator= (PyObject* rhsp)
01362 {
01363 if(ptr() == rhsp) return *this;
01364 set (rhsp);
01365 return *this;
01366 }
01367
01368 virtual bool accepts (PyObject *pyob) const
01369 {
01370 return pyob && PySequence_Check (pyob);
01371 }
01372
01373 size_type length () const
01374 {
01375 return PySequence_Length (ptr());
01376 }
01377
01378
01379 const T operator[](sequence_index_type index) const
01380 {
01381 return getItem(index);
01382 }
01383
01384 seqref<T> operator[](sequence_index_type index)
01385 {
01386 return seqref<T>(*this, index);
01387 }
01388
01389 virtual T getItem (sequence_index_type i) const
01390 {
01391 return T(asObject(PySequence_GetItem (ptr(), i)));
01392 }
01393
01394 virtual void setItem (sequence_index_type i, const T& ob)
01395 {
01396 if (PySequence_SetItem (ptr(), i, *ob) == -1)
01397 {
01398 throw Exception();
01399 }
01400 }
01401
01402 SeqBase<T> repeat (int count) const
01403 {
01404 return SeqBase<T> (PySequence_Repeat (ptr(), count), true);
01405 }
01406
01407 SeqBase<T> concat (const SeqBase<T>& other) const
01408 {
01409 return SeqBase<T> (PySequence_Concat(ptr(), *other), true);
01410 }
01411
01412
01413 const T front () const
01414 {
01415 return getItem(0);
01416 }
01417
01418 seqref<T> front()
01419 {
01420 return seqref<T>(*this, 0);
01421 }
01422
01423 const T back() const
01424 {
01425 return getItem(size()-1);
01426 }
01427
01428 seqref<T> back()
01429 {
01430 return seqref<T>(*this, size()-1);
01431 }
01432
01433 void verify_length(size_type required_size) const
01434 {
01435 if (size() != required_size)
01436 throw IndexError ("Unexpected SeqBase<T> length.");
01437 }
01438
01439 void verify_length(size_type min_size, size_type max_size) const
01440 {
01441 size_type n = size();
01442 if (n < min_size || n > max_size)
01443 throw IndexError ("Unexpected SeqBase<T> length.");
01444 }
01445
01446 class iterator
01447 : public random_access_iterator_parent(seqref<T>)
01448 {
01449 protected:
01450 friend class SeqBase<T>;
01451 SeqBase<T>* seq;
01452 int count;
01453
01454 public:
01455 ~iterator ()
01456 {}
01457
01458 iterator ()
01459 : seq( 0 )
01460 , count( 0 )
01461 {}
01462
01463 iterator (SeqBase<T>* s, int where)
01464 : seq( s )
01465 , count( where )
01466 {}
01467
01468 iterator (const iterator& other)
01469 : seq( other.seq )
01470 , count( other.count )
01471 {}
01472
01473 bool eql (const iterator& other) const
01474 {
01475 return (seq->ptr() == other.seq->ptr()) && (count == other.count);
01476 }
01477
01478 bool neq (const iterator& other) const
01479 {
01480 return (seq->ptr() != other.seq->ptr()) || (count != other.count);
01481 }
01482
01483 bool lss (const iterator& other) const
01484 {
01485 return (count < other.count);
01486 }
01487
01488 bool gtr (const iterator& other) const
01489 {
01490 return (count > other.count);
01491 }
01492
01493 bool leq (const iterator& other) const
01494 {
01495 return (count <= other.count);
01496 }
01497
01498 bool geq (const iterator& other) const
01499 {
01500 return (count >= other.count);
01501 }
01502
01503 seqref<T> operator*()
01504 {
01505 return seqref<T>(*seq, count);
01506 }
01507
01508 seqref<T> operator[] (sequence_index_type i)
01509 {
01510 return seqref<T>(*seq, count + i);
01511 }
01512
01513 iterator& operator=(const iterator& other)
01514 {
01515 if (this == &other) return *this;
01516 seq = other.seq;
01517 count = other.count;
01518 return *this;
01519 }
01520
01521 iterator operator+(int n) const
01522 {
01523 return iterator(seq, count + n);
01524 }
01525
01526 iterator operator-(int n) const
01527 {
01528 return iterator(seq, count - n);
01529 }
01530
01531 iterator& operator+=(int n)
01532 {
01533 count = count + n;
01534 return *this;
01535 }
01536
01537 iterator& operator-=(int n)
01538 {
01539 count = count - n;
01540 return *this;
01541 }
01542
01543 int operator-(const iterator& other) const
01544 {
01545 if (*seq != *other.seq)
01546 throw RuntimeError ("SeqBase<T>::iterator comparison error");
01547 return count - other.count;
01548 }
01549
01550
01551 iterator& operator++ ()
01552 { count++; return *this;}
01553
01554 iterator operator++ (int)
01555 { return iterator(seq, count++);}
01556
01557 iterator& operator-- ()
01558 { count--; return *this;}
01559
01560 iterator operator-- (int)
01561 { return iterator(seq, count--);}
01562
01563 std::string diagnose() const
01564 {
01565 std::OSTRSTREAM oss;
01566 oss << "iterator diagnosis " << seq << ", " << count << std::ends;
01567 return std::string(oss.str());
01568 }
01569 };
01570
01571 iterator begin ()
01572 {
01573 return iterator(this, 0);
01574 }
01575
01576 iterator end ()
01577 {
01578 return iterator(this, length());
01579 }
01580
01581 class const_iterator
01582 : public random_access_iterator_parent(const Object)
01583 {
01584 protected:
01585 friend class SeqBase<T>;
01586 const SeqBase<T>* seq;
01587 sequence_index_type count;
01588
01589 private:
01590 const_iterator (const SeqBase<T>* s, int where)
01591 : seq( s )
01592 , count( where )
01593 {}
01594
01595 public:
01596 ~const_iterator ()
01597 {}
01598
01599 const_iterator ()
01600 : seq( 0 )
01601 , count( 0 )
01602 {}
01603
01604 const_iterator(const const_iterator& other)
01605 : seq( other.seq )
01606 , count( other.count )
01607 {}
01608
01609 const T operator*() const
01610 {
01611 return seq->getItem(count);
01612 }
01613
01614 const T operator[] (sequence_index_type i) const
01615 {
01616 return seq->getItem(count + i);
01617 }
01618
01619 const_iterator& operator=(const const_iterator& other)
01620 {
01621 if (this == &other) return *this;
01622 seq = other.seq;
01623 count = other.count;
01624 return *this;
01625 }
01626
01627 const_iterator operator+(int n) const
01628 {
01629 return const_iterator(seq, count + n);
01630 }
01631
01632 bool eql (const const_iterator& other) const
01633 {
01634 return (seq->ptr() == other.seq->ptr()) && (count == other.count);
01635 }
01636
01637 bool neq (const const_iterator& other) const
01638 {
01639 return (seq->ptr() != other.seq->ptr()) || (count != other.count);
01640 }
01641
01642 bool lss (const const_iterator& other) const
01643 {
01644 return (count < other.count);
01645 }
01646
01647 bool gtr (const const_iterator& other) const
01648 {
01649 return (count > other.count);
01650 }
01651
01652 bool leq (const const_iterator& other) const
01653 {
01654 return (count <= other.count);
01655 }
01656
01657 bool geq (const const_iterator& other) const
01658 {
01659 return (count >= other.count);
01660 }
01661
01662 const_iterator operator-(int n)
01663 {
01664 return const_iterator(seq, count - n);
01665 }
01666
01667 const_iterator& operator+=(int n)
01668 {
01669 count = count + n;
01670 return *this;
01671 }
01672
01673 const_iterator& operator-=(int n)
01674 {
01675 count = count - n;
01676 return *this;
01677 }
01678
01679 int operator-(const const_iterator& other) const
01680 {
01681 if (*seq != *other.seq)
01682 throw RuntimeError ("SeqBase<T>::const_iterator::- error");
01683 return count - other.count;
01684 }
01685
01686 const_iterator& operator++ ()
01687 { count++; return *this;}
01688
01689 const_iterator operator++ (int)
01690 { return const_iterator(seq, count++);}
01691
01692 const_iterator& operator-- ()
01693 { count--; return *this;}
01694
01695 const_iterator operator-- (int)
01696 { return const_iterator(seq, count--);}
01697 };
01698
01699 const_iterator begin () const
01700 {
01701 return const_iterator(this, 0);
01702 }
01703
01704 const_iterator end () const
01705 {
01706 return const_iterator(this, length());
01707 }
01708 };
01709
01710
01711 typedef SeqBase<Object> Sequence;
01712
01713 template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
01714 template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
01715 template <TEMPLATE_TYPENAME T> bool operator< (const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
01716 template <TEMPLATE_TYPENAME T> bool operator> (const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
01717 template <TEMPLATE_TYPENAME T> bool operator<=(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
01718 template <TEMPLATE_TYPENAME T> bool operator>=(const EXPLICIT_TYPENAME SeqBase<T>::iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::iterator& right);
01719
01720 template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
01721 template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
01722 template <TEMPLATE_TYPENAME T> bool operator< (const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
01723 template <TEMPLATE_TYPENAME T> bool operator> (const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
01724 template <TEMPLATE_TYPENAME T> bool operator<=(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
01725 template <TEMPLATE_TYPENAME T> bool operator>=(const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& left, const EXPLICIT_TYPENAME SeqBase<T>::const_iterator& right);
01726
01727 PYCXX_EXPORT extern bool operator==(const Sequence::iterator& left, const Sequence::iterator& right);
01728 PYCXX_EXPORT extern bool operator!=(const Sequence::iterator& left, const Sequence::iterator& right);
01729 PYCXX_EXPORT extern bool operator< (const Sequence::iterator& left, const Sequence::iterator& right);
01730 PYCXX_EXPORT extern bool operator> (const Sequence::iterator& left, const Sequence::iterator& right);
01731 PYCXX_EXPORT extern bool operator<=(const Sequence::iterator& left, const Sequence::iterator& right);
01732 PYCXX_EXPORT extern bool operator>=(const Sequence::iterator& left, const Sequence::iterator& right);
01733
01734 PYCXX_EXPORT extern bool operator==(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
01735 PYCXX_EXPORT extern bool operator!=(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
01736 PYCXX_EXPORT extern bool operator< (const Sequence::const_iterator& left, const Sequence::const_iterator& right);
01737 PYCXX_EXPORT extern bool operator> (const Sequence::const_iterator& left, const Sequence::const_iterator& right);
01738 PYCXX_EXPORT extern bool operator<=(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
01739 PYCXX_EXPORT extern bool operator>=(const Sequence::const_iterator& left, const Sequence::const_iterator& right);
01740
01741
01742
01743
01744
01745
01746 typedef std::basic_string<Py_UNICODE> unicodestring;
01747 extern Py_UNICODE unicode_null_string[1];
01748
01749 class PYCXX_EXPORT Char: public Object
01750 {
01751 public:
01752 explicit Char (PyObject *pyob, bool owned = false): Object(pyob, owned)
01753 {
01754 validate();
01755 }
01756
01757 Char (const Object& ob): Object(ob)
01758 {
01759 validate();
01760 }
01761
01762 Char (const std::string& v = "")
01763 :Object(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true)
01764 {
01765 validate();
01766 }
01767
01768 Char (char v)
01769 : Object(PyString_FromStringAndSize (&v, 1), true)
01770 {
01771 validate();
01772 }
01773
01774 Char (Py_UNICODE v)
01775 : Object(PyUnicode_FromUnicode (&v, 1), true)
01776 {
01777 validate();
01778 }
01779
01780 Char& operator= (const Object& rhs)
01781 {
01782 return (*this = *rhs);
01783 }
01784
01785 Char& operator= (PyObject* rhsp)
01786 {
01787 if(ptr() == rhsp) return *this;
01788 set (rhsp);
01789 return *this;
01790 }
01791
01792
01793 virtual bool accepts (PyObject *pyob) const
01794 {
01795 return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob)) && PySequence_Length (pyob) == 1;
01796 }
01797
01798
01799 Char& operator= (const std::string& v)
01800 {
01801 set(PyString_FromStringAndSize (const_cast<char*>(v.c_str()),1), true);
01802 return *this;
01803 }
01804
01805 Char& operator= (char v)
01806 {
01807 set(PyString_FromStringAndSize (&v, 1), true);
01808 return *this;
01809 }
01810
01811 Char& operator= (const unicodestring& v)
01812 {
01813 set(PyUnicode_FromUnicode (const_cast<Py_UNICODE*>(v.data()),1), true);
01814 return *this;
01815 }
01816
01817 Char& operator= (Py_UNICODE v)
01818 {
01819 set(PyUnicode_FromUnicode (&v, 1), true);
01820 return *this;
01821 }
01822
01823
01824 operator String() const;
01825
01826 operator std::string () const
01827 {
01828 return std::string(PyString_AsString (ptr()));
01829 }
01830 };
01831
01832 #ifdef PYCXX_PYTHON_2TO3
01833
01834 class Bytes;
01835
01836 class PYCXX_EXPORT String: public SeqBase<Char>
01837 {
01838 public:
01839 virtual size_type capacity() const
01840 {
01841 return max_size();
01842 }
01843
01844 explicit String( PyObject *pyob, bool owned = false)
01845 : SeqBase<Char>( pyob, owned )
01846 {
01847 validate();
01848 }
01849
01850 String( const Object& ob): SeqBase<Char>(ob)
01851 {
01852 validate();
01853 }
01854
01855 String()
01856 : SeqBase<Char>( PyString_FromStringAndSize( "", 0 ), true )
01857 {
01858 validate();
01859 }
01860
01861 String( const std::string& v )
01862 : SeqBase<Char>( PyString_FromStringAndSize( const_cast<char*>(v.data()),
01863 static_cast<int>( v.length() ) ), true )
01864 {
01865 validate();
01866 }
01867
01868 String( const Py_UNICODE *s, int length )
01869 : SeqBase<Char>( PyUnicode_FromUnicode( s, length ), true )
01870 {
01871 validate();
01872 }
01873
01874 String( const char *s, const char *encoding, const char *error="strict" )
01875 : SeqBase<Char>( PyUnicode_Decode( s, strlen( s ), encoding, error ), true )
01876 {
01877 validate();
01878 }
01879
01880 String( const char *s, int len, const char *encoding, const char *error="strict" )
01881 : SeqBase<Char>( PyUnicode_Decode( s, len, encoding, error ), true )
01882 {
01883 validate();
01884 }
01885
01886 String( const std::string &s, const char *encoding, const char *error="strict" )
01887 : SeqBase<Char>( PyUnicode_Decode( s.c_str(), s.length(), encoding, error ), true )
01888 {
01889 validate();
01890 }
01891
01892 String( const std::string &v, std::string::size_type vsize )
01893 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v.data()),
01894 static_cast<int>( vsize ) ), true)
01895 {
01896 validate();
01897 }
01898
01899 String( const char *v, int vsize )
01900 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v), vsize ), true )
01901 {
01902 validate();
01903 }
01904
01905 String( const char *v )
01906 : SeqBase<Char>( PyString_FromString( v ), true )
01907 {
01908 validate();
01909 }
01910
01911
01912 String &operator=( const Object &rhs )
01913 {
01914 return *this = *rhs;
01915 }
01916
01917 String& operator= (PyObject *rhsp)
01918 {
01919 if( ptr() == rhsp )
01920 return *this;
01921 set (rhsp);
01922 return *this;
01923 }
01924
01925 virtual bool accepts (PyObject *pyob) const
01926 {
01927 return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob));
01928 }
01929
01930
01931 String& operator=( const std::string &v )
01932 {
01933 set( PyString_FromStringAndSize( const_cast<char*>( v.data() ),
01934 static_cast<int>( v.length() ) ), true );
01935 return *this;
01936 }
01937 String& operator=( const unicodestring &v )
01938 {
01939 set( PyUnicode_FromUnicode( const_cast<Py_UNICODE*>( v.data() ),
01940 static_cast<int>( v.length() ) ), true );
01941 return *this;
01942 }
01943
01944
01945 Bytes encode( const char *encoding, const char *error="strict" ) const;
01946
01947
01948 virtual size_type size() const
01949 {
01950 if( isUnicode() )
01951 {
01952 return static_cast<size_type>( PyUnicode_GET_SIZE (ptr()) );
01953 }
01954 else
01955 {
01956 return static_cast<size_type>( PyString_Size (ptr()) );
01957 }
01958 }
01959
01960 operator std::string() const
01961 {
01962 return as_std_string( "utf-8" );
01963 }
01964
01965 std::string as_std_string( const char *encoding, const char *error="strict" ) const;
01966
01967 unicodestring as_unicodestring() const
01968 {
01969 if( isUnicode() )
01970 {
01971 return unicodestring( PyUnicode_AS_UNICODE( ptr() ),
01972 static_cast<size_type>( PyUnicode_GET_SIZE( ptr() ) ) );
01973 }
01974 else
01975 {
01976 throw TypeError("can only return unicodestring from Unicode object");
01977 }
01978 }
01979
01980 const Py_UNICODE *unicode_data() const
01981 {
01982 if( isUnicode() )
01983 {
01984 return PyUnicode_AS_UNICODE( ptr() );
01985 }
01986 else
01987 {
01988 throw TypeError("can only return unicode_data from Unicode object");
01989 }
01990 }
01991 };
01992 class PYCXX_EXPORT Bytes: public SeqBase<Char>
01993 {
01994 public:
01995 virtual size_type capacity() const
01996 {
01997 return max_size();
01998 }
01999
02000 explicit Bytes (PyObject *pyob, bool owned = false): SeqBase<Char>(pyob, owned)
02001 {
02002 validate();
02003 }
02004
02005 Bytes (const Object& ob): SeqBase<Char>(ob)
02006 {
02007 validate();
02008 }
02009
02010 Bytes()
02011 : SeqBase<Char>( PyString_FromStringAndSize( "", 0 ), true )
02012 {
02013 validate();
02014 }
02015
02016 Bytes( const std::string& v )
02017 : SeqBase<Char>( PyString_FromStringAndSize( const_cast<char*>(v.data()), static_cast<int>( v.length() ) ), true )
02018 {
02019 validate();
02020 }
02021
02022 Bytes( const std::string& v, std::string::size_type vsize )
02023 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v.data()), static_cast<int>( vsize ) ), true)
02024 {
02025 validate();
02026 }
02027
02028 Bytes( const char *v, int vsize )
02029 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v), vsize ), true )
02030 {
02031 validate();
02032 }
02033
02034 Bytes( const char *v )
02035 : SeqBase<Char>( PyString_FromString( v ), true )
02036 {
02037 validate();
02038 }
02039
02040
02041 Bytes &operator= ( const Object& rhs )
02042 {
02043 return *this = *rhs;
02044 }
02045
02046 Bytes &operator= (PyObject *rhsp)
02047 {
02048 if( ptr() == rhsp )
02049 return *this;
02050 set (rhsp);
02051 return *this;
02052 }
02053
02054 virtual bool accepts( PyObject *pyob ) const
02055 {
02056 return pyob && (Py::_String_Check( pyob ) || Py::_Unicode_Check( pyob ));
02057 }
02058
02059
02060 Bytes &operator= (const std::string& v)
02061 {
02062 set( PyString_FromStringAndSize( const_cast<char*>( v.data() ),
02063 static_cast<int>( v.length() ) ), true );
02064 return *this;
02065 }
02066 Bytes &operator= (const unicodestring& v)
02067 {
02068 set( PyUnicode_FromUnicode( const_cast<Py_UNICODE*>( v.data() ),
02069 static_cast<int>( v.length() ) ), true );
02070 return *this;
02071 }
02072
02073 String decode( const char *encoding, const char *error="strict" )
02074 {
02075 return Object( PyString_AsDecodedObject( ptr(), encoding, error ) );
02076 }
02077
02078
02079 virtual size_type size () const
02080 {
02081 if( isUnicode() )
02082 {
02083 return static_cast<size_type>( PyUnicode_GET_SIZE (ptr()) );
02084 }
02085 else
02086 {
02087 return static_cast<size_type>( PyString_Size (ptr()) );
02088 }
02089 }
02090
02091 operator std::string () const
02092 {
02093 return as_std_string();
02094 }
02095
02096 std::string as_std_string() const
02097 {
02098 if( isUnicode() )
02099 {
02100 throw TypeError("cannot return std::string from Unicode object");
02101 }
02102 else
02103 {
02104 return std::string( PyString_AsString( ptr() ), static_cast<size_type>( PyString_Size( ptr() ) ) );
02105 }
02106 }
02107
02108 unicodestring as_unicodestring() const
02109 {
02110 if( isUnicode() )
02111 {
02112 return unicodestring( PyUnicode_AS_UNICODE( ptr() ),
02113 static_cast<size_type>( PyUnicode_GET_SIZE( ptr() ) ) );
02114 }
02115 else
02116 {
02117 throw TypeError("can only return unicodestring from Unicode object");
02118 }
02119 }
02120 };
02121
02122 #else
02123
02124 class PYCXX_EXPORT String: public SeqBase<Char>
02125 {
02126 public:
02127 virtual size_type capacity() const
02128 {
02129 return max_size();
02130 }
02131
02132 explicit String (PyObject *pyob, bool owned = false): SeqBase<Char>(pyob, owned)
02133 {
02134 validate();
02135 }
02136
02137 String (const Object& ob): SeqBase<Char>(ob)
02138 {
02139 validate();
02140 }
02141
02142 String()
02143 : SeqBase<Char>( PyString_FromStringAndSize( "", 0 ), true )
02144 {
02145 validate();
02146 }
02147
02148 String( const std::string& v )
02149 : SeqBase<Char>( PyString_FromStringAndSize( const_cast<char*>(v.data()),
02150 static_cast<int>( v.length() ) ), true )
02151 {
02152 validate();
02153 }
02154
02155 String( const char *s, const char *encoding, const char *error="strict" )
02156 : SeqBase<Char>( PyUnicode_Decode( s, strlen( s ), encoding, error ), true )
02157 {
02158 validate();
02159 }
02160
02161 String( const char *s, int len, const char *encoding, const char *error="strict" )
02162 : SeqBase<Char>( PyUnicode_Decode( s, len, encoding, error ), true )
02163 {
02164 validate();
02165 }
02166
02167 String( const std::string &s, const char *encoding, const char *error="strict" )
02168 : SeqBase<Char>( PyUnicode_Decode( s.c_str(), s.length(), encoding, error ), true )
02169 {
02170 validate();
02171 }
02172
02173 String( const std::string& v, std::string::size_type vsize )
02174 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v.data()),
02175 static_cast<int>( vsize ) ), true)
02176 {
02177 validate();
02178 }
02179
02180 String( const char *v, int vsize )
02181 : SeqBase<Char>(PyString_FromStringAndSize( const_cast<char*>(v), vsize ), true )
02182 {
02183 validate();
02184 }
02185
02186 String( const char* v )
02187 : SeqBase<Char>( PyString_FromString( v ), true )
02188 {
02189 validate();
02190 }
02191
02192
02193 String& operator= ( const Object& rhs )
02194 {
02195 return *this = *rhs;
02196 }
02197
02198 String& operator= (PyObject* rhsp)
02199 {
02200 if( ptr() == rhsp )
02201 return *this;
02202 set (rhsp);
02203 return *this;
02204 }
02205
02206 virtual bool accepts (PyObject *pyob) const
02207 {
02208 return pyob && (Py::_String_Check(pyob) || Py::_Unicode_Check(pyob));
02209 }
02210
02211
02212 String& operator= (const std::string& v)
02213 {
02214 set( PyString_FromStringAndSize( const_cast<char*>( v.data() ),
02215 static_cast<int>( v.length() ) ), true );
02216 return *this;
02217 }
02218 String& operator= (const unicodestring& v)
02219 {
02220 set( PyUnicode_FromUnicode( const_cast<Py_UNICODE*>( v.data() ),
02221 static_cast<int>( v.length() ) ), true );
02222 return *this;
02223 }
02224
02225
02226
02227 String encode( const char *encoding, const char *error="strict" ) const
02228 {
02229 if( isUnicode() )
02230 {
02231 return String( PyUnicode_AsEncodedString( ptr(), encoding, error ) );
02232 }
02233 else
02234 {
02235 return String( PyString_AsEncodedObject( ptr(), encoding, error ) );
02236 }
02237 }
02238
02239 String decode( const char *encoding, const char *error="strict" )
02240 {
02241 return Object( PyString_AsDecodedObject( ptr(), encoding, error ) );
02242 }
02243
02244
02245 virtual size_type size () const
02246 {
02247 if( isUnicode() )
02248 {
02249 return static_cast<size_type>( PyUnicode_GET_SIZE (ptr()) );
02250 }
02251 else
02252 {
02253 return static_cast<size_type>( PyString_Size (ptr()) );
02254 }
02255 }
02256
02257 operator std::string () const
02258 {
02259 return as_std_string();
02260 }
02261
02262 std::string as_std_string() const
02263 {
02264 if( isUnicode() )
02265 {
02266 throw TypeError("cannot return std::string from Unicode object");
02267 }
02268 else
02269 {
02270 return std::string( PyString_AsString( ptr() ), static_cast<size_type>( PyString_Size( ptr() ) ) );
02271 }
02272 }
02273
02274 std::string as_std_string( const char *encoding, const char *error="strict" ) const;
02275
02276 unicodestring as_unicodestring() const
02277 {
02278 if( isUnicode() )
02279 {
02280 return unicodestring( PyUnicode_AS_UNICODE( ptr() ),
02281 static_cast<size_type>( PyUnicode_GET_SIZE( ptr() ) ) );
02282 }
02283 else
02284 {
02285 throw TypeError("can only return unicodestring from Unicode object");
02286 }
02287 }
02288 };
02289 #endif
02290
02291
02292
02293 class PYCXX_EXPORT Tuple: public Sequence
02294 {
02295 public:
02296 virtual void setItem (sequence_index_type offset, const Object&ob)
02297 {
02298
02299 if(PyTuple_SetItem (ptr(), offset, new_reference_to(ob)) == -1)
02300 {
02301 throw Exception();
02302 }
02303 }
02304
02305
02306 explicit Tuple (PyObject *pyob, bool owned = false): Sequence (pyob, owned)
02307 {
02308 validate();
02309 }
02310
02311 Tuple (const Object& ob): Sequence(ob)
02312 {
02313 validate();
02314 }
02315
02316
02317 explicit Tuple (int size = 0)
02318 {
02319 set(PyTuple_New (size), true);
02320 validate ();
02321 for (sequence_index_type i=0; i < size; i++)
02322 {
02323 if(PyTuple_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
02324 {
02325 throw Exception();
02326 }
02327 }
02328 }
02329
02330 explicit Tuple (const Sequence& s)
02331 {
02332 sequence_index_type limit( sequence_index_type( s.length() ) );
02333
02334 set(PyTuple_New (limit), true);
02335 validate();
02336
02337 for(sequence_index_type i=0; i < limit; i++)
02338 {
02339 if(PyTuple_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
02340 {
02341 throw Exception();
02342 }
02343 }
02344 }
02345
02346
02347 Tuple& operator= (const Object& rhs)
02348 {
02349 return (*this = *rhs);
02350 }
02351
02352 Tuple& operator= (PyObject* rhsp)
02353 {
02354 if(ptr() == rhsp) return *this;
02355 set (rhsp);
02356 return *this;
02357 }
02358
02359 virtual bool accepts (PyObject *pyob) const
02360 {
02361 return pyob && Py::_Tuple_Check (pyob);
02362 }
02363
02364 Tuple getSlice (int i, int j) const
02365 {
02366 return Tuple (PySequence_GetSlice (ptr(), i, j), true);
02367 }
02368
02369 };
02370
02371 class PYCXX_EXPORT TupleN: public Tuple
02372 {
02373 public:
02374 TupleN()
02375 : Tuple( 0 )
02376 {
02377 }
02378
02379 TupleN( const Object &obj1 )
02380 : Tuple( 1 )
02381 {
02382 setItem( 0, obj1 );
02383 }
02384
02385 TupleN( const Object &obj1, const Object &obj2 )
02386 : Tuple( 2 )
02387 {
02388 setItem( 0, obj1 );
02389 setItem( 1, obj2 );
02390 }
02391
02392 TupleN( const Object &obj1, const Object &obj2, const Object &obj3 )
02393 : Tuple( 3 )
02394 {
02395 setItem( 0, obj1 );
02396 setItem( 1, obj2 );
02397 setItem( 2, obj3 );
02398 }
02399
02400 TupleN( const Object &obj1, const Object &obj2, const Object &obj3,
02401 const Object &obj4 )
02402 : Tuple( 4 )
02403 {
02404 setItem( 0, obj1 );
02405 setItem( 1, obj2 );
02406 setItem( 2, obj3 );
02407 setItem( 3, obj4 );
02408 }
02409
02410 TupleN( const Object &obj1, const Object &obj2, const Object &obj3,
02411 const Object &obj4, const Object &obj5 )
02412 : Tuple( 5 )
02413 {
02414 setItem( 0, obj1 );
02415 setItem( 1, obj2 );
02416 setItem( 2, obj3 );
02417 setItem( 3, obj4 );
02418 setItem( 4, obj5 );
02419 }
02420
02421 TupleN( const Object &obj1, const Object &obj2, const Object &obj3,
02422 const Object &obj4, const Object &obj5, const Object &obj6 )
02423 : Tuple( 6 )
02424 {
02425 setItem( 0, obj1 );
02426 setItem( 1, obj2 );
02427 setItem( 2, obj3 );
02428 setItem( 3, obj4 );
02429 setItem( 4, obj5 );
02430 setItem( 5, obj6 );
02431 }
02432
02433 TupleN( const Object &obj1, const Object &obj2, const Object &obj3,
02434 const Object &obj4, const Object &obj5, const Object &obj6,
02435 const Object &obj7 )
02436 : Tuple( 7 )
02437 {
02438 setItem( 0, obj1 );
02439 setItem( 1, obj2 );
02440 setItem( 2, obj3 );
02441 setItem( 3, obj4 );
02442 setItem( 4, obj5 );
02443 setItem( 5, obj6 );
02444 setItem( 6, obj7 );
02445 }
02446
02447 TupleN( const Object &obj1, const Object &obj2, const Object &obj3,
02448 const Object &obj4, const Object &obj5, const Object &obj6,
02449 const Object &obj7, const Object &obj8 )
02450 : Tuple( 8 )
02451 {
02452 setItem( 0, obj1 );
02453 setItem( 1, obj2 );
02454 setItem( 2, obj3 );
02455 setItem( 3, obj4 );
02456 setItem( 4, obj5 );
02457 setItem( 5, obj6 );
02458 setItem( 6, obj7 );
02459 setItem( 7, obj8 );
02460 }
02461
02462 TupleN( const Object &obj1, const Object &obj2, const Object &obj3,
02463 const Object &obj4, const Object &obj5, const Object &obj6,
02464 const Object &obj7, const Object &obj8, const Object &obj9 )
02465 : Tuple( 9 )
02466 {
02467 setItem( 0, obj1 );
02468 setItem( 1, obj2 );
02469 setItem( 2, obj3 );
02470 setItem( 3, obj4 );
02471 setItem( 4, obj5 );
02472 setItem( 5, obj6 );
02473 setItem( 6, obj7 );
02474 setItem( 7, obj8 );
02475 setItem( 8, obj9 );
02476 }
02477
02478 virtual ~TupleN()
02479 { }
02480 };
02481
02482
02483
02484
02485
02486 class PYCXX_EXPORT List: public Sequence
02487 {
02488 public:
02489
02490 explicit List (PyObject *pyob, bool owned = false): Sequence(pyob, owned)
02491 {
02492 validate();
02493 }
02494 List (const Object& ob): Sequence(ob)
02495 {
02496 validate();
02497 }
02498
02499 List (int size = 0)
02500 {
02501 set(PyList_New (size), true);
02502 validate();
02503 for (sequence_index_type i=0; i < size; i++)
02504 {
02505 if(PyList_SetItem (ptr(), i, new_reference_to(Py::_None())) == -1)
02506 {
02507 throw Exception();
02508 }
02509 }
02510 }
02511
02512
02513 List (const Sequence& s): Sequence()
02514 {
02515 int n = (int)s.length();
02516 set(PyList_New (n), true);
02517 validate();
02518 for (sequence_index_type i=0; i < n; i++)
02519 {
02520 if(PyList_SetItem (ptr(), i, new_reference_to(s[i])) == -1)
02521 {
02522 throw Exception();
02523 }
02524 }
02525 }
02526
02527 virtual size_type capacity() const
02528 {
02529 return max_size();
02530 }
02531
02532
02533 List& operator= (const Object& rhs)
02534 {
02535 return (*this = *rhs);
02536 }
02537
02538 List& operator= (PyObject* rhsp)
02539 {
02540 if(ptr() == rhsp) return *this;
02541 set (rhsp);
02542 return *this;
02543 }
02544
02545 virtual bool accepts (PyObject *pyob) const
02546 {
02547 return pyob && Py::_List_Check (pyob);
02548 }
02549
02550 List getSlice (int i, int j) const
02551 {
02552 return List (PyList_GetSlice (ptr(), i, j), true);
02553 }
02554
02555 void setSlice (int i, int j, const Object& v)
02556 {
02557 if(PyList_SetSlice (ptr(), i, j, *v) == -1)
02558 {
02559 throw Exception();
02560 }
02561 }
02562
02563 void append (const Object& ob)
02564 {
02565 if(PyList_Append (ptr(), *ob) == -1)
02566 {
02567 throw Exception();
02568 }
02569 }
02570
02571 void insert (int i, const Object& ob)
02572 {
02573 if(PyList_Insert (ptr(), i, *ob) == -1)
02574 {
02575 throw Exception();
02576 }
02577 }
02578
02579 void sort ()
02580 {
02581 if(PyList_Sort(ptr()) == -1)
02582 {
02583 throw Exception();
02584 }
02585 }
02586
02587 void reverse ()
02588 {
02589 if(PyList_Reverse(ptr()) == -1)
02590 {
02591 throw Exception();
02592 }
02593 }
02594 };
02595
02596
02597
02598
02599 template<TEMPLATE_TYPENAME T>
02600 class mapref
02601 {
02602 protected:
02603 MapBase<T>& s;
02604 Object key;
02605 T the_item;
02606
02607 public:
02608 mapref<T> (MapBase<T>& map, const std::string& k)
02609 : s(map), the_item()
02610 {
02611 key = String(k);
02612 if(map.hasKey(key)) the_item = map.getItem(key);
02613 }
02614
02615 mapref<T> (MapBase<T>& map, const Object& k)
02616 : s(map), key(k), the_item()
02617 {
02618 if(map.hasKey(key)) the_item = map.getItem(key);
02619 }
02620
02621 virtual ~mapref<T>()
02622 {}
02623
02624
02625
02626 mapref<T>& operator=(const mapref<T>& other)
02627 {
02628 if(this == &other) return *this;
02629 the_item = other.the_item;
02630 s.setItem(key, other.the_item);
02631 return *this;
02632 }
02633
02634 mapref<T>& operator= (const T& ob)
02635 {
02636 the_item = ob;
02637 s.setItem (key, ob);
02638 return *this;
02639 }
02640
02641
02642 operator T() const
02643 {
02644 return the_item;
02645 }
02646
02647
02648 PyObject* ptr () const
02649 {
02650 return the_item.ptr();
02651 }
02652
02653 int reference_count () const
02654 {
02655 return the_item.reference_count();
02656 }
02657
02658 Type type () const
02659 {
02660 return the_item.type();
02661 }
02662
02663 String str () const
02664 {
02665 return the_item.str();
02666 }
02667
02668 String repr () const
02669 {
02670 return the_item.repr();
02671 }
02672
02673 bool hasAttr (const std::string& attr_name) const
02674 {
02675 return the_item.hasAttr(attr_name);
02676 }
02677
02678 Object getAttr (const std::string& attr_name) const
02679 {
02680 return the_item.getAttr(attr_name);
02681 }
02682
02683 Object getItem (const Object& k) const
02684 {
02685 return the_item.getItem(k);
02686 }
02687
02688 long hashValue () const
02689 {
02690 return the_item.hashValue();
02691 }
02692
02693 bool isCallable () const
02694 {
02695 return the_item.isCallable();
02696 }
02697
02698 bool isInstance () const
02699 {
02700 return the_item.isInstance();
02701 }
02702
02703 bool isList () const
02704 {
02705 return the_item.isList();
02706 }
02707
02708 bool isMapping () const
02709 {
02710 return the_item.isMapping();
02711 }
02712
02713 bool isNumeric () const
02714 {
02715 return the_item.isNumeric();
02716 }
02717
02718 bool isSequence () const
02719 {
02720 return the_item.isSequence();
02721 }
02722
02723 bool isTrue () const
02724 {
02725 return the_item.isTrue();
02726 }
02727
02728 bool isType (const Type& t) const
02729 {
02730 return the_item.isType (t);
02731 }
02732
02733 bool isTuple() const
02734 {
02735 return the_item.isTuple();
02736 }
02737
02738 bool isString() const
02739 {
02740 return the_item.isString();
02741 }
02742
02743
02744 void setAttr (const std::string& attr_name, const Object& value)
02745 {
02746 the_item.setAttr(attr_name, value);
02747 }
02748
02749 void delAttr (const std::string& attr_name)
02750 {
02751 the_item.delAttr(attr_name);
02752 }
02753
02754 void delItem (const Object& k)
02755 {
02756 the_item.delItem(k);
02757 }
02758 };
02759
02760
02761 template< class T >
02762 bool operator==(const mapref<T>& left, const mapref<T>& right)
02763 {
02764 return true;
02765 }
02766
02767 template< class T >
02768 bool operator!=(const mapref<T>& left, const mapref<T>& right)
02769 {
02770 return true;
02771 }
02772
02773 template<TEMPLATE_TYPENAME T>
02774 class MapBase: public Object
02775 {
02776 protected:
02777 explicit MapBase<T>()
02778 {}
02779 public:
02780
02781
02782
02783
02784
02785 typedef size_t size_type;
02786 typedef Object key_type;
02787 typedef mapref<T> data_type;
02788 typedef std::pair< const T, T > value_type;
02789 typedef std::pair< const T, mapref<T> > reference;
02790 typedef const std::pair< const T, const T > const_reference;
02791 typedef std::pair< const T, mapref<T> > pointer;
02792
02793
02794 explicit MapBase<T> (PyObject *pyob, bool owned = false): Object(pyob, owned)
02795 {
02796 validate();
02797 }
02798
02799
02800 MapBase<T> (const Object& ob): Object(ob)
02801 {
02802 validate();
02803 }
02804
02805
02806 MapBase<T>& operator= (const Object& rhs)
02807 {
02808 return (*this = *rhs);
02809 }
02810
02811 MapBase<T>& operator= (PyObject* rhsp)
02812 {
02813 if(ptr() == rhsp) return *this;
02814 set (rhsp);
02815 return *this;
02816 }
02817
02818 virtual bool accepts (PyObject *pyob) const
02819 {
02820 return pyob && PyMapping_Check(pyob);
02821 }
02822
02823
02824
02825
02826 void clear ()
02827 {
02828 List k = keys();
02829 for(List::iterator i = k.begin(); i != k.end(); i++)
02830 {
02831 delItem(*i);
02832 }
02833 }
02834
02835 virtual size_type size() const
02836 {
02837 return PyMapping_Length (ptr());
02838 }
02839
02840
02841 T operator[](const std::string& key) const
02842 {
02843 return getItem(key);
02844 }
02845
02846 T operator[](const Object& key) const
02847 {
02848 return getItem(key);
02849 }
02850
02851 mapref<T> operator[](const std::string& key)
02852 {
02853 return mapref<T>(*this, key);
02854 }
02855
02856 mapref<T> operator[](const Object& key)
02857 {
02858 return mapref<T>(*this, key);
02859 }
02860
02861 int length () const
02862 {
02863 return PyMapping_Length (ptr());
02864 }
02865
02866 bool hasKey (const std::string& s) const
02867 {
02868 return PyMapping_HasKeyString (ptr(),const_cast<char*>(s.c_str())) != 0;
02869 }
02870
02871 bool hasKey (const Object& s) const
02872 {
02873 return PyMapping_HasKey (ptr(), s.ptr()) != 0;
02874 }
02875
02876 T getItem (const std::string& s) const
02877 {
02878 return T(
02879 asObject(PyMapping_GetItemString (ptr(),const_cast<char*>(s.c_str())))
02880 );
02881 }
02882
02883 T getItem (const Object& s) const
02884 {
02885 return T(
02886 asObject(PyObject_GetItem (ptr(), s.ptr()))
02887 );
02888 }
02889
02890 virtual void setItem (const char *s, const Object& ob)
02891 {
02892 if (PyMapping_SetItemString (ptr(), const_cast<char*>(s), *ob) == -1)
02893 {
02894 throw Exception();
02895 }
02896 }
02897
02898 virtual void setItem (const std::string& s, const Object& ob)
02899 {
02900 if (PyMapping_SetItemString (ptr(), const_cast<char*>(s.c_str()), *ob) == -1)
02901 {
02902 throw Exception();
02903 }
02904 }
02905
02906 virtual void setItem (const Object& s, const Object& ob)
02907 {
02908 if (PyObject_SetItem (ptr(), s.ptr(), ob.ptr()) == -1)
02909 {
02910 throw Exception();
02911 }
02912 }
02913
02914 void delItem (const std::string& s)
02915 {
02916 if (PyMapping_DelItemString (ptr(), const_cast<char*>(s.c_str())) == -1)
02917 {
02918 throw Exception();
02919 }
02920 }
02921
02922 void delItem (const Object& s)
02923 {
02924 if (PyMapping_DelItem (ptr(), *s) == -1)
02925 {
02926 throw Exception();
02927 }
02928 }
02929
02930 List keys () const
02931 {
02932 return List(PyMapping_Keys(ptr()), true);
02933 }
02934
02935 List values () const
02936 {
02937 return List(PyMapping_Values(ptr()), true);
02938 }
02939
02940 List items () const
02941 {
02942 return List(PyMapping_Items(ptr()), true);
02943 }
02944
02945 class iterator
02946 {
02947
02948 protected:
02949 typedef std::forward_iterator_tag iterator_category;
02950 typedef std::pair< const T, T > value_type;
02951 typedef int difference_type;
02952 typedef std::pair< const T, mapref<T> > pointer;
02953 typedef std::pair< const T, mapref<T> > reference;
02954
02955 friend class MapBase<T>;
02956
02957 MapBase<T>* map;
02958 List keys;
02959 int pos;
02960
02961 private:
02962 iterator( MapBase<T>* m, List k, int p )
02963 : map( m )
02964 , keys( k )
02965 , pos( p )
02966 {}
02967
02968 public:
02969 ~iterator ()
02970 {}
02971
02972 iterator ()
02973 : map( 0 )
02974 , keys()
02975 , pos()
02976 {}
02977
02978 iterator (MapBase<T>* m, bool end = false )
02979 : map( m )
02980 , keys( m->keys() )
02981 , pos( end ? keys.length() : 0 )
02982 {}
02983
02984 iterator (const iterator& other)
02985 : map( other.map )
02986 , keys( other.keys )
02987 , pos( other.pos )
02988 {}
02989
02990 reference operator*()
02991 {
02992 Object key = keys[ pos ];
02993 return std::make_pair(key, mapref<T>(*map,key));
02994 }
02995
02996 iterator& operator=(const iterator& other)
02997 {
02998 if (this == &other)
02999 return *this;
03000 map = other.map;
03001 keys = other.keys;
03002 pos = other.pos;
03003 return *this;
03004 }
03005
03006 bool eql(const iterator& right) const
03007 {
03008 return map->ptr() == right.map->ptr() && pos == right.pos;
03009 }
03010 bool neq( const iterator& right ) const
03011 {
03012 return map->ptr() != right.map->ptr() || pos != right.pos;
03013 }
03014
03015
03016
03017
03018
03019
03020 iterator& operator++ ()
03021 { pos++; return *this;}
03022
03023 iterator operator++ (int)
03024 { return iterator(map, keys, pos++);}
03025
03026 iterator& operator-- ()
03027 { pos--; return *this;}
03028
03029 iterator operator-- (int)
03030 { return iterator(map, keys, pos--);}
03031
03032 std::string diagnose() const
03033 {
03034 std::OSTRSTREAM oss;
03035 oss << "iterator diagnosis " << map << ", " << pos << std::ends;
03036 return std::string(oss.str());
03037 }
03038 };
03039
03040 iterator begin ()
03041 {
03042 return iterator(this);
03043 }
03044
03045 iterator end ()
03046 {
03047 return iterator(this, true);
03048 }
03049
03050 class const_iterator
03051 {
03052 protected:
03053 typedef std::forward_iterator_tag iterator_category;
03054 typedef const std::pair< const T, T > value_type;
03055 typedef int difference_type;
03056 typedef const std::pair< const T, T > pointer;
03057 typedef const std::pair< const T, T > reference;
03058
03059 friend class MapBase<T>;
03060 const MapBase<T>* map;
03061 List keys;
03062 int pos;
03063
03064 private:
03065 const_iterator( const MapBase<T>* m, List k, int p )
03066 : map( m )
03067 , keys( k )
03068 , pos( p )
03069 {}
03070
03071 public:
03072 ~const_iterator ()
03073 {}
03074
03075 const_iterator ()
03076 : map( 0 )
03077 , keys()
03078 , pos()
03079 {}
03080
03081 const_iterator (const MapBase<T>* m, bool end = false )
03082 : map( m )
03083 , keys( m->keys() )
03084 , pos( end ? keys.length() : 0 )
03085 {}
03086
03087 const_iterator(const const_iterator& other)
03088 : map( other.map )
03089 , keys( other.keys )
03090 , pos( other.pos )
03091 {}
03092
03093 bool eql(const const_iterator& right) const
03094 {
03095 return map->ptr() == right.map->ptr() && pos == right.pos;
03096 }
03097
03098 bool neq( const const_iterator& right ) const
03099 {
03100 return map->ptr() != right.map->ptr() || pos != right.pos;
03101 }
03102
03103 const_reference operator*()
03104 {
03105 Object key = keys[ pos ];
03106 return std::make_pair( key, mapref<T>( *map, key ) );
03107 }
03108
03109 const_iterator& operator=(const const_iterator& other)
03110 {
03111 if (this == &other) return *this;
03112 map = other.map;
03113 keys = other.keys;
03114 pos = other.pos;
03115 return *this;
03116 }
03117
03118
03119 const_iterator& operator++ ()
03120 { pos++; return *this;}
03121
03122 const_iterator operator++ (int)
03123 { return const_iterator(map, keys, pos++);}
03124
03125 const_iterator& operator-- ()
03126 { pos--; return *this;}
03127
03128 const_iterator operator-- (int)
03129 { return const_iterator(map, keys, pos--);}
03130 };
03131
03132 const_iterator begin () const
03133 {
03134 return const_iterator(this);
03135 }
03136
03137 const_iterator end () const
03138 {
03139 return const_iterator(this, true);
03140 }
03141
03142 };
03143
03144 typedef MapBase<Object> Mapping;
03145
03146 template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME MapBase<T>::iterator& left, const EXPLICIT_TYPENAME MapBase<T>::iterator& right);
03147 template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME MapBase<T>::iterator& left, const EXPLICIT_TYPENAME MapBase<T>::iterator& right);
03148 template <TEMPLATE_TYPENAME T> bool operator==(const EXPLICIT_TYPENAME MapBase<T>::const_iterator& left, const EXPLICIT_TYPENAME MapBase<T>::const_iterator& right);
03149 template <TEMPLATE_TYPENAME T> bool operator!=(const EXPLICIT_TYPENAME MapBase<T>::const_iterator& left, const EXPLICIT_TYPENAME MapBase<T>::const_iterator& right);
03150
03151 PYCXX_EXPORT extern bool operator==(const Mapping::iterator& left, const Mapping::iterator& right);
03152 PYCXX_EXPORT extern bool operator!=(const Mapping::iterator& left, const Mapping::iterator& right);
03153 PYCXX_EXPORT extern bool operator==(const Mapping::const_iterator& left, const Mapping::const_iterator& right);
03154 PYCXX_EXPORT extern bool operator!=(const Mapping::const_iterator& left, const Mapping::const_iterator& right);
03155
03156
03157
03158
03159 class PYCXX_EXPORT Dict: public Mapping
03160 {
03161 public:
03162
03163 explicit Dict (PyObject *pyob, bool owned=false): Mapping (pyob, owned)
03164 {
03165 validate();
03166 }
03167 Dict (const Object& ob): Mapping(ob)
03168 {
03169 validate();
03170 }
03171
03172 Dict ()
03173 {
03174 set(PyDict_New (), true);
03175 validate();
03176 }
03177
03178
03179 Dict& operator= (const Object& rhs)
03180 {
03181 return (*this = *rhs);
03182 }
03183
03184 Dict& operator= (PyObject* rhsp)
03185 {
03186 if(ptr() == rhsp) return *this;
03187 set(rhsp);
03188 return *this;
03189 }
03190
03191 virtual bool accepts (PyObject *pyob) const
03192 {
03193 return pyob && Py::_Dict_Check (pyob);
03194 }
03195 };
03196
03197 class PYCXX_EXPORT Callable: public Object
03198 {
03199 public:
03200
03201 explicit Callable (): Object() {}
03202 explicit Callable (PyObject *pyob, bool owned = false): Object (pyob, owned)
03203 {
03204 validate();
03205 }
03206
03207 Callable (const Object& ob): Object(ob)
03208 {
03209 validate();
03210 }
03211
03212
03213
03214 Callable& operator= (const Object& rhs)
03215 {
03216 return (*this = *rhs);
03217 }
03218
03219 Callable& operator= (PyObject* rhsp)
03220 {
03221 if(ptr() == rhsp) return *this;
03222 set (rhsp);
03223 return *this;
03224 }
03225
03226
03227 virtual bool accepts (PyObject *pyob) const
03228 {
03229 return pyob && PyCallable_Check (pyob);
03230 }
03231
03232
03233 Object apply(const Tuple& args) const
03234 {
03235 return asObject(PyObject_CallObject(ptr(), args.ptr()));
03236 }
03237
03238
03239 Object apply(const Tuple& args, const Dict& kw) const
03240 {
03241 return asObject( PyEval_CallObjectWithKeywords( ptr(), args.ptr(), kw.ptr() ) );
03242 }
03243
03244 Object apply(PyObject* pargs = 0) const
03245 {
03246 return apply (Tuple(pargs));
03247 }
03248 };
03249
03250 class PYCXX_EXPORT Module: public Object
03251 {
03252 public:
03253 explicit Module (PyObject* pyob, bool owned = false): Object (pyob, owned)
03254 {
03255 validate();
03256 }
03257
03258
03259 explicit Module (const std::string&s): Object()
03260 {
03261 PyObject *m = PyImport_AddModule( const_cast<char *>(s.c_str()) );
03262 set( m, false );
03263 validate ();
03264 }
03265
03266
03267 Module (const Module& ob): Object(*ob)
03268 {
03269 validate();
03270 }
03271
03272 Module& operator= (const Object& rhs)
03273 {
03274 return (*this = *rhs);
03275 }
03276
03277 Module& operator= (PyObject* rhsp)
03278 {
03279 if(ptr() == rhsp) return *this;
03280 set(rhsp);
03281 return *this;
03282 }
03283
03284 Dict getDict()
03285 {
03286 return Dict(PyModule_GetDict(ptr()));
03287
03288 }
03289 };
03290
03291
03292 inline Object Object::callMemberFunction( const std::string &function_name ) const
03293 {
03294 Callable target( getAttr( function_name ) );
03295 Tuple args( 0 );
03296 return target.apply( args );
03297 }
03298
03299 inline Object Object::callMemberFunction( const std::string &function_name, const Tuple &args ) const
03300 {
03301 Callable target( getAttr( function_name ) );
03302 return target.apply( args );
03303 }
03304
03305 inline Object Object::callMemberFunction( const std::string &function_name, const Tuple &args, const Dict &kw ) const
03306 {
03307 Callable target( getAttr( function_name ) );
03308 return target.apply( args, kw );
03309 }
03310
03311
03312 inline Object operator+ (const Object& a)
03313 {
03314 return asObject(PyNumber_Positive(*a));
03315 }
03316 inline Object operator- (const Object& a)
03317 {
03318 return asObject(PyNumber_Negative(*a));
03319 }
03320
03321 inline Object abs(const Object& a)
03322 {
03323 return asObject(PyNumber_Absolute(*a));
03324 }
03325
03326 inline std::pair<Object,Object> coerce(const Object& a, const Object& b)
03327 {
03328 PyObject *p1, *p2;
03329 p1 = *a;
03330 p2 = *b;
03331 if(PyNumber_Coerce(&p1,&p2) == -1)
03332 {
03333 throw Exception();
03334 }
03335 return std::pair<Object,Object>(asObject(p1), asObject(p2));
03336 }
03337
03338 inline Object operator+ (const Object& a, const Object& b)
03339 {
03340 return asObject(PyNumber_Add(*a, *b));
03341 }
03342 inline Object operator+ (const Object& a, int j)
03343 {
03344 return asObject(PyNumber_Add(*a, *Int(j)));
03345 }
03346 inline Object operator+ (const Object& a, double v)
03347 {
03348 return asObject(PyNumber_Add(*a, *Float(v)));
03349 }
03350 inline Object operator+ (int j, const Object& b)
03351 {
03352 return asObject(PyNumber_Add(*Int(j), *b));
03353 }
03354 inline Object operator+ (double v, const Object& b)
03355 {
03356 return asObject(PyNumber_Add(*Float(v), *b));
03357 }
03358
03359 inline Object operator- (const Object& a, const Object& b)
03360 {
03361 return asObject(PyNumber_Subtract(*a, *b));
03362 }
03363 inline Object operator- (const Object& a, int j)
03364 {
03365 return asObject(PyNumber_Subtract(*a, *Int(j)));
03366 }
03367 inline Object operator- (const Object& a, double v)
03368 {
03369 return asObject(PyNumber_Subtract(*a, *Float(v)));
03370 }
03371 inline Object operator- (int j, const Object& b)
03372 {
03373 return asObject(PyNumber_Subtract(*Int(j), *b));
03374 }
03375 inline Object operator- (double v, const Object& b)
03376 {
03377 return asObject(PyNumber_Subtract(*Float(v), *b));
03378 }
03379
03380 inline Object operator* (const Object& a, const Object& b)
03381 {
03382 return asObject(PyNumber_Multiply(*a, *b));
03383 }
03384 inline Object operator* (const Object& a, int j)
03385 {
03386 return asObject(PyNumber_Multiply(*a, *Int(j)));
03387 }
03388 inline Object operator* (const Object& a, double v)
03389 {
03390 return asObject(PyNumber_Multiply(*a, *Float(v)));
03391 }
03392 inline Object operator* (int j, const Object& b)
03393 {
03394 return asObject(PyNumber_Multiply(*Int(j), *b));
03395 }
03396 inline Object operator* (double v, const Object& b)
03397 {
03398 return asObject(PyNumber_Multiply(*Float(v), *b));
03399 }
03400
03401 inline Object operator/ (const Object& a, const Object& b)
03402 {
03403 return asObject(PyNumber_Divide(*a, *b));
03404 }
03405 inline Object operator/ (const Object& a, int j)
03406 {
03407 return asObject(PyNumber_Divide(*a, *Int(j)));
03408 }
03409 inline Object operator/ (const Object& a, double v)
03410 {
03411 return asObject(PyNumber_Divide(*a, *Float(v)));
03412 }
03413 inline Object operator/ (int j, const Object& b)
03414 {
03415 return asObject(PyNumber_Divide(*Int(j), *b));
03416 }
03417 inline Object operator/ (double v, const Object& b)
03418 {
03419 return asObject(PyNumber_Divide(*Float(v), *b));
03420 }
03421
03422 inline Object operator% (const Object& a, const Object& b)
03423 {
03424 return asObject(PyNumber_Remainder(*a, *b));
03425 }
03426 inline Object operator% (const Object& a, int j)
03427 {
03428 return asObject(PyNumber_Remainder(*a, *Int(j)));
03429 }
03430 inline Object operator% (const Object& a, double v)
03431 {
03432 return asObject(PyNumber_Remainder(*a, *Float(v)));
03433 }
03434 inline Object operator% (int j, const Object& b)
03435 {
03436 return asObject(PyNumber_Remainder(*Int(j), *b));
03437 }
03438 inline Object operator% (double v, const Object& b)
03439 {
03440 return asObject(PyNumber_Remainder(*Float(v), *b));
03441 }
03442
03443 inline Object type(const Exception&)
03444 {
03445 PyObject *ptype, *pvalue, *ptrace;
03446 PyErr_Fetch(&ptype, &pvalue, &ptrace);
03447 Object result;
03448 if(ptype) result = ptype;
03449 PyErr_Restore(ptype, pvalue, ptrace);
03450 return result;
03451 }
03452
03453 inline Object value(const Exception&)
03454 {
03455 PyObject *ptype, *pvalue, *ptrace;
03456 PyErr_Fetch(&ptype, &pvalue, &ptrace);
03457 Object result;
03458 if(pvalue) result = pvalue;
03459 PyErr_Restore(ptype, pvalue, ptrace);
03460 return result;
03461 }
03462
03463 inline Object trace(const Exception&)
03464 {
03465 PyObject *ptype, *pvalue, *ptrace;
03466 PyErr_Fetch(&ptype, &pvalue, &ptrace);
03467 Object result;
03468 if(ptrace) result = ptrace;
03469 PyErr_Restore(ptype, pvalue, ptrace);
03470 return result;
03471 }
03472
03473 template<TEMPLATE_TYPENAME T>
03474 String seqref<T>::str () const
03475 {
03476 return the_item.str();
03477 }
03478
03479 template<TEMPLATE_TYPENAME T>
03480 String seqref<T>::repr () const
03481 {
03482 return the_item.repr();
03483 }
03484
03485 }
03486 #endif // __CXX_Objects__h