Attribute implementation using JOHN E LENZ ideas.
The following macros convert a pair of set/get methods
into a "native" python attribute.
Use %attribute when you have a pair of get/set methods
%attribute(A, int, a, get_a, set_a);
If you don't provide a 'set' method, a 'read-only' attribute
is generated, ie, like in:
%attribute(A, int, c, get_c);
Use %attribute_ref when you have const/non-const reference
%attribute_ref(A, int, b);
%attribute_ref(class, type, refname, attr);
if the internal C++ reference methods have a different name from the
Then you can use the instances like:
print x.a # calls A::get_a
print x.b # calls A::b() const
NOTE: remember that if the type contains commas, such as
'std::pair<int,int>', you need to use the macro like:
%attribute_ref(A, SWIG_arg(std::pair<int,int>), pval);
where SWIG_arg() 'normalize' the type to be understood as a single
argument, otherwise the macro will get confused (see the 'cpp'
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
template <class C> inline
type Wrap ##_## attr ## _get(const C* _t)
template <class C> inline
void Wrap ##_## attr ## _set(C* _t, type _val)
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
#define Wrap ##_## attr ## _get(_t) getcode
#define Wrap ##_## attr ## _set(_t, _val) setcode
// Internal versions, need Wrap name
%define %attribute_T(Class, Wrap, type, attr, get, ...)
%ignore Class::__VA_ARGS__;
%_attribute(SWIG_arg(Class), Wrap, SWIG_arg(type),
attr, _t->get(), _t->__VA_ARGS__(_val))
%_attribute(SWIG_arg(Class), Wrap, SWIG_arg(type),
fprintf(stderr,"'attr' is a read-only attribute"))
%define %_attribute_ref_T(Class, Wrap, type, refname, attr)
%ignore Class::refname();
%ignore Class::refname() const;
%_attribute(SWIG_arg(Class), Wrap, SWIG_arg(type),
attr, _t->refname(), _t->refname() = _val)
%define %attribute_ref_T(Class, Wrap, type, refname, ...)
%_attribute_ref_T(SWIG_arg(Class), Wrap, SWIG_arg(type), refname, refname)
%_attribute_ref_T(SWIG_arg(Class), Wrap, SWIG_arg(type), refname, __VA_ARGS__)
%define %attribute(Class, type, attr, get, ...)
%attribute_T(SWIG_arg(Class), SWIG_Mangle(Class), SWIG_arg(type), attr, get, __VA_ARGS__)
%define %attribute_ref(Class, type, refname, ...)
%attribute_ref_T(SWIG_arg(Class), SWIG_Mangle(Class), SWIG_arg(type), refname, __VA_ARGS__)