~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Lib/typemaps/attribute.swg

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-06 10:27:08 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206102708-t37t62i45n595w0e
Tags: 1.3.33-2ubuntu1
* Merge with Debian; remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - Clean Runtime/ as well.
  - Force a few environment variables.
* debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
  like in:
16
16
 
17
17
      %attribute(A, int, a, get_a, set_a);
18
 
                                      
19
 
      struct A                        
20
 
      {                                       
21
 
        int get_a() const;                    
22
 
        void set_a(int aa);                   
23
 
      };                                      
 
18
 
 
19
      struct A
 
20
      {
 
21
        int get_a() const;
 
22
        void set_a(int aa);
 
23
      };
24
24
 
25
25
  If you don't provide a 'set' method, a 'read-only' attribute
26
26
  is generated, ie, like in:
27
27
 
28
28
      %attribute(A, int, c, get_c);
29
29
 
30
 
 
31
 
  Use %attribute_ref when you have const/non-const reference
 
30
  Use %attributeref when you have const/non-const reference
32
31
  access methods, like in:
33
32
 
34
 
      %attribute_ref(A, int, b);           
35
 
                                   
36
 
      struct A                     
37
 
      {                                    
38
 
        const int& b() const;      
39
 
        int& b();                          
40
 
      };                                   
41
 
                                   
42
 
      %attribute_ref(B, int, c);           
43
 
                                   
44
 
      struct B                     
45
 
      {                                    
46
 
        int& c();                          
47
 
      };                               
 
33
      %attributeref(A, int, b);
 
34
 
 
35
      struct A
 
36
      {
 
37
        const int& b() const;
 
38
        int& b();
 
39
      };
 
40
 
 
41
      %attributeref(B, int, c);
 
42
 
 
43
      struct B
 
44
      {
 
45
        int& c();
 
46
      };
48
47
 
49
48
  You can also use
50
49
 
51
 
      %attribute_ref(class, type, refname, attr);
52
 
  
 
50
      %attributeref(Class, AttributeType, AttributeName, AccessorMethod)
 
51
 
53
52
  if the internal C++ reference methods have a different name from the
54
 
  attribute you want.
55
 
  
56
 
  Then you can use the instances like:
57
 
 
58
 
      x = A()                        
59
 
      x.a = 3        # calls A::set_a    
60
 
      print x.a      # calls A::get_a    
61
 
                                     
62
 
      x.b = 3        # calls A::b()      
 
53
  attribute you want, so
 
54
 
 
55
      %attributeref(B, int, d, c);
 
56
 
 
57
  is the same as the last example, but instead of the attribute 'c' being
 
58
  called 'c', it is called 'd'.
 
59
 
 
60
  Now you can use the attributes like so:
 
61
 
 
62
      x = A()
 
63
      x.a = 3        # calls A::set_a
 
64
      print x.a      # calls A::get_a
 
65
 
 
66
      x.b = 3        # calls A::b()
63
67
      print x.b      # calls A::b() const
64
68
 
65
 
  NOTE: remember that if the type contains commas, such as
66
 
  'std::pair<int,int>', you need to use the macro like:
67
 
 
68
 
  %attribute_ref(A, %arg(std::pair<int,int>), pval);
69
 
 
70
 
  where %arg() 'normalize' the type to be understood as a single
71
 
  argument, otherwise the macro will get confused (see the 'cpp'
72
 
  documentation).
73
 
 
 
69
  Use %attribute2 instead of %attribute to indicate that reference-pointer
 
70
  translation is required. You use %attribute2 instead of %attribute in
 
71
  cases like this:
 
72
 
 
73
  %attribute2(MyClass, MyFoo, Foo, GetFoo, SetFoo);
 
74
  %inline %{
 
75
    struct MyFoo { 
 
76
      int x;
 
77
    };
 
78
    class MyClass {
 
79
      MyFoo foo;
 
80
    public:
 
81
      MyFoo& GetFoo() { return foo; }
 
82
      void SetFoo(const MyFoo& other) { foo = other; }
 
83
    };
 
84
  %}
 
85
 
 
86
  Here, the data type of the property is a wrapped type (MyFoo) and on the
 
87
  C++ side it is passed by reference. The problem is that the SWIG wrapper will
 
88
  pass around a pointer (MyFoo *) which is not compatible with the reference
 
89
  type of the accessors (MyFoo &). Therefore, if you use %attribute, you'll get
 
