move attribute.i to the typemap library, now is usable from all the languages
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@7717 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
25e3e4dee8
commit
8be1b7a07c
12 changed files with 251 additions and 159 deletions
75
Examples/test-suite/li_attribute.i
Normal file
75
Examples/test-suite/li_attribute.i
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
%module li_attribute
|
||||||
|
|
||||||
|
%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>;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,6 +11,7 @@ top_builddir = @top_builddir@
|
||||||
|
|
||||||
CPP_TEST_CASES += \
|
CPP_TEST_CASES += \
|
||||||
primitive_types \
|
primitive_types \
|
||||||
|
li_attribute \
|
||||||
li_cstring \
|
li_cstring \
|
||||||
|
|
||||||
C_TEST_CASES += \
|
C_TEST_CASES += \
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
*wrap* *.pyc *.so *.dll *.exp *.lib
|
*wrap* *.pyc *.so *.dll *.exp *.lib
|
||||||
Makefile
|
Makefile
|
||||||
argcargvtest.py
|
argcargvtest.py
|
||||||
attributetest.py
|
|
||||||
autodoc.py
|
autodoc.py
|
||||||
callback.py
|
callback.py
|
||||||
complextest.py
|
complextest.py
|
||||||
|
|
@ -459,3 +458,5 @@ inherit.py
|
||||||
empty.py
|
empty.py
|
||||||
virtual_derivation.py
|
virtual_derivation.py
|
||||||
langobj.py
|
langobj.py
|
||||||
|
li_attribute.py
|
||||||
|
typedef_struct.py
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ top_builddir = @top_builddir@
|
||||||
|
|
||||||
CPP_TEST_CASES += \
|
CPP_TEST_CASES += \
|
||||||
argcargvtest \
|
argcargvtest \
|
||||||
attributetest \
|
|
||||||
autodoc \
|
autodoc \
|
||||||
callback \
|
callback \
|
||||||
complextest \
|
complextest \
|
||||||
|
|
@ -25,6 +24,7 @@ CPP_TEST_CASES += \
|
||||||
input \
|
input \
|
||||||
inplaceadd \
|
inplaceadd \
|
||||||
kwargs \
|
kwargs \
|
||||||
|
li_attribute \
|
||||||
li_cstring \
|
li_cstring \
|
||||||
li_cwstring \
|
li_cwstring \
|
||||||
li_implicit \
|
li_implicit \
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import attributetest
|
import li_attribute
|
||||||
|
|
||||||
aa = attributetest.A(1,2,3)
|
aa = li_attribute.A(1,2,3)
|
||||||
|
|
||||||
if aa.a != 1:
|
if aa.a != 1:
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
@ -28,7 +28,7 @@ if aa.c != 3:
|
||||||
#if aa.c != 3:
|
#if aa.c != 3:
|
||||||
# raise RuntimeError
|
# raise RuntimeError
|
||||||
|
|
||||||
pi = attributetest.Param_i(7)
|
pi = li_attribute.Param_i(7)
|
||||||
if pi.value != 7:
|
if pi.value != 7:
|
||||||
raise RuntimeError
|
raise RuntimeError
|
||||||
|
|
||||||
|
|
@ -11,7 +11,13 @@ top_builddir = @top_builddir@
|
||||||
|
|
||||||
CPP_TEST_CASES = \
|
CPP_TEST_CASES = \
|
||||||
track_objects \
|
track_objects \
|
||||||
track_objects_directors
|
track_objects_directors \
|
||||||
|
primitive_types \
|
||||||
|
li_attribute \
|
||||||
|
li_cstring
|
||||||
|
|
||||||
|
C_TEST_CASES += \
|
||||||
|
li_cstring
|
||||||
|
|
||||||
include $(srcdir)/../common.mk
|
include $(srcdir)/../common.mk
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ top_builddir = @top_builddir@
|
||||||
|
|
||||||
CPP_TEST_CASES += \
|
CPP_TEST_CASES += \
|
||||||
primitive_types \
|
primitive_types \
|
||||||
|
li_attribute \
|
||||||
li_cstring \
|
li_cstring \
|
||||||
li_cwstring \
|
li_cwstring \
|
||||||
li_std_wstring
|
li_std_wstring
|
||||||
|
|
|
||||||
1
Lib/perl5/attribute.i
Normal file
1
Lib/perl5/attribute.i
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
%include <typemaps/attribute.swg>
|
||||||
|
|
@ -1,152 +1 @@
|
||||||
%{
|
%include <typemaps/attribute.swg>
|
||||||
#include <stdio.h>
|
|
||||||
%}
|
|
||||||
|
|
||||||
%include <pymacros.swg>
|
|
||||||
|
|
||||||
/*
|
|
||||||
Attribute implementation using JOHN E LENZ ideas.
|
|
||||||
|
|
||||||
The following macros convert a pair of set/get methods
|
|
||||||
into a "native" attribute.
|
|
||||||
|
|
||||||
Use %attribute when you have a pair of get/set methods
|
|
||||||
like in:
|
|
||||||
|
|
||||||
%attribute(A, int, a, get_a, set_a);
|
|
||||||
|
|
||||||
struct A
|
|
||||||
{
|
|
||||||
int get_a() const
|
|
||||||
{
|
|
||||||
return _a;
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_a(int aa)
|
|
||||||
{
|
|
||||||
_a = aa;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
access methods, like in:
|
|
||||||
|
|
||||||
%attribute_ref(A, int, b);
|
|
||||||
|
|
||||||
struct A
|
|
||||||
{
|
|
||||||
const int& b() const
|
|
||||||
{
|
|
||||||
return _b;
|
|
||||||
}
|
|
||||||
|
|
||||||
int& b()
|
|
||||||
{
|
|
||||||
return _b;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
You can also use
|
|
||||||
|
|
||||||
%attribute_ref(class, type, refname, attr);
|
|
||||||
|
|
||||||
if the internal C++ reference methods have a different name from the
|
|
||||||
attribute you want.
|
|
||||||
|
|
||||||
Then you can use the instances like:
|
|
||||||
|
|
||||||
x = A()
|
|
||||||
x.a = 3 # calls A::set_a
|
|
||||||
print x.a # calls A::get_a
|
|
||||||
|
|
||||||
x.b = 3 # calls A::b()
|
|
||||||
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, %arg(std::pair<int,int>), pval);
|
|
||||||
|
|
||||||
where %arg() 'normalize' the type to be understood as a single
|
|
||||||
argument, otherwise the macro will get confused (see the 'cpp'
|
|
||||||
documentation).
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
|
|
||||||
%extend Class {
|
|
||||||
type attr;
|
|
||||||
}
|
|
||||||
%{
|
|
||||||
template <class C> inline
|
|
||||||
type Wrap ##_## attr ## _get(const C* _t)
|
|
||||||
{ return getcode; }
|
|
||||||
|
|
||||||
template <class C> inline
|
|
||||||
void Wrap ##_## attr ## _set(C* _t, type _val)
|
|
||||||
{ setcode; }
|
|
||||||
%}
|
|
||||||
%enddef
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
|
|
||||||
%extend Class {
|
|
||||||
type attr;
|
|
||||||
}
|
|
||||||
%{
|
|
||||||
#define Wrap ##_## attr ## _get(_t) getcode
|
|
||||||
#define Wrap ##_## attr ## _set(_t, _val) setcode
|
|
||||||
%}
|
|
||||||
%enddef
|
|
||||||
#endif
|
|
||||||
//
|
|
||||||
// Internal versions, need Wrap name
|
|
||||||
//
|
|
||||||
|
|
||||||
%define %attribute_T(Class, Wrap, type, attr, get, ...)
|
|
||||||
%ignore Class::get;
|
|
||||||
#if #__VA_ARGS__ != ""
|
|
||||||
%ignore Class::__VA_ARGS__;
|
|
||||||
%_attribute(%arg(Class), Wrap, %arg(type),
|
|
||||||
attr, _t->get(), _t->__VA_ARGS__(_val))
|
|
||||||
#else
|
|
||||||
%_attribute(%arg(Class), Wrap, %arg(type),
|
|
||||||
attr, _t->get(),
|
|
||||||
fprintf(stderr,"'attr' is a read-only attribute"))
|
|
||||||
#endif
|
|
||||||
%enddef
|
|
||||||
|
|
||||||
%define %_attribute_ref_T(Class, Wrap, type, refname, attr)
|
|
||||||
%ignore Class::refname();
|
|
||||||
%ignore Class::refname() const;
|
|
||||||
%_attribute(%arg(Class), Wrap, %arg(type),
|
|
||||||
attr, _t->refname(), _t->refname() = _val)
|
|
||||||
%enddef
|
|
||||||
|
|
||||||
%define %attribute_ref_T(Class, Wrap, type, refname, ...)
|
|
||||||
#if #__VA_ARGS__ == ""
|
|
||||||
%_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, refname)
|
|
||||||
#else
|
|
||||||
%_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, __VA_ARGS__)
|
|
||||||
#endif
|
|
||||||
%enddef
|
|
||||||
|
|
||||||
//
|
|
||||||
// User versions
|
|
||||||
//
|
|
||||||
|
|
||||||
%define %attribute(Class, type, attr, get, ...)
|
|
||||||
%attribute_T(%arg(Class), %mangle(Class), %arg(type), attr, get, __VA_ARGS__)
|
|
||||||
%enddef
|
|
||||||
|
|
||||||
%define %attribute_ref(Class, type, refname, ...)
|
|
||||||
%attribute_ref_T(%arg(Class), %mangle(Class), %arg(type), refname, __VA_ARGS__)
|
|
||||||
%enddef
|
|
||||||
|
|
|
||||||
1
Lib/ruby/attribute.i
Normal file
1
Lib/ruby/attribute.i
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
%include <typemaps/attribute.swg>
|
||||||
1
Lib/tcl/attribute.i
Normal file
1
Lib/tcl/attribute.i
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
%include <typemaps/attribute.swg>
|
||||||
156
Lib/typemaps/attribute.swg
Normal file
156
Lib/typemaps/attribute.swg
Normal file
|
|
@ -0,0 +1,156 @@
|
||||||
|
%{
|
||||||
|
#include <stdio.h>
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include <typemaps/swigmacros.swg>
|
||||||
|
|
||||||
|
/*
|
||||||
|
Attribute implementation using JOHN E LENZ ideas.
|
||||||
|
|
||||||
|
The following macros convert a pair of set/get methods
|
||||||
|
into a "native" attribute.
|
||||||
|
|
||||||
|
Use %attribute when you have a pair of get/set methods
|
||||||
|
like in:
|
||||||
|
|
||||||
|
%attribute(A, int, a, get_a, set_a);
|
||||||
|
|
||||||
|
struct A
|
||||||
|
{
|
||||||
|
int get_a() const
|
||||||
|
{
|
||||||
|
return _a;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_a(int aa)
|
||||||
|
{
|
||||||
|
_a = aa;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
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
|
||||||
|
access methods, like in:
|
||||||
|
|
||||||
|
%attribute_ref(A, int, b);
|
||||||
|
|
||||||
|
struct A
|
||||||
|
{
|
||||||
|
const int& b() const
|
||||||
|
{
|
||||||
|
return _b;
|
||||||
|
}
|
||||||
|
|
||||||
|
int& b()
|
||||||
|
{
|
||||||
|
return _b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
You can also use
|
||||||
|
|
||||||
|
%attribute_ref(class, type, refname, attr);
|
||||||
|
|
||||||
|
if the internal C++ reference methods have a different name from the
|
||||||
|
attribute you want.
|
||||||
|
|
||||||
|
Then you can use the instances like:
|
||||||
|
|
||||||
|
x = A()
|
||||||
|
x.a = 3 # calls A::set_a
|
||||||
|
print x.a # calls A::get_a
|
||||||
|
|
||||||
|
x.b = 3 # calls A::b()
|
||||||
|
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, %arg(std::pair<int,int>), pval);
|
||||||
|
|
||||||
|
where %arg() 'normalize' the type to be understood as a single
|
||||||
|
argument, otherwise the macro will get confused (see the 'cpp'
|
||||||
|
documentation).
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
|
||||||
|
%extend Class {
|
||||||
|
type attr;
|
||||||
|
}
|
||||||
|
%{
|
||||||
|
template <class C> inline
|
||||||
|
type Wrap ##_## attr ## _get(const C* _t)
|
||||||
|
{ return getcode; }
|
||||||
|
|
||||||
|
template <class C> inline
|
||||||
|
type Wrap ##_## attr ## _get(C* _t)
|
||||||
|
{ return getcode; }
|
||||||
|
|
||||||
|
template <class C> inline
|
||||||
|
void Wrap ##_## attr ## _set(C* _t, type _val)
|
||||||
|
{ setcode; }
|
||||||
|
%}
|
||||||
|
%enddef
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
%define %_attribute(Class, Wrap, type, attr, getcode, setcode)
|
||||||
|
%extend Class {
|
||||||
|
type attr;
|
||||||
|
}
|
||||||
|
%{
|
||||||
|
#define Wrap ##_## attr ## _get(_t) getcode
|
||||||
|
#define Wrap ##_## attr ## _set(_t, _val) setcode
|
||||||
|
%}
|
||||||
|
%enddef
|
||||||
|
#endif
|
||||||
|
//
|
||||||
|
// Internal versions, need Wrap name
|
||||||
|
//
|
||||||
|
|
||||||
|
%define %attribute_T(Class, Wrap, type, attr, get, _Type...)
|
||||||
|
%ignore Class::get;
|
||||||
|
#if #_Type != ""
|
||||||
|
%ignore Class::_Type;
|
||||||
|
%_attribute(%arg(Class), Wrap, %arg(type),
|
||||||
|
attr, _t->get(), _t->_Type(_val))
|
||||||
|
#else
|
||||||
|
%_attribute(%arg(Class), Wrap, %arg(type),
|
||||||
|
attr, _t->get(),
|
||||||
|
fprintf(stderr,"'attr' is a read-only attribute"))
|
||||||
|
#endif
|
||||||
|
%enddef
|
||||||
|
|
||||||
|
%define %_attribute_ref_T(Class, Wrap, type, refname, attr)
|
||||||
|
%ignore Class::refname();
|
||||||
|
%ignore Class::refname() const;
|
||||||
|
%_attribute(%arg(Class), Wrap, %arg(type),
|
||||||
|
attr, _t->refname(), _t->refname() = _val)
|
||||||
|
%enddef
|
||||||
|
|
||||||
|
%define %attribute_ref_T(Class, Wrap, type, refname, _Type...)
|
||||||
|
#if #_Type == ""
|
||||||
|
%_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, refname)
|
||||||
|
#else
|
||||||
|
%_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, _Type)
|
||||||
|
#endif
|
||||||
|
%enddef
|
||||||
|
|
||||||
|
//
|
||||||
|
// User versions
|
||||||
|
//
|
||||||
|
|
||||||
|
%define %attribute(Class, type, attr, get, _Type...)
|
||||||
|
%attribute_T(%arg(Class), %mangle(Class), %arg(type), attr, get, _Type)
|
||||||
|
%enddef
|
||||||
|
|
||||||
|
%define %attribute_ref(Class, type, refname, _Type...)
|
||||||
|
%attribute_ref_T(%arg(Class), %mangle(Class), %arg(type), refname, _Type)
|
||||||
|
%enddef
|
||||||
Loading…
Add table
Add a link
Reference in a new issue