efl-csharp: Add implicit conversions for Eina.Value
authorLauro Moura <lauromoura@expertisesolutions.com.br>
Sun, 30 Dec 2018 15:57:16 +0000 (16:57 +0100)
committerShinwoo Kim <cinoo.kim@samsung.com>
Thu, 3 Jan 2019 09:00:26 +0000 (18:00 +0900)
Summary:
For basic types, this will make it easier to pass Eina.Values into
functions, without requiring to setup and later Set() or Get() calls.

As discussed on irc, this seems to be a better way to improve the Value
C# API than using method chaining.

Fixes T7388

Test Plan: run tests

Reviewers: segfaultxavi, felipealmeida

Reviewed By: segfaultxavi

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Maniphest Tasks: T7388

Differential Revision: https://phab.enlightenment.org/D7526

src/bindings/mono/eina_mono/eina_value.cs
src/tests/efl_mono/ValueEolian.cs

index 9ff4de5..be84e4a 100644 (file)
@@ -804,6 +804,204 @@ public class Value : IDisposable, IComparable<Value>, IEquatable<Value>
         return new Value(v);
     }
 
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(byte x)
+    {
+        var v = new Eina.Value(ValueType.Byte);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator byte(Value v)
+    {
+        byte b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(sbyte x)
+    {
+        var v = new Eina.Value(ValueType.SByte);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator sbyte(Value v)
+    {
+        sbyte b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(short x)
+    {
+        var v = new Eina.Value(ValueType.Short);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator short(Value v)
+    {
+        short b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(ushort x)
+    {
+        var v = new Eina.Value(ValueType.UShort);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator ushort(Value v)
+    {
+        ushort b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(int x)
+    {
+        var v = new Eina.Value(ValueType.Int32);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator int(Value v)
+    {
+        int b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(uint x)
+    {
+        var v = new Eina.Value(ValueType.UInt32);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator uint(Value v)
+    {
+        uint b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(long x)
+    {
+        var v = new Eina.Value(ValueType.Long);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator long(Value v)
+    {
+        long b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(ulong x)
+    {
+        var v = new Eina.Value(ValueType.ULong);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator ulong(Value v)
+    {
+        ulong b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(float x)
+    {
+        var v = new Eina.Value(ValueType.Float);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator float(Value v)
+    {
+        float b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(double x)
+    {
+        var v = new Eina.Value(ValueType.Double);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator double(Value v)
+    {
+        double b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator Value(string x)
+    {
+        var v = new Eina.Value(ValueType.String);
+        if (!v.Set(x))
+            throw new InvalidOperationException("Couldn't set value.");
+        return v;
+    }
+
+    /// <summary>Implicit conversion.</summary>
+    public static implicit operator string(Value v)
+    {
+        string b;
+        if (!v.Get(out b))
+            throw new InvalidOperationException("Couldn't get value.");
+        return b;
+    }
+
     /// <summary>Creates an Value instance from a given array description.</summary>
     private static Value FromArrayDesc(Eina.EinaNative.Value_Array arrayDesc)
     {
index 78da290..4d33dfa 100644 (file)
@@ -116,6 +116,43 @@ public static class TestEinaValueEolian {
             Test.AssertEquals(val, obj.value);
         }
     }
+
+    public static void TestEolianEinaValueImplicitOperators()
+    {
+        var obj = new Dummy.TestObject();
+        int payload = 1999;
+        obj.SetValue(payload);
+
+        var expected = new Eina.Value(1999);
+        var received = new Eina.Value(Eina.ValueType.String);
+
+        obj.OutValue(out received);
+        Test.AssertEquals(expected, received);
+        Test.AssertEquals(Eina.ValueType.Int32, received.GetValueType());
+
+        int i = received;
+        Test.AssertEquals(i, 1999);
+
+        expected = new Eina.Value("Hallo");
+        obj.SetValue("Hallo");
+
+        obj.OutValue(out received);
+        Test.AssertEquals(expected, received);
+        Test.AssertEquals(Eina.ValueType.String, received.GetValueType());
+
+        string s = received;
+        Test.AssertEquals(s, "Hallo");
+
+        // Casting
+        expected = new Eina.Value((double)15);
+        obj.SetValue((double)15);
+
+        obj.OutValue(out received);
+        Test.AssertEquals(expected, received);
+        Test.AssertEquals(Eina.ValueType.Double, received.GetValueType());
+
+
+    }
 }
 #pragma warning restore 1591
 }