90
  an error from your C/C++ compiler. %attribute2 translates between a pointer
 
91
  and a reference to eliminate the error. In case you're confused, let's make it
 
92
  simple: just use %attribute at first, but if the C/C++ compiler gives an error
 
93
  while compiling the wrapper, try %attribute2 instead.
 
94
 
 
95
  NOTE: remember that if the type contains commas, such as 'std::pair<int,int>',
 
96
  you need to use the macro like:
 
97
 
 
98
  %attributeref(A, %arg(std::pair<int,int>), pval);
 
99
 
 
100
  where %arg() 'normalizes' the type to be understood as a single
 
101
  argument, otherwise the macro will get confused by the comma.
74
102
*/
75
103
 
76
 
#ifndef %attribute_exception
77
 
#define %attribute_exception(code,msg) SWIG_exception_fail(code,msg)
78
 
#endif
79
 
 
80
104
//
81
 
// Define SWIG_ATTRIBUTE_TEMPLATE if you want to use templates.
 
105
// Define SWIG_ATTRIBUTE_TEMPLATE if you want to use templates instead of macros for the C++ get and set wrapper methods
 
106
// Does not always generate compileable code, use at your peril!
82
107
//
83
108
//#define SWIG_ATTRIBUTE_TEMPLATE
84
109
 
85
 
 
86
 
#if defined(__cplusplus) && defined(SWIG_ATTRIBUTE_TEMPLATE)
87
 
%define %_attribute(Class, Wrap, type, attr, getcode, setcode) 
88
 
%extend Class {
89
 
  type attr;
90
 
}
91
 
%{
92
 
  template <class C> inline
93
 
    type Wrap ##_## attr ## _get(const C* _t)
94
 
    { return getcode; }
95
 
 
96
 
  template <class C> inline
97
 
    type Wrap ##_## attr ## _get(C* _t)
98
 
    { return getcode; }
99
 
 
100
 
  template <class C> inline
101
 
    void Wrap ##_## attr ## _set(C* _t, type _val)
102
 
    { setcode; }
103
 
%}
104
 
%enddef
105
 
 
106
 
#else
107
 
 
108
 
%define %_attribute(Class, Wrap, type, attr, getcode, setcode) 
109
 
%extend Class {
110
 
  type attr;
111
 
}
112
 
%{
113
 
#define Wrap ##_## attr ## _get(_t) getcode
114
 
#define Wrap ##_## attr ## _set(_t, _val) setcode
115
 
%}
116
 
%enddef
117
 
#endif
118
 
//
119
 
// Internal versions, need Wrap name
120
 
//
121
 
 
122
 
%define %attribute_T(Class, Wrap, type, attr, get, set...) 
123
 
%ignore Class::get;
124
 
#if #set != ""
125
 
  %ignore Class::set;
126
 
  %_attribute(%arg(Class), Wrap, %arg(type),
127
 
              attr, _t->get(), _t->set(_val)) 
128
 
#else
129
 
  %_attribute(%arg(Class), Wrap, %arg(type),
130
 
              attr, _t->get(), 
131
 
              %attribute_exception(SWIG_AttributeError,"read-only  'attr' attribute");)
132
 
#endif
133
 
%enddef
134
 
 
135
 
 
136
 
%define %_attribute_ref_T(Class, Wrap, type, refname, attr) 
137
 
%ignore Class::refname();
138
 
%ignore Class::refname() const;
139
 
%_attribute(%arg(Class), Wrap, %arg(type),
140
 
            attr, _t->refname(), _t->refname() = _val) 
141
 
%enddef
142
 
 
143
 
%define %attribute_ref_T(Class, Wrap, type, refname, attr...) 
144
 
#if #attr == ""
145
 
  %_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, refname) 
146
 
#else
147
 
  %_attribute_ref_T(%arg(Class), Wrap, %arg(type), refname, attr)
148
 
#endif
149
 
%enddef
150
 
 
151
 
//
152
 
// User versions
153
 
//
154
 
 
155
 
%define %attribute(Class, type, attr, get, set...)
156
 
 %attribute_T(%arg(Class), %mangle(Class), %arg(type), attr, get, set)
157
 
%enddef
158
 
 
159
 
%define %attribute_ref(Class, type, refname, _Type...)
160
 
 %attribute_ref_T(%arg(Class), %mangle(Class), %arg(type), refname, _Type)
161
 
%enddef
162
 
 
 
110
%define %attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, GetMethodCall, SetMethodCall)
 
