ExceptionOr.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2021, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullRefPtr.h>
  28. #include <AK/Optional.h>
  29. #include <AK/RefPtr.h>
  30. #include <LibWeb/DOM/DOMException.h>
  31. namespace Web::DOM {
  32. template<typename ValueType>
  33. class ExceptionOr {
  34. public:
  35. ExceptionOr(const ValueType& result)
  36. : m_result(result)
  37. {
  38. }
  39. ExceptionOr(ValueType&& result)
  40. : m_result(move(result))
  41. {
  42. }
  43. ExceptionOr(const NonnullRefPtr<DOMException> exception)
  44. : m_exception(exception)
  45. {
  46. }
  47. ExceptionOr(ExceptionOr&& other) = default;
  48. ExceptionOr(const ExceptionOr& other) = default;
  49. ~ExceptionOr() = default;
  50. ValueType& value()
  51. {
  52. return m_result.value();
  53. }
  54. ValueType release_value()
  55. {
  56. return m_result.release_value();
  57. }
  58. const DOMException& exception() const
  59. {
  60. return *m_exception;
  61. }
  62. bool is_exception() const
  63. {
  64. return m_exception;
  65. }
  66. private:
  67. Optional<ValueType> m_result;
  68. RefPtr<DOMException> m_exception;
  69. };
  70. template<>
  71. class ExceptionOr<void> {
  72. public:
  73. ExceptionOr(const NonnullRefPtr<DOMException> exception)
  74. : m_exception(exception)
  75. {
  76. }
  77. ExceptionOr() = default;
  78. ExceptionOr(ExceptionOr&& other) = default;
  79. ExceptionOr(const ExceptionOr& other) = default;
  80. ~ExceptionOr() = default;
  81. const DOMException& exception() const
  82. {
  83. return *m_exception;
  84. }
  85. bool is_exception() const
  86. {
  87. return m_exception;
  88. }
  89. private:
  90. RefPtr<DOMException> m_exception;
  91. };
  92. }