maybe.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CRDTP_MAYBE_H_
  5. #define CRDTP_MAYBE_H_
  6. #include <cassert>
  7. #include <memory>
  8. namespace crdtp {
  9. // =============================================================================
  10. // detail::PtrMaybe, detail::ValueMaybe, templates for optional
  11. // pointers / values which are used in ../lib/Forward_h.template.
  12. // =============================================================================
  13. namespace detail {
  14. template <typename T>
  15. class PtrMaybe {
  16. public:
  17. PtrMaybe() = default;
  18. PtrMaybe(std::unique_ptr<T> value) : value_(std::move(value)) {}
  19. PtrMaybe(PtrMaybe&& other) noexcept : value_(std::move(other.value_)) {}
  20. void operator=(std::unique_ptr<T> value) { value_ = std::move(value); }
  21. T* fromJust() const {
  22. assert(value_);
  23. return value_.get();
  24. }
  25. T* fromMaybe(T* default_value) const {
  26. return value_ ? value_.get() : default_value;
  27. }
  28. bool isJust() const { return value_ != nullptr; }
  29. std::unique_ptr<T> takeJust() {
  30. assert(value_);
  31. return std::move(value_);
  32. }
  33. private:
  34. std::unique_ptr<T> value_;
  35. };
  36. template <typename T>
  37. class ValueMaybe {
  38. public:
  39. ValueMaybe() : is_just_(false), value_() {}
  40. ValueMaybe(T value) : is_just_(true), value_(std::move(value)) {}
  41. ValueMaybe(ValueMaybe&& other) noexcept
  42. : is_just_(other.is_just_), value_(std::move(other.value_)) {}
  43. void operator=(T value) {
  44. value_ = value;
  45. is_just_ = true;
  46. }
  47. const T& fromJust() const {
  48. assert(is_just_);
  49. return value_;
  50. }
  51. const T& fromMaybe(const T& default_value) const {
  52. return is_just_ ? value_ : default_value;
  53. }
  54. bool isJust() const { return is_just_; }
  55. T takeJust() {
  56. assert(is_just_);
  57. is_just_ = false;
  58. return std::move(value_);
  59. }
  60. private:
  61. bool is_just_;
  62. T value_;
  63. };
  64. template <typename T>
  65. struct MaybeTypedef {
  66. typedef PtrMaybe<T> type;
  67. };
  68. template <>
  69. struct MaybeTypedef<bool> {
  70. typedef ValueMaybe<bool> type;
  71. };
  72. template <>
  73. struct MaybeTypedef<int> {
  74. typedef ValueMaybe<int> type;
  75. };
  76. template <>
  77. struct MaybeTypedef<double> {
  78. typedef ValueMaybe<double> type;
  79. };
  80. template <>
  81. struct MaybeTypedef<std::string> {
  82. typedef ValueMaybe<std::string> type;
  83. };
  84. } // namespace detail
  85. template <typename T>
  86. using Maybe = typename detail::MaybeTypedef<T>::type;
  87. } // namespace crdtp
  88. #endif // CRDTP_MAYBE_H_