[Tizen] Ensuring that Registry.Unregister is called from the most derived class befor...
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / ObjectRegistry.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     using System;
21     using System.Runtime.InteropServices;
22
23
24     internal class ObjectRegistry : BaseHandle
25     {
26         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
27
28         internal ObjectRegistry(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ObjectRegistry_SWIGUpcast(cPtr), cMemoryOwn)
29         {
30             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
31         }
32
33         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ObjectRegistry obj)
34         {
35             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
36         }
37
38         protected override void Dispose(DisposeTypes type)
39         {
40             if (disposed)
41             {
42                 return;
43             }
44
45             if (type == DisposeTypes.Explicit)
46             {
47                 //Called by User
48                 //Release your own managed resources here.
49                 //You should release all of your own disposable objects here.
50
51             }
52
53             //Release your own unmanaged resources here.
54             //You should not access any managed member here except static instance.
55             //because the execution order of Finalizes is non-deterministic.
56
57             if (swigCPtr.Handle != global::System.IntPtr.Zero)
58             {
59                 if (swigCMemOwn)
60                 {
61                     swigCMemOwn = false;
62
63                     //Unreference this instance from Registry.
64                     Registry.Unregister(this);
65
66                     NDalicPINVOKE.delete_ObjectRegistry(swigCPtr);
67                 }
68                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
69             }
70
71             base.Dispose(type);
72         }
73
74
75         public class ObjectCreatedEventArgs : EventArgs
76         {
77             private BaseHandle _baseHandle;
78
79             public BaseHandle BaseHandle
80             {
81                 get
82                 {
83                     return _baseHandle;
84                 }
85                 set
86                 {
87                     _baseHandle = value;
88                 }
89             }
90         }
91
92         public class ObjectDestroyedEventArgs : EventArgs
93         {
94             private RefObject _refObject;
95
96             public RefObject RefObject
97             {
98                 get
99                 {
100                     return _refObject;
101                 }
102                 set
103                 {
104                     _refObject = value;
105                 }
106             }
107         }
108
109
110         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
111         private delegate void ObjectCreatedEventCallbackDelegate(IntPtr baseHandle);
112         private DaliEventHandler<object, ObjectCreatedEventArgs> _objectRegistryObjectCreatedEventHandler;
113         private ObjectCreatedEventCallbackDelegate _objectRegistryObjectCreatedEventCallbackDelegate;
114
115         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
116         private delegate void ObjectDestroyedEventCallbackDelegate(IntPtr fefObject);
117         private DaliEventHandler<object, ObjectDestroyedEventArgs> _objectRegistryObjectDestroyedEventHandler;
118         private ObjectDestroyedEventCallbackDelegate _objectRegistryObjectDestroyedEventCallbackDelegate;
119
120         public event DaliEventHandler<object, ObjectCreatedEventArgs> ObjectCreated
121         {
122             add
123             {
124                 lock (this)
125                 {
126                     // Restricted to only one listener
127                     if (_objectRegistryObjectCreatedEventHandler == null)
128                     {
129                         _objectRegistryObjectCreatedEventHandler += value;
130
131                         _objectRegistryObjectCreatedEventCallbackDelegate = new ObjectCreatedEventCallbackDelegate(OnObjectCreated);
132                         this.ObjectCreatedSignal().Connect(_objectRegistryObjectCreatedEventCallbackDelegate);
133                     }
134                 }
135             }
136
137             remove
138             {
139                 lock (this)
140                 {
141                     if (_objectRegistryObjectCreatedEventHandler != null)
142                     {
143                         this.ObjectCreatedSignal().Disconnect(_objectRegistryObjectCreatedEventCallbackDelegate);
144                     }
145
146                     _objectRegistryObjectCreatedEventHandler -= value;
147                 }
148             }
149         }
150
151         // Callback for ObjectRegistry ObjectCreatedSignal
152         private void OnObjectCreated(IntPtr baseHandle)
153         {
154             ObjectCreatedEventArgs e = new ObjectCreatedEventArgs();
155
156             // Populate all members of "e" (ObjectCreatedEventArgs) with real data
157             //e.BaseHandle = BaseHandle.GetBaseHandleFromPtr(baseHandle); //GetBaseHandleFromPtr() is not present in BaseHandle.cs. Not sure what is the reason?
158
159             if (_objectRegistryObjectCreatedEventHandler != null)
160             {
161                 //here we send all data to user event handlers
162                 _objectRegistryObjectCreatedEventHandler(this, e);
163             }
164         }
165
166         public event DaliEventHandler<object, ObjectDestroyedEventArgs> ObjectDestroyed
167         {
168             add
169             {
170                 lock (this)
171                 {
172                     // Restricted to only one listener
173                     if (_objectRegistryObjectDestroyedEventHandler == null)
174                     {
175                         _objectRegistryObjectDestroyedEventHandler += value;
176
177                         _objectRegistryObjectDestroyedEventCallbackDelegate = new ObjectDestroyedEventCallbackDelegate(OnObjectDestroyed);
178                         this.ObjectDestroyedSignal().Connect(_objectRegistryObjectDestroyedEventCallbackDelegate);
179                     }
180                 }
181             }
182
183             remove
184             {
185                 lock (this)
186                 {
187                     if (_objectRegistryObjectDestroyedEventHandler != null)
188                     {
189                         this.ObjectDestroyedSignal().Disconnect(_objectRegistryObjectDestroyedEventCallbackDelegate);
190                     }
191
192                     _objectRegistryObjectDestroyedEventHandler -= value;
193                 }
194             }
195         }
196
197         // Callback for ObjectRegistry ObjectDestroyedSignal
198         private void OnObjectDestroyed(IntPtr refObject)
199         {
200             ObjectDestroyedEventArgs e = new ObjectDestroyedEventArgs();
201
202             // Populate all members of "e" (ObjectDestroyedEventArgs) with real data
203             e.RefObject = RefObject.GetRefObjectFromPtr(refObject);
204
205             if (_objectRegistryObjectDestroyedEventHandler != null)
206             {
207                 //here we send all data to user event handlers
208                 _objectRegistryObjectDestroyedEventHandler(this, e);
209             }
210         }
211
212
213         public ObjectRegistry() : this(NDalicPINVOKE.new_ObjectRegistry__SWIG_0(), true)
214         {
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216         }
217
218         public ObjectRegistry(ObjectRegistry handle) : this(NDalicPINVOKE.new_ObjectRegistry__SWIG_1(ObjectRegistry.getCPtr(handle)), true)
219         {
220             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
221         }
222
223         public ObjectRegistry Assign(ObjectRegistry rhs)
224         {
225             ObjectRegistry ret = new ObjectRegistry(NDalicPINVOKE.ObjectRegistry_Assign(swigCPtr, ObjectRegistry.getCPtr(rhs)), false);
226             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
227             return ret;
228         }
229
230         public ObjectCreatedSignal ObjectCreatedSignal()
231         {
232             ObjectCreatedSignal ret = new ObjectCreatedSignal(NDalicPINVOKE.ObjectRegistry_ObjectCreatedSignal(swigCPtr), false);
233             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234             return ret;
235         }
236
237         public ObjectDestroyedSignal ObjectDestroyedSignal()
238         {
239             ObjectDestroyedSignal ret = new ObjectDestroyedSignal(NDalicPINVOKE.ObjectRegistry_ObjectDestroyedSignal(swigCPtr), false);
240             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
241             return ret;
242         }
243
244     }
245
246 }