Use a template for the C array holder class

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@69493 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2011-10-21 08:01:35 +00:00
parent 5ffc5d8681
commit 1f4d1451f2
3 changed files with 62 additions and 48 deletions

View File

@@ -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);

37
src/arrayholder.h Normal file
View File

@@ -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 T>
class wxCArrayHolder
{
public:
wxCArrayHolder() : m_array(NULL) {}
~wxCArrayHolder() {
delete [] m_array;
m_array = NULL;
}
T* m_array;
};
typedef wxCArrayHolder<int> wxIntCArrayHolder;
typedef wxCArrayHolder<wxString> wxStringCArrayHolder;
typedef wxCArrayHolder<wxDash> wxDashCArrayHolder;
//--------------------------------------------------------------------------

View File

@@ -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 <Type>
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<Type>&); // no copies
wxCArrayHolder& operator=(wxCArrayHolder<Type>); // no assignment
};
class wxIntArrayHolder
{
public:
wxIntArrayHolder();
~wxIntArrayHolder();
private:
wxIntArrayHolder(const wxIntArrayHolder&); // no copies
wxIntArrayHolder& operator=(wxIntArrayHolder); // no assignment
};
typedef wxCArrayHolder<int> wxIntCArrayHolder;
typedef wxCArrayHolder<wxString> wxStringCArrayHolder;
typedef wxCArrayHolder<wxDash> wxDashCArrayHolder;
//--------------------------------------------------------------------------