1 /** 2 * Copyright 2020-2021 Huawei Technologies Co., Ltd 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 #ifndef MINDSPORE_LITE_TOOLS_COMMON_OPTION_H_ 18 #define MINDSPORE_LITE_TOOLS_COMMON_OPTION_H_ 19 20 #include <type_traits> 21 #include <utility> 22 #include "src/common/log_adapter.h" 23 24 namespace mindspore { 25 namespace lite { 26 template <typename T> 27 struct InnerSome { InnerSomeInnerSome28 explicit InnerSome(const T &t) : _t(std::move(t)) {} 29 30 T _t; 31 }; 32 33 template <typename T> Some(T && t)34InnerSome<typename std::decay<T>::type> Some(T &&t) { 35 return InnerSome<typename std::decay<T>::type>(std::forward<T>(t)); 36 } 37 38 struct None {}; 39 40 template <typename T> 41 class Option { 42 public: Option()43 Option() : state(NONE) {} 44 Option(const T & t)45 explicit Option(const T &t) : data(t), state(SOME) {} 46 Option(T && t)47 explicit Option(T &&t) : data(std::move(t)), state(SOME) {} 48 Option(const InnerSome<T> & some)49 explicit Option(const InnerSome<T> &some) : data(some._t), state(SOME) {} 50 Option(const None & none)51 explicit Option(const None &none) : state(NONE) {} 52 Option(const Option<T> & that)53 Option(const Option<T> &that) : state(that.state) { 54 if (that.IsSome()) { 55 new (&data) T(that.data); 56 } 57 } 58 59 virtual ~Option() = default; 60 IsNone()61 bool IsNone() const { return state == NONE; } 62 IsSome()63 bool IsSome() const { return state == SOME; } 64 Get()65 const T &Get() const & { 66 MS_ASSERT(IsSome()); 67 return data; 68 } 69 Get()70 T &Get() & { 71 MS_ASSERT(IsSome()); 72 return data; 73 } 74 Get()75 T &&Get() && { 76 MS_ASSERT(IsSome()); 77 return std::move(data); 78 } 79 Get()80 const T &&Get() const && { 81 MS_ASSERT(IsSome()); 82 return std::move(data); 83 } 84 85 // oprerator override 86 Option<T> &operator=(const Option<T> &that) { 87 if (&that != this) { 88 if (IsSome()) { 89 data.~T(); 90 } 91 state = that.state; 92 if (that.IsSome()) { 93 new (&data) T(that.data); 94 } 95 } 96 97 return *this; 98 } 99 100 bool operator==(const Option<T> &that) const { 101 return (IsNone() && that.IsNone()) || (IsSome() && that.IsSome() && data == that.data); 102 } 103 104 bool operator!=(const Option<T> &that) const { return !(*this == that); } 105 106 bool operator==(const T &that) const { return IsSome() && data == that; } 107 108 bool operator!=(const T &that) const { return !(*this == that); } 109 110 private: 111 enum State { NONE = 0, SOME = 1 }; 112 113 T data; 114 State state; 115 }; 116 } // namespace lite 117 } // namespace mindspore 118 119 #endif // MINDSPORE_LITE_TOOLS_COMMON_OPTION_H_ 120