Conversion to Apache 2.0 license
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / builder / optional-value.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUILDER_OPTIONAL__
2 #define __DALI_TOOLKIT_INTERNAL_BUILDER_OPTIONAL__
3
4 /*
5  * Copyright (c) 2014 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 // INTERNAL INCLUDES
22 template <typename T>
23 struct OptionalTypes
24 {
25   typedef T ValueType;
26   typedef const T& ReturnType;
27   static ReturnType Get(const ValueType& v) { return v; }
28   static ValueType Set(const ReturnType v) { return v; }
29   static bool Ok(const ValueType& v) { return true; }
30 };
31
32 template <typename T>
33 struct OptionalTypes<T*>
34 {
35   typedef T* ValueType;
36   typedef const T* ReturnType;
37   static ReturnType Get(const ValueType v) { return v; }
38   static ValueType Set(const ReturnType v) { return v; }
39   static bool Ok(const ReturnType v) { return NULL != v; }
40 };
41
42 template <typename T>
43 struct OptionalTypes<T&>
44 {
45   typedef T* ValueType;
46   typedef const T& ReturnType;
47   static ReturnType Get(const ValueType v) { return *v; }
48   static ValueType Set(const ReturnType v) { return &v; }
49   static bool Ok(const ReturnType v) { return true; }
50 };
51
52 template <typename T>
53 class OptionalValue
54 {
55 public:
56   typedef void ( OptionalValue::*bool_type )() const;
57   typedef typename OptionalTypes<T>::ReturnType ReturnType;
58   typedef typename OptionalTypes<T>::ValueType ValueType;
59
60   OptionalValue(): mOk(false), mValue() {}
61   OptionalValue( T value ): mOk(OptionalTypes<T>::Ok(value)), mValue(OptionalTypes<T>::Set(value)) {}
62   OptionalValue( bool b, T value ): mOk(b), mValue(OptionalTypes<T>::Set(value)) {}
63
64   ReturnType operator *() const { return OptionalTypes<T>::Get(mValue); }
65
66   // safe bool idiom
67   operator bool_type() const {
68     return mOk == true ? &OptionalValue::this_type_does_not_support_comparisons : 0;
69   }
70
71 private:
72   bool mOk;
73   ValueType mValue;
74   void this_type_does_not_support_comparisons() const {}
75 };
76
77 template <typename T, typename U>
78 bool operator==( const OptionalValue<T>& lhs, const OptionalValue<U>& rhs )
79 {
80   lhs.this_type_does_not_support_comparisons();
81   return false;
82 }
83
84 template <typename T, typename U>
85 bool operator!=( const OptionalValue<T>& lhs, const OptionalValue<U>& rhs )
86 {
87   lhs.this_type_does_not_support_comparisons();
88   return false;
89 }
90
91 #endif // header