optional.h 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#ifndef ONEFLOW_CORE_COMMON_OPTIONAL_H_
#define ONEFLOW_CORE_COMMON_OPTIONAL_H_

20 21 22 23
#include <memory>
#include <type_traits>
#include <utility>
#include "oneflow/core/common/error.cfg.h"
24
#include "oneflow/core/common/type_traits.h"
25
#include "oneflow/core/common/just.h"
26 27

namespace oneflow {
28 29 30 31 32 33 34 35 36 37 38

struct InPlaceConstructType {
  explicit InPlaceConstructType() = default;
};
constexpr InPlaceConstructType InPlaceConstruct{};

struct NullOptType {
  explicit constexpr NullOptType(int) {}
};
constexpr NullOptType NullOpt{0};

39 40 41
namespace internal {

template<typename T, typename U = void>
42
class OptionalBase;
43 44

template<typename T>
45
class OptionalBase<T, typename std::enable_if<IsScalarType<T>::value>::type> {
46
 public:
47 48
  using value_type = T;
  using storage_type = T;
49

50 51
  OptionalBase() : init_(false), value_() {}
  ~OptionalBase() = default;
52

53 54 55 56 57 58 59
  explicit OptionalBase(const T& value) : init_(true), value_(value) {}
  explicit OptionalBase(T&& value) : init_(true), value_(std::move(value)) {}

  OptionalBase(const OptionalBase& base) : init_(base.init_), value_(base.value_) {}
  OptionalBase(OptionalBase&& base) noexcept : init_(base.init_), value_(std::move(base.value_)) {}

  OptionalBase& operator=(const T& value) {
60
    value_ = value;
61 62
    init_ = true;

63 64
    return *this;
  }
65
  OptionalBase& operator=(T&& value) {
66
    value_ = std::move(value);
67 68
    init_ = true;

69 70
    return *this;
  }
71
  OptionalBase& operator=(const OptionalBase& rhs) {
72
    value_ = rhs.value_;
73 74
    init_ = rhs.init_;

75 76
    return *this;
  }
77
  OptionalBase& operator=(OptionalBase&& rhs) noexcept {
78
    value_ = std::move(rhs.value_);
79 80
    init_ = rhs.init_;

81 82 83
    return *this;
  }

84 85 86 87 88
  T value() const& { return value_; }  // `T value() &&` goes here
  T& value() & { return value_; }

  bool has_value() const { return init_; }

89 90 91 92 93 94 95 96
  T value_or(const T& other) const {
    if (has_value()) {
      return value();
    } else {
      return other;
    }
  }

97
  void reset() { init_ = false; }
98 99

 private:
100
  bool init_;
101 102 103 104
  T value_;
};

template<typename T>
105
class OptionalBase<T, typename std::enable_if<std::is_reference<T>::value>::type> {
106
 public:
107 108 109 110 111 112 113 114 115 116 117
  using value_type = typename std::remove_reference<T>::type;
  using storage_type = value_type*;

  static_assert(std::is_lvalue_reference<T>::value, "rvalue reference is not supported here");

  OptionalBase() : value_(nullptr){};
  ~OptionalBase() = default;

  explicit OptionalBase(T value) : value_(&value) {}
  OptionalBase(const OptionalBase& base) : value_(base.value_) {}
  OptionalBase(OptionalBase&& base) noexcept : value_(base.value_) {}
118

119 120 121 122 123 124 125 126 127 128 129
  OptionalBase& operator=(T value) {
    value_ = &value;
    return *this;
  }
  OptionalBase& operator=(const OptionalBase& rhs) {
    value_ = rhs.value_;
    return *this;
  }
  OptionalBase& operator=(OptionalBase&& rhs) noexcept {
    value_ = std::move(rhs.value_);
    return *this;
130 131
  }

132 133 134 135 136
  const value_type& value() const { return *value_; }
  T value() { return *value_; }

  bool has_value() const { return value_; }

137 138 139 140 141 142 143 144
  const value_type& value_or(const value_type& other) const {
    if (has_value()) {
      return value();
    } else {
      return other;
    }
  }

145 146 147 148 149
  void reset() { value_ = nullptr; }

