Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / 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
27 namespace DPL {
28 template <typename Type>
29 class Optional
30 {
31     class Exception
32     {
33       public:
34         DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
35         DECLARE_EXCEPTION_TYPE(Base, NullReference)
36     };
37
38   public:
39     Optional() :
40         m_null(true),
41         m_value()
42     {}
43
44     Optional(const Type& t) :
45         m_null(false),
46         m_value(t)
47     {}
48
49     bool IsNull() const
50     {
51         return m_null;
52     }
53
54     Type& operator*()
55     {
56         if (m_null) {
57             Throw(typename Exception::NullReference);
58         }
59         return m_value;
60     }
61
62     const Type& operator*() const
63     {
64         if (m_null) {
65             Throw(typename Exception::NullReference);
66         }
67         return m_value;
68     }
69
70     const Type* operator->() const
71     {
72         if (m_null) {
73             Throw(typename Exception::NullReference);
74         }
75         return &m_value;
76     }
77
78     Type* operator->()
79     {
80         if (m_null) {
81             Throw(typename Exception::NullReference);
82         }
83         return &m_value;
84     }
85
86     bool operator!() const
87     {
88         return m_null;
89     }
90
91     Optional<Type>& operator=(const Type& other)
92     {
93         m_null = false;
94         m_value = other;
95         return *this;
96     }
97
98     bool operator==(const Optional<Type>& aSecond) const
99     {
100         return LogicalOperator<true>(*this, aSecond,
101                                      std::equal_to<Type>(), std::equal_to<bool>());
102     }
103
104     bool operator==(const Type& aSecond) const
105     {
106         return Optional<Type>(aSecond) == *this;
107     }
108
109     bool operator!=(const Optional<Type>& aSecond) const
110     {
111         return !(*this == aSecond);
112     }
113
114     bool operator<(const Optional<Type>& aSecond) const
115     {
116         return LogicalOperator<false>(*this, aSecond,
117                                       std::less<Type>(), std::less<bool>());
118     }
119
120     bool operator>(const Optional<Type>& aSecond) const
121     {
122         return LogicalOperator<false>(*this, aSecond,
123                                       std::greater<Type>(), std::greater<bool>());
124     }
125
126     bool operator<=(const Optional<Type>& aSecond) const
127     {
128         return *this == aSecond || *this < aSecond;
129     }
130
131     bool operator>=(const Optional<Type>& aSecond) const
132     {
133         return *this == aSecond || *this > aSecond;
134     }
135
136     static Optional<Type> Null;
137
138   private:
139     bool m_null;
140     Type m_value;
141
142     template <bool taEquality, typename taComparator, typename taNullComparator>
143     static bool LogicalOperator(const Optional<Type>& aFirst,
144                                 const Optional<Type>& aSecond,
145                                 taComparator aComparator,
146                                 taNullComparator aNullComparator)
147     {
148         if (aFirst.m_null == aSecond.m_null) {
149             if (aFirst.m_null) {
150                 return taEquality;
151             } else {
152                 return aComparator(aFirst.m_value, aSecond.m_value);
153             }
154         } else {
155             return aNullComparator(aFirst.m_null, aSecond.m_null);
156         }
157     }
158 };
159
160 template<typename Type>
161 Optional<Type> Optional<Type>::Null = Optional<Type>();
162 } //namespace DPL
163
164 template<typename Type>
165 std::ostream& operator<<(std::ostream& aStream,
166                          const DPL::Optional<Type>& aOptional)
167 {
168     if (aOptional.IsNull()) {
169         return aStream << "null optional";
170     } else {
171         return aStream << *aOptional;
172     }
173 }
174
175 #endif // DPL_OPTIONAL_VALUE_H