/*
    Copyright (c) 2007 SIEGE Development Team
    All rights reserved.
    
    This file is part of libSIEGE.
    
    This software is copyrighted work licensed under the terms of the
    2-clause BSD license. Please consult the file "license.txt" for
    details.
    
    If you did not recieve the file with this program, please email
    Tim Chas <darkuranium@gmail.com>.
*/

#ifndef __PYS_COMMON_H__
#define __PYS_COMMON_H__

#include <Python.h>
#include <structmember.h>
#include <siege/siege.h>

#if (PY_MAJOR_VERSION == 3) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 7)
#define PYS3
#else
#define PYS2
#endif // (PY_MAJOR_VERSION == 3) || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 7)

#ifdef PYS3
#define PYS_CAPSULE(var, type, destroy) PyCapsule_New((var), (type), (destroy))
#define PYS_CAPSULE_GET(var, type, from) do { var = PyCapsule_GetPointer(from, (type)); } while(0)
#define PYS_DESTROY_ARGS PyObject* self
#define PYS_DESTROY_GET(var, type) do { var = PyCapsule_GetPointer(self, (type)); } while(0)
#else
#define PYS_CAPSULE(var, type, destroy) PyCObject_FromVoidPtrAndDesc((var), (type), (destroy))
#define PYS_CAPSULE_GET(var, type, from) do { if(strcmp(PyCObject_GetDesc((from)), (type)) != 0) { var = NULL; break; } var = PyCObject_AsVoidPtr((from)); } while(0)
#define PYS_DESTROY_ARGS void* self, void* desc
#define PYS_DESTROY_GET(var, type) do { if(strcmp(desc, (type)) != 0) return; var = self; } while(0)
#endif // PYS3

#ifndef PyModule_AddIntMacro
#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant((m), #c, (c))
#endif // PyModule_AddIntMacro

void* pysHandleGet(PyObject* self, char* name, char* type);
void* pysHandleGetAttach(PyObject* from, PyObject* to, char* name, char* type);

#endif // __PY_COMMON_H__
