17
17
%attribute(A, int, a, get_a, set_a);
25
25
If you don't provide a 'set' method, a 'read-only' attribute
26
26
is generated, ie, like in:
28
28
%attribute(A, int, c, get_c);
31
Use %attribute_ref when you have const/non-const reference
30
Use %attributeref when you have const/non-const reference
32
31
access methods, like in:
34
%attribute_ref(A, int, b);
42
%attribute_ref(B, int, c);
33
%attributeref(A, int, b);
41
%attributeref(B, int, c);
51
%attribute_ref(class, type, refname, attr);
50
%attributeref(Class, AttributeType, AttributeName, AccessorMethod)
53
52
if the internal C++ reference methods have a different name from the
56
Then you can use the instances like:
59
x.a = 3 # calls A::set_a
60
print x.a # calls A::get_a
62
x.b = 3 # calls A::b()
53
attribute you want, so
55
%attributeref(B, int, d, c);
57
is the same as the last example, but instead of the attribute 'c' being
58
called 'c', it is called 'd'.
60
Now you can use the attributes like so:
63
x.a = 3 # calls A::set_a
64
print x.a # calls A::get_a
66
x.b = 3 # calls A::b()
63
67
print x.b # calls A::b() const
65
NOTE: remember that if the type contains commas, such as
66
'std::pair<int,int>', you need to use the macro like:
68
%attribute_ref(A, %arg(std::pair<int,int>), pval);
70
where %arg() 'normalize' the type to be understood as a single
71
argument, otherwise the macro will get confused (see the 'cpp'
69
Use %attribute2 instead of %attribute to indicate that reference-pointer
70
translation is required. You use %attribute2 instead of %attribute in
73
%attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo);
81
MyFoo& GetFoo() { return foo; }
82
void SetFoo(const MyFoo& other) { foo = other; }
86
Here, the data type of the property is a wrapped type (MyFoo) and on the
87
C++ side it is passed by reference. The problem is that the SWIG wrapper will
88
pass around a pointer (MyFoo *) which is not compatible with the reference
89
type of the accessors (MyFoo &). Therefore, if you use %attribute, you'll get
90
an error from your C/C++ compiler. %attribute2 translates between a pointer
91
and a reference to eliminate the error. In case you're confused, let's make it
92
simple: just use %attribute at first, but if the C/C++ compiler gives an error
93
while compiling the wrapper, try %attribute2 instead.
95
NOTE: remember that if the type contains commas, such as 'std::pair<int,int>',
96
you need to use the macro like:
98
%attributeref(A, %arg(std::pair<int,int>), pval);
100
where %arg() 'normalizes' the type to be understood as a single
101
argument, otherwise the macro will get confused by the comma.
76
#ifndef %attribute_exception
77
#define %attribute_exception(code,msg) SWIG_exception_fail(code,msg)
81
// Define SWIG_ATTRIBUTE_TEMPLATE if you want to use templates.
105
// Define SWIG_ATTRIBUTE_TEMPLATE if you want to use templates instead of macros for the C++ get and set wrapper methods
106
// Does not always generate compileable code, use at your peril!
83
108
//#define SWIG_ATTRIBUTE_TEMPLATE
86
#if defined(__cplusplus) && defined(SWIG_ATTRIBUTE_TEMPLATE)
87
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
92
template <class C> inline
93
type Wrap ##_## attr ## _get(const C* _t)
96
template <class C> inline
97
type Wrap ##_## attr ## _get(C* _t)
100
template <class C> inline
101
void Wrap ##_## attr ## _set(C* _t, type _val)
108
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
113
#define Wrap ##_## attr ## _get(_t) getcode
114
#define Wrap ##_## attr ## _set(_t, _val) setcode
119
// Internal versions, need Wrap name
122
%define %attribute_T(Class, Wrap, type, attr, get, set...)
126
%_attribute(%arg(Class), Wrap, %arg(type),
127
attr, _t->get(), _t->set(_val))
129
%_attribute(%arg(Class), Wrap, %arg(type),
131
%attribute_exception(SWIG_AttributeError,"read-only 'attr' attribute");)
136
%define %_attribute_ref_T(Class, Wrap, type, refname, attr)
137
%ignore Class::refname();
138
%ignore Class::refname() const;
139
%_attribute(%arg(Class), Wrap, %arg(type),
140
attr, _t->refname(), _t->refname() = _val)
143
%define %attribute_ref_T(Class, Wrap, type, refname, attr...)
145
%_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, refname)
147
%_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, attr)
155
%define %attribute(Class, type, attr, get, set...)
156
%attribute_T(%arg(Class), %mangle(Class), %arg(type), attr, get, set)
159
%define %attribute_ref(Class, type, refname, _Type...)
160
%attribute_ref_T(%arg(Class), %mangle(Class), %arg(type), refname, _Type)
110
%define %attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, GetMethodCall, SetMethodCall)
111
%ignore Class::GetMethod();
112
%ignore Class::GetMethod() const;
113
#if #SetMethod != #AttributeName
114
%ignore Class::SetMethod;
117
AttributeType AttributeName;
119
#if defined(__cplusplus) && defined(SWIG_ATTRIBUTE_TEMPLATE)
121
template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(const C* self_) {
122
return GetMethodCall;
124
template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(C* self_) {
125
return GetMethodCall;
127
template < class C > inline void %mangle(Class) ##_## AttributeName ## _set(C* self_, AttributeType val_) {
133
#define %mangle(Class) ##_## AttributeName ## _get(self_) GetMethodCall
134
#define %mangle(Class) ##_## AttributeName ## _set(self_, val_) SetMethodCall
139
%define %attribute_readonly(Class, AttributeType, AttributeName, GetMethod, GetMethodCall)
140
%ignore Class::GetMethod();
141
%ignore Class::GetMethod() const;
142
%immutable Class::AttributeName;
144
AttributeType AttributeName;
146
#if defined(__cplusplus) && defined(SWIG_ATTRIBUTE_TEMPLATE)
148
template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(const C* self_) {
149
return GetMethodCall;
151
template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(C* self_) {
152
return GetMethodCall;
157
#define %mangle(Class) ##_## AttributeName ## _get(self_) GetMethodCall
165
%define %attribute(Class, AttributeType, AttributeName, GetMethod, SetMethod...)
167
%attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, self_->GetMethod(), self_->SetMethod(val_))
169
%attribute_readonly(Class, AttributeType, AttributeName, GetMethod, self_->GetMethod())
173
%define %attribute2(Class, AttributeType, AttributeName, GetMethod, SetMethod...)
175
%attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, &self_->GetMethod(), self_->SetMethod(*val_))
177
%attribute_readonly(Class, AttributeType, AttributeName, GetMethod, &self_->GetMethod())
181
%define %attributeref(Class, AttributeType, AttributeName, AccessorMethod...)
182
#if #AccessorMethod != ""
183
%attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
185
%attribute_custom(Class, AttributeType, AttributeName, AttributeName, AttributeName, self_->AttributeName(), self_->AttributeName() = val_)
189
%define %attribute2ref(Class, AttributeType, AttributeName, AccessorMethod...)
190
#if #AccessorMethod != ""
191
%attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_)
193
%attribute_custom(Class, AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_)
197
// deprecated (same as %attributeref, but there is an argument order inconsistency)
198
%define %attribute_ref(Class, AttributeType, AccessorMethod, AttributeName...)
199
#if #AttributeName != ""
200
%attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
202
%attribute_custom(Class, AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)