AVA2 cross-platform client parity baseline
This commit is contained in:
@@ -0,0 +1,301 @@
|
||||
#pragma once
|
||||
|
||||
#include <Python.h>
|
||||
#include "PortPythonPointer.h"
|
||||
#include <string>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
|
||||
inline PyObject * Py_BuildNone()
|
||||
{
|
||||
Py_INCREF(Py_None);
|
||||
return Py_None;
|
||||
}
|
||||
|
||||
inline void Py_ReleaseNone()
|
||||
{
|
||||
Py_DECREF(Py_None);
|
||||
}
|
||||
|
||||
inline PyObject * Py_BadArgument()
|
||||
{
|
||||
PyErr_BadArgument();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline PyObject * Py_BuildException(const char * c_pszErr = nullptr, ...)
|
||||
{
|
||||
if (!c_pszErr)
|
||||
PyErr_Clear();
|
||||
else
|
||||
{
|
||||
char szErrBuf[512+1];
|
||||
va_list args;
|
||||
va_start(args, c_pszErr);
|
||||
vsnprintf(szErrBuf, sizeof(szErrBuf), c_pszErr, args);
|
||||
va_end(args);
|
||||
|
||||
PyErr_SetString(PyExc_RuntimeError, szErrBuf);
|
||||
}
|
||||
|
||||
return Py_BuildNone();
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetObject(PyObject* poArgs, int pos, PyObject** ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = poItem;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetLong(PyObject* poArgs, int pos, long* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem || !PyInt_Check(poItem))
|
||||
return false;
|
||||
|
||||
*ret = PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetDouble(PyObject* poArgs, int pos, double* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
if (PyFloat_Check(poItem)) {
|
||||
*ret = PyFloat_AsDouble(poItem);
|
||||
return true;
|
||||
} else if (PyInt_Check(poItem)) {
|
||||
*ret = (double)PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetFloat(PyObject* poArgs, int pos, float* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
if (PyFloat_Check(poItem)) {
|
||||
*ret = (float)PyFloat_AsDouble(poItem);
|
||||
return true;
|
||||
} else if (PyInt_Check(poItem)) {
|
||||
*ret = (float)PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, int* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
if (PyInt_Check(poItem))
|
||||
{
|
||||
*ret = (int)PyInt_AsLong(poItem);
|
||||
return true;
|
||||
}
|
||||
if (PyLong_Check(poItem))
|
||||
{
|
||||
*ret = (int)PyLong_AsUnsignedLongMask(poItem);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret)
|
||||
{
|
||||
int val = 0;
|
||||
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
||||
*ret = (unsigned char)val;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned char* ret)
|
||||
{
|
||||
int val = 0;
|
||||
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
||||
*ret = (unsigned char)val;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetInteger(PyObject* poArgs, int pos, unsigned short* ret)
|
||||
{
|
||||
int val = 0;
|
||||
bool result = PyTuple_GetInteger(poArgs, pos, &val);
|
||||
*ret = (unsigned short)val;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetLongLong(PyObject* poArgs, int pos, long long* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = PyLong_AsLongLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetUnsignedLong(PyObject* poArgs, int pos, unsigned long* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject * poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = PyLong_AsUnsignedLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetUnsignedInteger(PyObject* poArgs, int pos, unsigned int* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = (unsigned int)PyLong_AsUnsignedLong(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetString(PyObject* poArgs, int pos, char** ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem || !PyString_Check(poItem))
|
||||
return false;
|
||||
|
||||
*ret = PyString_AsString(poItem);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyTuple_GetBoolean(PyObject* poArgs, int pos, bool* ret)
|
||||
{
|
||||
if (pos < 0 || pos >= PyTuple_Size(poArgs))
|
||||
return false;
|
||||
|
||||
PyObject* poItem = PyTuple_GetItem(poArgs, pos);
|
||||
if (!poItem)
|
||||
return false;
|
||||
|
||||
*ret = PyLong_AsLong(poItem) ? true : false;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool __PyCallClassMemberFunc_ByCString(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, PyObject** ppoRet)
|
||||
{
|
||||
if (!poClass)
|
||||
{
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
PyObject * poFunc = PyObject_GetAttrString(poClass, (char *)c_szFunc);
|
||||
if (!poFunc)
|
||||
{
|
||||
PyErr_Clear();
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!PyCallable_Check(poFunc))
|
||||
{
|
||||
Py_DECREF(poFunc);
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
PyObject * poRet = PyObject_CallObject(poFunc, poArgs);
|
||||
if (!poRet)
|
||||
{
|
||||
PyErr_Print();
|
||||
Py_DECREF(poFunc);
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
|
||||
*ppoRet = poRet;
|
||||
Py_DECREF(poFunc);
|
||||
Py_XDECREF(poArgs);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs)
|
||||
{
|
||||
PyObject* poRet = nullptr;
|
||||
if (!poClass)
|
||||
{
|
||||
Py_XDECREF(poArgs);
|
||||
return false;
|
||||
}
|
||||
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
||||
return false;
|
||||
Py_DECREF(poRet);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, bool* pisRet)
|
||||
{
|
||||
PyObject* poRet = nullptr;
|
||||
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
||||
return false;
|
||||
|
||||
if (PyNumber_Check(poRet))
|
||||
*pisRet = (PyLong_AsLong(poRet) != 0);
|
||||
else
|
||||
*pisRet = true;
|
||||
|
||||
Py_DECREF(poRet);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool PyCallClassMemberFunc(PyObject* poClass, const char* c_szFunc, PyObject* poArgs, long * plRetValue)
|
||||
{
|
||||
PyObject* poRet = nullptr;
|
||||
if (!__PyCallClassMemberFunc_ByCString(poClass, c_szFunc, poArgs, &poRet))
|
||||
return false;
|
||||
|
||||
if (PyNumber_Check(poRet))
|
||||
{
|
||||
*plRetValue = PyLong_AsLong(poRet);
|
||||
Py_DECREF(poRet);
|
||||
return true;
|
||||
}
|
||||
|
||||
Py_DECREF(poRet);
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user