ff0e890e4744d2c4618616b6ee88ae6636459a76
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / EXaml / Operation / CreateObject.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 using System;
18 using System.Collections.Generic;
19 using System.Reflection;
20 using System.Text;
21 using Tizen.NUI.BaseComponents;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.Binding.Internals;
24 using Tizen.NUI.Xaml;
25
26 namespace Tizen.NUI.EXaml
27 {
28     internal class CreateObject : Operation
29     {
30         public CreateObject(GlobalDataList globalDataList, List<object> operationInfos)
31         {
32             this.typeIndex = (int)operationInfos[0];
33             this.xFactoryMethodIndex = (int)operationInfos[1];
34             if (3 == operationInfos.Count)
35             {
36                 this.paramList = operationInfos[2] as List<object>;
37             }
38             this.globalDataList = globalDataList;
39         }
40
41         private GlobalDataList globalDataList;
42         private int xFactoryMethodIndex;
43
44         public void Do()
45         {
46             object obj = null;
47
48             if (0 == globalDataList.GatheredInstances.Count && null != globalDataList.Root)
49             {
50                 obj = globalDataList.Root;
51             }
52             else
53             {
54                 var type = globalDataList.GatheredTypes[typeIndex];
55
56                 var xFactoryMethod = (0 <= xFactoryMethodIndex) ? globalDataList.GatheredMethods[xFactoryMethodIndex] : null;
57
58                 if (null == paramList)
59                 {
60                     if (null == xFactoryMethod)
61                     {
62                         obj = Activator.CreateInstance(type);
63                     }
64                     else
65                     {
66                         obj = xFactoryMethod.Invoke(null, Array.Empty<object>());
67                     }
68                 }
69                 else
70                 {
71                     for (int i = 0; i < paramList.Count; i++)
72                     {
73                         if (paramList[i] is Instance instance)
74                         {
75                             paramList[i] = globalDataList.GatheredInstances[instance.Index];
76                         }
77
78                         if (paramList[i] is IMarkupExtension markupExtension)
79                         {
80                             paramList[i] = markupExtension.ProvideValue(null);
81                         }
82                     }
83
84                     if (null == xFactoryMethod)
85                     {
86                         obj = Activator.CreateInstance(type, paramList.ToArray());
87                     }
88                     else
89                     {
90                         obj = xFactoryMethod.Invoke(null, paramList.ToArray());
91                     }
92                 }
93             }
94
95             if (null != obj)
96             {
97                 globalDataList.GatheredInstances.Add(obj);
98
99                 if (globalDataList.Root == null)
100                 {
101                     globalDataList.Root = obj;
102                 }
103
104                 if (obj is BindableObject bindableObject)
105                 {
106                     bindableObject.IsCreateByXaml = true;
107
108                     if (1 == globalDataList.GatheredInstances.Count)
109                     {
110                         NameScope nameScope = new NameScope();
111                         NameScope.SetNameScope(bindableObject, nameScope);
112                     }
113                 }
114             }
115             else
116             {
117                 throw new Exception($"Can't create instance typeIndex:{typeIndex}");
118             }
119         }
120
121         private int typeIndex;
122
123         private List<object> paramList;
124     }
125 }