diff --git a/etg/event.py b/etg/event.py index 267d0bef..d740616c 100644 --- a/etg/event.py +++ b/etg/event.py @@ -327,16 +327,16 @@ def run(): c.find('wxDropFilesEvent.files').array = True c.find('wxDropFilesEvent.files').transfer = True c.find('wxDropFilesEvent.noFiles').arraySize = True - + c.addHeaderCode('#include "arrayholder.h"') c.find('wxDropFilesEvent').setCppCode_sip("""\ if (files) { - wxStringArrayHolder* holder = new wxStringArrayHolder; + wxStringCArrayHolder* holder = new wxStringCArrayHolder; holder->m_array = files; // Make a PyObject for the holder, and transfer its ownership to self. PyObject* pyHolder = sipConvertFromNewType( - (void*)holder, sipType_wxStringArrayHolder, (PyObject*)sipSelf); + (void*)holder, sipType_wxStringCArrayHolder, (PyObject*)sipSelf); Py_DECREF(pyHolder); - sipCpp = new sipwxDropFilesEvent(id,(int)noFiles,holder->m_array); + sipCpp = new sipwxDropFilesEvent(id,(int)noFiles, holder->m_array); } else sipCpp = new sipwxDropFilesEvent(id); diff --git a/src/arrayholder.h b/src/arrayholder.h new file mode 100644 index 00000000..48925af7 --- /dev/null +++ b/src/arrayholder.h @@ -0,0 +1,37 @@ +//-------------------------------------------------------------------------- +// Name: src/arrayholder.h +// Purpose: A simple template class that can hold and delete a pointer +// to a C array +// +// Author: Robin Dunn +// +// Created: 20-Oct-2011 +// Copyright: (c) 2011 by Total Control Software +// Licence: wxWindows license +//-------------------------------------------------------------------------- +// +// Sometimes we need to hold on to a C array and keep it alive, but typical +// SIP code will treat it as a temporary and delete it as soon as the ctor or +// method call is done. This class can hold a pointer to the array and will +// delete the array in its dtor, and by making this class be wrappable we can +// make a PyObject for it that is then owned by some other object, and +// Python's GC will take care of the delaying the cleanup until it's no longer +// needed. + +template +class wxCArrayHolder +{ +public: + wxCArrayHolder() : m_array(NULL) {} + ~wxCArrayHolder() { + delete [] m_array; + m_array = NULL; + } + T* m_array; +}; + +typedef wxCArrayHolder wxIntCArrayHolder; +typedef wxCArrayHolder wxStringCArrayHolder; +typedef wxCArrayHolder wxDashCArrayHolder; + +//-------------------------------------------------------------------------- diff --git a/src/wxpy_utils.sip b/src/wxpy_utils.sip index 2183a2c7..da392dad 100644 --- a/src/wxpy_utils.sip +++ b/src/wxpy_utils.sip @@ -169,54 +169,31 @@ PyObject* wxPyConstructObject(void* ptr, %End //-------------------------------------------------------------------------- - - -%ModuleHeaderCode - // This class provides a way to wrap a C array of wxStrings in a wrapped - // object that can be used to manage the lifetime of the array. - class wxStringArrayHolder - { - public: - wxStringArrayHolder() : m_array(NULL) {} - ~wxStringArrayHolder() { - delete [] m_array; - m_array = NULL; - } - wxString* m_array; - }; - - // And the same for integers - class wxIntArrayHolder - { - public: - wxIntArrayHolder() : m_array(NULL) {} - ~wxIntArrayHolder() { - delete [] m_array; - m_array = NULL; - } - int* m_array; - }; - +// Sometimes we need to hold on to a C array and keep it alive, but typical +// SIP code will treat it as a temporary and delete it as soon as the ctor or +// method call is done. This class can hold a pointer to the array and will +// delete the array in its dtor, and by making this class be wrappable we can +// make a PyObject for it that is then owned by some other object, and +// Python's GC will take care of the delaying the cleanup until it's no longer +// needed. +template +class wxCArrayHolder +{ +%TypeHeaderCode +#include "arrayholder.h" %End -class wxStringArrayHolder -{ public: - wxStringArrayHolder(); - ~wxStringArrayHolder(); + wxCArrayHolder(); + ~wxCArrayHolder(); private: - wxStringArrayHolder(const wxStringArrayHolder&); // no copies - wxStringArrayHolder& operator=(wxStringArrayHolder); // no assignment + wxCArrayHolder(const wxCArrayHolder&); // no copies + wxCArrayHolder& operator=(wxCArrayHolder); // no assignment }; -class wxIntArrayHolder -{ -public: - wxIntArrayHolder(); - ~wxIntArrayHolder(); - -private: - wxIntArrayHolder(const wxIntArrayHolder&); // no copies - wxIntArrayHolder& operator=(wxIntArrayHolder); // no assignment -}; \ No newline at end of file +typedef wxCArrayHolder wxIntCArrayHolder; +typedef wxCArrayHolder wxStringCArrayHolder; +typedef wxCArrayHolder wxDashCArrayHolder; + +//--------------------------------------------------------------------------