1 /////////////////////////////////////////////////////////////////////////////
7 // Created: 13-Sept-2003
9 // Copyright: (c) 2003 by Total Control Software
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
16 //---------------------------------------------------------------------------
18 #ifndef wxPyUSE_EXPORT
19 // Helper functions for dealing with SWIG objects and such. These are
20 // located here so they know about the SWIG types and functions declared
21 // in the wrapper code.
23 #include <wx/hashmap.h>
24 WX_DECLARE_STRING_HASH_MAP( swig_type_info*, wxPyTypeInfoHashMap );
27 // Maintains a hashmap of className to swig_type_info pointers. Given the
28 // name of a class either looks up the type info in the cache, or scans the
29 // SWIG tables for it.
30 extern PyObject* wxPyPtrTypeMap;
32 swig_type_info* wxPyFindSwigType(const wxChar* className) {
34 static wxPyTypeInfoHashMap* typeInfoCache = NULL;
36 if (typeInfoCache == NULL)
37 typeInfoCache = new wxPyTypeInfoHashMap;
39 wxString name(className);
40 swig_type_info* swigType = (*typeInfoCache)[name];
43 // it wasn't in the cache, so look it up from SWIG
44 name.Append(wxT(" *"));
45 swigType = SWIG_TypeQuery(name.mb_str());
47 // if it still wasn't found, try looking for a mapped name
52 if ((item = PyDict_GetItemString(wxPyPtrTypeMap,
53 (char*)(const char*)name.mbc_str())) != NULL) {
54 name = wxString(PyString_AsString(item), *wxConvCurrent);
55 name.Append(wxT(" *"));
56 swigType = SWIG_TypeQuery(name.mb_str());
60 // and add it to the map if found
61 (*typeInfoCache)[className] = swigType;
68 // Check if a class name is a type known to SWIG
69 bool wxPyCheckSwigType(const wxChar* className) {
71 swig_type_info* swigType = wxPyFindSwigType(className);
72 return swigType != NULL;
76 // Given a pointer to a C++ object and a class name, construct a Python proxy
78 PyObject* wxPyConstructObject(void* ptr,
79 const wxChar* className,
82 swig_type_info* swigType = wxPyFindSwigType(className);
83 wxCHECK_MSG(swigType != NULL, NULL, wxT("Unknown type in wxPyConstructObject"));
85 return SWIG_Python_NewPointerObj(ptr, swigType, setThisOwn);
89 // Extract a pointer to the wrapped C++ object from a Python proxy object.
90 // Ensures that the proxy object is of the specified (or derived) type. If
91 // not able to perform the conversion then a Python exception is set and the
92 // error should be handled properly in the caller. Returns True on success.
93 bool wxPyConvertSwigPtr(PyObject* obj, void **ptr,
94 const wxChar* className) {
96 swig_type_info* swigType = wxPyFindSwigType(className);
97 wxCHECK_MSG(swigType != NULL, false, wxT("Unknown type in wxPyConvertSwigPtr"));
99 return SWIG_Python_ConvertPtr(obj, ptr, swigType, SWIG_POINTER_EXCEPTION) != -1;
103 // Make a SWIGified pointer object suitable for a .this attribute
104 PyObject* wxPyMakeSwigPtr(void* ptr, const wxChar* className) {
106 PyObject* robj = NULL;
108 swig_type_info* swigType = wxPyFindSwigType(className);
109 wxCHECK_MSG(swigType != NULL, NULL, wxT("Unknown type in wxPyMakeSwigPtr"));
111 #if SWIG_VERSION < 0x010328
112 #ifdef SWIG_COBJECT_TYPES
113 robj = PySwigObject_FromVoidPtrAndDesc((void *) ptr, (char *)swigType->name);
117 robj = SWIG_PackVoidPtr(result, ptr, swigType->name, sizeof(result)) ?
118 PyString_FromString(result) : 0;
121 #else // SWIG_VERSION >= 1.3.28
122 robj = PySwigObject_New(ptr, swigType, 0);
128 // Python's PyInstance_Check does not return True for instances of new-style
129 // classes. This should get close enough for both new and old classes but I
130 // should re-evaluate the need for doing instance checks...
131 bool wxPyInstance_Check(PyObject* obj) {
132 return PyObject_HasAttrString(obj, "__class__") != 0;
137 // This one checks if the object is an instance of a SWIG proxy class (it has
138 // a .this attribute, and the .this attribute is a PySwigObject.)
139 bool wxPySwigInstance_Check(PyObject* obj) {
140 static PyObject* this_str = NULL;
141 if (this_str == NULL)
142 this_str = PyString_FromString("this");
144 PyObject* this_attr = PyObject_GetAttr(obj, this_str);
146 bool retval = (PySwigObject_Check(this_attr) != 0);
147 Py_DECREF(this_attr);
157 // Export a C API in a struct. Other modules will be able to load this from
158 // the wx._core_ module and will then have safe access to these functions,
159 // even if they are located in another shared library.
160 static wxPyCoreAPI API = {
167 wxPyBeginAllowThreads,
169 wxPyBeginBlockThreads,
183 wxBitmap_LIST_helper,
184 wxString_LIST_helper,
185 wxAcceleratorEntry_LIST_helper,
194 wxPySimple_typecheck,
197 wxPyCBH_setCallbackInfo,
198 wxPyCBH_findCallback,
199 wxPyCBH_callCallback,
200 wxPyCBH_callCallbackObj,
208 wxArrayString2PyList_helper,
209 wxArrayInt2PyList_helper,
213 wxPyOORClientData_dtor,
215 wxPyCBInputStream_create,
216 wxPyCBInputStream_copy,
219 wxPySwigInstance_Check,
232 #ifndef wxPyUSE_EXPORT
233 // Make our API structure a CObject so other modules can import it
235 PyObject* cobj = PyCObject_FromVoidPtr(&API, NULL);
236 PyDict_SetItemString(d,"_wxPyCoreAPI", cobj);
241 //---------------------------------------------------------------------------