1 %module li_attribute 2 3 %include <exception.i> 4 5 //#define SWIG_ATTRIBUTE_TEMPLATE 6 %include <attribute.i> 7 8 %{ 9 // forward reference needed if using SWIG_ATTRIBUTE_TEMPLATE 10 struct A; 11 struct MyFoo; // %attribute2 does not work with templates 12 %} 13 14 %attribute(A, int, a, get_a, set_a); 15 %attributeref(A, int, b); 16 17 %attributeref(Param<int>, int, value); 18 19 20 %attribute(A, int, c, get_c); /* read-only */ 21 %attributeref(A, int, d, b); /* renames accessor method 'b' to attribute name 'd' */ 22 23 %attributeref(B, A*, a) 24 25 %inline 26 { 27 struct A 28 { AA29 A(int a, int b, int c) : _a(a), _b(b), _c(c) 30 { 31 } 32 get_aA33 int get_a() const 34 { 35 return _a; 36 } 37 set_aA38 void set_a(int aa) 39 { 40 _a = aa; 41 } 42 43 /* only one ref method */ bA44 int& b() 45 { 46 return _b; 47 } 48 get_cA49 int get_c() const 50 { 51 return _c; 52 } 53 private: 54 int _a; 55 int _b; 56 int _c; 57 }; 58 59 template <class C> 60 struct Param 61 { ParamParam62 Param(C v) : _v(v) 63 { 64 } 65 valueParam66 const int& value() const 67 { 68 return _v; 69 } 70 valueParam71 int& value() 72 { 73 return _v; 74 } 75 private: 76 C _v; 77 }; 78 79 80 struct B { BB81 B(A *a) : mA(a) 82 { 83 } 84 aB85 A*& a() { return mA; } 86 87 protected: 88 A* mA; 89 }; 90 91 } 92 93 %template(Param_i) Param<int>; 94 95 96 // class/struct attribute with get/set methods using return/pass by reference 97 %attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo); 98 %inline %{ 99 struct MyFoo { MyFooMyFoo100 MyFoo() : x(-1) {} 101 int x; 102 }; 103 class MyClass { 104 MyFoo foo; 105 public: GetFoo()106 MyFoo& GetFoo() { return foo; } SetFoo(const MyFoo & other)107 void SetFoo(const MyFoo& other) { foo = other; } 108 }; 109 %} 110 111 112 // class/struct attribute with get/set methods using return/pass by value 113 %attributeval(MyClassVal, MyFoo, ReadWriteFoo, GetFoo, SetFoo); 114 %attributeval(MyClassVal, MyFoo, ReadOnlyFoo, GetFoo); 115 %inline %{ 116 class MyClassVal { 117 MyFoo foo; 118 public: GetFoo()119 MyFoo GetFoo() { return foo; } SetFoo(MyFoo other)120 void SetFoo(MyFoo other) { foo = other; } 121 }; 122 %} 123 124 125 // string attribute with get/set methods using return/pass by value 126 %include <std_string.i> 127 %attributestring(MyStringyClass, std::string, ReadWriteString, GetString, SetString); 128 %attributestring(MyStringyClass, std::string, ReadOnlyString, GetString); 129 %inline %{ 130 class MyStringyClass { 131 std::string str; 132 public: MyStringyClass(const std::string & val)133 MyStringyClass(const std::string &val) : str(val) {} GetString()134 std::string GetString() { return str; } SetString(std::string other)135 void SetString(std::string other) { str = other; } 136 }; 137 %} 138 139 140