summaryrefslogtreecommitdiff
path: root/src/bindings
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2019-07-11 15:52:52 -0300
committerVitor Sousa <vitorsousa@expertisesolutions.com.br>2019-07-11 15:55:04 -0300
commit6be38caeaec8e7808af9d0ab4b34465c84bb6e76 (patch)
tree66592df6e3fe5c09b9b417ea3e770bbe49e514da /src/bindings
parenta19bb21edf75256e8dfe043d591d05bbf23a29a3 (diff)
csharp: Add helpers to get/set Values from Objects
Summary: The user can construct an `Eina.Value` from a plain C# `object`, using reflection to get the correct type of object and construct the correct underlying C value. Also added the `Unwrap()` method to return a C# object representing the wrapped value. Both operations are useful when using `Eina.Value` to Get/Set values from `PropertyInfo` targets as in ``` var v = new Eina.Value(propInfo.GetValue(sourceObj)); ... propInfo.SetValue(targetObj, v.Unwrap()); ``` Currently, containers are not supported. It will be added in a following commit. Depends on D9270 Reviewers: felipealmeida, vitor.sousa, segfaultxavi Reviewed By: vitor.sousa Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D9272
Diffstat (limited to 'src/bindings')
-rw-r--r--src/bindings/mono/eina_mono/eina_value.cs170
1 files changed, 169 insertions, 1 deletions
diff --git a/src/bindings/mono/eina_mono/eina_value.cs b/src/bindings/mono/eina_mono/eina_value.cs
index 898a4966bb..37412b679b 100644
--- a/src/bindings/mono/eina_mono/eina_value.cs
+++ b/src/bindings/mono/eina_mono/eina_value.cs
@@ -916,7 +916,86 @@ public class Value : IDisposable, IComparable<Value>, IEquatable<Value>
916 private Value() 916 private Value()
917 { 917 {
918 this.Handle = Alloc(); 918 this.Handle = Alloc();
919
920 if (this.Handle == IntPtr.Zero)
921 {
922 throw new OutOfMemoryException("Failed to allocate memory for Eina.Value");
923 }
924
919 this.Ownership = Ownership.Managed; 925 this.Ownership = Ownership.Managed;
926 MemoryNative.Memset(this.Handle, 0, eina_value_sizeof());
927 }
928
929 /// <summary>Creates a new Value from the given C# value.</summary>
930 /// <param name="obj">The object to be wrapped.</param>
931 public Value(object obj) : this()
932 {
933 var objType = obj.GetType();
934
935 if (objType == typeof(sbyte))
936 {
937 Setup(ValueType.SByte);
938 Set((sbyte)obj);
939 }
940 else if (objType == typeof(byte))
941 {
942 Setup(ValueType.Byte);
943 Set((byte)obj);
944 }
945 else if (objType == typeof(short))
946 {
947 Setup(ValueType.Short);
948 Set((short)obj);
949 }
950 else if (objType == typeof(ushort))
951 {
952 Setup(ValueType.UShort);
953 Set((ushort)obj);
954 }
955 else if (objType == typeof(int))
956 {
957 Setup(ValueType.Int32);
958 Set((int)obj);
959 }
960 else if (objType == typeof(uint))
961 {
962 Setup(ValueType.UInt32);
963 Set((uint)obj);
964 }
965 else if (objType == typeof(long))
966 {
967 Setup(ValueType.Int64);
968 Set((long)obj);
969 }
970 else if (objType == typeof(ulong))
971 {
972 Setup(ValueType.UInt64);
973 Set((ulong)obj);
974 }
975 else if (objType == typeof(float))
976 {
977 Setup(ValueType.Float);
978 Set((float)obj);
979 }
980 else if (objType == typeof(double))
981 {
982 Setup(ValueType.Double);
983 Set((double)obj);
984 }
985 else if (objType == typeof(string))
986 {
987 Setup(ValueType.String);
988 Set(obj as string);
989 }
990 else if (typeof(Efl.Object).IsAssignableFrom(objType))
991 {
992 Setup(ValueType.Object);
993 Set(obj as Efl.Object);
994 }
995 else
996 {
997 throw new ArgumentException($"Unsupported type for direct construction: {objType}");
998 }
920 } 999 }
921 1000
922 public Value(IntPtr handle, Ownership ownership = Ownership.Managed) 1001 public Value(IntPtr handle, Ownership ownership = Ownership.Managed)
@@ -1390,6 +1469,95 @@ public class Value : IDisposable, IComparable<Value>, IEquatable<Value>
1390 return b; 1469 return b;
1391 } 1470 }
1392 1471
1472 /// <summary>Unwrap the value into its underlying C# value.
1473 ///
1474 /// <para>Useful for methods like <see crev="PropertyInfo.SetValue(object, object)" />
1475 /// as it will unpack the value to it correct C# type.</para>
1476 /// </summary>
1477 /// <returns>The C# value wrapped by this value.</returns>
1478 public object Unwrap()
1479 {
1480 switch (GetValueType())
1481 {
1482 case ValueType.SByte:
1483 {
1484 sbyte o;
1485 Get(out o);
1486 return o;
1487 }
1488 case ValueType.Byte:
1489 {
1490 byte o;
1491 Get(out o);
1492 return o;
1493 }
1494 case ValueType.Short:
1495 {
1496 short o;
1497 Get(out o);
1498 return o;
1499 }
1500 case ValueType.UShort:
1501 {
1502 ushort o;
1503 Get(out o);
1504 return o;
1505 }
1506 case ValueType.Int32:
1507 {
1508 int o;
1509 Get(out o);
1510 return o;
1511 }
1512 case ValueType.UInt32:
1513 {
1514 uint o;
1515 Get(out o);
1516 return o;
1517 }
1518 case ValueType.Int64:
1519 case ValueType.Long:
1520 {
1521 long o;
1522 Get(out o);
1523 return o;
1524 }
1525 case ValueType.UInt64:
1526 case ValueType.ULong:
1527 {
1528 ulong o;
1529 Get(out o);
1530 return o;
1531 }
1532 case ValueType.Float:
1533 {
1534 float o;
1535 Get(out o);
1536 return o;
1537 }
1538 case ValueType.Double:
1539 {
1540 double o;
1541 Get(out o);
1542 return o;
1543 }
1544 case ValueType.String:
1545 {
1546 string o;
1547 Get(out o);
1548 return o;
1549 }
1550 case ValueType.Object:
1551 {
1552 Efl.Object o;
1553 Get(out o);
1554 return o;
1555 }
1556 default:
1557 throw new InvalidOperationException($"Unsupported value type to unwrap: {GetValueType()}");
1558 }
1559 }
1560
1393 // Efl.Object conversions are made explicit to avoid ambiguity between 1561 // Efl.Object conversions are made explicit to avoid ambiguity between
1394 // Set(Efl.Object) and Set(Value) when dealing with classes derived from 1562 // Set(Efl.Object) and Set(Value) when dealing with classes derived from
1395 // Efl.Object. 1563 // Efl.Object.
@@ -1828,7 +1996,7 @@ public class Value : IDisposable, IComparable<Value>, IEquatable<Value>
1828 if (!GetValueType().IsString()) 1996 if (!GetValueType().IsString())
1829 { 1997 {
1830 throw (new ArgumentException( 1998 throw (new ArgumentException(
1831 "Trying to set non-string value on a string Eina.Value")); 1999 "Trying to set string value on a non-string Eina.Value"));
1832 } 2000 }
1833 2001
1834 // No need to worry about ownership as eina_value_set will copy the passed string. 2002 // No need to worry about ownership as eina_value_set will copy the passed string.