From 3e188e508d877e9c459a4fa09425456c17d7cf97 Mon Sep 17 00:00:00 2001 From: Artem Serebriyskiy Date: Sat, 25 May 2013 02:08:26 +0400 Subject: Add test case for attributes with moderately complex templates * New test case tests that %attribute macros correctly supports passing template with multiple parameters as class name or attribute type name * Some further changes were made to %attribute macros - now AttributeType is protected with %arg as well. This allows you to have attributes of type e.g. std::pair etc Update CHANGES file for %attribute template fixes Closes #48 --- CHANGES.current | 4 + Examples/test-suite/common.mk | 1 + Examples/test-suite/li_attribute_template.i | 110 +++++++++++++++++++++ .../python/li_attribute_template_runme.py | 67 +++++++++++++ Lib/typemaps/attribute.swg | 20 ++-- 5 files changed, 192 insertions(+), 10 deletions(-) create mode 100644 Examples/test-suite/li_attribute_template.i create mode 100644 Examples/test-suite/python/li_attribute_template_runme.py diff --git a/CHANGES.current b/CHANGES.current index 3d41780c2..565a4ec0f 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release. Version 2.0.10 (in progress) ============================ +2013-05-25: Artem Serebriyskiy + SVN Patch ticket #338 - fixes to %attribute macros for template usage + with %arg. + 2013-05-19: wsfulton Fix ccache-swig internal error bug due to premature file cleanup. diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index 793055097..9a335b46e 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -238,6 +238,7 @@ CPP_TEST_CASES += \ kind \ langobj \ li_attribute \ + li_attribute_template \ li_boost_shared_ptr \ li_boost_shared_ptr_bits \ li_boost_shared_ptr_template \ diff --git a/Examples/test-suite/li_attribute_template.i b/Examples/test-suite/li_attribute_template.i new file mode 100644 index 000000000..3d4c108ef --- /dev/null +++ b/Examples/test-suite/li_attribute_template.i @@ -0,0 +1,110 @@ +%module li_attribute_template + +%include + +//#define SWIG_ATTRIBUTE_TEMPLATE +%include +%include + +%inline +{ + class Foo { + public: + Foo( int _value ) { value = _value; } + int value; + }; + + template< class T1, class T2> + struct pair{ + pair( T1 t1, T2 t2 ): + first(t1), second(t2) {;} + + T1 first; + T2 second; + }; + + template< class T1, class T2> + struct C + { + C(int a, int b, int c) : + _a(a), _b(b), _c(c), _d(a), _e(b), + _f(a,b), _g(b,c) + { + +/* + _f.first = _a; + _f.second = _b; + + _g.first = _b; + _g.second = _c; +*/ + + } + + int get_value() const + { + return _a; + } + + void set_value(int aa) + { + _a = aa; + } + + /* only one ref method */ + int& get_ref() + { + return _b; + } + + Foo get_class_value() const { return _d; } + void set_class_value( Foo foo) { _d = foo; } + + const Foo& get_class_ref() const { return _e; } + void set_class_ref( const Foo& foo ) { _e = foo; } + + pair get_template_value() const { return _f; } + void set_template_value( const pair f ) { _f = f; } + + const pair& get_template_ref() const { return _g; } + void set_template_ref( const pair& g ) { _g = g; } + + std::string get_string() { return str; } + void set_string(std::string other) { str = other; } + + private: + int _a; + int _b; + int _c; + Foo _d; + Foo _e; + pair _f; + pair _g; + + std::string str; + }; + +} + +%define %instantiate_C( T1, T2 ) +%template (pair_ ## T1 ## T2 ) pair; +// Primitive types +%attribute( %arg(C), int, a, get_value, set_value ); +%attributeref( %arg(C), int, b, get_ref ); + +// Strings +%attributestring(%arg(C), std::string, str, get_string, set_string); + +// Class types +%attributeval( %arg(C), Foo, d, get_class_value, set_class_value ); +%attribute2( %arg(C), Foo, e, get_class_ref, set_class_ref ); + +// Moderately templated types +%attributeval( %arg(C), %arg(pair), f, get_template_value, set_template_value ); +%attribute2( %arg(C), %arg(pair), g, get_template_ref, set_template_ref ); + +%template (C ## T1 ## T2) C; +%enddef + + +%instantiate_C(int,int); diff --git a/Examples/test-suite/python/li_attribute_template_runme.py b/Examples/test-suite/python/li_attribute_template_runme.py new file mode 100644 index 000000000..7423053f9 --- /dev/null +++ b/Examples/test-suite/python/li_attribute_template_runme.py @@ -0,0 +1,67 @@ +# Check usage of template attributes + +import li_attribute_template + +chell = li_attribute_template.Cintint(1,2,3) + +def rassert( what, master ): + if what != master: + print what + raise RuntimeError + +## Testing primitive by value attribute +rassert( chell.a, 1 ) + +chell.a = 3 +rassert( chell.a, 3 ) + +## Testing primitive by ref attribute + +rassert( chell.b, 2 ) + +chell.b = 5 +rassert( chell.b,5 ) + +## Testing string +chell.str = "abc" +rassert( chell.str, "abc" ) + +# Testing class by value + +rassert( chell.d.value, 1 ) + +chell.d = li_attribute_template.Foo(2) +rassert( chell.d.value, 2 ) + +# Testing class by reference + +rassert( chell.e.value, 2 ) + +chell.e= li_attribute_template.Foo(3) +rassert( chell.e.value, 3 ) + +chell.e.value = 4 +rassert( chell.e.value, 4 ) + +# Testing moderately complex template by value +rassert( chell.f.first, 1 ) +rassert( chell.f.second, 2 ) + +pair = li_attribute_template.pair_intint(3,4) +chell.f = pair +rassert( chell.f.first, 3 ) +rassert( chell.f.second, 4 ) + +# Testing moderately complex template by ref +rassert( chell.g.first, 2 ) +rassert( chell.g.second, 3 ) + +pair = li_attribute_template.pair_intint(4,5) +chell.g = pair +rassert( chell.g.first, 4 ) +rassert( chell.g.second, 5 ) + +chell.g.first = 6 +chell.g.second = 7 +rassert( chell.g.first, 6 ) +rassert( chell.g.second, 7 ) diff --git a/Lib/typemaps/attribute.swg b/Lib/typemaps/attribute.swg index d30b51183..46fc80fd2 100644 --- a/Lib/typemaps/attribute.swg +++ b/Lib/typemaps/attribute.swg @@ -195,42 +195,42 @@ %define %attribute(Class, AttributeType, AttributeName, GetMethod, SetMethod...) #if #SetMethod != "" - %attribute_custom(%arg(Class), AttributeType, AttributeName, GetMethod, SetMethod, self_->GetMethod(), self_->SetMethod(val_)) + %attribute_custom(%arg(Class), %arg(AttributeType), AttributeName, GetMethod, SetMethod, self_->GetMethod(), self_->SetMethod(val_)) #else - %attribute_readonly(%arg(Class), AttributeType, AttributeName, GetMethod, self_->GetMethod()) + %attribute_readonly(%arg(Class), %arg(AttributeType), AttributeName, GetMethod, self_->GetMethod()) #endif %enddef %define %attribute2(Class, AttributeType, AttributeName, GetMethod, SetMethod...) #if #SetMethod != "" - %attribute_custom(%arg(Class), AttributeType, AttributeName, GetMethod, SetMethod, &self_->GetMethod(), self_->SetMethod(*val_)) + %attribute_custom(%arg(Class), %arg(AttributeType), AttributeName, GetMethod, SetMethod, &self_->GetMethod(), self_->SetMethod(*val_)) #else - %attribute_readonly(%arg(Class), AttributeType, AttributeName, GetMethod, &self_->GetMethod()) + %attribute_readonly(%arg(Class), %arg(AttributeType), AttributeName, GetMethod, &self_->GetMethod()) #endif %enddef %define %attributeref(Class, AttributeType, AttributeName, AccessorMethod...) #if #AccessorMethod != "" - %attribute_custom(%arg(Class), AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_) + %attribute_custom(%arg(Class), %arg(AttributeType), AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_) #else - %attribute_custom(%arg(Class), AttributeType, AttributeName, AttributeName, AttributeName, self_->AttributeName(), self_->AttributeName() = val_) + %attribute_custom(%arg(Class), %arg(AttributeType), AttributeName, AttributeName, AttributeName, self_->AttributeName(), self_->AttributeName() = val_) #endif %enddef %define %attribute2ref(Class, AttributeType, AttributeName, AccessorMethod...) #if #AccessorMethod != "" - %attribute_custom(%arg(Class), AttributeType, AttributeName, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_) + %attribute_custom(%arg(Class), %arg(AttributeType), AttributeName, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_) #else - %attribute_custom(%arg(Class), AttributeType, AccessorName, AccessorName, AccessorName, &self_->AccessorName(), self_->AccessorName() = *val_) + %attribute_custom(%arg(Class), %arg(AttributeType), AccessorName, AccessorName, AccessorName, &self_->AccessorName(), self_->AccessorName() = *val_) #endif %enddef // deprecated (same as %attributeref, but there is an argument order inconsistency) %define %attribute_ref(Class, AttributeType, AccessorMethod, AttributeName...) #if #AttributeName != "" - %attribute_custom(%arg(Class), AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_) + %attribute_custom(%arg(Class), %arg(AttributeType), AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_) #else - %attribute_custom(%arg(Class), AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_) + %attribute_custom(%arg(Class), %arg(AttributeType), AccessorMethod, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_) #endif %enddef -- cgit v1.2.1