You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

option.h 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright 2019 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. #ifndef PREDICT_COMMON_OPTION_H_
  17. #define PREDICT_COMMON_OPTION_H_
  18. #include <type_traits>
  19. #include <utility>
  20. #include "common/mslog.h"
  21. namespace mindspore {
  22. namespace predict {
  23. template <typename T>
  24. struct InnerSome {
  25. explicit InnerSome(const T &t) : _t(std::move(t)) {}
  26. T _t;
  27. };
  28. template <typename T>
  29. InnerSome<typename std::decay<T>::type> Some(T &&t) {
  30. return InnerSome<typename std::decay<T>::type>(std::forward<T>(t));
  31. }
  32. struct None {};
  33. template <typename T>
  34. class Option {
  35. public:
  36. Option() : state(NONE) {}
  37. explicit Option(const T &t) : data(t), state(SOME) {}
  38. explicit Option(T &&t) : data(std::move(t)), state(SOME) {}
  39. explicit Option(const InnerSome<T> &some) : data(some._t), state(SOME) {}
  40. explicit Option(const None &none) : state(NONE) {}
  41. Option(const Option<T> &that) : state(that.state) {
  42. if (that.IsSome()) {
  43. new (&data) T(that.data);
  44. }
  45. }
  46. virtual ~Option() = default;
  47. bool IsNone() const { return state == NONE; }
  48. bool IsSome() const { return state == SOME; }
  49. const T &Get() const & {
  50. MS_ASSERT(IsSome());
  51. return data;
  52. }
  53. T &Get() & {
  54. MS_ASSERT(IsSome());
  55. return data;
  56. }
  57. T &&Get() && {
  58. MS_ASSERT(IsSome());
  59. return std::move(data);
  60. }
  61. const T &&Get() const && {
  62. MS_ASSERT(IsSome());
  63. return std::move(data);
  64. }
  65. // oprerator override
  66. Option<T> &operator=(const Option<T> &that) {
  67. if (&that != this) {
  68. if (IsSome()) {
  69. data.~T();
  70. }
  71. state = that.state;
  72. if (that.IsSome()) {
  73. new (&data) T(that.data);
  74. }
  75. }
  76. return *this;
  77. }
  78. bool operator==(const Option<T> &that) const {
  79. return (IsNone() && that.IsNone()) || (IsSome() && that.IsSome() && data == that.data);
  80. }
  81. bool operator!=(const Option<T> &that) const { return !(*this == that); }
  82. bool operator==(const T &that) const { return IsSome() && data == that; }
  83. bool operator!=(const T &that) const { return !(*this == that); }
  84. private:
  85. enum State { NONE = 0, SOME = 1 };
  86. T data;
  87. State state;
  88. };
  89. } // namespace predict
  90. } // namespace mindspore
  91. #endif // PREDICT_COMMON_OPTION_H_