required.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. namespace base {
  9. template <typename T>
  10. struct required {
  11. template <
  12. typename U,
  13. typename = std::enable_if_t<std::is_constructible_v<T, U&&>>>
  14. constexpr required(U &&value) noexcept : _value(std::forward<U>(value)) {
  15. }
  16. template <
  17. typename U,
  18. typename = std::enable_if_t<std::is_assignable_v<T, U&&>>>
  19. constexpr required &operator=(U &&value) noexcept {
  20. _value = std::forward<U>(value);
  21. return *this;
  22. }
  23. constexpr required(required &&) = default;
  24. constexpr required(const required &) = default;
  25. constexpr required &operator=(required &&) = default;
  26. constexpr required &operator=(const required &) = default;
  27. [[nodiscard]] constexpr T &operator*() noexcept {
  28. return _value;
  29. }
  30. [[nodiscard]] constexpr const T &operator*() const noexcept {
  31. return _value;
  32. }
  33. [[nodiscard]] constexpr T &value() noexcept {
  34. return _value;
  35. }
  36. [[nodiscard]] constexpr const T &value() const noexcept {
  37. return _value;
  38. }
  39. [[nodiscard]] constexpr T *operator->() noexcept {
  40. return &_value;
  41. }
  42. [[nodiscard]] constexpr const T *operator->() const noexcept {
  43. return &_value;
  44. }
  45. [[nodiscard]] constexpr T *get() noexcept {
  46. return &_value;
  47. }
  48. [[nodiscard]] constexpr const T *get() const noexcept {
  49. return &_value;
  50. }
  51. [[nodiscard]] operator T&() noexcept {
  52. return _value;
  53. }
  54. [[nodiscard]] operator const T&() const noexcept {
  55. return _value;
  56. }
  57. private:
  58. T _value;
  59. };
  60. } // namespace base