[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / optional-value.h
1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_OPTIONAL_H
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_OPTIONAL_H
3
4 /*
5  * Copyright (c) 2021 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 template<typename T>
22 struct OptionalTypes
23 {
24   typedef T         ValueType;
25   typedef const T&  ReturnType;
26   static ReturnType Get(const ValueType& v)
27   {
28     return v;
29   }
30   static ValueType Set(const ReturnType v)
31   {
32     return v;
33   }
34   static bool Ok(const ValueType& v)
35   {
36     return true;
37   }
38 };
39
40 template<typename T>
41 struct OptionalTypes<T*>
42 {
43   typedef T*        ValueType;
44   typedef const T*  ReturnType;
45   static ReturnType Get(const ValueType v)
46   {
47     return v;
48   }
49   static ValueType Set(const ReturnType v)
50   {
51     return v;
52   }
53   static bool Ok(const ReturnType v)
54   {
55     return NULL != v;
56   }
57 };
58
59 template<typename T>
60 struct OptionalTypes<T&>
61 {
62   typedef T*        ValueType;
63   typedef const T&  ReturnType;
64   static ReturnType Get(const ValueType v)
65   {
66     return *v;
67   }
68   static ValueType Set(const ReturnType v)
69   {
70     return &v;
71   }
72   static bool Ok(const ReturnType v)
73   {
74     return true;
75   }
76 };
77
78 template<typename T>
79 class OptionalValue
80 {
81 public:
82   typedef typename OptionalTypes<T>::ReturnType ReturnType;
83   typedef typename OptionalTypes<T>::ValueType  ValueType;
84
85   OptionalValue()
86   : mOk(false),
87     mValue()
88   {
89   }
90   OptionalValue(T value)
91   : mOk(OptionalTypes<T>::Ok(value)),
92     mValue(OptionalTypes<T>::Set(value))
93   {
94   }
95   OptionalValue(bool b, T value)
96   : mOk(b),
97     mValue(OptionalTypes<T>::Set(value))
98   {
99   }
100
101   ReturnType operator*() const
102   {
103     return OptionalTypes<T>::Get(mValue);
104   }
105
106   explicit operator bool() const
107   {
108     return mOk;
109   }
110
111 private:
112   bool      mOk;
113   ValueType mValue;
114 };
115
116 template<typename T, typename U>
117 bool operator==(const OptionalValue<T>& lhs, const OptionalValue<U>& rhs)
118 {
119   lhs.this_type_does_not_support_comparisons();
120   return false;
121 }
122
123 template<typename T, typename U>
124 bool operator!=(const OptionalValue<T>& lhs, const OptionalValue<U>& rhs)
125 {
126   lhs.this_type_does_not_support_comparisons();
127   return false;
128 }
129
130 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_OPTIONAL_H