Device interface library for multiple platforms including Mbed.

Dependents:   DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Optional.hpp Source File

Optional.hpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #ifndef MaximInterfaceCore_Optional_hpp
00034 #define MaximInterfaceCore_Optional_hpp
00035 
00036 #include "None.hpp"
00037 #include "SafeBool.hpp"
00038 
00039 // Include for std::swap.
00040 #include <algorithm>
00041 #include <utility>
00042 
00043 namespace MaximInterfaceCore {
00044 
00045 /// @brief %Optional value container similar to std::optional.
00046 /// @details
00047 /// To prevent the need for aligned storage, this implementation imposes that
00048 /// types must be DefaultConstructible, CopyConstructible, and CopyAssignable.
00049 /// No exceptions are thrown when accessing a valueless Optional.
00050 template <typename T> class Optional {
00051 public:
00052   typedef T value_type;
00053 
00054   Optional() : value_(), hasValue_(false) {}
00055 
00056   Optional(None) : value_(), hasValue_(false) {}
00057 
00058   Optional(const T & value) : value_(value), hasValue_(true) {}
00059 
00060   template <typename U>
00061   explicit Optional(const Optional<U> & other)
00062       : value_(other.value()), hasValue_(other.hasValue()) {}
00063 
00064   Optional & operator=(None) {
00065     reset();
00066     return *this;
00067   }
00068 
00069   Optional & operator=(const T & value) {
00070     value_ = value;
00071     hasValue_ = true;
00072     return *this;
00073   }
00074 
00075   template <typename U> Optional & operator=(const Optional<U> & other) {
00076     assign(other);
00077     return *this;
00078   }
00079 
00080   Optional & operator=(const Optional & other) {
00081     assign(other);
00082     return *this;
00083   }
00084 
00085   bool hasValue() const { return hasValue_; }
00086 
00087   operator SafeBool() const { return makeSafeBool(hasValue()); }
00088 
00089   const T & value() const { return value_; }
00090 
00091   T & value() {
00092     return const_cast<T &>(static_cast<const Optional &>(*this).value());
00093   }
00094 
00095   const T & operator*() const { return value(); }
00096 
00097   T & operator*() {
00098     return const_cast<T &>(static_cast<const Optional &>(*this).operator*());
00099   }
00100 
00101   const T * operator->() const { return &value(); }
00102 
00103   T * operator->() {
00104     return const_cast<T *>(static_cast<const Optional &>(*this).operator->());
00105   }
00106 
00107   const T & valueOr(const T & defaultValue) const {
00108     return hasValue() ? value() : defaultValue;
00109   }
00110 
00111   void swap(Optional & other) {
00112     if (hasValue_ || other.hasValue_) {
00113       using std::swap;
00114       swap(value_, other.value_);
00115       swap(hasValue_, other.hasValue_);
00116     }
00117   }
00118 
00119   void reset() {
00120     if (hasValue_) {
00121       hasValue_ = false;
00122       value_ = T();
00123     }
00124   }
00125 
00126 private:
00127   template <typename U> void assign(const Optional<U> & other) {
00128     if (hasValue_ || other.hasValue()) {
00129       value_ = other.value();
00130       hasValue_ = other.hasValue();
00131     }
00132   }
00133 
00134   T value_;
00135   bool hasValue_;
00136 };
00137 
00138 template <typename T> Optional<T> makeOptional(const T & value) {
00139   return value;
00140 }
00141 
00142 template <typename T> void swap(Optional<T> & lhs, Optional<T> & rhs) {
00143   lhs.swap(rhs);
00144 }
00145 
00146 template <typename T, typename U>
00147 bool operator==(const Optional<T> & lhs, const Optional<U> & rhs) {
00148   if (lhs.hasValue() != rhs.hasValue()) {
00149     return false;
00150   }
00151   if (!lhs.hasValue()) {
00152     return true;
00153   }
00154   return lhs.value() == rhs.value();
00155 }
00156 
00157 template <typename T, typename U>
00158 bool operator!=(const Optional<T> & lhs, const Optional<U> & rhs) {
00159   if (lhs.hasValue() != rhs.hasValue()) {
00160     return true;
00161   }
00162   if (!lhs.hasValue()) {
00163     return false;
00164   }
00165   return lhs.value() != rhs.value();
00166 }
00167 
00168 template <typename T, typename U>
00169 bool operator<(const Optional<T> & lhs, const Optional<U> & rhs) {
00170   if (!rhs.hasValue()) {
00171     return false;
00172   }
00173   if (!lhs.hasValue()) {
00174     return true;
00175   }
00176   return lhs.value() < rhs.value();
00177 }
00178 
00179 template <typename T, typename U>
00180 bool operator<=(const Optional<T> & lhs, const Optional<U> & rhs) {
00181   if (!lhs.hasValue()) {
00182     return true;
00183   }
00184   if (!rhs.hasValue()) {
00185     return false;
00186   }
00187   return lhs.value() <= rhs.value();
00188 }
00189 
00190 template <typename T, typename U>
00191 bool operator>(const Optional<T> & lhs, const Optional<U> & rhs) {
00192   if (!lhs.hasValue()) {
00193     return false;
00194   }
00195   if (!rhs.hasValue()) {
00196     return true;
00197   }
00198   return lhs.value() > rhs.value();
00199 }
00200 
00201 template <typename T, typename U>
00202 bool operator>=(const Optional<T> & lhs, const Optional<U> & rhs) {
00203   if (!rhs.hasValue()) {
00204     return true;
00205   }
00206   if (!lhs.hasValue()) {
00207     return false;
00208   }
00209   return lhs.value() >= rhs.value();
00210 }
00211 
00212 template <typename T> bool operator==(const Optional<T> & opt, None) {
00213   return !opt.hasValue();
00214 }
00215 
00216 template <typename T> bool operator==(None, const Optional<T> & opt) {
00217   return operator==(opt, none);
00218 }
00219 
00220 template <typename T> bool operator!=(const Optional<T> & opt, None) {
00221   return !operator==(opt, none);
00222 }
00223 
00224 template <typename T> bool operator!=(None, const Optional<T> & opt) {
00225   return operator!=(opt, none);
00226 }
00227 
00228 template <typename T> bool operator<(const Optional<T> &, None) {
00229   return false;
00230 }
00231 
00232 template <typename T> bool operator<(None, const Optional<T> & opt) {
00233   return opt.hasValue();
00234 }
00235 
00236 template <typename T> bool operator<=(const Optional<T> & opt, None) {
00237   return !operator>(opt, none);
00238 }
00239 
00240 template <typename T> bool operator<=(None, const Optional<T> & opt) {
00241   return !operator>(none, opt);
00242 }
00243 
00244 template <typename T> bool operator>(const Optional<T> & opt, None) {
00245   return operator<(none, opt);
00246 }
00247 
00248 template <typename T> bool operator>(None, const Optional<T> & opt) {
00249   return operator<(opt, none);
00250 }
00251 
00252 template <typename T> bool operator>=(const Optional<T> & opt, None) {
00253   return !operator<(opt, none);
00254 }
00255 
00256 template <typename T> bool operator>=(None, const Optional<T> & opt) {
00257   return !operator<(none, opt);
00258 }
00259 
00260 template <typename T, typename U>
00261 bool operator==(const Optional<T> & opt, const U & value) {
00262   return opt.hasValue() ? opt.value() == value : false;
00263 }
00264 
00265 template <typename T, typename U>
00266 bool operator==(const T & value, const Optional<U> & opt) {
00267   return operator==(opt, value);
00268 }
00269 
00270 template <typename T, typename U>
00271 bool operator!=(const Optional<T> & opt, const U & value) {
00272   return opt.hasValue() ? opt.value() != value : true;
00273 }
00274 
00275 template <typename T, typename U>
00276 bool operator!=(const T & value, const Optional<U> & opt) {
00277   return operator!=(opt, value);
00278 }
00279 
00280 template <typename T, typename U>
00281 bool operator<(const Optional<T> & opt, const U & value) {
00282   return opt.hasValue() ? opt.value() < value : true;
00283 }
00284 
00285 template <typename T, typename U>
00286 bool operator<(const T & value, const Optional<U> & opt) {
00287   return opt.hasValue() ? value < opt.value() : false;
00288 }
00289 
00290 template <typename T, typename U>
00291 bool operator<=(const Optional<T> & opt, const U & value) {
00292   return opt.hasValue() ? opt.value() <= value : true;
00293 }
00294 
00295 template <typename T, typename U>
00296 bool operator<=(const T & value, const Optional<U> & opt) {
00297   return opt.hasValue() ? value <= opt.value() : false;
00298 }
00299 
00300 template <typename T, typename U>
00301 bool operator>(const Optional<T> & opt, const U & value) {
00302   return opt.hasValue() ? opt.value() > value : false;
00303 }
00304 
00305 template <typename T, typename U>
00306 bool operator>(const T & value, const Optional<U> & opt) {
00307   return opt.hasValue() ? value > opt.value() : true;
00308 }
00309 
00310 template <typename T, typename U>
00311 bool operator>=(const Optional<T> & opt, const U & value) {
00312   return opt.hasValue() ? opt.value() >= value : false;
00313 }
00314 
00315 template <typename T, typename U>
00316 bool operator>=(const T & value, const Optional<U> & opt) {
00317   return opt.hasValue() ? value >= opt.value() : true;
00318 }
00319 
00320 } // namespace MaximInterfaceCore
00321 
00322 #endif