From 13d5942cbb5a93023bc54c82241babdfd32520ab Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sat, 8 Jun 2013 23:30:44 +0000 Subject: [PATCH] Add wxArrayPtrWrapperTemplate to wrap the wxArrays using the pointer version of the macro git-svn-id: https://svn.wxwidgets.org/svn/wx/wxPython/Phoenix/trunk@74135 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- etg/richtextbuffer.py | 2 +- etgtools/tweaker_tools.py | 65 ++++++++++++++++++++++++++++++++++++++- wscript | 3 +- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/etg/richtextbuffer.py b/etg/richtextbuffer.py index 262e422c..8ab14ec4 100644 --- a/etg/richtextbuffer.py +++ b/etg/richtextbuffer.py @@ -83,7 +83,7 @@ def run(): # Can this even work? module.addItem( - tools.wxArrayWrapperTemplate('wxRichTextObjectPtrArray', 'wxRichTextObject', module)) + tools.wxArrayPtrWrapperTemplate('wxRichTextObjectPtrArray', 'wxRichTextObject', module)) module.addItem( tools.wxArrayWrapperTemplate('wxRichTextObjectPtrArrayArray', 'wxRichTextObjectPtrArray', module)) diff --git a/etgtools/tweaker_tools.py b/etgtools/tweaker_tools.py index 88e1a050..fc57f011 100644 --- a/etgtools/tweaker_tools.py +++ b/etgtools/tweaker_tools.py @@ -972,6 +972,69 @@ del _{ArrayClass_pyName}___repr__ +# Same as the above, but for use with WX_DEFINE_ARRAY_PTR +def wxArrayPtrWrapperTemplate(ArrayClass, ItemClass, module): + moduleName = module.module + ArrayClass_pyName = removeWxPrefix(ArrayClass) + + # *** TODO: This can probably be done in a way that is not SIP-specfic. + # Try creating extractor objects from scratch and attach cppMethods to + # them as needed, etc.. + + return extractors.WigCode('''\ +class {ArrayClass} +{{ +public: + SIP_SSIZE_T __len__(); + %MethodCode + sipRes = sipCpp->GetCount(); + %End + + {ItemClass}* __getitem__(size_t index); + %MethodCode + if (index < sipCpp->GetCount()) {{ + sipRes = sipCpp->Item(index); + }} + else {{ + wxPyErr_SetString(PyExc_IndexError, "sequence index out of range"); + sipError = sipErrorFail; + }} + %End + + int __contains__({ItemClass}* obj); + %MethodCode + int idx = sipCpp->Index(obj, false); + sipRes = idx != wxNOT_FOUND; + %End + + void append({ItemClass}* obj); + %MethodCode + sipCpp->Add(obj); + %End + + // TODO: add support for index(value, [start, [stop]]) + int index({ItemClass}* obj); + %MethodCode + int idx = sipCpp->Index(obj, false); + if (idx == wxNOT_FOUND) {{ + sipError = sipErrorFail; + wxPyErr_SetString(PyExc_ValueError, + "sequence.index(x): x not in sequence"); + }} + sipRes = idx; + %End +}}; + +%Extract(id=pycode{moduleName}) +def _{ArrayClass_pyName}___repr__(self): + return "{ArrayClass_pyName}: " + repr(list(self)) +{ArrayClass_pyName}.__repr__ = _{ArrayClass_pyName}___repr__ +del _{ArrayClass_pyName}___repr__ +%End +'''.format(**locals())) + + + def ObjArrayHelperTemplate(objType, sipType, errmsg): """ @@ -985,7 +1048,7 @@ def ObjArrayHelperTemplate(objType, sipType, errmsg): (like a wxList or wxArray) being used. If there is an overloaded method that uses one of those types then the C array overload should just be ignored. But for those cases where the C array is the only option then this - helper can be used to make the array. + helper can be used to make the array from a sequence. """ cppCode = """\ diff --git a/wscript b/wscript index b0336175..79a60b1b 100644 --- a/wscript +++ b/wscript @@ -196,8 +196,9 @@ def configure(conf): args='--cxxflags --libs xrc,xml,core,net' + rpath, uselib_store='WXXRC', mandatory=True) + libname = '' if cfg.MONOLITHIC else 'richtext,' # workaround bug in wx-config conf.check_cfg(path=conf.options.wx_config, package='', - args='--cxxflags --libs richtext,core,net' + rpath, + args='--cxxflags --libs %score,net' % libname + rpath, uselib_store='WXRICHTEXT', mandatory=True) # ** Add code for new modules here