• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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_CORE_MINDRT_INCLUDE_ASYNC_OPTION_H
18 #define MINDSPORE_CORE_MINDRT_INCLUDE_ASYNC_OPTION_H
19 
20 #include <utility>
21 
22 #include "actor/log.h"
23 
24 namespace mindspore {
25 template <typename T>
26 struct InnerSome {
InnerSomeInnerSome27   explicit InnerSome(const T &t) : _t(std::move(t)) {}
28   T _t;
29 };
30 
31 template <typename T>
Some(T && t)32 InnerSome<typename std::decay<T>::type> Some(T &&t) {
33   return InnerSome<typename std::decay<T>::type>(std::forward<T>(t));
34 }
35 
36 struct MindrtNone {};
37 
38 template <typename T>
39 class Option {
40  public:
Option()41   Option() : data(), state(NONE) {}
42 
Option(const T t)43   explicit Option(const T t) : data(t), state(SOME) {}
44 
Option(T && t)45   explicit Option(T &&t) : data(std::move(t)), state(SOME) {}
46 
Option(const InnerSome<T> & some)47   explicit Option(const InnerSome<T> &some) : data(some._t), state(SOME) {}
48 
Option(const MindrtNone & none)49   explicit Option(const MindrtNone &none) : data(), state(NONE) {}
50 
Option(const Option<T> & that)51   Option(const Option<T> &that) : data(), state(that.state) {
52     if (that.IsSome()) {
53       data = that.data;
54     }
55   }
56 
~Option()57   virtual ~Option() {}
58 
IsNone()59   bool IsNone() const { return state == NONE; }
60 
IsSome()61   bool IsSome() const { return state == SOME; }
62 
Get()63   const T &Get() const & {
64     MINDRT_ASSERT(IsSome());
65     return data;
66   }
67 
Get()68   T &&Get() && {
69     MINDRT_ASSERT(IsSome());
70     return std::move(data);
71   }
72 
Get()73   const T &&Get() const && {
74     MINDRT_ASSERT(IsSome());
75     return std::move(data);
76   }
77 
78   // oprerator override
79   Option<T> &operator=(const Option<T> &that) {
80     if (&that != this) {
81       state = that.state;
82       if (that.IsSome()) {
83         data = that.data;
84       }
85     }
86 
87     return *this;
88   }
89 
90   bool operator==(const Option<T> &that) const {
91     return (IsNone() && that.IsNone()) || (IsSome() && that.IsSome() && data == that.data);
92   }
93 
94   bool operator!=(const Option<T> &that) const { return !(*this == that); }
95 
96   bool operator==(const T &that) const { return IsSome() && data == that; }
97 
98   bool operator!=(const T &that) const { return !(*this == that); }
99 
100  private:
101   enum State { NONE = 0, SOME = 1 };
102 
103   T data;
104   State state;
105 };
106 }  // namespace mindspore
107 
108 #endif
109