maybe.hpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #pragma once
  2. namespace nall {
  3. struct nothing_t {};
  4. static nothing_t nothing;
  5. struct else_t {};
  6. template<typename T>
  7. struct maybe {
  8. inline maybe() {}
  9. inline maybe(nothing_t) {}
  10. inline maybe(const T& source) { operator=(source); }
  11. inline maybe(T&& source) { operator=(move(source)); }
  12. inline maybe(const maybe& source) { operator=(source); }
  13. inline maybe(maybe&& source) { operator=(move(source)); }
  14. inline ~maybe() { reset(); }
  15. inline auto operator=(nothing_t) -> maybe& { reset(); return *this; }
  16. inline auto operator=(const T& source) -> maybe& { reset(); _valid = true; new(&_value.t) T(source); return *this; }
  17. inline auto operator=(T&& source) -> maybe& { reset(); _valid = true; new(&_value.t) T(move(source)); return *this; }
  18. inline auto operator=(const maybe& source) -> maybe& {
  19. if(this == &source) return *this;
  20. reset();
  21. if(_valid = source._valid) new(&_value.t) T(source.get());
  22. return *this;
  23. }
  24. inline auto operator=(maybe&& source) -> maybe& {
  25. if(this == &source) return *this;
  26. reset();
  27. if(_valid = source._valid) new(&_value.t) T(move(source.get()));
  28. return *this;
  29. }
  30. inline explicit operator bool() const { return _valid; }
  31. inline auto reset() -> void { if(_valid) { _value.t.~T(); _valid = false; } }
  32. inline auto data() -> T* { return _valid ? &_value.t : nullptr; }
  33. inline auto get() -> T& { assert(_valid); return _value.t; }
  34. inline auto data() const -> const T* { return ((maybe*)this)->data(); }
  35. inline auto get() const -> const T& { return ((maybe*)this)->get(); }
  36. inline auto operator->() -> T* { return data(); }
  37. inline auto operator->() const -> const T* { return data(); }
  38. inline auto operator*() -> T& { return get(); }
  39. inline auto operator*() const -> const T& { return get(); }
  40. inline auto operator()() -> T& { return get(); }
  41. inline auto operator()() const -> const T& { return get(); }
  42. inline auto operator()(const T& invalid) const -> const T& { return _valid ? get() : invalid; }
  43. private:
  44. union U {
  45. T t;
  46. U() {}
  47. ~U() {}
  48. } _value;
  49. bool _valid = false;
  50. };
  51. template<typename T>
  52. struct maybe<T&> {
  53. inline maybe() : _value(nullptr) {}
  54. inline maybe(nothing_t) : _value(nullptr) {}
  55. inline maybe(const T& source) : _value((T*)&source) {}
  56. inline maybe(const maybe& source) : _value(source._value) {}
  57. inline auto operator=(nothing_t) -> maybe& { _value = nullptr; return *this; }
  58. inline auto operator=(const T& source) -> maybe& { _value = (T*)&source; return *this; }
  59. inline auto operator=(const maybe& source) -> maybe& { _value = source._value; return *this; }
  60. inline explicit operator bool() const { return _value; }
  61. inline auto reset() -> void { _value = nullptr; }
  62. inline auto data() -> T* { return _value; }
  63. inline auto get() -> T& { assert(_value); return *_value; }
  64. inline auto data() const -> const T* { return ((maybe*)this)->data(); }
  65. inline auto get() const -> const T& { return ((maybe*)this)->get(); }
  66. inline auto operator->() -> T* { return data(); }
  67. inline auto operator->() const -> const T* { return data(); }
  68. inline auto operator*() -> T& { return get(); }
  69. inline auto operator*() const -> const T& { return get(); }
  70. inline auto operator()() -> T& { return get(); }
  71. inline auto operator()() const -> const T& { return get(); }
  72. inline auto operator()(const T& invalid) const -> const T& { return _value ? get() : invalid; }
  73. private:
  74. T* _value;
  75. };
  76. }