|
From: Paweł J. <p.j...@gu...> - 2012-11-06 09:49:10
|
Thank you Alex for your response. I've found a solution, far from neat
but working. Just for the record:
1. A c-library provides
// getter returns 1 on success, 0 on failure
int get_attibute(void *S, double *value);
// setter is typical
void set_attribute(void *S, double value);
'S' states for some structure, its specific type doesn't matter (here
void pointer).
2. The goal is to have a class 'S' in the target language, and instances
of that class having attribute 'attribute'. If the attribute has been
set, it should be a number, otherwise it should be nothing (None, null,
nil, ...).
3. The solution below works ok, but is I'm not quite happy with that:
- it uses python-specific, so works only for python
- it uses symbols 'arg1' and 'result' explicitly in typemap, which is
not SWIGy, i guess
- it poduces some redundancy (doesn't pain that much)
/* interface file s.i */
%module s
%{
#define S void
#define S_attribute_get(self) (0.0)
#define S_attribute_set(self, value) set_attribute(self, value)
%}
%typemap(out, noblock=1) double attribute {
$result = (get_attribute(arg1, &result) ?
SWIG_From_double(result) :
SWIG_Py_Void());
}
typedef struct { } S;
%extend S { double attribute; }
/* part of _wrap_S_attribute_get function in generated s_wrap.c */
...
// void pointer handled pretty well by SWIG
arg1 = (S *)(argp1);
// redundant initialization of result to 0.0
result = (double)S_attribute_get(arg1);
// the actual 'get' operation
resultobj = (get_attribute(arg1, &result) ?
SWIG_From_double(result) :
SWIG_Py_Void());
// now resultobj is PyFloat or PyNone
return resultobj;
Cheers,
--
Pawe/l Jackowski
p.j...@gu...
|