forked from matplotlib/matplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjects.hxx
3040 lines (2613 loc) · 80.5 KB
/
Objects.hxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------------
//
// Copyright (c) 1998 - 2007, The Regents of the University of California
// Produced at the Lawrence Livermore National Laboratory
// All rights reserved.
//
// This file is part of PyCXX. For details,see http://cxx.sourceforge.net/. The
// full copyright notice is contained in the file COPYRIGHT located at the root
// of the PyCXX distribution.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the disclaimer below.
// - Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the disclaimer (as noted below) in the
// documentation and/or materials provided with the distribution.
// - Neither the name of the UC/LLNL nor the names of its contributors may be
// used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OF THE UNIVERSITY OF
// CALIFORNIA, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
//-----------------------------------------------------------------------------
#ifndef __CXX_Objects__h
#define __CXX_Objects__h
#include "CXX/WrapPython.h"
#include "CXX/Version.hxx"
#include "CXX/Config.hxx"
#include "CXX/Exception.hxx"
#include <iostream>
#include STR_STREAM
#include <string>
#include <iterator>
#include <utility>
#include <typeinfo>
namespace Py
{
typedef int sequence_index_type; // type of an index into a sequence
// Forward declarations
class Object;
class Type;
template<TEMPLATE_TYPENAME T> class SeqBase;
class String;
class List;
template<TEMPLATE_TYPENAME T> class MapBase;
// new_reference_to also overloaded below on Object
inline PyObject* new_reference_to(PyObject* p)
{
Py::_XINCREF(p);
return p;
}
// returning Null() from an extension method triggers a
// Python exception
inline PyObject* Null()
{
return (static_cast<PyObject*>(0));
}
//===========================================================================//
// class Object
// The purpose of this class is to serve as the most general kind of
// Python object, for the purpose of writing C++ extensions in Python
// Objects hold a PyObject* which they own. This pointer is always a
// valid pointer to a Python object. In children we must maintain this behavior.
//
// Instructions on how to make your own class MyType descended from Object:
// (0) Pick a base class, either Object or perhaps SeqBase<T> or MapBase<T>.
// This example assumes Object.
// (1) Write a routine int MyType_Check (PyObject *) modeled after PyInt_Check,
// PyFloat_Check, etc.
// (2) Add method accepts:
// virtual bool accepts (PyObject *pyob) const {
// return pyob && MyType_Check (pyob);
// }
// (3) Include the following constructor and copy constructor
//
/*
explicit MyType (PyObject *pyob): Object(pyob) {
validate();
}
MyType(const Object& other): Object(other.ptr()) {
validate();
}
*/
// Alernate version for the constructor to allow for construction from owned pointers:
/*
explicit MyType (PyObject *pyob): Object(pyob) {
validate();
}
*/
// You may wish to add other constructors; see the classes below for examples.
// Each constructor must use "set" to set the pointer
// and end by validating the pointer you have created.
// (4) Each class needs at least these two assignment operators:
/*
MyType& operator= (const Object& rhs) {
return (*this = *rhs);
}
Mytype& operator= (PyObject* rhsp) {
if(ptr() == rhsp) return *this;
set(rhsp);
return *this;
}
*/
// Note on accepts: constructors call the base class
// version of a virtual when calling the base class constructor,
// so the test has to be done explicitly in a descendent.
// If you are inheriting from PythonExtension<T> to define an object
// note that it contains PythonExtension<T>::check
// which you can use in accepts when writing a wrapper class.
// See Demo/range.h and Demo/range.cxx for an example.
class Object
{
private:
// the pointer to the Python object
// Only Object sets this directly.
// The default constructor for Object sets it to Py_None and
// child classes must use "set" to set it
//
PyObject* p;
protected:
void set (PyObject* pyob, bool owned = false)
{
release();
p = pyob;
if (!owned)
{
Py::_XINCREF (p);
}
validate();
}
void release ()
{
Py::_XDECREF (p);
p = 0;
}
void validate();
public:
// Constructor acquires new ownership of pointer unless explicitly told not to.
explicit Object (PyObject* pyob=Py::_None(), bool owned = false): p (pyob)
{
if(!owned)
{
Py::_XINCREF (p);
}
validate();
}
// Copy constructor acquires new ownership of pointer
Object (const Object& ob): p(ob.p)
{
Py::_XINCREF (p);
validate();
}
// Assignment acquires new ownership of pointer
Object& operator= (const Object& rhs)
{
set(rhs.p);
return *this;
}
Object& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (rhsp);
return *this;
}
// Destructor
virtual ~Object ()
{
release ();
}
// Loaning the pointer to others, retain ownership
PyObject* operator* () const
{
return p;
}
// Explicit reference_counting changes
void increment_reference_count()
{
Py::_XINCREF(p);
}
void decrement_reference_count()
{
// not allowed to commit suicide, however
if(reference_count() == 1)
throw RuntimeError("Object::decrement_reference_count error.");
Py::_XDECREF(p);
}
// Would like to call this pointer() but messes up STL in SeqBase<T>
PyObject* ptr () const
{
return p;
}
//
// Queries
//
// Can pyob be used in this object's constructor?
virtual bool accepts (PyObject *pyob) const
{
return (pyob != 0);
}
Py_ssize_t reference_count () const
{ // the reference count
return p ? p->ob_refcnt : 0;
}
Type type () const; // the type object associated with this one
String str () const; // the str() representation
std::string as_string() const;
String repr () const; // the repr () representation
List dir () const; // the dir() list
bool hasAttr (const std::string& s) const
{
return PyObject_HasAttrString (p, const_cast<char*>(s.c_str())) ? true: false;
}
Object getAttr (const std::string& s) const
{
return Object (PyObject_GetAttrString (p, const_cast<char*>(s.c_str())), true);
}
Object getItem (const Object& key) const
{
return Object (PyObject_GetItem(p, *key), true);
}
long hashValue () const
{
return PyObject_Hash (p);
}
//
// int print (FILE* fp, int flags=Py_Print_RAW)
//{
// return PyObject_Print (p, fp, flags);
//}
//
bool is(PyObject *pother) const
{ // identity test
return p == pother;
}
bool is(const Object& other) const
{ // identity test
return p == other.p;
}
bool isNone() const
{
return p == _None();
}
bool isCallable () const
{
return PyCallable_Check (p) != 0;
}
bool isInstance () const
{
return PyInstance_Check (p) != 0;
}
bool isDict () const
{
return Py::_Dict_Check (p);
}
bool isList () const
{
return Py::_List_Check (p);
}
bool isMapping () const
{
return PyMapping_Check (p) != 0;
}
bool isNumeric () const
{
return PyNumber_Check (p) != 0;
}
bool isSequence () const
{
return PySequence_Check (p) != 0;
}
bool isTrue () const
{
return PyObject_IsTrue (p) != 0;
}
bool isType (const Type& t) const;
bool isTuple() const
{
return Py::_Tuple_Check(p);
}
bool isString() const
{
return Py::_String_Check(p) || Py::_Unicode_Check(p);
}
bool isUnicode() const
{
return Py::_Unicode_Check(p);
}
// Commands
void setAttr (const std::string& s, const Object& value)
{
if(PyObject_SetAttrString (p, const_cast<char*>(s.c_str()), *value) == -1)
throw AttributeError ("getAttr failed.");
}
void delAttr (const std::string& s)
{
if(PyObject_DelAttrString (p, const_cast<char*>(s.c_str())) == -1)
throw AttributeError ("delAttr failed.");
}
// PyObject_SetItem is too weird to be using from C++
// so it is intentionally omitted.
void delItem (const Object& key)
{
//if(PyObject_DelItem(p, *key) == -1)
// failed to link on Windows?
throw KeyError("delItem failed.");
}
// Equality and comparison use PyObject_RichCompareBool
bool operator==(const Object& o2) const
{
int k = PyObject_RichCompareBool (p, *o2, Py_EQ);
if (PyErr_Occurred()) throw Exception();
return k != 0;
}
bool operator!=(const Object& o2) const
{
int k = PyObject_RichCompareBool (p, *o2, Py_NE);
if (PyErr_Occurred()) throw Exception();
return k != 0;
}
bool operator>=(const Object& o2) const
{
int k = PyObject_RichCompareBool (p, *o2, Py_GE);
if (PyErr_Occurred()) throw Exception();
return k != 0;
}
bool operator<=(const Object& o2) const
{
int k = PyObject_RichCompareBool (p, *o2, Py_LE);
if (PyErr_Occurred()) throw Exception();
return k != 0;
}
bool operator<(const Object& o2) const
{
int k = PyObject_RichCompareBool (p, *o2, Py_LT);
if (PyErr_Occurred()) throw Exception();
return k != 0;
}
bool operator>(const Object& o2) const
{
int k = PyObject_RichCompareBool (p, *o2, Py_GT);
if (PyErr_Occurred()) throw Exception();
return k != 0;
}
};
// End of class Object
inline PyObject* new_reference_to(const Object& g)
{
PyObject* p = g.ptr();
Py::_XINCREF(p);
return p;
}
// Nothing() is what an extension method returns if
// there is no other return value.
inline Object Nothing()
{
return Object(Py::_None());
}
// Python special None value
inline Object None()
{
return Object(Py::_None());
}
// Python special Boolean values
inline Object False()
{
return Object(Py::_False());
}
inline Object True()
{
return Object(Py::_True());
}
// TMM: 31May'01 - Added the #ifndef so I can exlude iostreams.
#ifndef CXX_NO_IOSTREAMS
std::ostream& operator<< (std::ostream& os, const Object& ob);
#endif
// Class Type
class Type: public Object
{
public:
explicit Type (PyObject* pyob, bool owned = false): Object(pyob, owned)
{
validate();
}
Type (const Object& ob): Object(*ob)
{
validate();
}
Type(const Type& t): Object(t)
{
validate();
}
Type& operator= (const Object& rhs)
{
return (*this = *rhs);
}
Type& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (rhsp);
return *this;
}
virtual bool accepts (PyObject *pyob) const
{
return pyob && Py::_Type_Check (pyob);
}
};
//
// Convert an owned Python pointer into a CXX Object
//
inline Object asObject (PyObject *p)
{
return Object(p, true);
}
// ===============================================
// class boolean
class Boolean: public Object
{
public:
// Constructor
Boolean (PyObject *pyob, bool owned = false): Object (pyob, owned)
{
validate();
}
Boolean (const Boolean& ob): Object(*ob)
{
validate();
}
// create from bool
Boolean (bool v=false)
{
set(PyBool_FromLong(v ? 1 : 0), true);
validate();
}
explicit Boolean (const Object& ob)
{
set(*ob, true);
validate();
}
// Assignment acquires new ownership of pointer
Boolean& operator= (const Object& rhs)
{
return (*this = *rhs);
}
Boolean& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (rhsp, true);
return *this;
}
// Membership
virtual bool accepts (PyObject *pyob) const
{
return pyob && Py::_Boolean_Check (pyob);
}
// convert to long
operator bool() const
{
return PyObject_IsTrue (ptr()) != 0;
}
Boolean& operator= (bool v)
{
set (PyBool_FromLong (v ? 1 : 0), true);
return *this;
}
};
// ===============================================
// class Int
class Int: public Object
{
public:
// Constructor
Int (PyObject *pyob, bool owned = false): Object (pyob, owned)
{
validate();
}
Int (const Int& ob): Object(*ob)
{
validate();
}
// create from long
Int (long v = 0L): Object(PyInt_FromLong(v), true)
{
validate();
}
// create from int
Int (int v)
{
long w = v;
set(PyInt_FromLong(w), true);
validate();
}
// create from bool
Int (bool v)
{
long w = v ? 1 : 0;
set(PyInt_FromLong(w), true);
validate();
}
explicit Int (const Object& ob)
{
set(PyNumber_Int(*ob), true);
validate();
}
// Assignment acquires new ownership of pointer
Int& operator= (const Object& rhs)
{
return (*this = *rhs);
}
Int& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (PyNumber_Int(rhsp), true);
return *this;
}
// Membership
virtual bool accepts (PyObject *pyob) const
{
return pyob && Py::_Int_Check (pyob);
}
// convert to long
operator long() const
{
return PyInt_AsLong (ptr());
}
#ifdef HAVE_LONG_LONG
// convert to long long
PY_LONG_LONG asLongLong() const
{
return PyLong_AsLongLong (ptr());
}
// convert to unsigned long long
unsigned PY_LONG_LONG asUnsignedLongLong() const
{
return PyLong_AsUnsignedLongLong (ptr());
}
#endif
// assign from an int
Int& operator= (int v)
{
set (PyInt_FromLong (long(v)), true);
return *this;
}
// assign from long
Int& operator= (long v)
{
set (PyInt_FromLong (v), true);
return *this;
}
#ifdef HAVE_LONG_LONG
// assign from long long
Int& operator= (PY_LONG_LONG v)
{
set (PyLong_FromLongLong (v), true);
return *this;
}
// assign from unsigned long long
Int& operator= (unsigned PY_LONG_LONG v)
{
set (PyLong_FromUnsignedLongLong (v), true);
return *this;
}
#endif
};
// ===============================================
// class Long
class Long: public Object
{
public:
// Constructor
explicit Long (PyObject *pyob, bool owned = false): Object (pyob, owned)
{
validate();
}
Long (const Long& ob): Object(ob.ptr())
{
validate();
}
// create from long
explicit Long (long v = 0L)
: Object(PyLong_FromLong(v), true)
{
validate();
}
// create from unsigned long
explicit Long (unsigned long v)
: Object(PyLong_FromUnsignedLong(v), true)
{
validate();
}
// create from int
explicit Long (int v)
: Object(PyLong_FromLong(static_cast<long>(v)), true)
{
validate();
}
// try to create from any object
Long (const Object& ob)
: Object(PyNumber_Long(*ob), true)
{
validate();
}
// Assignment acquires new ownership of pointer
Long& operator= (const Object& rhs)
{
return (*this = *rhs);
}
Long& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (PyNumber_Long(rhsp), true);
return *this;
}
// Membership
virtual bool accepts (PyObject *pyob) const
{
return pyob && Py::_Long_Check (pyob);
}
// convert to long
operator long() const
{
return PyLong_AsLong (ptr());
}
// convert to unsigned
operator unsigned long() const
{
return PyLong_AsUnsignedLong (ptr());
}
operator double() const
{
return PyLong_AsDouble (ptr());
}
// assign from an int
Long& operator= (int v)
{
set(PyLong_FromLong (long(v)), true);
return *this;
}
// assign from long
Long& operator= (long v)
{
set(PyLong_FromLong (v), true);
return *this;
}
// assign from unsigned long
Long& operator= (unsigned long v)
{
set(PyLong_FromUnsignedLong (v), true);
return *this;
}
};
#ifdef HAVE_LONG_LONG
// ===============================================
// class LongLong
class LongLong: public Object
{
public:
// Constructor
explicit LongLong (PyObject *pyob, bool owned = false): Object (pyob, owned)
{
validate();
}
LongLong (const LongLong& ob): Object(ob.ptr())
{
validate();
}
// create from long long
explicit LongLong (PY_LONG_LONG v = 0L)
: Object(PyLong_FromLongLong(v), true)
{
validate();
}
// create from unsigned long long
explicit LongLong (unsigned PY_LONG_LONG v)
: Object(PyLong_FromUnsignedLongLong(v), true)
{
validate();
}
// create from long
explicit LongLong (long v)
: Object(PyLong_FromLongLong(v), true)
{
validate();
}
// create from unsigned long
explicit LongLong (unsigned long v)
: Object(PyLong_FromUnsignedLongLong(v), true)
{
validate();
}
// create from int
explicit LongLong (int v)
: Object(PyLong_FromLongLong(static_cast<PY_LONG_LONG>(v)), true)
{
validate();
}
// try to create from any object
LongLong (const Object& ob)
: Object(PyNumber_Long(*ob), true)
{
validate();
}
// Assignment acquires new ownership of pointer
LongLong& operator= (const Object& rhs)
{
return (*this = *rhs);
}
LongLong& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (PyNumber_Long(rhsp), true);
return *this;
}
// Membership
virtual bool accepts (PyObject *pyob) const
{
return pyob && Py::_Long_Check (pyob);
}
// convert to long long
operator PY_LONG_LONG() const
{
return PyLong_AsLongLong (ptr());
}
// convert to unsigned long
operator unsigned PY_LONG_LONG() const
{
return PyLong_AsUnsignedLongLong (ptr());
}
// convert to long
operator long() const
{
return PyLong_AsLong (ptr());
}
// convert to unsigned
operator unsigned long() const
{
return PyLong_AsUnsignedLong (ptr());
}
operator double() const
{
return PyLong_AsDouble (ptr());
}
// assign from an int
LongLong& operator= (int v)
{
set(PyLong_FromLongLong (long(v)), true);
return *this;
}
// assign from long long
LongLong& operator= (PY_LONG_LONG v)
{
set(PyLong_FromLongLong (v), true);
return *this;
}
// assign from unsigned long long
LongLong& operator= (unsigned PY_LONG_LONG v)
{
set(PyLong_FromUnsignedLongLong (v), true);
return *this;
}
// assign from long
LongLong& operator= (long v)
{
set(PyLong_FromLongLong (v), true);
return *this;
}
// assign from unsigned long
LongLong& operator= (unsigned long v)
{
set(PyLong_FromUnsignedLongLong (v), true);
return *this;
}
};
#endif
// ===============================================
// class Float
//
class Float: public Object
{
public:
// Constructor
explicit Float (PyObject *pyob, bool owned = false): Object(pyob, owned)
{
validate();
}
Float (const Float& f): Object(f)
{
validate();
}
// make from double
explicit Float (double v=0.0)
: Object(PyFloat_FromDouble (v), true)
{
validate();
}
// try to make from any object
Float (const Object& ob)
: Object(PyNumber_Float(*ob), true)
{
validate();
}
Float& operator= (const Object& rhs)
{
return (*this = *rhs);
}
Float& operator= (PyObject* rhsp)
{
if(ptr() == rhsp) return *this;
set (PyNumber_Float(rhsp), true);
return *this;
}
// Membership
virtual bool accepts (PyObject *pyob) const
{
return pyob && Py::_Float_Check (pyob);
}
// convert to double
operator double () const
{
return PyFloat_AsDouble (ptr());
}
// assign from a double
Float& operator= (double v)
{
set(PyFloat_FromDouble (v), true);
return *this;
}
// assign from an int
Float& operator= (int v)
{
set(PyFloat_FromDouble (double(v)), true);
return *this;
}
// assign from long
Float& operator= (long v)
{
set(PyFloat_FromDouble (double(v)), true);
return *this;
}
// assign from an Int
Float& operator= (const Int& iob)
{
set(PyFloat_FromDouble (double(long(iob))), true);
return *this;
}
};
// ===============================================
// class Complex
class Complex: public Object
{
public:
// Constructor
explicit Complex (PyObject *pyob, bool owned = false): Object(pyob, owned)
{
validate();
}
Complex (const Complex& f): Object(f)
{
validate();
}