[NUI] Add license, delete unnecessary code (#2679)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Common / ObjectRegistry.cs
1 /*
2  * Copyright(c) 2021 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 using System;
19 using System.Runtime.InteropServices;
20
21 namespace Tizen.NUI
22 {
23     internal class ObjectRegistry : BaseHandle
24     {
25         internal ObjectRegistry(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
26         {
27         }
28
29         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ObjectRegistry obj)
30         {
31             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
32         }
33
34         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
35         {
36             Interop.ObjectRegistry.DeleteObjectRegistry(swigCPtr);
37         }
38
39         public class ObjectCreatedEventArgs : EventArgs
40         {
41             private BaseHandle baseHandle;
42
43             public BaseHandle BaseHandle
44             {
45                 get
46                 {
47                     return baseHandle;
48                 }
49                 set
50                 {
51                     baseHandle = value;
52                 }
53             }
54         }
55
56         public class ObjectDestroyedEventArgs : EventArgs
57         {
58             private RefObject refObject;
59
60             public RefObject RefObject
61             {
62                 get
63                 {
64                     return refObject;
65                 }
66                 set
67                 {
68                     refObject = value;
69                 }
70             }
71         }
72
73         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
74         private delegate void ObjectCreatedEventCallbackDelegate(IntPtr baseHandle);
75         private DaliEventHandler<object, ObjectCreatedEventArgs> objectRegistryObjectCreatedEventHandler;
76         private ObjectCreatedEventCallbackDelegate objectRegistryObjectCreatedEventCallbackDelegate;
77
78         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
79         private delegate void ObjectDestroyedEventCallbackDelegate(IntPtr fefObject);
80         private DaliEventHandler<object, ObjectDestroyedEventArgs> objectRegistryObjectDestroyedEventHandler;
81         private ObjectDestroyedEventCallbackDelegate objectRegistryObjectDestroyedEventCallbackDelegate;
82
83         public event DaliEventHandler<object, ObjectCreatedEventArgs> ObjectCreated
84         {
85             add
86             {
87                 // Restricted to only one listener
88                 if (objectRegistryObjectCreatedEventHandler == null)
89                 {
90                     objectRegistryObjectCreatedEventHandler += value;
91
92                     objectRegistryObjectCreatedEventCallbackDelegate = new ObjectCreatedEventCallbackDelegate(OnObjectCreated);
93                     this.ObjectCreatedSignal().Connect(objectRegistryObjectCreatedEventCallbackDelegate);
94                 }
95             }
96
97             remove
98             {
99                 if (objectRegistryObjectCreatedEventHandler != null)
100                 {
101                     this.ObjectCreatedSignal().Disconnect(objectRegistryObjectCreatedEventCallbackDelegate);
102                 }
103
104                 objectRegistryObjectCreatedEventHandler -= value;
105             }
106         }
107
108         // Callback for ObjectRegistry ObjectCreatedSignal
109         private void OnObjectCreated(IntPtr baseHandle)
110         {
111             ObjectCreatedEventArgs e = new ObjectCreatedEventArgs();
112
113             // Populate all members of "e" (ObjectCreatedEventArgs) with real data
114             //e.BaseHandle = BaseHandle.GetBaseHandleFromPtr(baseHandle); //GetBaseHandleFromPtr() is not present in BaseHandle.cs. Not sure what is the reason?
115
116             if (objectRegistryObjectCreatedEventHandler != null)
117             {
118                 //here we send all data to user event handlers
119                 objectRegistryObjectCreatedEventHandler(this, e);
120             }
121         }
122
123         public event DaliEventHandler<object, ObjectDestroyedEventArgs> ObjectDestroyed
124         {
125             add
126             {
127                 // Restricted to only one listener
128                 if (objectRegistryObjectDestroyedEventHandler == null)
129                 {
130                     objectRegistryObjectDestroyedEventHandler += value;
131
132                     objectRegistryObjectDestroyedEventCallbackDelegate = new ObjectDestroyedEventCallbackDelegate(OnObjectDestroyed);
133                     this.ObjectDestroyedSignal().Connect(objectRegistryObjectDestroyedEventCallbackDelegate);
134                 }
135             }
136
137             remove
138             {
139                 if (objectRegistryObjectDestroyedEventHandler != null)
140                 {
141                     this.ObjectDestroyedSignal().Disconnect(objectRegistryObjectDestroyedEventCallbackDelegate);
142                 }
143
144                 objectRegistryObjectDestroyedEventHandler -= value;
145             }
146         }
147
148         // Callback for ObjectRegistry ObjectDestroyedSignal
149         private void OnObjectDestroyed(IntPtr refObject)
150         {
151             ObjectDestroyedEventArgs e = new ObjectDestroyedEventArgs();
152
153             // Populate all members of "e" (ObjectDestroyedEventArgs) with real data
154             e.RefObject = RefObject.GetRefObjectFromPtr(refObject);
155
156             if (objectRegistryObjectDestroyedEventHandler != null)
157             {
158                 //here we send all data to user event handlers
159                 objectRegistryObjectDestroyedEventHandler(this, e);
160             }
161         }
162
163
164         public ObjectRegistry() : this(Interop.ObjectRegistry.NewObjectRegistry(), true)
165         {
166             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167         }
168
169         public ObjectRegistry(ObjectRegistry handle) : this(Interop.ObjectRegistry.NewObjectRegistry(ObjectRegistry.getCPtr(handle)), true)
170         {
171             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
172         }
173
174         public ObjectRegistry Assign(ObjectRegistry rhs)
175         {
176             ObjectRegistry ret = new ObjectRegistry(Interop.ObjectRegistry.Assign(SwigCPtr, ObjectRegistry.getCPtr(rhs)), false);
177             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178             return ret;
179         }
180
181         public ObjectCreatedSignal ObjectCreatedSignal()
182         {
183             ObjectCreatedSignal ret = new ObjectCreatedSignal(Interop.ObjectRegistry.ObjectCreatedSignal(SwigCPtr), false);
184             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
185             return ret;
186         }
187
188         public ObjectDestroyedSignal ObjectDestroyedSignal()
189         {
190             ObjectDestroyedSignal ret = new ObjectDestroyedSignal(Interop.ObjectRegistry.ObjectDestroyedSignal(SwigCPtr), false);
191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192             return ret;
193         }
194     }
195 }