Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / core / include / dpl / optional.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        optional_value.h
18  * @author      Lukasz Wrzosek (l.wrzosek@samsung.com)
19  * @version     1.0
20  */
21
22 #ifndef DPL_OPTIONAL_H
23 #define DPL_OPTIONAL_H
24
25 #include <dpl/exception.h>
26 #include <dpl/availability.h>
27
28 namespace DPL {
29 template <typename Type>
30 class Optional
31 {
32     class Exception
33     {
34       public:
35         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
36         DECLARE_EXCEPTION_TYPE(Base, NullReference)
37     };
38
39   public:
40     Optional() :
41         m_null(true),
42         m_value()
43     {}
44
45     Optional(const Type& t) :
46         m_null(false),
47         m_value(t)
48     {}
49
50     bool IsNull() const
51     {
52         return m_null;
53     }
54
55     Type& operator*()
56     {
57         if (m_null) {
58             Throw(typename Exception::NullReference);
59         }
60         return m_value;
61     }
62
63     const Type& operator*() const
64     {
65         if (m_null) {
66             Throw(typename Exception::NullReference);
67         }
68         return m_value;
69     }
70
71     const Type* operator->() const
72     {
73         if (m_null) {
74             Throw(typename Exception::NullReference);
75         }
76         return &m_value;
77     }
78
79     Type* operator->()
80     {
81         if (m_null) {
82             Throw(typename Exception::NullReference);
83         }
84         return &m_value;
85     }
86
87     bool operator!() const
88     {
89         return m_null;
90     }
91
92     Optional<Type>& operator=(const Type& other)
93     {
94         m_null = false;
95         m_value = other;
96         return *this;
97     }
98
99     bool operator==(const Optional<Type>& aSecond) const
100     {
101         return LogicalOperator<true>(*this, aSecond,
102                                      std::equal_to<Type>(), std::equal_to<bool>());
103     }
104
105     bool operator==(const Type& aSecond) const
106     {
107         return Optional<Type>(aSecond) == *this;
108     }
109
110     bool operator!=(const Optional<Type>& aSecond) const
111     {
112         return !(*this == aSecond);
113     }
114
115     bool operator<(const Optional<Type>& aSecond) const
116     {
117         return LogicalOperator<false>(*this, aSecond,
118                                       std::less<Type>(), std::less<bool>());
119     }
120
121     bool operator>(const Optional<Type>& aSecond) const
122     {
123         return LogicalOperator<false>(*this, aSecond,
124                                       std::greater<Type>(), std::greater<bool>());
125     }
126
127     bool operator<=(const Optional<Type>& aSecond) const
128     {
129         return *this == aSecond || *this < aSecond;
130     }
131
132     bool operator>=(const Optional<Type>& aSecond) const
133     {
134         return *this == aSecond || *this > aSecond;
135     }
136
137     static Optional<Type> Null;
138
139   private:
140     bool m_null;
141     Type m_value;
142
143     template <bool taEquality, typename taComparator, typename taNullComparator>
144     static bool LogicalOperator(const Optional<Type>& aFirst,
145                                 const Optional<Type>& aSecond,
146                                 taComparator aComparator,
147                                 taNullComparator aNullComparator)
148     {
149         if (aFirst.m_null == aSecond.m_null) {
150             if (aFirst.m_null) {
151                 return taEquality;
152             } else {
153                 return aComparator(aFirst.m_value, aSecond.m_value);
154             }
155         } else {
156             return aNullComparator(aFirst.m_null, aSecond.m_null);
157         }
158     }
159 } DPL_DEPRECATED_WITH_MESSAGE("Use boost::optional instead");
160
161 template<typename Type>
162 Optional<Type> Optional<Type>::Null = Optional<Type>();
163 } //namespace DPL
164
165 template<typename Type>
166 std::ostream& operator<<(std::ostream& aStream,
167                          const DPL::Optional<Type>& aOptional)
168 {
169     if (aOptional.IsNull()) {
170         return aStream << "null optional";
171     } else {
172         return aStream << *aOptional;
173     }
174 }
175
176 #endif // DPL_OPTIONAL_VALUE_H