Merge remote-tracking branch 'oauth2/tizen'
[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                     NDalicPINVOKE.delete_ObjectRegistry(swigCPtr);
63                 }
64                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
65             }
66
67             base.Dispose(type);
68         }
69
70
71         public class ObjectCreatedEventArgs : EventArgs
72         {
73             private BaseHandle _baseHandle;
74
75             public BaseHandle BaseHandle
76             {
77                 get
78                 {
79                     return _baseHandle;
80                 }
81                 set
82                 {
83                     _baseHandle = value;
84                 }
85             }
86         }
87
88         public class ObjectDestroyedEventArgs : EventArgs
89         {
90             private RefObject _refObject;
91
92             public RefObject RefObject
93             {
94                 get
95                 {
96                     return _refObject;
97                 }
98                 set
99                 {
100                     _refObject = value;
101                 }
102             }
103         }
104
105
106         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
107         private delegate void ObjectCreatedEventCallbackDelegate(IntPtr baseHandle);
108         private DaliEventHandler<object, ObjectCreatedEventArgs> _objectRegistryObjectCreatedEventHandler;
109         private ObjectCreatedEventCallbackDelegate _objectRegistryObjectCreatedEventCallbackDelegate;
110
111         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
112         private delegate void ObjectDestroyedEventCallbackDelegate(IntPtr fefObject);
113         private DaliEventHandler<object, ObjectDestroyedEventArgs> _objectRegistryObjectDestroyedEventHandler;
114         private ObjectDestroyedEventCallbackDelegate _objectRegistryObjectDestroyedEventCallbackDelegate;
115
116         public event DaliEventHandler<object, ObjectCreatedEventArgs> ObjectCreated
117         {
118             add
119             {
120                 lock (this)
121                 {
122                     // Restricted to only one listener
123                     if (_objectRegistryObjectCreatedEventHandler == null)
124                     {
125                         _objectRegistryObjectCreatedEventHandler += value;
126
127                         _objectRegistryObjectCreatedEventCallbackDelegate = new ObjectCreatedEventCallbackDelegate(OnObjectCreated);
128                         this.ObjectCreatedSignal().Connect(_objectRegistryObjectCreatedEventCallbackDelegate);
129                     }
130                 }
131             }
132
133             remove
134             {
135                 lock (this)
136                 {
137                     if (_objectRegistryObjectCreatedEventHandler != null)
138                     {
139                         this.ObjectCreatedSignal().Disconnect(_objectRegistryObjectCreatedEventCallbackDelegate);
140                     }
141
142                     _objectRegistryObjectCreatedEventHandler -= value;
143                 }
144             }
145         }
146
147         // Callback for ObjectRegistry ObjectCreatedSignal
148         private void OnObjectCreated(IntPtr baseHandle)
149         {
150             ObjectCreatedEventArgs e = new ObjectCreatedEventArgs();
151
152             // Populate all members of "e" (ObjectCreatedEventArgs) with real data
153             //e.BaseHandle = BaseHandle.GetBaseHandleFromPtr(baseHandle); //GetBaseHandleFromPtr() is not present in BaseHandle.cs. Not sure what is the reason?
154
155             if (_objectRegistryObjectCreatedEventHandler != null)
156             {
157                 //here we send all data to user event handlers
158                 _objectRegistryObjectCreatedEventHandler(this, e);
159             }
160         }
161
162         public event DaliEventHandler<object, ObjectDestroyedEventArgs> ObjectDestroyed
163         {
164             add
165             {
166                 lock (this)
167                 {
168                     // Restricted to only one listener
169                     if (_objectRegistryObjectDestroyedEventHandler == null)
170                     {
171                         _objectRegistryObjectDestroyedEventHandler += value;
172
173                         _objectRegistryObjectDestroyedEventCallbackDelegate = new ObjectDestroyedEventCallbackDelegate(OnObjectDestroyed);
174                         this.ObjectDestroyedSignal().Connect(_objectRegistryObjectDestroyedEventCallbackDelegate);
175                     }
176                 }
177             }
178
179             remove
180             {
181                 lock (this)
182                 {
183                     if (_objectRegistryObjectDestroyedEventHandler != null)
184                     {
185                         this.ObjectDestroyedSignal().Disconnect(_objectRegistryObjectDestroyedEventCallbackDelegate);
186                     }
187
188                     _objectRegistryObjectDestroyedEventHandler -= value;
189                 }
190             }
191         }
192
193         // Callback for ObjectRegistry ObjectDestroyedSignal
194         private void OnObjectDestroyed(IntPtr refObject)
195         {
196             ObjectDestroyedEventArgs e = new ObjectDestroyedEventArgs();
197
198             // Populate all members of "e" (ObjectDestroyedEventArgs) with real data
199             e.RefObject = RefObject.GetRefObjectFromPtr(refObject);
200
201             if (_objectRegistryObjectDestroyedEventHandler != null)
202             {
203                 //here we send all data to user event handlers
204                 _objectRegistryObjectDestroyedEventHandler(this, e);
205             }
206         }
207
208
209         public ObjectRegistry() : this(NDalicPINVOKE.new_ObjectRegistry__SWIG_0(), true)
210         {
211             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212         }
213
214         public ObjectRegistry(ObjectRegistry handle) : this(NDalicPINVOKE.new_ObjectRegistry__SWIG_1(ObjectRegistry.getCPtr(handle)), true)
215         {
216             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
217         }
218
219         public ObjectRegistry Assign(ObjectRegistry rhs)
220         {
221             ObjectRegistry ret = new ObjectRegistry(NDalicPINVOKE.ObjectRegistry_Assign(swigCPtr, ObjectRegistry.getCPtr(rhs)), false);
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223             return ret;
224         }
225
226         public ObjectCreatedSignal ObjectCreatedSignal()
227         {
228             ObjectCreatedSignal ret = new ObjectCreatedSignal(NDalicPINVOKE.ObjectRegistry_ObjectCreatedSignal(swigCPtr), false);
229             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230             return ret;
231         }
232
233         public ObjectDestroyedSignal ObjectDestroyedSignal()
234         {
235             ObjectDestroyedSignal ret = new ObjectDestroyedSignal(NDalicPINVOKE.ObjectRegistry_ObjectDestroyedSignal(swigCPtr), false);
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return ret;
238         }
239
240     }
241
242 }