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