summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVitor Sousa <vitorsousa@expertisesolutions.com.br>2019-06-28 18:20:01 -0300
committerJaehyun-Cho <35288529+Jaehyun-Cho@users.noreply.github.com>2019-07-05 09:57:23 +0900
commit73df0d47ff914211ee0a0b5fe46a381f1cd30f10 (patch)
tree4c805aafe893038363f97bd1b18be7264536d08d
parenta94c72f4dec91153d45b5e98bdb169b56fe0132b (diff)
csharp: encapsulate some internal code of EoWrapper
Summary: Encapsulate some parts of EoWrapper making them less accessible to lib users. This can avoid unnecessary and risky usage of code that is only intended for internal usage. `inherited` field was made private and renamed to `generated`. Now its value can only be obtained through the `IsGeneratedBindingClass` property. `handle` field was made private. `eventLock` was renamed to `eflBindingEventLock` `ConstructingHandle` property set was made private. Constructors that are used to create new EFL# managed objects by wrapping a preexisting eo handle now receive a specific struct wrapping the handle pointer. This can avoid faulty interactions with the Reflection engine used only for generated classes that implement this constructor. Test Plan: meson test Reviewers: lauromoura, felipealmeida, YOhoho Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D9212
-rw-r--r--src/bin/eolian_mono/eolian/mono/events.hh4
-rw-r--r--src/bin/eolian_mono/eolian/mono/function_definition.hh5
-rw-r--r--src/bin/eolian_mono/eolian/mono/klass.hh6
-rw-r--r--src/bindings/mono/eo_mono/EoWrapper.cs28
-rw-r--r--src/bindings/mono/eo_mono/iwrapper.cs20
-rw-r--r--src/tests/efl_mono/EoConstruction.cs9
6 files changed, 48 insertions, 24 deletions
diff --git a/src/bin/eolian_mono/eolian/mono/events.hh b/src/bin/eolian_mono/eolian/mono/events.hh
index 9b4b8c3..c1a4503 100644
--- a/src/bin/eolian_mono/eolian/mono/events.hh
+++ b/src/bin/eolian_mono/eolian/mono/events.hh
@@ -435,7 +435,7 @@ struct event_definition_generator
435 scope_tab << "{\n" 435 scope_tab << "{\n"
436 << scope_tab << scope_tab << "add\n" 436 << scope_tab << scope_tab << "add\n"
437 << scope_tab << scope_tab << "{\n" 437 << scope_tab << scope_tab << "{\n"
438 << scope_tab << scope_tab << scope_tab << "lock (eventLock)\n" 438 << scope_tab << scope_tab << scope_tab << "lock (eflBindingEventLock)\n"
439 << scope_tab << scope_tab << scope_tab << "{\n" 439 << scope_tab << scope_tab << scope_tab << "{\n"
440 << scope_tab << scope_tab << scope_tab << scope_tab << "Efl.EventCb callerCb = (IntPtr data, ref Efl.Event.NativeStruct evt) =>\n" 440 << scope_tab << scope_tab << scope_tab << scope_tab << "Efl.EventCb callerCb = (IntPtr data, ref Efl.Event.NativeStruct evt) =>\n"
441 << scope_tab << scope_tab << scope_tab << scope_tab << "{\n" 441 << scope_tab << scope_tab << scope_tab << scope_tab << "{\n"
@@ -460,7 +460,7 @@ struct event_definition_generator
460 << scope_tab << scope_tab << "}\n\n" 460 << scope_tab << scope_tab << "}\n\n"
461 << scope_tab << scope_tab << "remove\n" 461 << scope_tab << scope_tab << "remove\n"
462 << scope_tab << scope_tab << "{\n" 462 << scope_tab << scope_tab << "{\n"
463 << scope_tab << scope_tab << scope_tab << "lock (eventLock)\n" 463 << scope_tab << scope_tab << scope_tab << "lock (eflBindingEventLock)\n"
464 << scope_tab << scope_tab << scope_tab << "{\n" 464 << scope_tab << scope_tab << scope_tab << "{\n"
465 << scope_tab << scope_tab << scope_tab << scope_tab << "string key = \"_" << upper_c_name << "\";\n" 465 << scope_tab << scope_tab << scope_tab << scope_tab << "string key = \"_" << upper_c_name << "\";\n"
466 << scope_tab << scope_tab << scope_tab << scope_tab << "RemoveNativeEventHandler(" << library_name << ", key, value);\n" 466 << scope_tab << scope_tab << scope_tab << scope_tab << "RemoveNativeEventHandler(" << library_name << ", key, value);\n"
diff --git a/src/bin/eolian_mono/eolian/mono/function_definition.hh b/src/bin/eolian_mono/eolian/mono/function_definition.hh
index f2dda1e..8c43008 100644
--- a/src/bin/eolian_mono/eolian/mono/function_definition.hh
+++ b/src/bin/eolian_mono/eolian/mono/function_definition.hh
@@ -178,9 +178,10 @@ struct function_definition_generator
178 178
179 std::string self = "this.NativeHandle"; 179 std::string self = "this.NativeHandle";
180 180
181 // inherited is set in the constructor, true if this instance is from a pure C# class (not generated). 181 // IsGeneratedBindingClass is set in the constructor, true if this
182 // instance is from a pure C# class (not generated).
182 if (do_super && !f.is_static) 183 if (do_super && !f.is_static)
183 self = "(inherited ? Efl.Eo.Globals.efl_super(" + self + ", this.NativeClass) : " + self + ")"; 184 self = "(IsGeneratedBindingClass ? " + self + " : Efl.Eo.Globals.efl_super(" + self + ", this.NativeClass))";
184 else if (f.is_static) 185 else if (f.is_static)
185 self = ""; 186 self = "";
186 187
diff --git a/src/bin/eolian_mono/eolian/mono/klass.hh b/src/bin/eolian_mono/eolian/mono/klass.hh
index 8b018f6..8f99326 100644
--- a/src/bin/eolian_mono/eolian/mono/klass.hh
+++ b/src/bin/eolian_mono/eolian/mono/klass.hh
@@ -214,7 +214,7 @@ struct klass
214 << scope_tab << scope_tab << name_helpers::klass_get_name(cls) << "();\n" 214 << scope_tab << scope_tab << name_helpers::klass_get_name(cls) << "();\n"
215 << scope_tab << "/// <summary>Initializes a new instance of the <see cref=\"" << interface_name << "\"/> class.\n" 215 << scope_tab << "/// <summary>Initializes a new instance of the <see cref=\"" << interface_name << "\"/> class.\n"
216 << scope_tab << "/// Internal usage: This is used when interacting with C code and should not be used directly.</summary>\n" 216 << scope_tab << "/// Internal usage: This is used when interacting with C code and should not be used directly.</summary>\n"
217 << scope_tab << "private " << concrete_name << "(System.IntPtr raw) : base(raw)\n" 217 << scope_tab << "private " << concrete_name << "(Efl.Eo.Globals.WrappingHandle wh) : base(wh)\n"
218 << scope_tab << "{\n" 218 << scope_tab << "{\n"
219 << scope_tab << "}\n\n" 219 << scope_tab << "}\n\n"
220 ) 220 )
@@ -511,7 +511,7 @@ struct klass
511 << scope_tab << "/// <summary>Initializes a new instance of the <see cref=\"" << inherit_name << "\"/> class.\n" 511 << scope_tab << "/// <summary>Initializes a new instance of the <see cref=\"" << inherit_name << "\"/> class.\n"
512 << scope_tab << "/// Internal usage: Constructs an instance from a native pointer. This is used when interacting with C code and should not be used directly.</summary>\n" 512 << scope_tab << "/// Internal usage: Constructs an instance from a native pointer. This is used when interacting with C code and should not be used directly.</summary>\n"
513 << scope_tab << "/// <param name=\"raw\">The native pointer to be wrapped.</param>\n" 513 << scope_tab << "/// <param name=\"raw\">The native pointer to be wrapped.</param>\n"
514 << scope_tab << "protected " << inherit_name << "(System.IntPtr raw) : base(raw)\n" 514 << scope_tab << "protected " << inherit_name << "(Efl.Eo.Globals.WrappingHandle wh) : base(wh)\n"
515 << scope_tab << "{\n" 515 << scope_tab << "{\n"
516 << scope_tab << "}\n\n" 516 << scope_tab << "}\n\n"
517 ).generate(sink, std::make_tuple(constructors, constructors, constructors), context)) 517 ).generate(sink, std::make_tuple(constructors, constructors, constructors), context))
@@ -526,7 +526,7 @@ struct klass
526 scope_tab << "[Efl.Eo.PrivateNativeClass]\n" 526 scope_tab << "[Efl.Eo.PrivateNativeClass]\n"
527 << scope_tab << "private class " << inherit_name << "Realized : " << inherit_name << "\n" 527 << scope_tab << "private class " << inherit_name << "Realized : " << inherit_name << "\n"
528 << scope_tab << "{\n" 528 << scope_tab << "{\n"
529 << scope_tab << scope_tab << "private " << inherit_name << "Realized(IntPtr ptr) : base(ptr)\n" 529 << scope_tab << scope_tab << "private " << inherit_name << "Realized(Efl.Eo.Globals.WrappingHandle wh) : base(wh)\n"
530 << scope_tab << scope_tab << "{\n" 530 << scope_tab << scope_tab << "{\n"
531 << scope_tab << scope_tab << "}\n" 531 << scope_tab << scope_tab << "}\n"
532 << scope_tab << "}\n" 532 << scope_tab << "}\n"
diff --git a/src/bindings/mono/eo_mono/EoWrapper.cs b/src/bindings/mono/eo_mono/EoWrapper.cs
index 7e512ac..5b14b48 100644
--- a/src/bindings/mono/eo_mono/EoWrapper.cs
+++ b/src/bindings/mono/eo_mono/EoWrapper.cs
@@ -12,9 +12,9 @@ namespace Eo
12 12
13public abstract class EoWrapper : IWrapper, IDisposable 13public abstract class EoWrapper : IWrapper, IDisposable
14{ 14{
15 protected readonly object eventLock = new object(); 15 protected readonly object eflBindingEventLock = new object();
16 protected bool inherited = false; 16 private bool generated = true;
17 protected System.IntPtr handle = IntPtr.Zero; 17 private System.IntPtr handle = IntPtr.Zero;
18 18
19 private static Efl.EventCb ownershipUniqueDelegate = new Efl.EventCb(OwnershipUniqueCallback); 19 private static Efl.EventCb ownershipUniqueDelegate = new Efl.EventCb(OwnershipUniqueCallback);
20 private static Efl.EventCb ownershipSharedDelegate = new Efl.EventCb(OwnershipSharedCallback); 20 private static Efl.EventCb ownershipSharedDelegate = new Efl.EventCb(OwnershipSharedCallback);
@@ -32,7 +32,7 @@ public abstract class EoWrapper : IWrapper, IDisposable
32 /// <param name="ch">Tag struct storing the native handle of the object being constructed.</param> 32 /// <param name="ch">Tag struct storing the native handle of the object being constructed.</param>
33 protected EoWrapper(ConstructingHandle ch) 33 protected EoWrapper(ConstructingHandle ch)
34 { 34 {
35 inherited = true; 35 generated = false;
36 handle = Efl.Eo.Globals.efl_constructor(Efl.Eo.Globals.efl_super(ch.NativeHandle, Efl.Eo.Globals.efl_class_get(ch.NativeHandle))); 36 handle = Efl.Eo.Globals.efl_constructor(Efl.Eo.Globals.efl_super(ch.NativeHandle, Efl.Eo.Globals.efl_class_get(ch.NativeHandle)));
37 if (handle == IntPtr.Zero) 37 if (handle == IntPtr.Zero)
38 { 38 {
@@ -47,11 +47,12 @@ public abstract class EoWrapper : IWrapper, IDisposable
47 } 47 }
48 48
49 /// <summary>Initializes a new instance of the <see cref="Object"/> class. 49 /// <summary>Initializes a new instance of the <see cref="Object"/> class.
50 /// Internal usage: Constructs an instance from a native pointer. This is used when interacting with C code and should not be used directly.</summary> 50 /// Internal usage: Constructs an instance from a native pointer. This is used when interacting with C code and should not be used directly.
51 /// Do not implement this constructor.</summary>
51 /// <param name="raw">The native pointer to be wrapped.</param> 52 /// <param name="raw">The native pointer to be wrapped.</param>
52 protected EoWrapper(System.IntPtr raw) 53 protected EoWrapper(Efl.Eo.Globals.WrappingHandle wh)
53 { 54 {
54 handle = raw; 55 handle = wh.NativeHandle;
55 AddWrapperSupervisor(); 56 AddWrapperSupervisor();
56 } 57 }
57 58
@@ -67,9 +68,9 @@ public abstract class EoWrapper : IWrapper, IDisposable
67 [CallerFilePath] string file = null, 68 [CallerFilePath] string file = null,
68 [CallerLineNumber] int line = 0) 69 [CallerLineNumber] int line = 0)
69 { 70 {
70 inherited = ((object)this).GetType() != managedType; 71 generated = ((object)this).GetType() == managedType;
71 IntPtr actual_klass = baseKlass; 72 IntPtr actual_klass = baseKlass;
72 if (inherited) 73 if (!generated)
73 { 74 {
74 actual_klass = Efl.Eo.ClassRegister.GetInheritKlassOrRegister(baseKlass, ((object)this).GetType()); 75 actual_klass = Efl.Eo.ClassRegister.GetInheritKlassOrRegister(baseKlass, ((object)this).GetType());
75 } 76 }
@@ -82,7 +83,7 @@ public abstract class EoWrapper : IWrapper, IDisposable
82 parent_ptr = parent.NativeHandle; 83 parent_ptr = parent.NativeHandle;
83 } 84 }
84 85
85 if (!inherited) 86 if (generated)
86 { 87 {
87 handle = Efl.Eo.Globals._efl_add_internal_start(file, line, actual_klass, parent_ptr, 1, 0); 88 handle = Efl.Eo.Globals._efl_add_internal_start(file, line, actual_klass, parent_ptr, 1, 0);
88 } 89 }
@@ -123,6 +124,11 @@ public abstract class EoWrapper : IWrapper, IDisposable
123 get; 124 get;
124 } 125 }
125 126
127 protected bool IsGeneratedBindingClass
128 {
129 get { return generated; }
130 }
131
126 /// <summary>Releases the underlying native instance.</summary> 132 /// <summary>Releases the underlying native instance.</summary>
127 protected virtual void Dispose(bool disposing) 133 protected virtual void Dispose(bool disposing)
128 { 134 {
@@ -292,7 +298,7 @@ public abstract class EoWrapper : IWrapper, IDisposable
292 NativeHandle = h; 298 NativeHandle = h;
293 } 299 }
294 300
295 public IntPtr NativeHandle { get; set; } 301 public IntPtr NativeHandle { get; private set; }
296 } 302 }
297 303
298 public abstract class NativeMethods : Efl.Eo.NativeClass 304 public abstract class NativeMethods : Efl.Eo.NativeClass
diff --git a/src/bindings/mono/eo_mono/iwrapper.cs b/src/bindings/mono/eo_mono/iwrapper.cs
index caf28a7..7c2eeb4 100644
--- a/src/bindings/mono/eo_mono/iwrapper.cs
+++ b/src/bindings/mono/eo_mono/iwrapper.cs
@@ -624,14 +624,15 @@ public class Globals
624 try 624 try
625 { 625 {
626 var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 626 var flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
627 constructor = managedType.GetConstructor(flags, null, new Type[1] { typeof(System.IntPtr) }, null); 627 constructor = managedType.GetConstructor(flags, null, new Type[1] { typeof(WrappingHandle) }, null);
628 } 628 }
629 catch (InvalidOperationException) 629 catch (InvalidOperationException)
630 { 630 {
631 throw new InvalidOperationException($"Can't get constructor for type {managedType}"); 631 throw new InvalidOperationException($"Can't get constructor for type {managedType}");
632 } 632 }
633 633
634 var ret = (Efl.Eo.IWrapper) constructor.Invoke(new object[1] { handle }); 634 WrappingHandle wh = new WrappingHandle(handle);
635 var ret = (Efl.Eo.IWrapper) constructor.Invoke(new object[1] { wh });
635 636
636 if (ret == null) 637 if (ret == null)
637 { 638 {
@@ -715,6 +716,21 @@ public class Globals
715 Monitor.Exit(Efl.All.InitLock); 716 Monitor.Exit(Efl.All.InitLock);
716 } 717 }
717 718
719 /// <sumary>
720 /// Internal struct used by the binding to pass the native handle pointer
721 /// to the managed object wrapping constructor.
722 /// Internal usage only: do not use this class in inherited classes.
723 /// </sumary>
724 public struct WrappingHandle
725 {
726 public WrappingHandle(IntPtr h)
727 {
728 NativeHandle = h;
729 }
730
731 public IntPtr NativeHandle { get; private set; }
732 }
733
718} // Globals 734} // Globals
719 735
720public static class Config 736public static class Config
diff --git a/src/tests/efl_mono/EoConstruction.cs b/src/tests/efl_mono/EoConstruction.cs
index 4708bef..bfcf40b 100644
--- a/src/tests/efl_mono/EoConstruction.cs
+++ b/src/tests/efl_mono/EoConstruction.cs
@@ -27,9 +27,10 @@ class InheritedConstructibleObject : Dummy.ConstructibleObject
27 public int DefaultConstrutorCallCount { get; set; } = 0; 27 public int DefaultConstrutorCallCount { get; set; } = 0;
28 public int SpecialConstrutorCallCount { get; set; } = 0; 28 public int SpecialConstrutorCallCount { get; set; } = 0;
29 29
30 public bool InheritedFlag 30 /// <summary>Pointer to the native class description.</summary>
31 public bool IsInheritedClass
31 { 32 {
32 get { return inherited; } 33 get { return !this.IsGeneratedBindingClass; }
33 } 34 }
34 35
35 public override int MultiplyIntegerValue(int v) 36 public override int MultiplyIntegerValue(int v)
@@ -56,7 +57,7 @@ class TestEoConstruction
56 public static void TestInheritedEoDirectConstruction() 57 public static void TestInheritedEoDirectConstruction()
57 { 58 {
58 var obj = new InheritedConstructibleObject(); 59 var obj = new InheritedConstructibleObject();
59 Test.AssertEquals(obj.InheritedFlag, true); 60 Test.AssertEquals(obj.IsInheritedClass, true);
60 Test.AssertEquals(obj.NativeConstructionCount, 1); 61 Test.AssertEquals(obj.NativeConstructionCount, 1);
61 Test.AssertEquals(obj.DefaultConstructionCount, 1); 62 Test.AssertEquals(obj.DefaultConstructionCount, 1);
62 Test.AssertEquals(obj.SpecialConstructionCount, 0); 63 Test.AssertEquals(obj.SpecialConstructionCount, 0);
@@ -75,7 +76,7 @@ class TestEoConstruction
75 Test.AssertEquals(obj.MultiplyIntegerValue(21), 42); 76 Test.AssertEquals(obj.MultiplyIntegerValue(21), 42);
76 77
77 var obj2 = (InheritedConstructibleObject) obj.ConstructTypeAndStore(typeof(InheritedConstructibleObject)); 78 var obj2 = (InheritedConstructibleObject) obj.ConstructTypeAndStore(typeof(InheritedConstructibleObject));
78 Test.AssertEquals(obj2.InheritedFlag, true); 79 Test.AssertEquals(obj2.IsInheritedClass, true);
79 Test.AssertEquals(obj2.NativeConstructionCount, 1); 80 Test.AssertEquals(obj2.NativeConstructionCount, 1);
80 Test.AssertEquals(obj2.DefaultConstructionCount, 0); 81 Test.AssertEquals(obj2.DefaultConstructionCount, 0);
81 Test.AssertEquals(obj2.SpecialConstructionCount, 1); 82 Test.AssertEquals(obj2.SpecialConstructionCount, 1);