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