21ef05268f2a3613d1bed8faa05e6ae3d6a26db1
[platform/core/csapi/tizenfx.git] / internals / src / EflSharp / EflSharp / efl / WrapperSupervisor.cs
1 using System;
2 using EventDictionary = System.Collections.Generic.Dictionary<(System.IntPtr desc, object evtDelegate), (System.IntPtr evtCallerPtr, Efl.EventCb evtCaller)>;
3
4 namespace Efl
5 {
6
7 namespace Eo
8 {
9
10 /// <summary>Observe the ownership state  of an Eo wrapper and control its life-cycle.</summary>
11 public class WrapperSupervisor
12 {
13     private System.WeakReference weakRef;
14 #pragma warning disable CS0414
15     private Efl.Eo.IWrapper sharedRef;
16 #pragma warning restore CS0414
17     private EventDictionary eoEvents;
18
19     /// <summary>Create a new supervisor for the given.</summary>
20     /// <param name="obj">Efl object to be supervised.</param>
21     public WrapperSupervisor(Efl.Eo.IWrapper obj)
22     {
23         weakRef = new WeakReference(obj);
24         sharedRef = null;
25         eoEvents = new EventDictionary();
26     }
27
28     /// <summary>Efl object being supervised.</summary>
29     public Efl.Eo.IWrapper Target
30     {
31         get
32         {
33             return (Efl.Eo.IWrapper) weakRef.Target;
34         }
35     }
36
37     /// <summary>Dictionary that holds the events related with the supervised object.</summary>
38     public EventDictionary EoEvents
39     {
40         get
41         {
42             return eoEvents;
43         }
44     }
45
46     /// <summary>To be called when the object is uniquely owned by C#, removing its strong reference and making it available to garbage collection.</summary>
47     public void MakeUnique()
48     {
49         sharedRef = null;
50     }
51
52     /// <summary>To be called when the object is owned in the native library too, adding a strong reference to it and making it unavailable for garbage collection.</summary>
53     public void MakeShared()
54     {
55         if (this.Target == null)
56             throw new InvalidOperationException("Tried to make a null reference shared.");
57         sharedRef = this.Target;
58     }
59 }
60
61 }
62
63 }
64