From ce6cd0811ea7f9598e745e3afd75416bf6c68719 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 14 Jan 2019 20:50:24 -0500 Subject: [PATCH] C Korlib code de-duplication Moves all PyHSPlasma struct definitions into a private header file. Also adds move constructor and assignment support to PyObjectRef. --- korlib/CMakeLists.txt | 1 + korlib/PyHSPlasma_private.h | 40 +++++++++++++++++++++++++++++++++++++ korlib/bumpmap.cpp | 12 ++--------- korlib/bumpmap.h | 5 ----- korlib/korlib.h | 16 +++++++++++++++ korlib/module.cpp | 4 ++-- korlib/sound.cpp | 17 +--------------- korlib/sound.h | 4 ---- korlib/texture.cpp | 7 +------ korlib/texture.h | 4 ---- 10 files changed, 63 insertions(+), 47 deletions(-) create mode 100644 korlib/PyHSPlasma_private.h diff --git a/korlib/CMakeLists.txt b/korlib/CMakeLists.txt index b0fc144..867a092 100644 --- a/korlib/CMakeLists.txt +++ b/korlib/CMakeLists.txt @@ -20,6 +20,7 @@ endif() set(korlib_HEADERS bumpmap.h korlib.h + PyHSPlasma_private.h sound.h texture.h ) diff --git a/korlib/PyHSPlasma_private.h b/korlib/PyHSPlasma_private.h new file mode 100644 index 0000000..f7b9c21 --- /dev/null +++ b/korlib/PyHSPlasma_private.h @@ -0,0 +1,40 @@ +/* This file is part of Korman. + * + * Korman is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Korman is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Korman. If not, see . + */ + +#ifndef _PYHSPLASMA_PRIVATE_H +#define _PYHSPLASMA_PRIVATE_H + +#include "korlib.h" + +typedef struct { + PyObject_HEAD + class plMipmap* fThis; + bool fPyOwned; +} pyMipmap; + +typedef struct { + PyObject_HEAD + class hsStream* fThis; + bool fPyOwned; +} pyStream; + +typedef struct { + PyObject_HEAD + class plWAVHeader* fThis; + bool fPyOwned; +} pyWAVHeader; + +#endif diff --git a/korlib/bumpmap.cpp b/korlib/bumpmap.cpp index 5f29b78..33cd6be 100644 --- a/korlib/bumpmap.cpp +++ b/korlib/bumpmap.cpp @@ -15,6 +15,8 @@ */ #include "bumpmap.h" +#include "PyHSPlasma_private.h" + #include static uint32_t MakeUInt32Color(float r, float g, float b, float a) { @@ -24,14 +26,6 @@ static uint32_t MakeUInt32Color(float r, float g, float b, float a) { (uint32_t(b * 255.9f) << 0); } -typedef struct { - PyObject_HEAD - plMipmap* fThis; - bool fPyOwned; -} pyMipmap; - -extern "C" { - PyObject* create_bump_LUT(PyObject*, PyObject* args) { static const int kLUTHeight = 16; static const int kLUTWidth = 16; @@ -110,5 +104,3 @@ PyObject* create_bump_LUT(PyObject*, PyObject* args) { Py_RETURN_NONE; } - -}; diff --git a/korlib/bumpmap.h b/korlib/bumpmap.h index 13a2d9a..b950faf 100644 --- a/korlib/bumpmap.h +++ b/korlib/bumpmap.h @@ -19,11 +19,6 @@ #include "korlib.h" -extern "C" { - PyObject* create_bump_LUT(PyObject*, PyObject* args); -}; - #endif // _KORLIB_BUMPMAP_H - diff --git a/korlib/korlib.h b/korlib/korlib.h index 6c42d8f..d608d45 100644 --- a/korlib/korlib.h +++ b/korlib/korlib.h @@ -21,6 +21,7 @@ #include #include +#include #define _pycs(x) const_cast(x) #define arrsize(a) (sizeof(a) / sizeof((a)[0])) @@ -31,14 +32,29 @@ class PyObjectRef { public: PyObjectRef() : m_object() { } PyObjectRef(PyObject* o) : m_object(o) { } + PyObjectRef(const PyObjectRef& copy) = delete; + PyObjectRef(PyObjectRef&& move) { + m_object = move.m_object; + move.m_object = NULL; + } + ~PyObjectRef() { Py_XDECREF(m_object); } operator PyObject*() const { return m_object; } + PyObjectRef& operator =(PyObject* rhs) { Py_XDECREF(m_object); m_object = rhs; return *this; } + + PyObjectRef& operator =(PyObjectRef&& move) { + m_object = move.m_object; + move.m_object = NULL; + return *this; + } + + PyObjectRef& operator =(const PyObjectRef& copy) = delete; }; #endif // _KORLIB_H diff --git a/korlib/module.cpp b/korlib/module.cpp index d183b21..7c20fbf 100644 --- a/korlib/module.cpp +++ b/korlib/module.cpp @@ -21,8 +21,6 @@ // This konstant is compared against that in the Python module to prevent sneaky errors... #define KORLIB_API_VERSION 1 -extern "C" { - static PyMethodDef korlib_Methods[] = { { _pycs("create_bump_LUT"), (PyCFunction)create_bump_LUT, METH_VARARGS, NULL }, { _pycs("inspect_vorbisfile"), (PyCFunction)inspect_vorbisfile, METH_VARARGS, NULL }, @@ -43,6 +41,8 @@ static PyModuleDef korlib_Module = { NULL, /* m_free */ }; +extern "C" { + PyMODINIT_FUNC PyInit__korlib() { PyObject* module = PyModule_Create(&korlib_Module); diff --git a/korlib/sound.cpp b/korlib/sound.cpp index 78fff01..b0889b0 100644 --- a/korlib/sound.cpp +++ b/korlib/sound.cpp @@ -15,6 +15,7 @@ */ #include "sound.h" +#include "PyHSPlasma_private.h" #include #include @@ -22,20 +23,6 @@ static const int BITS_PER_SAMPLE = 16; -extern "C" { - -typedef struct { - PyObject_HEAD - hsStream* fThis; - bool fPyOwned; -} pyStream; - -typedef struct { - PyObject_HEAD - plWAVHeader* fThis; - bool fPyOwned; -} pyWAVHeader; - static size_t _read_stream(void* ptr, size_t size, size_t nmemb, void* datasource) { hsStream* s = static_cast(datasource); // hsStream is a bit overzealous protecting us against overreads, so we need to @@ -115,5 +102,3 @@ PyObject* inspect_vorbisfile(PyObject*, PyObject* args) { // Therefore, we only need to return the size. return PyLong_FromSsize_t(size); } - -}; diff --git a/korlib/sound.h b/korlib/sound.h index c67eb1e..32f366c 100644 --- a/korlib/sound.h +++ b/korlib/sound.h @@ -19,10 +19,6 @@ #include "korlib.h" -extern "C" { - PyObject* inspect_vorbisfile(PyObject*, PyObject* args); -}; - #endif // _KORLIB_SOUND_H diff --git a/korlib/texture.cpp b/korlib/texture.cpp index d7ae9c8..84fd9aa 100644 --- a/korlib/texture.cpp +++ b/korlib/texture.cpp @@ -15,6 +15,7 @@ */ #include "texture.h" +#include "PyHSPlasma_private.h" #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN @@ -205,12 +206,6 @@ typedef struct { bool m_imageInverted; } pyGLTexture; -typedef struct { - PyObject_HEAD - plMipmap* fThis; - bool fPyOwned; -} pyMipmap; - // =============================================================================================== static void pyGLTexture_dealloc(pyGLTexture* self) { diff --git a/korlib/texture.h b/korlib/texture.h index 99dff81..b74ce0b 100644 --- a/korlib/texture.h +++ b/korlib/texture.h @@ -19,13 +19,9 @@ #include "korlib.h" -extern "C" { - PyObject* scale_image(PyObject*, PyObject*, PyObject*); extern PyTypeObject pyGLTexture_Type; PyObject* Init_pyGLTexture_Type(); -}; - #endif // _KORLIB_TEXTURE_H