[dali_2.3.34] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / common / type-abstraction.h
1 #ifndef DALI_INTERNAL_TYPE_ABSTRACTION_H
2 #define DALI_INTERNAL_TYPE_ABSTRACTION_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <climits>
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/common/owner-pointer.h>
26 #include <dali/public-api/object/ref-object.h>
27
28 namespace Dali
29 {
30 namespace Internal
31 {
32 /**
33  * Template helpers to strip of const reference and reference to prevent anyone
34  * from passing references as message parameters
35  */
36
37 // For basic types, they are always pass by copy
38 template<typename Type>
39 struct BasicType
40 {
41   using HolderType  = Type;
42   using PassingType = Type;
43 };
44
45 // For complex types that are copied into the message,
46 // they are passed as const reference when they don't need to be copied
47 template<typename Type>
48 struct ComplexType
49 {
50   using HolderType  = Type;
51   using PassingType = const Type&;
52 };
53
54 // For complex types that are owned by the message,
55 // They are passed and hold in an OwnerPointer
56 template<typename Type>
57 struct OwnedType
58 {
59   using HolderType  = OwnerPointer<Type>;
60   using PassingType = OwnerPointer<Type>&;
61 };
62
63 // Default for Vector3 and other structures
64 template<class T>
65 struct ParameterType : public ComplexType<T>
66 {
67 };
68
69 // For message owned parameters
70 template<class T>
71 struct ParameterType<OwnerPointer<T> > : public OwnedType<T>
72 {
73 };
74
75 // Basic types types
76 template<typename T>
77 struct ParameterType<T*> : public BasicType<T*>
78 {
79 };
80 template<typename T>
81 struct ParameterType<const T*> : public BasicType<const T*>
82 {
83 };
84 template<>
85 struct ParameterType<int> : public BasicType<int>
86 {
87 };
88 template<>
89 struct ParameterType<unsigned int> : public BasicType<unsigned int>
90 {
91 };
92 template<>
93 struct ParameterType<float> : public BasicType<float>
94 {
95 };
96 template<>
97 struct ParameterType<bool> : public BasicType<bool>
98 {
99 };
100 template<>
101 struct ParameterType<short int> : public BasicType<short int>
102 {
103 };
104 template<>
105 struct ParameterType<unsigned short int> : public BasicType<unsigned short int>
106 {
107 };
108
109 #if SCHAR_MAX != SHRT_MAX
110 template<>
111 struct ParameterType<char> : public BasicType<char>
112 {
113 };
114 template<>
115 struct ParameterType<unsigned char> : public BasicType<unsigned char>
116 {
117 };
118 #endif
119
120 #if INT_MAX != LONG_MAX
121 template<>
122 struct ParameterType<long> : public BasicType<long>
123 {
124 };
125 template<>
126 struct ParameterType<unsigned long> : public BasicType<unsigned long>
127 {
128 };
129 #endif
130
131 //TODO: Passing intrusive pointers through messages is potentially dangerous,
132 //      this should be checked
133 template<typename T>
134 struct ParameterType<IntrusivePtr<T> >
135 : public BasicType<IntrusivePtr<T> >
136 {
137 };
138
139 // poorly constructed types, types should not be defined as references
140 // this will trigger a compilation error.
141 template<class U>
142 struct ParameterType<U&>
143 {
144 };
145 template<class U>
146 struct ParameterType<const U&>
147 {
148 };
149
150 } //namespace Internal
151
152 } //namespace Dali
153
154 #endif // DALI_INTERNAL_TYPE_ABSTRACTION_H