2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file optional_value.h
18 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
22 #ifndef DPL_OPTIONAL_H
23 #define DPL_OPTIONAL_H
25 #include <dpl/exception.h>
26 #include <dpl/availability.h>
29 template <typename Type>
35 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base)
36 DECLARE_EXCEPTION_TYPE(Base, NullReference)
45 Optional(const Type& t) :
58 Throw(typename Exception::NullReference);
63 const Type& operator*() const
66 Throw(typename Exception::NullReference);
71 const Type* operator->() const
74 Throw(typename Exception::NullReference);
82 Throw(typename Exception::NullReference);
87 bool operator!() const
92 Optional<Type>& operator=(const Type& other)
99 bool operator==(const Optional<Type>& aSecond) const
101 return LogicalOperator<true>(*this, aSecond,
102 std::equal_to<Type>(), std::equal_to<bool>());
105 bool operator==(const Type& aSecond) const
107 return Optional<Type>(aSecond) == *this;
110 bool operator!=(const Optional<Type>& aSecond) const
112 return !(*this == aSecond);
115 bool operator<(const Optional<Type>& aSecond) const
117 return LogicalOperator<false>(*this, aSecond,
118 std::less<Type>(), std::less<bool>());
121 bool operator>(const Optional<Type>& aSecond) const
123 return LogicalOperator<false>(*this, aSecond,
124 std::greater<Type>(), std::greater<bool>());
127 bool operator<=(const Optional<Type>& aSecond) const
129 return *this == aSecond || *this < aSecond;
132 bool operator>=(const Optional<Type>& aSecond) const
134 return *this == aSecond || *this > aSecond;
137 static Optional<Type> Null;
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)
149 if (aFirst.m_null == aSecond.m_null) {
153 return aComparator(aFirst.m_value, aSecond.m_value);
156 return aNullComparator(aFirst.m_null, aSecond.m_null);
159 } DPL_DEPRECATED_WITH_MESSAGE("Use boost::optional instead");
161 template<typename Type>
162 Optional<Type> Optional<Type>::Null = Optional<Type>();
165 template<typename Type>
166 std::ostream& operator<<(std::ostream& aStream,
167 const DPL::Optional<Type>& aOptional)
169 if (aOptional.IsNull()) {
170 return aStream << "null optional";
172 return aStream << *aOptional;
176 #endif // DPL_OPTIONAL_VALUE_H