111
  %ignore Class::GetMethod();
 
112
  %ignore Class::GetMethod() const;
 
113
  #if #SetMethod != #AttributeName
 
114
    %ignore Class::SetMethod;
 
115
  #endif
 
116
  %extend Class {
 
117
    AttributeType AttributeName;
 
118
  }
 
119
#if defined(__cplusplus) && defined(SWIG_ATTRIBUTE_TEMPLATE)
 
120
  %{
 
121
    template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(const C* self_) {
 
122
      return GetMethodCall;
 
123
    }
 
124
    template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(C* self_) {
 
125
      return GetMethodCall;
 
126
    }
 
127
    template < class C > inline void %mangle(Class) ##_## AttributeName ## _set(C* self_, AttributeType val_) {
 
128
      SetMethodCall;
 
129
    }
 
130
  %}
 
131
#else
 
132
  %{
 
133
    #define %mangle(Class) ##_## AttributeName ## _get(self_) GetMethodCall
 
134
    #define %mangle(Class) ##_## AttributeName ## _set(self_, val_) SetMethodCall
 
135
  %}
 
136
#endif
 
137
%enddef
 
138
 
 
139
%define %attribute_readonly(Class, AttributeType, AttributeName, GetMethod, GetMethodCall)
 
140
  %ignore Class::GetMethod();
 
141
  %ignore Class::GetMethod() const;
 
142
  %immutable Class::AttributeName;
 
143
  %extend Class {
 
144
    AttributeType AttributeName;
 
145
  }
 
146
#if defined(__cplusplus) && defined(SWIG_ATTRIBUTE_TEMPLATE)
 
147
  %{
 
148
    template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(const C* self_) {
 
149
      return GetMethodCall;
 
150
    }
 
151
    template < class C > inline AttributeType %mangle(Class) ##_## AttributeName ## _get(C* self_) {
 
152
      return GetMethodCall;
 
153
    }
 
154
  %}
 
155
#else
 
156
  %{
 
157
    #define %mangle(Class) ##_## AttributeName ## _get(self_) GetMethodCall
 
158
  %}
 
159
#endif
 
160
%enddef
 
161
 
 
162
 
 
163
// User macros
 
164
 
 
165
%define %attribute(Class, AttributeType, AttributeName, GetMethod, SetMethod...)
 
166
  #if #SetMethod != ""
 
167
    %attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, self_->GetMethod(), self_->SetMethod(val_))
 
168
  #else
 
169
    %attribute_readonly(Class, AttributeType, AttributeName, GetMethod, self_->GetMethod())
 
170
  #endif
 
171
%enddef
 
172
 
 
173
%define %attribute2(Class, AttributeType, AttributeName, GetMethod, SetMethod...)
 
174
  #if #SetMethod != ""
 
175
    %attribute_custom(Class, AttributeType, AttributeName, GetMethod, SetMethod, &self_->GetMethod(), self_->SetMethod(*val_))
 
176
  #else
 
177
    %attribute_readonly(Class, AttributeType, AttributeName, GetMethod, &self_->GetMethod())
 
178
  #endif
 
179
%enddef
 
180
 
 
181
%define %attributeref(Class, AttributeType, AttributeName, AccessorMethod...)
 
182
  #if #AccessorMethod != ""
 
183
    %attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
 
184
  #else
 
185
    %attribute_custom(Class, AttributeType, AttributeName, AttributeName, AttributeName, self_->AttributeName(), self_->AttributeName() = val_)
 
186
  #endif
 
187
%enddef
 
188
 
 
189
%define %attribute2ref(Class, AttributeType, AttributeName, AccessorMethod...)
 
190
  #if #AccessorMethod != ""
 
191
    %attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_)
 
192
  #else
 
193
    %attribute_custom(Class, AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, &self_->AccessorMethod(), self_->AccessorMethod() = *val_)
 
194
  #endif
 
195
%enddef
 
196
 
 
197
// deprecated (same as %attributeref, but there is an argument order inconsistency)
 
198
%define %attribute_ref(Class, AttributeType, AccessorMethod, AttributeName...)
 
199
  #if #AttributeName != ""
 
200
    %attribute_custom(Class, AttributeType, AttributeName, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
 
201
  #else
 
202
    %attribute_custom(Class, AttributeType, AccessorMethod, AccessorMethod, AccessorMethod, self_->AccessorMethod(), self_->AccessorMethod() = val_)
 
203
  #endif
 
204
%enddef
163
205