 private:
  storage_type value_;
};
150

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
template<typename T>
class OptionalBase<
    T, typename std::enable_if<!IsScalarType<T>::value && !std::is_reference<T>::value>::type> {
 public:
  using value_type = T;
  using storage_type = std::shared_ptr<T>;

  OptionalBase() : value_(nullptr){};
  ~OptionalBase() = default;

  template<typename... Args>
  explicit OptionalBase(InPlaceConstructType, Args&&... args)
      : value_(std::make_shared<T>(std::forward<Args>(args)...)) {}

  explicit OptionalBase(const T& value) : value_(std::make_shared<T>(value)) {}
  explicit OptionalBase(T&& value) : value_(std::make_shared<T>(std::move(value))) {}

  explicit OptionalBase(const storage_type& value) : value_(value) {}
  explicit OptionalBase(storage_type&& value) : value_(std::move(value)) {}

  OptionalBase(const OptionalBase&) = default;
  OptionalBase(OptionalBase&&) noexcept = default;

  OptionalBase& operator=(const T& value) {
175 176 177 178 179 180 181
    if (value_) {
      *value_ = value;
    } else {
      value_ = std::make_shared<T>(value);
    }
    return *this;
  }
182
  OptionalBase& operator=(T&& value) {
183 184 185
    if (value_) {
      *value_ = std::move(value);
    } else {
186
      value_ = std::make_shared<T>(std::move(value));
187 188 189
    }
    return *this;
  }
190 191 192 193 194 195 196 197 198 199 200

  OptionalBase& operator=(const storage_type& value) {
    value_ = value;
    return *this;
  }
  OptionalBase& operator=(storage_type&& value) {
    value_ = std::move(value);
    return *this;
  }

  OptionalBase& operator=(const OptionalBase& rhs) {
201 202 203
    value_ = rhs.value_;
    return *this;
  }
204
  OptionalBase& operator=(OptionalBase&& rhs) noexcept {
205 206 207 208
    value_ = std::move(rhs.value_);
    return *this;
  }

209 210 211 212 213 214 215 216 217
  const storage_type& value() const& { return value_; }
  storage_type& value() & { return value_; }

  // generate a temporary object to allow `const auto& x = optval().value()` where `optval()` is a
  // function call which returns a temporary Optional
  storage_type value() && { return std::move(value_); }

  bool has_value() const { return bool(value_); }

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  const storage_type& value_or(const storage_type& other) const& {
    if (has_value()) {
      return value_;
    } else {
      return other;
    }
  }

  storage_type value_or(const storage_type& other) && {
    if (has_value()) {
      return std::move(value_);
    } else {
      return other;
    }
  }

  storage_type value_or(storage_type&& other) const& {
    if (has_value()) {
      return value_;
    } else {
      return std::move(other);
    }
  }

  storage_type value_or(storage_type&& other) && {
    if (has_value()) {
      return std::move(value_);
    } else {
      return std::move(other);
    }
  }

  // we introduce a dependent name `U` to delay the instantiation,
  // so only the default parameter of `U` is allowed
  template<typename U = value_type>
  typename std::enable_if<!std::is_abstract<U>::value, const U&>::type value_or(
      const value_type& other) const& {
    static_assert(std::is_same<U, value_type>::value, "expected default U");

    if (has_value()) {
      return *value_;
    } else {
      return other;
    }
  }

  template<typename U = value_type>
  typename std::enable_if<!std::is_abstract<U>::value, U>::type value_or(
      const value_type& other) && {
    static_assert(std::is_same<U, value_type>::value, "expected default U");

    if (has_value()) {
      return std::move(*value_);
    } else {
      return other;
    }
  }

  template<typename U = value_type>
  typename std::enable_if<!std::is_abstract<U>::value, U>::type value_or(
      value_type&& other) const& {
    static_assert(std::is_same<U, value_type>::value, "expected default U");

    if (has_value()) {
      return *value_;
    } else {
      return std::move(other);
    }
  }

  template<typename U = value_type>
  typename std::enable_if<!std::is_abstract<U>::value, U>::type value_or(value_type&& other) && {
    static_assert(std::is_same<U, value_type>::value, "expected default U");

    if (has_value()) {
      return std::move(*value_);
    } else {
      return std::move(other);
    }
  }

299
  void reset() { value_.reset(); }
300 301

 private:
302
  storage_type value_;
303 304 305 306 307
};

}  // namespace internal

template<typename T>
308
class Optional final : private internal::OptionalBase<T> {
309
 private:
310
  using base = internal::OptionalBase<T>;
311

312
 public:
313 314
  using value_type = typename base::value_type;
  using storage_type = typename base::storage_type;
315

316
  Optional() = default;
317 318
  ~Optional() = default;

319 320
  Optional(NullOptType)  // NOLINT(google-explicit-constructor)
      : base() {}
321

322 323 324 325 326 327 328
  template<
      typename Arg1, typename... ArgN,
      typename std::enable_if<!(sizeof...(ArgN) == 0
                                && std::is_same<Optional, typename std::decay<Arg1>::type>::value),
                              int>::type = 0>
  Optional(Arg1&& v1, ArgN&&... vn)  // NOLINT(google-explicit-constructor)
      : base(std::forward<Arg1>(v1), std::forward<ArgN>(vn)...) {}
329

330 331
  Optional(const Optional&) = default;
  Optional(Optional&&) noexcept = default;
332

333 334 335 336 337
  template<typename U,
           typename std::enable_if<!std::is_same<Optional, typename std::decay<U>::type>::value,
                                   int>::type = 0>
  Optional& operator=(U&& val) {
    return static_cast<Optional&>(static_cast<base&>(*this) = std::forward<U>(val));
338 339
  }

340 341
  Optional& operator=(const Optional& rhs) = default;
  Optional& operator=(Optional&& rhs) noexcept = default;
342

343
  template<typename U>
T
Twice 已提交
344
  decltype(auto) value_or(U&& other) const& {
345 346 347 348
    return base::value_or(std::forward<U>(other));
  }

  template<typename U>
T
Twice 已提交
349
  decltype(auto) value_or(U&& other) && {
350
    return std::move(*this).base::value_or(std::forward<U>(other));
351 352
  }

353
  bool has_value() const { return base::has_value(); }
354
  explicit operator bool() const { return has_value(); }
355

T
Twice 已提交
356
  decltype(auto) Data_YouAreNotAllowedToCallThisFuncOutsideThisFile() && {
357 358
    return std::move(*this).base::value();
  }
359

360
  void reset() { base::reset(); }
361 362 363 364 365
};

}  // namespace oneflow

#endif  // ONEFLOW_CORE_COMMON_OPTIONAL_H_