PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. %module li_attribute
  2. %include <exception.i>
  3. //#define SWIG_ATTRIBUTE_TEMPLATE
  4. %include <attribute.i>
  5. %{
  6. // forward reference needed if using SWIG_ATTRIBUTE_TEMPLATE
  7. struct A;
  8. struct MyFoo; // %attribute2 does not work with templates
  9. %}
  10. %attribute(A, int, a, get_a, set_a);
  11. %attributeref(A, int, b);
  12. %attributeref(Param<int>, int, value);
  13. %attribute(A, int, c, get_c); /* read-only */
  14. %attributeref(A, int, d, b); /* renames accessor method 'b' to attribute name 'd' */
  15. %attributeref(B, A*, a)
  16. %inline
  17. {
  18. struct A
  19. {
  20. A(int a, int b, int c) : _a(a), _b(b), _c(c)
  21. {
  22. }
  23. int get_a() const
  24. {
  25. return _a;
  26. }
  27. void set_a(int aa)
  28. {
  29. _a = aa;
  30. }
  31. /* only one ref method */
  32. int& b()
  33. {
  34. return _b;
  35. }
  36. int get_c() const
  37. {
  38. return _c;
  39. }
  40. private:
  41. int _a;
  42. int _b;
  43. int _c;
  44. };
  45. template <class C>
  46. struct Param
  47. {
  48. Param(C v) : _v(v)
  49. {
  50. }
  51. const int& value() const
  52. {
  53. return _v;
  54. }
  55. int& value()
  56. {
  57. return _v;
  58. }
  59. private:
  60. C _v;
  61. };
  62. struct B {
  63. B(A *a) : mA(a)
  64. {
  65. }
  66. A*& a() { return mA; }
  67. protected:
  68. A* mA;
  69. };
  70. }
  71. %template(Param_i) Param<int>;
  72. // class/struct attribute with get/set methods using return/pass by reference
  73. %attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo);
  74. %inline %{
  75. struct MyFoo {
  76. MyFoo() : x(-1) {}
  77. int x;
  78. };
  79. class MyClass {
  80. MyFoo foo;
  81. public:
  82. MyFoo& GetFoo() { return foo; }
  83. void SetFoo(const MyFoo& other) { foo = other; }
  84. };
  85. %}
  86. // class/struct attribute with get/set methods using return/pass by value
  87. %attributeval(MyClassVal, MyFoo, ReadWriteFoo, GetFoo, SetFoo);
  88. %attributeval(MyClassVal, MyFoo, ReadOnlyFoo, GetFoo);
  89. %inline %{
  90. class MyClassVal {
  91. MyFoo foo;
  92. public:
  93. MyFoo GetFoo() { return foo; }
  94. void SetFoo(MyFoo other) { foo = other; }
  95. };
  96. %}
  97. // string attribute with get/set methods using return/pass by value
  98. %include <std_string.i>
  99. %attributestring(MyStringyClass, std::string, ReadWriteString, GetString, SetString);
  100. %attributestring(MyStringyClass, std::string, ReadOnlyString, GetString);
  101. %inline %{
  102. class MyStringyClass {
  103. std::string str;
  104. public:
  105. MyStringyClass(const std::string &val) : str(val) {}
  106. std::string GetString() { return str; }
  107. void SetString(std::string other) { str = other; }
  108. };
  109. %}