PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/li_attribute.i

#
Swig | 139 lines | 108 code | 30 blank | 1 comment | 0 complexity | fcab8230ce29aceb8c474e33d409e1d8 MD5 | raw file
  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
 10struct A;
 11struct 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  {
 29    A(int a, int b, int c) : _a(a), _b(b), _c(c)
 30    {
 31    }
 32    
 33    int get_a() const 
 34    {
 35      return _a;
 36    }
 37    
 38    void set_a(int aa) 
 39    {
 40      _a = aa;
 41    }
 42
 43    /* only one ref method */
 44    int& b() 
 45    {
 46      return _b;
 47    }    
 48
 49    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  {
 62    Param(C v) : _v(v)
 63    {
 64    }
 65
 66    const int& value() const 
 67    {
 68      return _v;
 69    }
 70    
 71    int& value() 
 72    {
 73      return _v;
 74    }    
 75  private:
 76    C _v;
 77  }; 
 78  
 79  
 80  struct B {
 81    B(A *a) : mA(a)
 82    {
 83    }
 84    
 85    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 { 
100    MyFoo() : x(-1) {}
101    int x;
102  };
103  class MyClass {
104    MyFoo foo;
105  public:
106    MyFoo& GetFoo() { return foo; }
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:
119    MyFoo GetFoo() { return foo; }
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:
133    MyStringyClass(const std::string &val) : str(val) {}
134    std::string GetString() { return str; }
135    void SetString(std::string other) { str = other; }
136  };
137%} 
138
139