summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2018-12-30 16:57:16 +0100
committerXavi Artigas <xavierartigas@yahoo.es>2018-12-30 17:08:25 +0100
commit3b7efdc80fba3852bc029239cfc741dee3904f66 (patch)
tree4202e695f8fc9d279c762fb75830a75a5a5f49ee
parentc6509aee0fc4871524690411f3a9dde8ac447c0c (diff)
efl-csharp: Add implicit conversions for Eina.Value
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
-rw-r--r--src/bindings/mono/eina_mono/eina_value.cs198
-rw-r--r--src/tests/efl_mono/ValueEolian.cs37
2 files changed, 235 insertions, 0 deletions
diff --git a/src/bindings/mono/eina_mono/eina_value.cs b/src/bindings/mono/eina_mono/eina_value.cs
index 9ff4de5b13..be84e4adff 100644
--- a/src/bindings/mono/eina_mono/eina_value.cs
+++ b/src/bindings/mono/eina_mono/eina_value.cs
@@ -804,6 +804,204 @@ public class Value : IDisposable, IComparable<Value>, IEquatable<Value>
804 return new Value(v); 804 return new Value(v);
805 } 805 }
806 806
807 /// <summary>Implicit conversion.</summary>
808 public static implicit operator Value(byte x)
809 {
810 var v = new Eina.Value(ValueType.Byte);
811 if (!v.Set(x))
812 throw new InvalidOperationException("Couldn't set value.");
813 return v;
814 }
815
816 /// <summary>Implicit conversion.</summary>
817 public static implicit operator byte(Value v)
818 {
819 byte b;
820 if (!v.Get(out b))
821 throw new InvalidOperationException("Couldn't get value.");
822 return b;
823 }
824
825 /// <summary>Implicit conversion.</summary>
826 public static implicit operator Value(sbyte x)
827 {
828 var v = new Eina.Value(ValueType.SByte);
829 if (!v.Set(x))
830 throw new InvalidOperationException("Couldn't set value.");
831 return v;
832 }
833
834 /// <summary>Implicit conversion.</summary>
835 public static implicit operator sbyte(Value v)
836 {
837 sbyte b;
838 if (!v.Get(out b))
839 throw new InvalidOperationException("Couldn't get value.");
840 return b;
841 }
842
843 /// <summary>Implicit conversion.</summary>
844 public static implicit operator Value(short x)
845 {
846 var v = new Eina.Value(ValueType.Short);
847 if (!v.Set(x))
848 throw new InvalidOperationException("Couldn't set value.");
849 return v;
850 }
851
852 /// <summary>Implicit conversion.</summary>
853 public static implicit operator short(Value v)
854 {
855 short b;
856 if (!v.Get(out b))
857 throw new InvalidOperationException("Couldn't get value.");
858 return b;
859 }
860
861 /// <summary>Implicit conversion.</summary>
862 public static implicit operator Value(ushort x)
863 {
864 var v = new Eina.Value(ValueType.UShort);
865 if (!v.Set(x))
866 throw new InvalidOperationException("Couldn't set value.");
867 return v;
868 }
869
870 /// <summary>Implicit conversion.</summary>
871 public static implicit operator ushort(Value v)
872 {
873 ushort b;
874 if (!v.Get(out b))
875 throw new InvalidOperationException("Couldn't get value.");
876 return b;
877 }
878
879 /// <summary>Implicit conversion.</summary>
880 public static implicit operator Value(int x)
881 {
882 var v = new Eina.Value(ValueType.Int32);
883 if (!v.Set(x))
884 throw new InvalidOperationException("Couldn't set value.");
885 return v;
886 }
887
888 /// <summary>Implicit conversion.</summary>
889 public static implicit operator int(Value v)
890 {
891 int b;
892 if (!v.Get(out b))
893 throw new InvalidOperationException("Couldn't get value.");
894 return b;
895 }
896
897 /// <summary>Implicit conversion.</summary>
898 public static implicit operator Value(uint x)
899 {
900 var v = new Eina.Value(ValueType.UInt32);
901 if (!v.Set(x))
902 throw new InvalidOperationException("Couldn't set value.");
903 return v;
904 }
905
906 /// <summary>Implicit conversion.</summary>
907 public static implicit operator uint(Value v)
908 {
909 uint b;
910 if (!v.Get(out b))
911 throw new InvalidOperationException("Couldn't get value.");
912 return b;
913 }
914
915 /// <summary>Implicit conversion.</summary>
916 public static implicit operator Value(long x)
917 {
918 var v = new Eina.Value(ValueType.Long);
919 if (!v.Set(x))
920 throw new InvalidOperationException("Couldn't set value.");
921 return v;
922 }
923
924 /// <summary>Implicit conversion.</summary>
925 public static implicit operator long(Value v)
926 {
927 long b;
928 if (!v.Get(out b))
929 throw new InvalidOperationException("Couldn't get value.");
930 return b;
931 }
932
933 /// <summary>Implicit conversion.</summary>
934 public static implicit operator Value(ulong x)
935 {
936 var v = new Eina.Value(ValueType.ULong);
937 if (!v.Set(x))
938 throw new InvalidOperationException("Couldn't set value.");
939 return v;
940 }
941
942 /// <summary>Implicit conversion.</summary>
943 public static implicit operator ulong(Value v)
944 {
945 ulong b;
946 if (!v.Get(out b))
947 throw new InvalidOperationException("Couldn't get value.");
948 return b;
949 }
950
951 /// <summary>Implicit conversion.</summary>
952 public static implicit operator Value(float x)
953 {
954 var v = new Eina.Value(ValueType.Float);
955 if (!v.Set(x))
956 throw new InvalidOperationException("Couldn't set value.");
957 return v;
958 }
959
960 /// <summary>Implicit conversion.</summary>
961 public static implicit operator float(Value v)
962 {
963 float b;
964 if (!v.Get(out b))
965 throw new InvalidOperationException("Couldn't get value.");
966 return b;
967 }
968
969 /// <summary>Implicit conversion.</summary>
970 public static implicit operator Value(double x)
971 {
972 var v = new Eina.Value(ValueType.Double);
973 if (!v.Set(x))
974 throw new InvalidOperationException("Couldn't set value.");
975 return v;
976 }
977
978 /// <summary>Implicit conversion.</summary>
979 public static implicit operator double(Value v)
980 {
981 double b;
982 if (!v.Get(out b))
983 throw new InvalidOperationException("Couldn't get value.");
984 return b;
985 }
986
987 /// <summary>Implicit conversion.</summary>
988 public static implicit operator Value(string x)
989 {
990 var v = new Eina.Value(ValueType.String);
991 if (!v.Set(x))
992 throw new InvalidOperationException("Couldn't set value.");
993 return v;
994 }
995
996 /// <summary>Implicit conversion.</summary>
997 public static implicit operator string(Value v)
998 {
999 string b;
1000 if (!v.Get(out b))
1001 throw new InvalidOperationException("Couldn't get value.");
1002 return b;
1003 }
1004
807 /// <summary>Creates an Value instance from a given array description.</summary> 1005 /// <summary>Creates an Value instance from a given array description.</summary>
808 private static Value FromArrayDesc(Eina.EinaNative.Value_Array arrayDesc) 1006 private static Value FromArrayDesc(Eina.EinaNative.Value_Array arrayDesc)
809 { 1007 {
diff --git a/src/tests/efl_mono/ValueEolian.cs b/src/tests/efl_mono/ValueEolian.cs
index 78da29087a..4d33dfacde 100644
--- a/src/tests/efl_mono/ValueEolian.cs
+++ b/src/tests/efl_mono/ValueEolian.cs
@@ -116,6 +116,43 @@ public static class TestEinaValueEolian {
116 Test.AssertEquals(val, obj.value); 116 Test.AssertEquals(val, obj.value);
117 } 117 }
118 } 118 }
119
120 public static void TestEolianEinaValueImplicitOperators()
121 {
122 var obj = new Dummy.TestObject();
123 int payload = 1999;
124 obj.SetValue(payload);
125
126 var expected = new Eina.Value(1999);
127 var received = new Eina.Value(Eina.ValueType.String);
128
129 obj.OutValue(out received);
130 Test.AssertEquals(expected, received);
131 Test.AssertEquals(Eina.ValueType.Int32, received.GetValueType());
132
133 int i = received;
134 Test.AssertEquals(i, 1999);
135
136 expected = new Eina.Value("Hallo");
137 obj.SetValue("Hallo");
138
139 obj.OutValue(out received);
140 Test.AssertEquals(expected, received);
141 Test.AssertEquals(Eina.ValueType.String, received.GetValueType());
142
143 string s = received;
144 Test.AssertEquals(s, "Hallo");
145
146 // Casting
147 expected = new Eina.Value((double)15);
148 obj.SetValue((double)15);
149
150 obj.OutValue(out received);
151 Test.AssertEquals(expected, received);
152 Test.AssertEquals(Eina.ValueType.Double, received.GetValueType());
153
154
155 }
119} 156}
120#pragma warning restore 1591 157#pragma warning restore 1591
121} 158}