NCBI C++ ToolKit
pythonpp_pdt.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: pythonpp_pdt.hpp 93082 2021-03-03 22:10:53Z ucko $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Sergey Sikorskiy
27 *
28 * File Description: Tiny Python API wrappers
29 *
30 * Status: *Initial*
31 *
32 * ===========================================================================
33 */
34 
35 #ifndef PYTHONPP_PDT_H
36 #define PYTHONPP_PDT_H
37 
38 #include "pythonpp_object.hpp"
39 #include <corelib/ncbistr.hpp>
40 
42 
43 /// Flag showing whether all pythonpp::CString objects should be created as
44 /// Python unicode strings (TRUE value) or regular strings (FALSE value)
45 extern bool g_PythonStrDefToUnicode;
46 
47 
48 namespace pythonpp
49 {
50 
51 // PyBoolObject
52 class CBool : public CObject
53 {
54 public:
55  CBool(PyObject* obj, EOwnership ownership = eAcquireOwnership)
56  : CObject(obj, ownership)
57  {
58  if ( !HasExactSameType(obj) ) {
59  throw CTypeError("Invalid conversion");
60  }
61  }
62  CBool(const CObject& obj)
63  : CObject(obj)
64  {
65  if ( !HasExactSameType(obj) ) {
66  throw CTypeError("Invalid conversion");
67  }
68  }
69  CBool(const CBool& obj)
70  : CObject(obj)
71  {
72  }
73  CBool(bool value)
74  : CObject(value ? Py_True : Py_False) // NCBI_FAKE_WARNING
75  {
76  }
77  CBool(long value)
78  {
79  PyObject* tmp_obj = PyBool_FromLong(value);
80  if ( !tmp_obj ) {
81  throw CTypeError("Invalid conversion");
82  }
83  Set(tmp_obj, eTakeOwnership);
84  }
85 
86 public:
87  // Assign operators ...
88  CBool& operator= (const CObject& obj)
89  {
90  if ( this != &obj ) {
91  if ( !HasExactSameType(obj) ) {
92  throw CTypeError("Invalid conversion");
93  }
94  Set(obj);
95  }
96  return *this;
97  }
98  CBool& operator= (PyObject* obj)
99  {
100  if ( Get() != obj ) {
101  if ( !HasExactSameType(obj) ) {
102  throw CTypeError("Invalid conversion");
103  }
104  Set(obj);
105  }
106  return *this;
107  }
108 
109 public:
110  // Type conversion operators ...
111  operator bool(void) const
112  {
113  return Get() == Py_True;
114  }
115 
116 public:
117  static bool HasSameType(PyObject* obj)
118  {
119  return PyBool_Check (obj);
120  }
121  static bool HasExactSameType(PyObject* obj)
122  {
123  return PyBool_Check (obj);
124  }
125 };
126 
127 #if PY_MAJOR_VERSION < 3
128 // PyInt_Type
129 class CInt : public CObject
130 {
131 //PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
132 //PyAPI_FUNC(unsigned long) PyInt_AsUnsignedLongMask(PyObject *);
133 //PyAPI_FUNC(long) PyInt_GetMax(void);
134 // long PyInt_AS_LONG( PyObject *io)
135 // unsigned long long PyInt_AsUnsignedLongLongMask( PyObject *io)
136 
137 public:
138  CInt(PyObject* obj, EOwnership ownership = eAcquireOwnership)
139  : CObject(obj, ownership)
140  {
141  }
142  CInt(const CObject& obj)
143  {
144  PyObject* tmp_obj = PyNumber_Int(obj);
145  if ( !tmp_obj ) {
146  throw CTypeError("Invalid conversion");
147  }
148  Set(tmp_obj, eTakeOwnership);
149  }
150  CInt(const CInt& obj)
151  : CObject(obj)
152  {
153  }
154  CInt(int value)
155  : CObject(PyInt_FromLong(long(value)), eTakeOwnership)
156  {
157  }
158  CInt(long value = 0L)
159  : CObject(PyInt_FromLong(value), eTakeOwnership)
160  {
161  }
162 
163 public:
164  // Assign operators ...
165  CInt& operator= (const CObject& obj)
166  {
167  if ( this != &obj ) {
168  PyObject* tmp_obj = PyNumber_Int(obj);
169  if ( !tmp_obj ) {
170  throw CTypeError("Invalid conversion");
171  }
172  Set(tmp_obj, eTakeOwnership);
173  }
174  return *this;
175  }
176  CInt& operator= (PyObject* obj)
177  {
178  if ( Get() != obj ) {
179  PyObject* tmp_obj = PyNumber_Int(obj);
180  if ( !tmp_obj ) {
181  throw CTypeError("Invalid conversion");
182  }
183  Set(tmp_obj, eTakeOwnership);
184  }
185  return *this;
186  }
188  {
189  Set(PyInt_FromLong(long(value)), eTakeOwnership);
190  return *this;
191  }
193  {
194  Set(PyInt_FromLong(value), eTakeOwnership);
195  return *this;
196  }
197 
198 public:
199  // Type conversion operators ...
200  operator long() const
201  {
202  return PyInt_AsLong(Get());
203  }
204 
205 public:
206  static bool HasSameType(PyObject* obj)
207  {
208  return PyInt_Check (obj);
209  }
210  static bool HasExactSameType(PyObject* obj)
211  {
212  return PyInt_CheckExact (obj);
213  }
214 };
215 #endif
216 
217 // PyLong_Type
218 class CLong : public CObject
219 {
220 //PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *);
221 //PyAPI_FUNC(double) _PyLong_AsScaledDouble(PyObject *vv, int *e);
222 
223 //PyAPI_FUNC(PyObject *) PyLong_FromVoidPtr(void *);
224 //PyAPI_FUNC(void *) PyLong_AsVoidPtr(PyObject *);
225 //PyAPI_FUNC(PyObject *) PyLong_FromString(char *, char **, int);
226 
227 // ???
228 //PyAPI_FUNC(CInt) _PyLong_Sign(PyObject *v);
229 //PyAPI_FUNC(size_t) _PyLong_NumBits(PyObject *v);
230 //PyAPI_FUNC(PyObject *) _PyLong_FromByteArray(
231 // const unsigned char* bytes, size_t n,
232 // int little_endian, int is_signed);
233 //PyAPI_FUNC(CInt) _PyLong_AsByteArray(PyLongObject* v,
234 // unsigned char* bytes, size_t n,
235 // int little_endian, int is_signed);
236 
237 // PyObject* PyLong_FromUnicode( Py_UNICODE *u, int length, int base)
238 
239 public:
240  CLong(PyObject* obj, EOwnership ownership = eAcquireOwnership)
241  : CObject(obj, ownership)
242  {
243  }
244  CLong(const CObject& obj)
245  {
246  PyObject* tmp_obj = PyNumber_Long(obj);
247  if ( !tmp_obj ) {
248  throw CTypeError("Invalid conversion");
249  }
250  Set(tmp_obj, eTakeOwnership);
251  }
252  CLong(const CLong& obj)
253  : CObject(obj)
254  {
255  }
256 #if PY_MAJOR_VERSION < 3
258  : CObject(PyLong_FromLong(static_cast<long>(value)), eTakeOwnership)
259  {
260  }
261 #else
262  CLong(int value)
263  : CObject(PyLong_FromLong(value), eTakeOwnership)
264  {
265  }
266  CLong(unsigned int value)
267  : CObject(PyLong_FromUnsignedLong(value), eTakeOwnership)
268  {
269  }
270 #endif
271  CLong(long value = 0L)
272  : CObject(PyLong_FromLong(value), eTakeOwnership)
273  {
274  }
275  CLong(unsigned long value)
276  : CObject(PyLong_FromUnsignedLong(value), eTakeOwnership)
277  {
278  }
279  CLong(long long value)
280  : CObject(PyLong_FromLongLong(value), eTakeOwnership)
281  {
282  }
283  CLong(unsigned long long value)
284  : CObject(PyLong_FromUnsignedLongLong(value), eTakeOwnership)
285  {
286  }
287  CLong(double value)
288  : CObject(PyLong_FromDouble(value), eTakeOwnership)
289  {
290  }
291 
292 public:
293  // Assign operators ...
294  CLong& operator= (const CObject& obj)
295  {
296  if ( this != &obj ) {
297  PyObject* tmp_obj = PyNumber_Long(obj);
298  if ( !tmp_obj ) {
299  throw CTypeError("Invalid conversion");
300  }
301  Set(tmp_obj, eTakeOwnership);
302  }
303  return *this;
304  }
305  CLong& operator= (PyObject* obj)
306  {
307  if ( Get() != obj ) {
308  PyObject* tmp_obj = PyNumber_Long(obj);
309  if ( !tmp_obj ) {
310  throw CTypeError("Invalid conversion");
311  }
312  Set(tmp_obj, eTakeOwnership);
313  }
314  return *this;
315  }
316 #if PY_MAJOR_VERSION < 3
318  {
319  Set(PyLong_FromLong(long(value)), eTakeOwnership);
320  return *this;
321  }
322 #else
323  CLong& operator= (int value)
324  {
325  Set(PyLong_FromLong(value), eTakeOwnership);
326  return *this;
327  }
328  CLong& operator= (unsigned int value)
329  {
330  Set(PyLong_FromUnsignedLong(value), eTakeOwnership);
331  return *this;
332  }
333 #endif
335  {
336  Set(PyLong_FromLong(value), eTakeOwnership);
337  return *this;
338  }
339  CLong& operator= (unsigned long value)
340  {
341  Set(PyLong_FromUnsignedLong(value), eTakeOwnership);
342  return *this;
343  }
344  CLong& operator= (long long value)
345  {
346  Set(PyLong_FromLongLong(value), eTakeOwnership);
347  return *this;
348  }
349  CLong& operator= (unsigned long long value)
350  {
351  Set(PyLong_FromUnsignedLongLong(value), eTakeOwnership);
352  return *this;
353  }
355  {
356  Set(PyLong_FromDouble(value), eTakeOwnership);
357  return *this;
358  }
359 
360 public:
361  // Type conversion operators ...
362 #if PY_MAJOR_VERSION >= 3
363  operator int() const
364  {
365  return _PyLong_AsInt(Get());
366  }
367  operator unsigned int() const
368  {
369  return PyLong_AsUnsignedLong(Get());
370  }
371 #endif
372  operator long() const
373  {
374  return PyLong_AsLong(Get());
375  }
376  operator unsigned long() const
377  {
378  return PyLong_AsUnsignedLong(Get());
379  }
380  operator long long() const
381  {
382  return PyLong_AsLongLong(Get());
383  }
384  operator unsigned long long() const
385  {
386  return PyLong_AsUnsignedLongLong(Get());
387  }
388  operator double() const
389  {
390  return PyLong_AsDouble(Get());
391  }
392 
393 public:
394  static bool HasSameType(PyObject* obj)
395  {
396  return PyLong_Check (obj);
397  }
398  static bool HasExactSameType(PyObject* obj)
399  {
400  return PyLong_CheckExact (obj);
401  }
402 };
403 
404 #if PY_MAJOR_VERSION >= 3
405 typedef CLong CInt;
406 #endif
407 
408 // PyFloat_Type
409 class CFloat : public CObject
410 {
411 /* Return Python CFloat from string PyObject. Second argument ignored on
412 input, and, if non-NULL, NULL is stored into *junk (this tried to serve a
413 purpose once but can't be made to work as intended). */
414 //PyAPI_FUNC(PyObject *) PyFloat_FromString(PyObject*, char** junk);
415 
416 /* Extract C double from Python CFloat. The macro version trades safety for
417 speed. */
418 //PyAPI_FUNC(double) PyFloat_AsDouble(PyObject *);
419 
420 /* Write repr(v) into the char buffer argument, followed by null byte. The
421 buffer must be "big enough"; >= 100 is very safe.
422 PyFloat_AsReprString(buf, x) strives to print enough digits so that
423 PyFloat_FromString(buf) then reproduces x exactly. */
424 //PyAPI_FUNC(void) PyFloat_AsReprString(char*, PyFloatObject *v);
425 
426 /* Write str(v) into the char buffer argument, followed by null byte. The
427 buffer must be "big enough"; >= 100 is very safe. Note that it's
428 unusual to be able to get back the CFloat you started with from
429 PyFloat_AsString's result -- use PyFloat_AsReprString() if you want to
430 preserve precision across conversions. */
431 //PyAPI_FUNC(void) PyFloat_AsString(char*, PyFloatObject *v);
432 
433 // ???
434 //PyAPI_FUNC(CInt) _PyFloat_Pack4(double x, unsigned char *p, int le);
435 //PyAPI_FUNC(CInt) _PyFloat_Pack8(double x, unsigned char *p, int le);
436 //PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
437 //PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
438 
439 // PyObject* PyFloat_FromDouble( double v)
440 // double PyFloat_AS_DOUBLE( PyObject *pyfloat)
441 
442 public:
443  CFloat(PyObject* obj, EOwnership ownership = eAcquireOwnership)
444  : CObject(obj, ownership)
445  {
446  }
447  CFloat(const CObject& obj)
448  {
449  PyObject* tmp_obj = PyNumber_Float(obj);
450  if ( !tmp_obj ) {
451  throw CTypeError("Invalid conversion");
452  }
453  Set(tmp_obj, eTakeOwnership);
454  }
455  CFloat(const CFloat& obj)
456  : CObject(obj)
457  {
458  }
459  CFloat(double value = 0.0)
460  : CObject(PyFloat_FromDouble(value), eTakeOwnership)
461  {
462  }
463 
464 public:
465  // Assign operators ...
466  CFloat& operator= (const CObject& obj)
467  {
468  if ( this != &obj ) {
469  PyObject* tmp_obj = PyNumber_Float(obj);
470  if ( !tmp_obj ) {
471  throw CTypeError("Invalid conversion");
472  }
473  Set(tmp_obj, eTakeOwnership);
474  }
475  return *this;
476  }
477  CFloat& operator= (PyObject* obj)
478  {
479  if ( Get() != obj ) {
480  PyObject* tmp_obj = PyNumber_Float(obj);
481  if ( !tmp_obj ) {
482  throw CTypeError("Invalid conversion");
483  }
484  Set(tmp_obj, eTakeOwnership);
485  }
486  return *this;
487  }
489  {
490  Set(PyFloat_FromDouble(value), eTakeOwnership);
491  return *this;
492  }
493 
494 public:
495  // Type conversion operators ...
496  operator double() const
497  {
498  return PyFloat_AsDouble(Get());
499  }
500 
501 public:
502  static bool HasSameType(PyObject* obj)
503  {
504  return PyFloat_Check (obj);
505  }
506  static bool HasExactSameType(PyObject* obj)
507  {
508  return PyFloat_CheckExact (obj);
509  }
510 };
511 
512 class CComplex : public CObject
513 {
514 // Complex Numbers as C Structures ...
515 //PyAPI_FUNC(Py_complex) c_sum(Py_complex, Py_complex);
516 //PyAPI_FUNC(Py_complex) c_diff(Py_complex, Py_complex);
517 //PyAPI_FUNC(Py_complex) c_neg(Py_complex);
518 //PyAPI_FUNC(Py_complex) c_prod(Py_complex, Py_complex);
519 //PyAPI_FUNC(Py_complex) c_quot(Py_complex, Py_complex);
520 //PyAPI_FUNC(Py_complex) c_pow(Py_complex, Py_complex);
521 
522 //PyAPI_FUNC(PyObject *) PyComplex_FromCComplex(Py_complex);
523 //PyAPI_FUNC(PyObject *) PyComplex_FromDoubles(double real, double imag);
524 //PyAPI_FUNC(double) PyComplex_RealAsDouble(PyObject *op);
525 //PyAPI_FUNC(double) PyComplex_ImagAsDouble(PyObject *op);
526 //PyAPI_FUNC(Py_complex) PyComplex_AsCComplex(PyObject *op);
527 
528 public:
529  CComplex(PyObject* obj, EOwnership ownership = eAcquireOwnership)
530  : CObject(obj, ownership)
531  {
532  }
533  CComplex(const CObject& obj)
534  {
535  if ( !HasExactSameType(obj) ) {
536  throw CTypeError("Invalid conversion");
537  }
538  Set(obj);
539  }
540  CComplex(const CComplex& obj)
541  : CObject(obj)
542  {
543  }
544  CComplex(double v = 0.0, double w = 0.0)
545  : CObject(PyComplex_FromDoubles(v, w), eTakeOwnership)
546  {
547  }
548 
549 public:
550  // Assign operators ...
552  {
553  if ( this != &obj ) {
554  if ( !HasExactSameType(obj) ) {
555  throw CTypeError("Invalid conversion");
556  }
557  Set(obj);
558  }
559  return *this;
560  }
561  CComplex& operator= (PyObject* obj)
562  {
563  if ( Get() != obj ) {
564  if ( !HasExactSameType(obj) ) {
565  throw CTypeError("Invalid conversion");
566  }
567  Set(obj);
568  }
569  return *this;
570  }
572  {
573  Set(PyComplex_FromDoubles(value, 0.0), eTakeOwnership);
574  return *this;
575  }
576 
577 public:
578  // Type conversion operators ...
579  operator double() const
580  {
581  return PyFloat_AsDouble(Get());
582  }
583 
584 public:
585  double GetReal() const
586  {
587  return PyComplex_RealAsDouble(Get());
588  }
589  double GetImag() const
590  {
591  return PyComplex_ImagAsDouble(Get());
592  }
593 
594 public:
595  static bool HasSameType(PyObject* obj)
596  {
597  return PyComplex_Check (obj);
598  }
599  static bool HasExactSameType(PyObject* obj)
600  {
601  return PyComplex_CheckExact (obj);
602  }
603 };
604 
605 // Not implemented yet ...
606 class CChar : public CObject
607 {
608 public:
609 protected:
610 private:
611 };
612 
613 // PyString_Type
614 // PyBaseString_Type
615 class CString : public CObject
616 {
617 //PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
618 // Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
619 //PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
620 // Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
621 //PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
622 //PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
623 //PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
624 //PyAPI_FUNC(CInt) _PyString_Resize(PyObject **, int);
625 //PyAPI_FUNC(CInt) _PyString_Eq(PyObject *, PyObject*);
626 //PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
627 //PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
628 // int, char**, int*);
629 //PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, int,
630 // const char *, int,
631 // const char *);
632 
633 //PyAPI_FUNC(void) PyString_InternInPlace(PyObject **);
634 //PyAPI_FUNC(void) PyString_InternImmortal(PyObject **);
635 //PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *);
636 //PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
637 //PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
638 //PyAPI_FUNC(PyObject*) PyString_Decode(
639 // const char *s, /* encoded string */
640 // int size, /* size of buffer */
641 // const char *encoding, /* encoding */
642 // const char *errors /* error handling */
643 // );
644 //PyAPI_FUNC(PyObject*) PyString_Encode(
645 // const char *s, /* string char buffer */
646 // int size, /* number of chars to encode */
647 // const char *encoding, /* encoding */
648 // const char *errors /* error handling */
649 // );
650 
651 //PyAPI_FUNC(PyObject*) PyString_AsEncodedObject(
652 // PyObject *str, /* string object */
653 // const char *encoding, /* encoding */
654 // const char *errors /* error handling */
655 // );
656 //PyAPI_FUNC(PyObject*) PyString_AsDecodedObject(
657 // PyObject *str, /* string object */
658 // const char *encoding, /* encoding */
659 // const char *errors /* error handling */
660 // );
661 
662 //PyAPI_FUNC(CInt) PyString_AsStringAndSize(
663 // register PyObject *obj, /* string or Unicode object */
664 // register char **s, /* pointer to buffer variable */
665 // register int *len /* pointer to length variable or NULL
666 // (only possible for 0-terminated
667 // strings) */
668 // );
669 
670 // int PyString_GET_SIZE( PyObject *string)
671 // char* PyString_AS_STRING( PyObject *string)
672 
673 // int PyUnicode_GET_SIZE( PyObject *o)
674 // int PyUnicode_GET_DATA_SIZE( PyObject *o)
675 // Py_UNICODE* PyUnicode_AS_UNICODE( PyObject *o)
676 // const char* PyUnicode_AS_DATA( PyObject *o)
677 // int Py_UNICODE_ISSPACE( Py_UNICODE ch)
678 // int Py_UNICODE_ISLOWER( Py_UNICODE ch)
679 // int Py_UNICODE_ISUPPER( Py_UNICODE ch)
680 // int Py_UNICODE_ISTITLE( Py_UNICODE ch)
681 // int Py_UNICODE_ISLINEBREAK( Py_UNICODE ch)
682 // int Py_UNICODE_ISDECIMAL( Py_UNICODE ch)
683 // int Py_UNICODE_ISDIGIT( Py_UNICODE ch)
684 // int Py_UNICODE_ISNUMERIC( Py_UNICODE ch)
685 // int Py_UNICODE_ISALPHA( Py_UNICODE ch)
686 // int Py_UNICODE_ISALNUM( Py_UNICODE ch)
687 // Py_UNICODE Py_UNICODE_TOLOWER( Py_UNICODE ch)
688 // Py_UNICODE Py_UNICODE_TOUPPER( Py_UNICODE ch)
689 // Py_UNICODE Py_UNICODE_TOTITLE( Py_UNICODE ch)
690 // int Py_UNICODE_TODECIMAL( Py_UNICODE ch)
691 // int Py_UNICODE_TODIGIT( Py_UNICODE ch)
692 // double Py_UNICODE_TONUMERIC( Py_UNICODE ch)
693 // PyObject* PyUnicode_FromUnicode( const Py_UNICODE *u, int size)
694 // Py_UNICODE* PyUnicode_AsUnicode( PyObject *unicode)
695 // int PyUnicode_GetSize( PyObject *unicode)
696 // PyObject* PyUnicode_FromEncodedObject( PyObject *obj, const char *encoding, const char *errors)
697 // PyObject* PyUnicode_FromObject( PyObject *obj)
698 // PyObject* PyUnicode_FromWideChar( const wchar_t *w, int size)
699 // int PyUnicode_AsWideChar( PyUnicodeObject *unicode, wchar_t *w, int size)
700 
701 public:
702  CString(void)
703 #if PY_MAJOR_VERSION >= 3
704  : CObject(PyUnicode_FromStringAndSize("", 0), eTakeOwnership)
705 #else
706  : CObject(PyString_FromStringAndSize("", 0), eTakeOwnership)
707 #endif
708  {
709  }
710  CString(PyObject* obj, EOwnership ownership = eAcquireOwnership)
711  : CObject(obj, ownership)
712  {
713  if ( !HasExactSameType(obj) ) {
714  throw CTypeError("Invalid conversion");
715  }
716  }
717  CString(const CObject& obj)
718  {
719  if ( !HasExactSameType(obj) ) {
720  throw CTypeError("Invalid conversion");
721  }
722  Set(obj);
723  }
724  CString(const CString& obj)
725  : CObject(obj)
726  {
727  }
728  CString(const string& str)
729  {
730  operator= (str);
731  }
732  CString(const char* str)
733  {
734  operator= (str);
735  }
736  CString(const char* str, size_t str_size)
737  {
738  operator= (string(str, str_size));
739  }
740 
741 public:
742  // Assign operators ...
744  {
745  if ( this != &obj ) {
746  if ( !HasExactSameType(obj) ) {
747  throw CTypeError("Invalid conversion");
748  }
749  Set(obj);
750  }
751  return *this;
752  }
753  CString& operator= (PyObject* obj)
754  {
755  if ( Get() != obj ) {
756  if ( !HasExactSameType(obj) ) {
757  throw CTypeError("Invalid conversion");
758  }
759  Set(obj);
760  }
761  return *this;
762  }
763  CString& operator= (const string& str)
764  {
765 #if PY_MAJOR_VERSION < 3
767 #endif
768  basic_string<Py_UNICODE> str_uni(CUtf8::AsBasicString<Py_UNICODE>(str));
769 #if PY_MAJOR_VERSION >= 3
770  Set(PyUnicode_FromWideChar(str_uni.data(), str_uni.size()),
772 #else
773  Set(PyUnicode_FromUnicode(str_uni.data(), str_uni.size()), eTakeOwnership);
774 #endif
775 #if PY_MAJOR_VERSION < 3
776  }
777  else {
778  Set(PyString_FromStringAndSize(str.data(), str.size()), eTakeOwnership);
779  }
780 #endif
781  return *this;
782  }
783  CString& operator= (const char* str)
784  {
785  return operator= (string(str));
786  }
787 
788 public:
789  size_t GetSize (void) const
790  {
791  if ( PyUnicode_Check(Get()) ) {
792 #if PY_VERSION_HEX >= 0x03030000
793  return static_cast<size_t>( PyUnicode_GET_LENGTH( Get() ) );
794 #else
795  return static_cast<size_t>( PyUnicode_GET_SIZE( Get() ) );
796 #endif
797  } else {
798 #if PY_MAJOR_VERSION >= 3
799  return static_cast<size_t>( PyBytes_Size ( Get() ) );
800 #else
801  return static_cast<size_t>( PyString_Size ( Get() ) );
802 #endif
803  }
804  }
805  operator string (void) const
806  {
807  return AsStdSring();
808  }
809 
810  string AsStdSring(void) const
811  {
812  if( PyUnicode_Check(Get()) ) {
813 #if PY_VERSION_HEX >= 0x03030000
814  Py_ssize_t size;
815  auto utf = PyUnicode_AsUTF8AndSize(Get(), &size);
816  return string(utf, size);
817 #else
818  return CUtf8::AsUTF8(
819  PyUnicode_AS_UNICODE( Get() ),
820  static_cast<size_t>( PyUnicode_GET_SIZE( Get() ) ) );
821 #endif
822  } else {
823 #if PY_MAJOR_VERSION >= 3
824  return string(PyBytes_AsString(Get()),
825  static_cast<size_t>(PyBytes_Size(Get())));
826 #else
827  return string( PyString_AsString( Get() ), static_cast<size_t>( PyString_Size( Get() ) ) );
828 #endif
829  }
830  }
831 
832 public:
833  static bool HasExactSameType(PyObject* obj)
834  {
835 #if PY_MAJOR_VERSION >= 3
836  return PyUnicode_CheckExact(obj) || PyBytes_CheckExact(obj);
837 #else
838  return PyString_CheckExact(obj) || PyUnicode_CheckExact(obj);
839 #endif
840  }
841  static bool HasSameType(PyObject* obj)
842  {
843 #if PY_MAJOR_VERSION >= 3
844  return PyUnicode_Check(obj) || PyBytes_Check(obj);
845 #else
846  return PyString_Check(obj) || PyUnicode_Check(obj);
847 #endif
848  }
849 };
850 
851 
852 class CBinary : public CObject
853 {
854 public:
855  CBinary(void)
856 #if PY_MAJOR_VERSION >= 3
857  : CObject(PyBytes_FromStringAndSize("", 0), eTakeOwnership)
858 #else
859  : CObject(PyString_FromStringAndSize("", 0), eTakeOwnership)
860 #endif
861  {
862  }
863  CBinary(PyObject* obj, EOwnership ownership = eAcquireOwnership)
864  : CObject(obj, ownership)
865  {
866  if ( !HasExactSameType(obj) ) {
867  throw CTypeError("Invalid conversion");
868  }
869  }
870  CBinary(const CObject& obj)
871  {
872  if ( !HasExactSameType(obj) ) {
873  throw CTypeError("Invalid conversion");
874  }
875  Set(obj);
876  }
877  CBinary(const CBinary& obj)
878  : CObject(obj)
879  {
880  }
881  CBinary(const string& str)
882  {
883  operator= (str);
884  }
885  CBinary(const char* str, size_t str_size)
886  {
887  operator= (string(str, str_size));
888  }
889 
890 public:
891  // Assign operators ...
893  {
894  if (this != &obj) {
895  if ( !HasExactSameType(obj) ) {
896  throw CTypeError("Invalid conversion");
897  }
898  Set(obj);
899  }
900  return *this;
901  }
902  CBinary& operator= (PyObject* obj)
903  {
904  if (Get() != obj) {
905  if ( !HasExactSameType(obj) ) {
906  throw CTypeError("Invalid conversion");
907  }
908  Set(obj);
909  }
910  return *this;
911  }
912  CBinary& operator= (const string& str)
913  {
914 #if PY_MAJOR_VERSION >= 3
915  Set(PyBytes_FromStringAndSize(str.data(), str.size()), eTakeOwnership);
916 #else
917  Set(PyString_FromStringAndSize(str.data(), str.size()),
919 #endif
920  return *this;
921  }
922  CBinary& operator= (const char* str)
923  {
924  return operator= (string(str));
925  }
926 
927 public:
928  size_t GetSize (void) const
929  {
930 #if PY_MAJOR_VERSION >= 3
931  return static_cast<size_t>(PyBytes_Size(Get()));
932 #else
933  return static_cast<size_t>(PyString_Size(Get()));
934 #endif
935  }
936  operator string (void) const
937  {
938  return AsStdSring();
939  }
940 
941  string AsStdSring(void) const
942  {
943 #if PY_MAJOR_VERSION >= 3
944  return string(PyBytes_AsString(Get()),
945  static_cast<size_t>(PyBytes_Size(Get())));
946 #else
947  return string(PyString_AsString(Get()),
948  static_cast<size_t>(PyString_Size(Get())));
949 #endif
950  }
951 
952 public:
953  static bool HasExactSameType(PyObject* obj)
954  {
955 #if PY_MAJOR_VERSION >= 3
956  return PyBytes_CheckExact(obj);
957 #else
958  return PyString_CheckExact(obj);
959 #endif
960  }
961  static bool HasSameType(PyObject* obj)
962  {
963 #if PY_MAJOR_VERSION >= 3
964  return PyBytes_Check(obj);
965 #else
966  return PyString_Check(obj);
967 #endif
968  }
969 };
970 
971 
972 #if 0
973 // PyFile_Type
974 class CFile : public CObject
975 {
976 // ????
977 //PyAPI_FUNC(CInt) PyFile_SetEncoding(PyObject *, const char *);
978 //PyAPI_FUNC(CInt) PyObject_AsFileDescriptor(PyObject *);
979 
980 
981 // Documented ...
982 //PyAPI_FUNC(PyObject *) PyFile_FromString(char *, char *);
983 //PyAPI_FUNC(PyObject *) PyFile_FromFile(FILE *, char *, char *, (*)(FILE *));
984 //PyAPI_FUNC(FILE *) PyFile_AsFile(PyObject *);
985 //PyAPI_FUNC(PyObject *) PyFile_GetLine(PyObject *, int);
986 //PyAPI_FUNC(PyObject *) PyFile_Name(PyObject *);
987 //PyAPI_FUNC(void) PyFile_SetBufSize(PyObject *, int);
988 // int PyFile_Encoding( PyFileObject *p, char *enc)
989 //PyAPI_FUNC(CInt) PyFile_SoftSpace(PyObject *, int);
990 //PyAPI_FUNC(CInt) PyFile_WriteObject(PyObject *, PyObject *, int);
991 //PyAPI_FUNC(CInt) PyFile_WriteString(const char *, PyObject *);
992 
993 public:
994 public:
995  static bool HasExactSameType(PyObject* obj)
996  {
997  return PyFile_CheckExact(obj);
998  }
999  static bool HasSameType(PyObject* obj)
1000  {
1001  return PyFile_Check(obj);
1002  }
1003 };
1004 #endif
1005 
1006 ///////////////////////////////////////////////////////////////////////////
1007 inline CObject operator+ (const CObject& a, int j)
1008 {
1009  PyObject* tmp_obj = PyNumber_Add(a.Get(), CInt(j).Get());
1010  if ( !tmp_obj ) {
1011  throw CArithmeticError("PyNumber_Add");
1012  }
1013  return CObject(tmp_obj, eTakeOwnership);
1014 }
1015 inline CObject operator+ (const CObject& a, double v)
1016 {
1017  PyObject* tmp_obj = PyNumber_Add(a.Get(), CFloat(v).Get());
1018  if ( !tmp_obj ) {
1019  throw CArithmeticError("PyNumber_Add");
1020  }
1021  return CObject(tmp_obj, eTakeOwnership);
1022 }
1023 inline CObject operator+ (CInt j, const CObject& b)
1024 {
1025  PyObject* tmp_obj = PyNumber_Add(CInt(j).Get(), b.Get());
1026  if ( !tmp_obj ) {
1027  throw CArithmeticError("PyNumber_Add");
1028  }
1029  return CObject(tmp_obj, eTakeOwnership);
1030 }
1031 inline CObject operator+ (double v, const CObject& b)
1032 {
1033  PyObject* tmp_obj = PyNumber_Add(CFloat(v).Get(), b.Get());
1034  if ( !tmp_obj ) {
1035  throw CArithmeticError("PyNumber_Add");
1036  }
1037  return CObject(tmp_obj, eTakeOwnership);
1038 }
1039 inline CObject operator- (const CObject& a, int j)
1040 {
1041  PyObject* tmp_obj = PyNumber_Subtract(a.Get(), CInt(j).Get());
1042  if ( !tmp_obj ) {
1043  throw CArithmeticError("PyNumber_Subtract");
1044  }
1045  return CObject(tmp_obj, eTakeOwnership);
1046 }
1047 inline CObject operator- (const CObject& a, double v)
1048 {
1049  PyObject* tmp_obj = PyNumber_Subtract(a.Get(), CFloat(v).Get());
1050  if ( !tmp_obj ) {
1051  throw CArithmeticError("PyNumber_Subtract");
1052  }
1053  return CObject(tmp_obj, eTakeOwnership);
1054 }
1055 inline CObject operator- (CInt j, const CObject& b)
1056 {
1057  PyObject* tmp_obj = PyNumber_Subtract(CInt(j).Get(), b.Get());
1058  if ( !tmp_obj ) {
1059  throw CArithmeticError("PyNumber_Subtract");
1060  }
1061  return CObject(tmp_obj, eTakeOwnership);
1062 }
1063 inline CObject operator- (double v, const CObject& b)
1064 {
1065  PyObject* tmp_obj = PyNumber_Subtract(CFloat(v).Get(), b.Get());
1066  if ( !tmp_obj ) {
1067  throw CArithmeticError("PyNumber_Subtract");
1068  }
1069  return CObject(tmp_obj, eTakeOwnership);
1070 }
1071 inline CObject operator* (const CObject& a, int j)
1072 {
1073  PyObject* tmp_obj = PyNumber_Multiply(a.Get(), CInt(j).Get());
1074  if ( !tmp_obj ) {
1075  throw CArithmeticError("PyNumber_Multiply");
1076  }
1077  return CObject(tmp_obj, eTakeOwnership);
1078 }
1079 inline CObject operator* (const CObject& a, double v)
1080 {
1081  PyObject* tmp_obj = PyNumber_Multiply(a.Get(), CFloat(v).Get());
1082  if ( !tmp_obj ) {
1083  throw CArithmeticError("PyNumber_Multiply");
1084  }
1085  return CObject(tmp_obj, eTakeOwnership);
1086 }
1087 inline CObject operator* (CInt j, const CObject& b)
1088 {
1089  PyObject* tmp_obj = PyNumber_Multiply(CInt(j).Get(), b.Get());
1090  if ( !tmp_obj ) {
1091  throw CArithmeticError("PyNumber_Multiply");
1092  }
1093  return CObject(tmp_obj, eTakeOwnership);
1094 }
1095 inline CObject operator* (double v, const CObject& b)
1096 {
1097  PyObject* tmp_obj = PyNumber_Multiply(CFloat(v).Get(), b.Get());
1098  if ( !tmp_obj ) {
1099  throw CArithmeticError("PyNumber_Multiply");
1100  }
1101  return CObject(tmp_obj, eTakeOwnership);
1102 }
1103 inline CObject operator/ (const CObject& a, int j)
1104 {
1105  PyObject* tmp_obj = PyNumber_TrueDivide(a.Get(), CInt(j).Get());
1106  if ( !tmp_obj ) {
1107  throw CArithmeticError("PyNumber_TrueDivide");
1108  }
1109  return CObject(tmp_obj, eTakeOwnership);
1110 }
1111 inline CObject operator/ (const CObject& a, double v)
1112 {
1113  PyObject* tmp_obj = PyNumber_TrueDivide(a.Get(), CFloat(v).Get());
1114  if ( !tmp_obj ) {
1115  throw CArithmeticError("PyNumber_TrueDivide");
1116  }
1117  return CObject(tmp_obj, eTakeOwnership);
1118 }
1119 inline CObject operator/ (CInt j, const CObject& b)
1120 {
1121  PyObject* tmp_obj = PyNumber_TrueDivide(CInt(j).Get(), b.Get());
1122  if ( !tmp_obj ) {
1123  throw CArithmeticError("PyNumber_TrueDivide");
1124  }
1125  return CObject(tmp_obj, eTakeOwnership);
1126 }
1127 inline CObject operator/ (double v, const CObject& b)
1128 {
1129  PyObject* tmp_obj = PyNumber_TrueDivide(CFloat(v).Get(), b.Get());
1130  if ( !tmp_obj ) {
1131  throw CArithmeticError("PyNumber_TrueDivide");
1132  }
1133  return CObject(tmp_obj, eTakeOwnership);
1134 }
1135 inline CObject operator% (const CObject& a, int j)
1136 {
1137  PyObject* tmp_obj = PyNumber_Remainder(a.Get(), CInt(j).Get());
1138  if ( !tmp_obj ) {
1139  throw CArithmeticError("PyNumber_Remainder");
1140  }
1141  return CObject(tmp_obj, eTakeOwnership);
1142 }
1143 inline CObject operator% (const CObject& a, double v)
1144 {
1145  PyObject* tmp_obj = PyNumber_Remainder(a.Get(), CFloat(v).Get());
1146  if ( !tmp_obj ) {
1147  throw CArithmeticError("PyNumber_Remainder");
1148  }
1149  return CObject(tmp_obj, eTakeOwnership);
1150 }
1151 inline CObject operator% (CInt j, const CObject& b)
1152 {
1153  PyObject* tmp_obj = PyNumber_Remainder(CInt(j).Get(), b.Get());
1154  if ( !tmp_obj ) {
1155  throw CArithmeticError("PyNumber_Remainder");
1156  }
1157  return CObject(tmp_obj, eTakeOwnership);
1158 }
1159 inline CObject operator% (double v, const CObject& b)
1160 {
1161  PyObject* tmp_obj = PyNumber_Remainder(CFloat(v).Get(), b.Get());
1162  if ( !tmp_obj ) {
1163  throw CArithmeticError("PyNumber_Remainder");
1164  }
1165  return CObject(tmp_obj, eTakeOwnership);
1166 }
1167 
1168 } // namespace pythonpp
1169 
1171 
1172 #endif // PYTHONPP_PDT_H
1173 
CFile –.
Definition: ncbifile.hpp:1605
CInt –.
Definition: Int.hpp:66
CObject –.
Definition: ncbiobj.hpp:180
string AsStdSring(void) const
CBinary & operator=(const CObject &obj)
static bool HasExactSameType(PyObject *obj)
CBinary(const string &str)
CBinary(const CObject &obj)
CBinary(const CBinary &obj)
CBinary(PyObject *obj, EOwnership ownership=eAcquireOwnership)
CBinary(const char *str, size_t str_size)
size_t GetSize(void) const
static bool HasSameType(PyObject *obj)
CBool(const CBool &obj)
static bool HasExactSameType(PyObject *obj)
static bool HasSameType(PyObject *obj)
CBool(bool value)
CBool(const CObject &obj)
CBool & operator=(const CObject &obj)
CBool(PyObject *obj, EOwnership ownership=eAcquireOwnership)
CBool(long value)
CComplex(const CObject &obj)
CComplex(PyObject *obj, EOwnership ownership=eAcquireOwnership)
CComplex(double v=0.0, double w=0.0)
static bool HasExactSameType(PyObject *obj)
CComplex(const CComplex &obj)
static bool HasSameType(PyObject *obj)
double GetImag() const
double GetReal() const
CComplex & operator=(const CObject &obj)
CFloat(const CObject &obj)
CFloat(const CFloat &obj)
static bool HasSameType(PyObject *obj)
CFloat(double value=0.0)
CFloat & operator=(const CObject &obj)
CFloat(PyObject *obj, EOwnership ownership=eAcquireOwnership)
static bool HasExactSameType(PyObject *obj)
CInt(const CObject &obj)
static bool HasExactSameType(PyObject *obj)
CInt(int value)
CInt & operator=(const CObject &obj)
CInt(PyObject *obj, EOwnership ownership=eAcquireOwnership)
CInt(const CInt &obj)
static bool HasSameType(PyObject *obj)
CInt(long value=0L)
static bool HasExactSameType(PyObject *obj)
CLong(unsigned long long value)
CLong(long value=0L)
CLong(double value)
CLong(const CObject &obj)
CLong(PyObject *obj, EOwnership ownership=eAcquireOwnership)
CLong(unsigned long value)
static bool HasSameType(PyObject *obj)
CLong(const CLong &obj)
CLong(CInt value)
CLong(long long value)
CLong & operator=(const CObject &obj)
CObject(void)
* Generic operations on objects *‍/
void Set(PyObject *obj, EOwnership ownership=eAcquireOwnership)
Not exception-safe this time.
PyObject * Get(void) const
CString(const char *str, size_t str_size)
CString(const CObject &obj)
CString(const CString &obj)
CString(const char *str)
CString(PyObject *obj, EOwnership ownership=eAcquireOwnership)
string AsStdSring(void) const
static bool HasSameType(PyObject *obj)
size_t GetSize(void) const
CString & operator=(const CObject &obj)
CString(const string &str)
static bool HasExactSameType(PyObject *obj)
The NCBI C++ standard methods for dealing with std::string.
#define bool
Definition: bool.h:34
static const char * str(char *buf, int n)
Definition: stats.c:84
string
Definition: cgiapp.hpp:690
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static CStringUTF8 AsUTF8(const CTempString &src, EEncoding encoding, EValidate validate=eNoValidate)
Convert into UTF8 from a C/C++ string.
Definition: ncbistr.hpp:3883
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
const TYPE & Get(const CNamedParameterList *param)
const struct ncbi::grid::netcache::search::fields::SIZE size
CObject operator+(const CObject &a)
CObject operator%(const CObject &a, const CObject &b)
CObject operator*(const CObject &a, const CObject &b)
CObject operator-(const CObject &a)
CObject operator/(const CObject &a, const CObject &b)
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
unsigned int a
Definition: ncbi_localip.c:102
static BOOL utf
Definition: pcre2grep.c:291
bool g_PythonStrDefToUnicode
Flag showing whether all pythonpp::CString objects should be created as Python unicode strings (TRUE ...
Modified on Fri Sep 20 14:57:39 2024 by modify_doxy.py rev. 669887