874e2d851496a81712e2eae12f54b4109ea83692
[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) 2019 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/public-api/object/ref-object.h>
26 #include <dali/internal/common/owner-pointer.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 /**
35  * Template helpers to strip of const reference and reference to prevent anyone
36  * from passing references as message parameters
37  */
38
39 // For basic types, they are always pass by copy
40 template <typename Type>
41 struct BasicType
42 {
43   typedef Type HolderType;
44   typedef Type PassingType;
45 };
46
47 // For complex types that are copied into the message,
48 // they are passed as const reference when they don't need to be copied
49 template <typename Type>
50 struct ComplexType
51 {
52   typedef Type HolderType;
53   typedef const Type& PassingType;
54 };
55
56 // For complex types that are owned by the message,
57 // They are passed and hold in an OwnerPointer
58 template <typename Type>
59 struct OwnedType
60 {
61   typedef OwnerPointer<Type> HolderType;
62   typedef OwnerPointer<Type>& PassingType;
63 };
64
65
66 // Default for Vector3 and other structures
67 template <class T> struct ParameterType : public ComplexType< T > {};
68
69 // For message owned parameters
70 template <class T> struct ParameterType< OwnerPointer<T> > : public OwnedType< T > {};
71
72 // Basic types types
73 template <typename T> struct ParameterType< T* > : public BasicType< T* > {};
74 template <typename T> struct ParameterType< const T* > : public BasicType< const T* > {};
75 template <> struct ParameterType< int > : public BasicType< int > {};
76 template <> struct ParameterType< unsigned int > : public BasicType< unsigned int > {};
77 template <> struct ParameterType< float > : public BasicType< float > {};
78 template <> struct ParameterType< bool > : public BasicType< bool > {};
79 template <> struct ParameterType< short int > : public BasicType< short int > {};
80
81 #if INT_MAX != LONG_MAX
82 template <> struct ParameterType< long > : public BasicType< long > {};
83 template <> struct ParameterType< unsigned long > : public BasicType< unsigned long > {};
84 #endif
85
86 //TODO: Passing intrusive pointers through messages is potentially dangerous,
87 //      this should be checked
88 template <typename T> struct ParameterType< IntrusivePtr<T> >
89 : public BasicType< IntrusivePtr<T> > {};
90
91 // poorly constructed types, types should not be defined as references
92 // this will trigger a compilation error.
93 template <class U> struct ParameterType< U& > {};
94 template <class U> struct ParameterType< const U& > {};
95
96 } //namespace Internal
97
98 } //namespace Dali
99
100 #endif // DALI_INTERNAL_TYPE_ABSTRACTION_H