Added support for C# operators on DALi handle objects
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / SWIG / dali-operator.i
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // common name mappings
19 #if defined(SWIGCSHARP)
20 %rename(Assign)            operator=;
21 %rename(Add)               operator+;
22 %rename(AddAssign)         operator+=;
23 %rename(Subtract)          operator-;
24 %rename(SubtractAssign)    operator-=;
25 %rename(Multiply)          operator*;
26 %rename(MultiplyAssign)    operator*=;
27 %rename(Divide)            operator/;
28 %rename(DivideAssign)      operator/=;
29 %rename(Assign)            operator=;
30 %rename(EqualTo)           operator==;
31 %rename(NotEqualTo)        operator!=;
32 %rename(LessThan)          operator<;
33 %rename(GreaterThan)       operator>;
34 %rename(ValueOfIndex)      operator[];
35 #endif
36
37 %typemap(cscode) Dali::Vector2 %{
38   public static Vector2 operator+(Vector2 arg1, Vector2 arg2) {
39     return arg1.Add(arg2);
40   }
41
42   public static Vector2 operator-(Vector2 arg1, Vector2 arg2) {
43     return arg1.Subtract(arg2);
44   }
45
46   public static Vector2 operator-(Vector2 arg1) {
47     return arg1.Subtract();
48   }
49
50   public static Vector2 operator*(Vector2 arg1, Vector2 arg2) {
51     return arg1.Multiply(arg2);
52   }
53
54   public static Vector2 operator*(Vector2 arg1, float arg2) {
55     return arg1.Multiply(arg2);
56   }
57
58   public static Vector2 operator/(Vector2 arg1, Vector2 arg2) {
59     return arg1.Divide(arg2);
60   }
61
62   public static Vector2 operator/(Vector2 arg1, float arg2) {
63     return arg1.Divide(arg2);
64   }
65
66   public float this[uint index]
67   {
68     get
69     {
70       return ValueOfIndex(index);
71     }
72   }
73
74   public static Vector2 GetVector2FromPtr(global::System.IntPtr cPtr) {
75     Vector2 ret = new Vector2(cPtr, false);
76     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
77        return ret;
78   }
79
80 %}
81
82 %typemap(cscode) Dali::Vector3 %{
83   public static Vector3 operator+(Vector3 arg1, Vector3 arg2) {
84     return arg1.Add(arg2);
85   }
86
87   public static Vector3 operator-(Vector3 arg1, Vector3 arg2) {
88     return arg1.Subtract(arg2);
89   }
90
91   public static Vector3 operator-(Vector3 arg1) {
92     return arg1.Subtract();
93   }
94
95   public static Vector3 operator*(Vector3 arg1, Vector3 arg2) {
96     return arg1.Multiply(arg2);
97   }
98
99   public static Vector3 operator*(Vector3 arg1, float arg2) {
100     return arg1.Multiply(arg2);
101   }
102
103   public static Vector3 operator/(Vector3 arg1, Vector3 arg2) {
104     return arg1.Divide(arg2);
105   }
106
107   public static Vector3 operator/(Vector3 arg1, float arg2) {
108     return arg1.Divide(arg2);
109   }
110
111   public float this[uint index]
112   {
113     get
114     {
115       return ValueOfIndex(index);
116     }
117   }
118
119   public static Vector3 GetVector3FromPtr(global::System.IntPtr cPtr) {
120     Vector3 ret = new Vector3(cPtr, false);
121     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122        return ret;
123   }
124
125 %}
126
127 %typemap(cscode) Dali::Vector4 %{
128   public static Vector4 operator+(Vector4 arg1, Vector4 arg2) {
129     return arg1.Add(arg2);
130   }
131
132   public static Vector4 operator-(Vector4 arg1, Vector4 arg2) {
133     return arg1.Subtract(arg2);
134   }
135
136   public static Vector4 operator-(Vector4 arg1) {
137     return arg1.Subtract();
138   }
139
140   public static Vector4 operator*(Vector4 arg1, Vector4 arg2) {
141     return arg1.Multiply(arg2);
142   }
143
144   public static Vector4 operator*(Vector4 arg1, float arg2) {
145     return arg1.Multiply(arg2);
146   }
147
148   public static Vector4 operator/(Vector4 arg1, Vector4 arg2) {
149     return arg1.Divide(arg2);
150   }
151
152   public static Vector4 operator/(Vector4 arg1, float arg2) {
153     return arg1.Divide(arg2);
154   }
155
156   public float this[uint index]
157   {
158     get
159     {
160       return ValueOfIndex(index);
161     }
162   }
163
164   public static Vector4 GetVector4FromPtr(global::System.IntPtr cPtr) {
165     Vector4 ret = new Vector4(cPtr, false);
166     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167        return ret;
168   }
169
170 %}
171
172 %typemap(cscode) Dali::Matrix %{
173   public static Vector4 operator*(Matrix arg1, Vector4 arg2) {
174     return arg1.Multiply(arg2);
175   }
176 %}
177
178 %typemap(cscode) Dali::Quaternion %{
179   public static Quaternion operator+(Quaternion arg1, Quaternion arg2) {
180     return arg1.Add(arg2);
181   }
182
183   public static Quaternion operator-(Quaternion arg1, Quaternion arg2) {
184     return arg1.Subtract(arg2);
185   }
186
187   public static Quaternion operator-(Quaternion arg1) {
188     return arg1.Subtract();
189   }
190
191   public static Quaternion operator*(Quaternion arg1, Quaternion arg2) {
192     return arg1.Multiply(arg2);
193   }
194
195   public static Vector3 operator*(Quaternion arg1, Vector3 arg2) {
196     return arg1.Multiply(arg2);
197   }
198
199   public static Quaternion operator*(Quaternion arg1, float arg2) {
200     return arg1.Multiply(arg2);
201   }
202
203   public static Quaternion operator/(Quaternion arg1, Quaternion arg2) {
204     return arg1.Divide(arg2);
205   }
206
207   public static Quaternion operator/(Quaternion arg1, float arg2) {
208     return arg1.Divide(arg2);
209   }
210 %}
211
212 %typemap(cscode) Dali::Property::Array %{
213   public Property.Value this[uint index]
214   {
215     get
216     {
217       return ValueOfIndex(index);
218     }
219   }
220 %}
221
222 %typemap(cscode) Dali::Property::Map %{
223   public Property.Value this[string key]
224   {
225     get
226     {
227       return ValueOfIndex(key);
228     }
229   }
230
231   public Property.Value this[int key]
232   {
233     get
234     {
235       return ValueOfIndex(key);
236     }
237   }
238 %}
239
240 %typemap(cscode) Dali::Uint16Pair %{
241   public static bool operator<(Uint16Pair arg1, Uint16Pair arg2) {
242     return arg1.LessThan(arg2);
243   }
244
245   public static bool operator>(Uint16Pair arg1, Uint16Pair arg2) {
246     return arg1.GreaterThan(arg2);
247   }
248 %}
249
250 /**
251  * Extend the C++ base handle to include  a IsHandleEmpty() function
252  * This is because from C# we can't wrap the operator BooleanType() function
253  */
254 %extend Dali::BaseHandle {
255    bool HasBody() const {
256
257      // C++ code. DALi uses Handle <-> Body design pattern.
258      // This function checks the Handle to see if it has a body attached ( possible to have empty handles).
259      // Handles in DALi can be converted into a boolean type
260      // to check if the handle has a valid body attached to it.
261      // Internally checking *$self will  checks IntrusivePtr<Dali::RefObject> mObjectHandle in BaseHandle;
262      if( *$self )
263      {
264        return true;
265      }
266      else
267      {
268        return false;
269      }
270     }
271
272      // Check if two handles point to the same body / ref-object.
273      bool IsEqual( const BaseHandle& rhs ) const {
274
275      // C++ code. Check if two handles reference the same implemtion
276      if( *$self == rhs)
277      {
278        return true;
279      }
280      else
281      {
282        return false;
283      }
284     }
285
286 };
287
288 /**
289  * Extend C# base handle to support true / false testing of base handle
290  * so we can do
291  *  if ( actor )
292  *  {
293  *    ...
294  *  }
295  */
296 %typemap(cscode) Dali::BaseHandle %{
297
298   // Returns the bool value true to indicate that an operand is true and returns false otherwise.
299   public static bool operator true(BaseHandle handle)
300   {
301     // if the C# object is null, return false
302     if( BaseHandle.ReferenceEquals( handle, null ) )
303     {
304       return false;
305     }
306     // returns true if the handle has a body, false otherwise
307     return handle.HasBody();
308   }
309
310   // Returns the bool false  to indicate that an operand is false and returns true otherwise.
311   public static bool operator false(BaseHandle  handle)
312   {
313     // if the C# object is null, return true
314     if( BaseHandle.ReferenceEquals( handle, null ) )
315     {
316       return true;
317     }
318     return !handle.HasBody();
319   }
320
321   // Explicit conversion from Handle to bool.
322   public static explicit operator bool(BaseHandle handle)
323   {
324      // if the C# object is null, return false
325     if( BaseHandle.ReferenceEquals( handle, null ) )
326     {
327       return false;
328     }
329     // returns true if the handle has a body, false otherwise
330     return handle.HasBody();
331   }
332
333   // Equality operator
334   public static bool operator == (BaseHandle x, BaseHandle y)
335   {
336     // if the C# objects are the same return true
337     if(  BaseHandle.ReferenceEquals( x, y ) )
338     {
339       return true;
340     }
341     if ( !BaseHandle.ReferenceEquals( x, null ) && !BaseHandle.ReferenceEquals( y, null ) )
342     {
343       // drop into native code to see if both handles point to the same body
344       return x.IsEqual( y) ;
345     }
346     return false;
347
348   }
349
350   // Inequality operator. Returns Null if either operand is Null
351   public static bool operator !=(BaseHandle x, BaseHandle y)
352   {
353     return !(x==y);
354   }
355
356   // Logical AND operator for &&
357   // It's possible when doing a && this function (opBitwiseAnd) is never called due
358   // to short circuiting. E.g.
359   // If you perform x && y What actually is called is
360   // BaseHandle.op_False( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseAnd(x,y) )
361   //
362   public static BaseHandle operator &(BaseHandle x, BaseHandle y)
363   {
364     if( x == y )
365     {
366       return x;
367     }
368     return null;
369   }
370
371   // Logical OR operator for ||
372   // It's possible when doing a || this function (opBitwiseOr) is never called due
373   // to short circuiting. E.g.
374   // If you perform x || y What actually is called is
375   // BaseHandle.op_True( x ) ? BaseHandle.op_True( x ) : BaseHandle.opTrue( BaseHandle.opBitwiseOr(x,y) )
376   public static BaseHandle operator |(BaseHandle x, BaseHandle y)
377   {
378     if ( !BaseHandle.ReferenceEquals( x, null ) || !BaseHandle.ReferenceEquals( y, null ) )
379     {
380        if( x.HasBody() )
381        {
382          return x;
383        }
384        if( y.HasBody() )
385        {
386          return y;
387        }
388        return null;
389     }
390     return null;
391   }
392
393   // Logical ! operator
394   public static bool operator !(BaseHandle x)
395   {
396     // if the C# object is null, return true
397     if( BaseHandle.ReferenceEquals( x, null ) )
398     {
399       return true;
400     }
401     if( x.HasBody() )
402     {
403       return false;
404     }
405     return true;
406   }
407
408 %}