\doxysection{Py.\+hpp}
\hypertarget{Py_8hpp_source}{}\label{Py_8hpp_source}\index{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Py.hpp@{/home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Py.hpp}}

\begin{DoxyCode}{0}
\DoxyCodeLine{00001\ }
\DoxyCodeLine{00002\ \textcolor{preprocessor}{\#ifndef\ GYM\_PCB\_PY\_H}}
\DoxyCodeLine{00003\ \textcolor{preprocessor}{\#define\ GYM\_PCB\_PY\_H}}
\DoxyCodeLine{00004\ }
\DoxyCodeLine{00005\ \textcolor{preprocessor}{\#define\ PY\_ONLY(args...)\ args}}
\DoxyCodeLine{00006\ }
\DoxyCodeLine{00007\ \textcolor{preprocessor}{\#define\ NPY\_NO\_DEPRECATED\_API\ NPY\_1\_7\_API\_VERSION}}
\DoxyCodeLine{00008\ \textcolor{preprocessor}{\#define\ NO\_IMPORT\_ARRAY}}
\DoxyCodeLine{00009\ \textcolor{preprocessor}{\#define\ PY\_ARRAY\_UNIQUE\_SYMBOL\ \_pcbenv2\_module\_PyArray\_API}}
\DoxyCodeLine{00010\ \textcolor{preprocessor}{\#include\ <numpy/ndarrayobject.h>}}
\DoxyCodeLine{00011\ }
\DoxyCodeLine{00012\ \textcolor{preprocessor}{\#include\ "{}Geometry.hpp"{}}}
\DoxyCodeLine{00013\ }
\DoxyCodeLine{00014\ \textcolor{keyword}{namespace\ }py}
\DoxyCodeLine{00015\ \{}
\DoxyCodeLine{00016\ }
\DoxyCodeLine{00018\ }
\DoxyCodeLine{00019\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ Ty\ =\ PyObject>}
\DoxyCodeLine{00020\ \textcolor{keyword}{inline}\ Ty\ *ValueError(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00021\ \{}
\DoxyCodeLine{00022\ \ \ \ \ PyErr\_SetString(PyExc\_ValueError,\ s);}
\DoxyCodeLine{00023\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00024\ \}}
\DoxyCodeLine{00025\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ Ty\ =\ PyObject>}
\DoxyCodeLine{00026\ \textcolor{keyword}{inline}\ Ty\ *MemoryError(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00027\ \{}
\DoxyCodeLine{00028\ \ \ \ \ PyErr\_SetString(PyExc\_MemoryError,\ s);}
\DoxyCodeLine{00029\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00030\ \}}
\DoxyCodeLine{00031\ \textcolor{keyword}{template}<\textcolor{keyword}{typename}\ Ty\ =\ PyObject>}
\DoxyCodeLine{00032\ \textcolor{keyword}{inline}\ Ty\ *Exception(PyObject\ *exc,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00033\ \{}
\DoxyCodeLine{00034\ \ \ \ \ PyErr\_SetString(exc,\ s);}
\DoxyCodeLine{00035\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00036\ \}}
\DoxyCodeLine{00037\ \textcolor{keyword}{inline}\ PyObject\ *Exception(\textcolor{keyword}{const}\ std::exception\ \&e)}
\DoxyCodeLine{00038\ \{}
\DoxyCodeLine{00039\ \ \ \ \ PyErr\_SetString(PyExc\_Exception,\ e.what());}
\DoxyCodeLine{00040\ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{00041\ \}}
\DoxyCodeLine{00042\ }
\DoxyCodeLine{00043\ }
\DoxyCodeLine{00045\ }
\DoxyCodeLine{00046\ \textcolor{keyword}{inline}\ PyObject\ *String(\textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *s)}
\DoxyCodeLine{00047\ \{}
\DoxyCodeLine{00048\ \ \ \ \ \textcolor{keywordflow}{return}\ PyUnicode\_FromString(s);}
\DoxyCodeLine{00049\ \}}
\DoxyCodeLine{00050\ \textcolor{keyword}{inline}\ PyObject\ *String(\textcolor{keyword}{const}\ std::string\ \&s)}
\DoxyCodeLine{00051\ \{}
\DoxyCodeLine{00052\ \ \ \ \ \textcolor{keywordflow}{return}\ PyUnicode\_FromStringAndSize(s.c\_str(),\ s.size());}
\DoxyCodeLine{00053\ \}}
\DoxyCodeLine{00054\ }
\DoxyCodeLine{00055\ }
\DoxyCodeLine{00057\ }
\DoxyCodeLine{00058\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{bool}\ TupleN\_Check(PyObject\ *py,\ uint\ n)}
\DoxyCodeLine{00059\ \{}
\DoxyCodeLine{00060\ \ \ \ \ \textcolor{keywordflow}{return}\ PyTuple\_Check(py)\ \&\&\ PyTuple\_Size(py)\ ==\ n;}
\DoxyCodeLine{00061\ \}}
\DoxyCodeLine{00062\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{bool}\ TupleMin\_Check(PyObject\ *py,\ uint\ n)}
\DoxyCodeLine{00063\ \{}
\DoxyCodeLine{00064\ \ \ \ \ \textcolor{keywordflow}{return}\ PyTuple\_Check(py)\ \&\&\ PyTuple\_Size(py)\ >=\ n;}
\DoxyCodeLine{00065\ \}}
\DoxyCodeLine{00066\ }
\DoxyCodeLine{00067\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{double}\ Number\_AsDouble(PyObject\ *py)}
\DoxyCodeLine{00068\ \{}
\DoxyCodeLine{00069\ \ \ \ \ \textcolor{keywordflow}{if}\ (PyFloat\_Check(py))}
\DoxyCodeLine{00070\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ PyFloat\_AsDouble(py);}
\DoxyCodeLine{00071\ \ \ \ \ \textcolor{keyword}{auto}\ F\ =\ PyNumber\_Float(py);}
\DoxyCodeLine{00072\ \ \ \ \ assert(F);}
\DoxyCodeLine{00073\ \ \ \ \ \textcolor{keywordflow}{if}\ (!F)}
\DoxyCodeLine{00074\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ std::numeric\_limits<double>::quiet\_NaN();}
\DoxyCodeLine{00075\ \ \ \ \ \textcolor{keyword}{auto}\ d\ =\ PyFloat\_AsDouble(F);}
\DoxyCodeLine{00076\ \ \ \ \ Py\_DECREF(F);}
\DoxyCodeLine{00077\ \ \ \ \ \textcolor{keywordflow}{return}\ d;}
\DoxyCodeLine{00078\ \}}
\DoxyCodeLine{00079\ }
\DoxyCodeLine{00080\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{bool}\ String\_Check(PyObject\ *py)}
\DoxyCodeLine{00081\ \{}
\DoxyCodeLine{00082\ \ \ \ \ \textcolor{keywordflow}{return}\ py\ \&\&\ PyUnicode\_Check(py);}
\DoxyCodeLine{00083\ \}}
\DoxyCodeLine{00084\ \textcolor{keyword}{inline}\ std::string\ String\_AsStdString(PyObject\ *py)}
\DoxyCodeLine{00085\ \{}
\DoxyCodeLine{00086\ \textcolor{preprocessor}{\#if\ PY\_VERSION\_HEX\ >=\ 0x03030000}}
\DoxyCodeLine{00087\ \ \ \ \ \textcolor{keywordflow}{return}\ std::string(PyUnicode\_AsUTF8(py));}
\DoxyCodeLine{00088\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00089\ \textcolor{preprocessor}{\#error\ "{}Python\ version\ <\ 3.3\ not\ supported,\ go\ upgrade!"{}}}
\DoxyCodeLine{00090\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00091\ \}}
\DoxyCodeLine{00092\ \textcolor{keyword}{inline}\ std::string\_view\ String\_AsStringView(PyObject\ *py)}
\DoxyCodeLine{00093\ \{}
\DoxyCodeLine{00094\ \textcolor{preprocessor}{\#if\ PY\_VERSION\_HEX\ >=\ 0x03030000}}
\DoxyCodeLine{00095\ \ \ \ \ Py\_ssize\_t\ size;}
\DoxyCodeLine{00096\ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *ucs\ =\ PyUnicode\_AsUTF8AndSize(py,\ \&size);}
\DoxyCodeLine{00097\ \ \ \ \ \textcolor{keywordflow}{return}\ std::string\_view(ucs,\ size);}
\DoxyCodeLine{00098\ \textcolor{preprocessor}{\#else}}
\DoxyCodeLine{00099\ \textcolor{preprocessor}{\#error\ "{}Python\ version\ <\ 3.3\ not\ supported,\ go\ upgrade!"{}}}
\DoxyCodeLine{00100\ \textcolor{preprocessor}{\#endif}}
\DoxyCodeLine{00101\ \}}
\DoxyCodeLine{00102\ }
\DoxyCodeLine{00103\ \textcolor{keyword}{inline}\ std::string\ String\_AsStdString(PyObject\ *py,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *error)}
\DoxyCodeLine{00104\ \{}
\DoxyCodeLine{00105\ \ \ \ \ \textcolor{keywordflow}{if}\ (!String\_Check(py))}
\DoxyCodeLine{00106\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{throw}\ std::invalid\_argument(error);}
\DoxyCodeLine{00107\ \ \ \ \ \textcolor{keywordflow}{return}\ String\_AsStdString(py);}
\DoxyCodeLine{00108\ \}}
\DoxyCodeLine{00109\ }
\DoxyCodeLine{00110\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{void}\ Dict\_StealItemString(PyObject\ *dict,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *key,\ PyObject\ *v)}
\DoxyCodeLine{00111\ \{}
\DoxyCodeLine{00112\ \ \ \ \ PyDict\_SetItemString(dict,\ key,\ v);}
\DoxyCodeLine{00113\ \ \ \ \ Py\_DECREF(v);}
\DoxyCodeLine{00114\ \}}
\DoxyCodeLine{00115\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{void}\ Dict\_StealItem(PyObject\ *dict,\ PyObject\ *key,\ PyObject\ *v)}
\DoxyCodeLine{00116\ \{}
\DoxyCodeLine{00117\ \ \ \ \ PyDict\_SetItem(dict,\ key,\ v);}
\DoxyCodeLine{00118\ \ \ \ \ Py\_DECREF(v);}
\DoxyCodeLine{00119\ \}}
\DoxyCodeLine{00120\ }
\DoxyCodeLine{00121\ \textcolor{keyword}{inline}\ \textcolor{keywordtype}{void}\ DECREF\_safe(PyObject\ *v)}
\DoxyCodeLine{00122\ \{}
\DoxyCodeLine{00123\ \ \ \ \ \textcolor{keywordflow}{if}\ (v)}
\DoxyCodeLine{00124\ \ \ \ \ \ \ \ \ Py\_DECREF(v);}
\DoxyCodeLine{00125\ \}}
\DoxyCodeLine{00126\ }
\DoxyCodeLine{00127\ \textcolor{keyword}{inline}\ PyObject\ *None()}
\DoxyCodeLine{00128\ \{}
\DoxyCodeLine{00129\ \ \ \ \ Py\_INCREF(Py\_None);}
\DoxyCodeLine{00130\ \ \ \ \ \textcolor{keywordflow}{return}\ Py\_None;}
\DoxyCodeLine{00131\ \}}
\DoxyCodeLine{00132\ }
\DoxyCodeLine{00133\ \textcolor{keyword}{inline}\ PyObject\ *NewRef(PyObject\ *py)}
\DoxyCodeLine{00134\ \{}
\DoxyCodeLine{00135\ \ \ \ \ Py\_INCREF(py);}
\DoxyCodeLine{00136\ \ \ \ \ \textcolor{keywordflow}{return}\ py;}
\DoxyCodeLine{00137\ \}}
\DoxyCodeLine{00138\ }
\DoxyCodeLine{00139\ }
\DoxyCodeLine{00140\ \textcolor{comment}{//\ SAFETY}}
\DoxyCodeLine{00141\ }
\DoxyCodeLine{00143\ \textcolor{keyword}{class\ }GIL\_guard}
\DoxyCodeLine{00144\ \{}
\DoxyCodeLine{00145\ \textcolor{keyword}{public}:}
\DoxyCodeLine{00146\ \ \ \ \ GIL\_guard()}
\DoxyCodeLine{00147\ \ \ \ \ \{}
\DoxyCodeLine{00148\ \ \ \ \ \ \ \ \ state\ =\ PyGILState\_Ensure();}
\DoxyCodeLine{00149\ \ \ \ \ \}}
\DoxyCodeLine{00150\ \ \ \ \ \string~GIL\_guard()}
\DoxyCodeLine{00151\ \ \ \ \ \{}
\DoxyCodeLine{00152\ \ \ \ \ \ \ \ \ PyGILState\_Release(state);}
\DoxyCodeLine{00153\ \ \ \ \ \}}
\DoxyCodeLine{00154\ \ \ \ \ GIL\_guard(GIL\_guard\&\&\ moved)\ noexcept\ :\ state(moved.state)}
\DoxyCodeLine{00155\ \ \ \ \ \{}
\DoxyCodeLine{00156\ \ \ \ \ \ \ \ \ moved.state\ =\ PyGILState\_UNLOCKED;}
\DoxyCodeLine{00157\ \ \ \ \ \}}
\DoxyCodeLine{00158\ \ \ \ \ GIL\_guard\&\ operator=(GIL\_guard\&\&\ moved)\ \textcolor{keyword}{noexcept}}
\DoxyCodeLine{00159\ \ \ \ \ \{}
\DoxyCodeLine{00160\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\textcolor{keyword}{this}\ !=\ \&moved)\ \{}
\DoxyCodeLine{00161\ \ \ \ \ \ \ \ \ \ \ \ \ PyGILState\_Release(state);}
\DoxyCodeLine{00162\ \ \ \ \ \ \ \ \ \ \ \ \ state\ =\ moved.state;}
\DoxyCodeLine{00163\ \ \ \ \ \ \ \ \ \ \ \ \ moved.state\ =\ PyGILState\_UNLOCKED;}
\DoxyCodeLine{00164\ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{00165\ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ *\textcolor{keyword}{this};}
\DoxyCodeLine{00166\ \ \ \ \ \}}
\DoxyCodeLine{00167\ \ \ \ \ GIL\_guard(\textcolor{keyword}{const}\ GIL\_guard\&)\ =\ \textcolor{keyword}{delete};}
\DoxyCodeLine{00168\ }
\DoxyCodeLine{00169\ \textcolor{keyword}{private}:}
\DoxyCodeLine{00170\ \ \ \ \ PyGILState\_STATE\ state;}
\DoxyCodeLine{00171\ \};}
\DoxyCodeLine{00172\ }
\DoxyCodeLine{00173\ \}\ \textcolor{comment}{//\ namespace\ py}}
\DoxyCodeLine{00174\ }
\DoxyCodeLine{00175\ \textcolor{preprocessor}{\#include\ "{}PyObject.hpp"{}}}
\DoxyCodeLine{00176\ }
\DoxyCodeLine{00177\ \textcolor{preprocessor}{\#endif\ }\textcolor{comment}{//\ GYM\_PCB\_PY\_H}}

\end{DoxyCode}
