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