Release 4.0.0-preview1-00301
[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
18 namespace Tizen.NUI
19 {
20
21     using System;
22     using System.Runtime.InteropServices;
23
24
25     internal class ObjectRegistry : BaseHandle
26     {
27         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
28
29         internal ObjectRegistry(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ObjectRegistry_SWIGUpcast(cPtr), cMemoryOwn)
30         {
31             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
32         }
33
34         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ObjectRegistry obj)
35         {
36             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
37         }
38
39         protected override void Dispose(DisposeTypes type)
40         {
41             if (disposed)
42             {
43                 return;
44             }
45
46             if (type == DisposeTypes.Explicit)
47             {
48                 //Called by User
49                 //Release your own managed resources here.
50                 //You should release all of your own disposable objects here.
51
52             }
53
54             //Release your own unmanaged resources here.
55             //You should not access any managed member here except static instance.
56             //because the execution order of Finalizes is non-deterministic.
57
58             if (swigCPtr.Handle != global::System.IntPtr.Zero)
59             {
60                 if (swigCMemOwn)
61                 {
62                     swigCMemOwn = false;
63                     NDalicPINVOKE.delete_ObjectRegistry(swigCPtr);
64                 }
65                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
66             }
67
68             base.Dispose(type);
69         }
70
71
72         public class ObjectCreatedEventArgs : EventArgs
73         {
74             private BaseHandle _baseHandle;
75
76             public BaseHandle BaseHandle
77             {
78                 get
79                 {
80                     return _baseHandle;
81                 }
82                 set
83                 {
84                     _baseHandle = value;
85                 }
86             }
87         }
88
89         public class ObjectDestroyedEventArgs : EventArgs
90         {
91             private RefObject _refObject;
92
93             public RefObject RefObject
94             {
95                 get
96                 {
97                     return _refObject;
98                 }
99                 set
100                 {
101                     _refObject = value;
102                 }
103             }
104         }
105
106
107         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
108         private delegate void ObjectCreatedEventCallbackDelegate(IntPtr baseHandle);
109         private DaliEventHandler<object, ObjectCreatedEventArgs> _objectRegistryObjectCreatedEventHandler;
110         private ObjectCreatedEventCallbackDelegate _objectRegistryObjectCreatedEventCallbackDelegate;
111
112         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
113         private delegate void ObjectDestroyedEventCallbackDelegate(IntPtr fefObject);
114         private DaliEventHandler<object, ObjectDestroyedEventArgs> _objectRegistryObjectDestroyedEventHandler;
115         private ObjectDestroyedEventCallbackDelegate _objectRegistryObjectDestroyedEventCallbackDelegate;
116
117         public event DaliEventHandler<object, ObjectCreatedEventArgs> ObjectCreated
118         {
119             add
120             {
121                 lock (this)
122                 {
123                     // Restricted to only one listener
124                     if (_objectRegistryObjectCreatedEventHandler == null)
125                     {
126                         _objectRegistryObjectCreatedEventHandler += value;
127
128                         _objectRegistryObjectCreatedEventCallbackDelegate = new ObjectCreatedEventCallbackDelegate(OnObjectCreated);
129                         this.ObjectCreatedSignal().Connect(_objectRegistryObjectCreatedEventCallbackDelegate);
130                     }
131                 }
132             }
133
134             remove
135             {
136                 lock (this)
137                 {
138                     if (_objectRegistryObjectCreatedEventHandler != null)
139                     {
140                         this.ObjectCreatedSignal().Disconnect(_objectRegistryObjectCreatedEventCallbackDelegate);
141                     }
142
143                     _objectRegistryObjectCreatedEventHandler -= value;
144                 }
145             }
146         }
147
148         // Callback for ObjectRegistry ObjectCreatedSignal
149         private void OnObjectCreated(IntPtr baseHandle)
150         {
151             ObjectCreatedEventArgs e = new ObjectCreatedEventArgs();
152
153             // Populate all members of "e" (ObjectCreatedEventArgs) with real data
154             //e.BaseHandle = BaseHandle.GetBaseHandleFromPtr(baseHandle); //GetBaseHandleFromPtr() is not present in BaseHandle.cs. Not sure what is the reason?
155
156             if (_objectRegistryObjectCreatedEventHandler != null)
157             {
158                 //here we send all data to user event handlers
159                 _objectRegistryObjectCreatedEventHandler(this, e);
160             }
161         }
162
163         public event DaliEventHandler<object, ObjectDestroyedEventArgs> ObjectDestroyed
164         {
165             add
166             {
167                 lock (this)
168                 {
169                     // Restricted to only one listener
170                     if (_objectRegistryObjectDestroyedEventHandler == null)
171                     {
172                         _objectRegistryObjectDestroyedEventHandler += value;
173
174                         _objectRegistryObjectDestroyedEventCallbackDelegate = new ObjectDestroyedEventCallbackDelegate(OnObjectDestroyed);
175                         this.ObjectDestroyedSignal().Connect(_objectRegistryObjectDestroyedEventCallbackDelegate);
176                     }
177                 }
178             }
179
180             remove
181             {
182                 lock (this)
183                 {
184                     if (_objectRegistryObjectDestroyedEventHandler != null)
185                     {
186                         this.ObjectDestroyedSignal().Disconnect(_objectRegistryObjectDestroyedEventCallbackDelegate);
187                     }
188
189                     _objectRegistryObjectDestroyedEventHandler -= value;
190                 }
191             }
192         }
193
194         // Callback for ObjectRegistry ObjectDestroyedSignal
195         private void OnObjectDestroyed(IntPtr refObject)
196         {
197             ObjectDestroyedEventArgs e = new ObjectDestroyedEventArgs();
198
199             // Populate all members of "e" (ObjectDestroyedEventArgs) with real data
200             e.RefObject = RefObject.GetRefObjectFromPtr(refObject);
201
202             if (_objectRegistryObjectDestroyedEventHandler != null)
203             {
204                 //here we send all data to user event handlers
205                 _objectRegistryObjectDestroyedEventHandler(this, e);
206             }
207         }
208
209
210         public ObjectRegistry() : this(NDalicPINVOKE.new_ObjectRegistry__SWIG_0(), true)
211         {
212             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213         }
214
215         public ObjectRegistry(ObjectRegistry handle) : this(NDalicPINVOKE.new_ObjectRegistry__SWIG_1(ObjectRegistry.getCPtr(handle)), true)
216         {
217             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218         }
219
220         public ObjectRegistry Assign(ObjectRegistry rhs)
221         {
222             ObjectRegistry ret = new ObjectRegistry(NDalicPINVOKE.ObjectRegistry_Assign(swigCPtr, ObjectRegistry.getCPtr(rhs)), false);
223             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224             return ret;
225         }
226
227         public ObjectCreatedSignal ObjectCreatedSignal()
228         {
229             ObjectCreatedSignal ret = new ObjectCreatedSignal(NDalicPINVOKE.ObjectRegistry_ObjectCreatedSignal(swigCPtr), false);
230             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
231             return ret;
232         }
233
234         public ObjectDestroyedSignal ObjectDestroyedSignal()
235         {
236             ObjectDestroyedSignal ret = new ObjectDestroyedSignal(NDalicPINVOKE.ObjectRegistry_ObjectDestroyedSignal(swigCPtr), false);
237             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
238             return ret;
239         }
240
241     }
242
243 }