The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

125 lines
4.3KB

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