Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_RESULT_H_INCLUDED
  24. #define JUCE_RESULT_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Represents the 'success' or 'failure' of an operation, and holds an associated
  28. error message to describe the error when there's a failure.
  29. E.g.
  30. @code
  31. Result myOperation()
  32. {
  33. if (doSomeKindOfFoobar())
  34. return Result::ok();
  35. else
  36. return Result::fail ("foobar didn't work!");
  37. }
  38. const Result result (myOperation());
  39. if (result.wasOk())
  40. {
  41. ...it's all good...
  42. }
  43. else
  44. {
  45. warnUserAboutFailure ("The foobar operation failed! Error message was: "
  46. + result.getErrorMessage());
  47. }
  48. @endcode
  49. */
  50. class JUCE_API Result
  51. {
  52. public:
  53. //==============================================================================
  54. /** Creates and returns a 'successful' result. */
  55. static Result ok() noexcept { return Result(); }
  56. /** Creates a 'failure' result.
  57. If you pass a blank error message in here, a default "Unknown Error" message
  58. will be used instead.
  59. */
  60. static Result fail (const String& errorMessage) noexcept;
  61. //==============================================================================
  62. /** Returns true if this result indicates a success. */
  63. bool wasOk() const noexcept;
  64. /** Returns true if this result indicates a failure.
  65. You can use getErrorMessage() to retrieve the error message associated
  66. with the failure.
  67. */
  68. bool failed() const noexcept;
  69. /** Returns true if this result indicates a success.
  70. This is equivalent to calling wasOk().
  71. */
  72. operator bool() const noexcept;
  73. /** Returns true if this result indicates a failure.
  74. This is equivalent to calling failed().
  75. */
  76. bool operator!() const noexcept;
  77. /** Returns the error message that was set when this result was created.
  78. For a successful result, this will be an empty string;
  79. */
  80. const String& getErrorMessage() const noexcept;
  81. //==============================================================================
  82. Result (const Result&);
  83. Result& operator= (const Result&);
  84. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  85. Result (Result&&) noexcept;
  86. Result& operator= (Result&&) noexcept;
  87. #endif
  88. bool operator== (const Result& other) const noexcept;
  89. bool operator!= (const Result& other) const noexcept;
  90. private:
  91. String errorMessage;
  92. // The default constructor is not for public use!
  93. // Instead, use Result::ok() or Result::fail()
  94. Result() noexcept;
  95. explicit Result (const String&) noexcept;
  96. // These casts are private to prevent people trying to use the Result object in numeric contexts
  97. operator int() const;
  98. operator void*() const;
  99. };
  100. #endif // JUCE_RESULT_H_INCLUDED