attribute tidy up. %attributeref replaces %attribute_ref
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@10000 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
4513953e96
commit
2a89d09add
5 changed files with 102 additions and 134 deletions
|
|
@ -1,6 +1,14 @@
|
||||||
Version 1.3.32 (in progress)
|
Version 1.3.32 (in progress)
|
||||||
============================
|
============================
|
||||||
|
|
||||||
|
10/16/2007: wsfulton
|
||||||
|
Deprecate %attribute_ref and replace with %attributeref. There is just an argument
|
||||||
|
order change in order to maintain consistency with %attribute, from:
|
||||||
|
|
||||||
|
%attribute_ref(Class, AttributeType, AccessorMethod, AttributeName)
|
||||||
|
to
|
||||||
|
%attributeref(Class, AttributeType, AttributeName, AccessorMethod)
|
||||||
|
|
||||||
10/16/2007: olly
|
10/16/2007: olly
|
||||||
[Tcl] Fix several ocurrences of "warning: deprecated conversion
|
[Tcl] Fix several ocurrences of "warning: deprecated conversion
|
||||||
from string constant to 'char*'" from GCC 4.2 in generated C/C++
|
from string constant to 'char*'" from GCC 4.2 in generated C/C++
|
||||||
|
|
|
||||||
|
|
@ -8,18 +8,19 @@
|
||||||
%{
|
%{
|
||||||
// forward reference needed if using SWIG_ATTRIBUTE_TEMPLATE
|
// forward reference needed if using SWIG_ATTRIBUTE_TEMPLATE
|
||||||
class A;
|
class A;
|
||||||
|
class MyFoo; // %attribute2 does not work with templates
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%attribute(A, int, a, get_a, set_a);
|
%attribute(A, int, a, get_a, set_a);
|
||||||
%attribute_ref(A, int, b);
|
%attributeref(A, int, b);
|
||||||
|
|
||||||
%attribute_ref(Param<int>, int, value);
|
%attributeref(Param<int>, int, value);
|
||||||
|
|
||||||
|
|
||||||
%attribute(A, int, c, get_c); /* read-only */
|
%attribute(A, int, c, get_c); /* read-only */
|
||||||
%attribute_ref(A, int, b, d); /* different attribute name 'd' */
|
%attributeref(A, int, d, b); /* renames accessor method 'b' to attribute name 'd' */
|
||||||
|
|
||||||
%attribute_ref(B, A*, a)
|
%attributeref(B, A*, a)
|
||||||
|
|
||||||
%inline
|
%inline
|
||||||
{
|
{
|
||||||
|
|
@ -92,3 +93,16 @@ class A;
|
||||||
%template(Param_i) Param<int>;
|
%template(Param_i) Param<int>;
|
||||||
|
|
||||||
|
|
||||||
|
%attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo);
|
||||||
|
%inline %{
|
||||||
|
struct MyFoo {
|
||||||
|
int x;
|
||||||
|
};
|
||||||
|
class MyClass {
|
||||||
|
MyFoo foo;
|
||||||
|
public:
|
||||||
|
MyFoo& GetFoo() { return foo; }
|
||||||
|
void SetFoo(const MyFoo& other) { foo = other; }
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,75 +0,0 @@
|
||||||
%module attributetest
|
|
||||||
|
|
||||||
%include attribute.i
|
|
||||||
|
|
||||||
%attribute(A, int, a, get_a, set_a);
|
|
||||||
%attribute_ref(A, int, b);
|
|
||||||
|
|
||||||
%attribute_ref(Param<int>, int, value);
|
|
||||||
|
|
||||||
|
|
||||||
%attribute(A, int, c, get_c); /* read-only */
|
|
||||||
%attribute_ref(A, int, b, d); /* different attribute name 'd' */
|
|
||||||
|
|
||||||
%inline
|
|
||||||
{
|
|
||||||
struct A
|
|
||||||
{
|
|
||||||
A(int a, int b, int c) : _a(a), _b(b), _c(c)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_a() const
|
|
||||||
{
|
|
||||||
return _a;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_a(int aa)
|
|
||||||
{
|
|
||||||
_a = aa;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int& b() const
|
|
||||||
{
|
|
||||||
return _b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int& b()
|
|
||||||
{
|
|
||||||
return _b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_c() const
|
|
||||||
{
|
|
||||||
return _c;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
int _a;
|
|
||||||
int _b;
|
|
||||||
int _c;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class C>
|
|
||||||
struct Param
|
|
||||||
{
|
|
||||||
Param(C v) : _v(v)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
const int& value() const
|
|
||||||
{
|
|
||||||
return _v;
|
|
||||||
}
|
|
||||||
|
|
||||||
int& value()
|
|
||||||
{
|
|
||||||
return _v;
|
|
||||||
}
|
|
||||||
private:
|
|
||||||
C _v;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
%template(Param_i) Param<int>;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -43,4 +43,10 @@ if b.a.c != 3:
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
|
|
||||||
|
myFoo = li_attribute.MyFoo
|
||||||
|
myFoo.x = 8
|
||||||
|
myClass = li_attribute.MyClass
|
||||||
|
myClass.Foo = myFoo
|
||||||
|
if myClass.Foo.x != 8:
|
||||||
|
raise RuntimeError
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,57 +15,59 @@
|
||||||
like in:
|
like in:
|
||||||
|
|
||||||
%attribute(A, int, a, get_a, set_a);
|
%attribute(A, int, a, get_a, set_a);
|
||||||
|
|
||||||
struct A
|
struct A
|
||||||
{
|
{
|
||||||
int get_a() const;
|
int get_a() const;
|
||||||
void set_a(int aa);
|
void set_a(int aa);
|
||||||
};
|
};
|
||||||
|
|
||||||
If you don't provide a 'set' method, a 'read-only' attribute
|
If you don't provide a 'set' method, a 'read-only' attribute
|
||||||
is generated, ie, like in:
|
is generated, ie, like in:
|
||||||
|
|
||||||
%attribute(A, int, c, get_c);
|
%attribute(A, int, c, get_c);
|
||||||
|
|
||||||
Use %attribute_ref when you have const/non-const reference
|
Use %attributeref when you have const/non-const reference
|
||||||
access methods, like in:
|
access methods, like in:
|
||||||
|
|
||||||
%attribute_ref(A, int, b);
|
%attributeref(A, int, b);
|
||||||
|
|
||||||
struct A
|
struct A
|
||||||
{
|
{
|
||||||
const int& b() const;
|
const int& b() const;
|
||||||
int& b();
|
int& b();
|
||||||
};
|
};
|
||||||
|
|
||||||
%attribute_ref(B, int, c);
|
%attributeref(B, int, c);
|
||||||
|
|
||||||
struct B
|
struct B
|
||||||
{
|
{
|
||||||
int& c();
|
int& c();
|
||||||
};
|
};
|
||||||
|
|
||||||
You can also use
|
You can also use
|
||||||
|
|
||||||
%attribute_ref(class, type, refname, attr);
|
%attributeref(Class, AttributeType, AttributeName, AccessorMethod)
|
||||||
|
|
||||||
if the internal C++ reference methods have a different name from the
|
if the internal C++ reference methods have a different name from the
|
||||||
attribute you want. There's an inconsistency here (retained for
|
attribute you want, so
|
||||||
compatibility): when using %attribute_ref, the C++ symbol name comes
|
|
||||||
before the attribute name, but when you use %attribute, the attribute
|
%attributeref(B, int, d, c);
|
||||||
name comes before the C++ symbol names.
|
|
||||||
|
is the same as the last example, but instead of the attribute 'c' being
|
||||||
|
called 'c', it is called 'd'.
|
||||||
|
|
||||||
Now you can use the attributes like so:
|
Now you can use the attributes like so:
|
||||||
|
|
||||||
x = A()
|
x = A()
|
||||||
x.a = 3 # calls A::set_a
|
x.a = 3 # calls A::set_a
|
||||||
print x.a # calls A::get_a
|
print x.a # calls A::get_a
|
||||||
|
|
||||||
x.b = 3 # calls A::b()
|
x.b = 3 # calls A::b()
|
||||||
print x.b # calls A::b() const
|
print x.b # calls A::b() const
|
||||||
|
|
||||||
Use %attribute2 instead of %attribute to indicate that reference-pointer
|
Use %attribute2 instead of %attribute to indicate that reference-pointer
|
||||||
translation is required. You use %attribute2 instead of %attribute in
|
translation is required. You use %attribute2 instead of %attribute in
|
||||||
cases like this:
|
cases like this:
|
||||||
|
|
||||||
%attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo);
|
%attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo);
|
||||||
|
|
@ -79,29 +81,29 @@
|
||||||
MyFoo& GetFoo() { return foo; }
|
MyFoo& GetFoo() { return foo; }
|
||||||
void SetFoo(const MyFoo& other) { foo = other; }
|
void SetFoo(const MyFoo& other) { foo = other; }
|
||||||
};
|
};
|
||||||
%}
|
%}
|
||||||
|
|
||||||
Here, the data type of the property is a wrapped type (MyFoo) and on the
|
Here, the data type of the property is a wrapped type (MyFoo) and on the
|
||||||
C++ side it is passed by reference. The problem is that the SWIG wrapper will
|
C++ side it is passed by reference. The problem is that the SWIG wrapper will
|
||||||
pass around a pointer (MyFoo *) which is not compatible with the reference
|
pass around a pointer (MyFoo *) which is not compatible with the reference
|
||||||
type of the accessors (MyFoo &). Therefore, if you use %attribute, you'll get
|
type of the accessors (MyFoo &). Therefore, if you use %attribute, you'll get
|
||||||
an error from your C/C++ compiler. %attribute2 translates between a pointer
|
an error from your C/C++ compiler. %attribute2 translates between a pointer
|
||||||
and a reference to eliminate the error. In case you're confused, I'll make it
|
and a reference to eliminate the error. In case you're confused, let's make it
|
||||||
simple: just use %attribute at first, but if the C/C++ compiler gives an error
|
simple: just use %attribute at first, but if the C/C++ compiler gives an error
|
||||||
while compiling the wrapper, try %attribute2 instead.
|
while compiling the wrapper, try %attribute2 instead.
|
||||||
|
|
||||||
NOTE: remember that if the type contains commas, such as 'std::pair<int,int>',
|
NOTE: remember that if the type contains commas, such as 'std::pair<int,int>',
|
||||||
you need to use the macro like:
|
you need to use the macro like:
|
||||||
|
|
||||||
%attribute_ref(A, %arg(std::pair<int,int>), pval);
|
%attributeref(A, %arg(std::pair<int,int>), pval);
|
||||||
|
|
||||||
where %arg() 'normalizes' the type to be understood as a single
|
where %arg() 'normalizes' the type to be understood as a single
|
||||||
argument, otherwise the macro will get confused by the comma (see the
|
argument, otherwise the macro will get confused by the comma.
|
||||||
'cpp' documentation).
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//
|
//
|
||||||
// Define SWIG_ATTRIBUTE_TEMPLATE if you want to use templates instead of macros for the C++ get and set wrapper methods
|
// Define SWIG_ATTRIBUTE_TEMPLATE if you want to use templates instead of macros for the C++ get and set wrapper methods
|
||||||
|
// Does not always generate compileable code, use at your peril!
|
||||||
//
|
//
|
||||||
//#define SWIG_ATTRIBUTE_TEMPLATE
|
//#define SWIG_ATTRIBUTE_TEMPLATE
|
||||||
|
|
||||||
|
|
@ -157,6 +159,9 @@
|
||||||
#endif
|
#endif
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
|
||||||
|
// User macros
|
||||||
|
|
||||||
%define %attribute(Class, AttributeType, AttributeName, GetMethod, SetMethod...)
|
%define %attribute(Class, AttributeType, AttributeName, GetMethod, SetMethod...)
|
||||||
#if #SetMethod != ""
|
#if #SetMethod != ""
|
||||||
%attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, self_->GetMethod(), self_->SetMethod(val_))
|
%attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, self_->GetMethod(), self_->SetMethod(val_))
|
||||||
|
|
@ -173,18 +178,28 @@
|
||||||
#endif
|
#endif
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
%define %attribute_ref(Class, AttributeType, Accessor, AttributeName...)
|
%define %attributeref(Class, AttributeType, AttributeName, AccessorMethod...)
|
||||||
#if #AttributeName != ""
|
#if #AccessorMethod != ""
|
||||||
%attribute_custom(Class, AttributeType, AttributeName, Accessor, Accessor, self_->Accessor(), self_->Accessor() = val_)
|
%attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
|
||||||
#else
|
#else
|
||||||
%attribute_custom(Class, AttributeType, Accessor, Accessor, Accessor, self_->Accessor(), self_->Accessor() = val_)
|
%attribute_custom(Class, AttributeType, AttributeName, AttributeName, AttributeName, self_->AttributeName(), self_->AttributeName() = val_)
|
||||||
#endif
|
#endif
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
%define %attribute2_ref(Class, AttributeType, Accessor, AttributeName...)
|
%define %attribute2ref(Class, AttributeType, AttributeName, AccessorMethod...)
|
||||||
#if #AttributeName != ""
|
#if #AccessorMethod != ""
|
||||||
%attribute_custom(Class, AttributeType, AttributeName, Accessor, Accessor, &self_->Accessor(), self_->Accessor() = *val_)
|
%attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_)
|
||||||
#else
|
#else
|
||||||
%attribute_custom(Class, AttributeType, Accessor, Accessor, Accessor, &self_->Accessor(), self_->Accessor() = *val_)
|
%attribute_custom(Class, AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_)
|
||||||
#endif
|
#endif
|
||||||
%enddef
|
%enddef
|
||||||
|
|
||||||
|
// deprecated (same as %attributeref, but there is an argument order inconsistency)
|
||||||
|
%define %attribute_ref(Class, AttributeType, AccessorMethod, AttributeName...)
|
||||||
|
#if #AttributeName != ""
|
||||||
|
%attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
|
||||||
|
#else
|
||||||
|
%attribute_custom(Class, AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
|
||||||
|
#endif
|
||||||
|
%enddef
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue