Updated all header files to new format
[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 void (OptionalValue::*bool_type)() const;
83   typedef typename OptionalTypes<T>::ReturnType ReturnType;
84   typedef typename OptionalTypes<T>::ValueType  ValueType;
85
86   OptionalValue()
87   : mOk(false),
88     mValue()
89   {
90   }
91   OptionalValue(T value)
92   : mOk(OptionalTypes<T>::Ok(value)),
93     mValue(OptionalTypes<T>::Set(value))
94   {
95   }
96   OptionalValue(bool b, T value)
97   : mOk(b),
98     mValue(OptionalTypes<T>::Set(value))
99   {
100   }
101
102   ReturnType operator*() const
103   {
104     return OptionalTypes<T>::Get(mValue);
105   }
106
107   // safe bool idiom
108   operator bool_type() const
109   {
110     return mOk == true ? &OptionalValue::this_type_does_not_support_comparisons : 0;
111   }
112
113 private:
114   bool      mOk;
115   ValueType mValue;
116   void      this_type_does_not_support_comparisons() const
117   {
118   }
119 };
120
121 template<typename T, typename U>
122 bool operator==(const OptionalValue<T>& lhs, const OptionalValue<U>& rhs)
123 {
124   lhs.this_type_does_not_support_comparisons();
125   return false;
126 }
127
128 template<typename T, typename U>
129 bool operator!=(const OptionalValue<T>& lhs, const OptionalValue<U>& rhs)
130 {
131   lhs.this_type_does_not_support_comparisons();
132   return false;
133 }
134
135 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_OPTIONAL_H