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.

122 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_RESULT_JUCEHEADER__
  19. #define __JUCE_RESULT_JUCEHEADER__
  20. #include "../text/juce_String.h"
  21. //==============================================================================
  22. /**
  23. Represents the 'success' or 'failure' of an operation, and holds an associated
  24. error message to describe the error when there's a failure.
  25. E.g.
  26. @code
  27. Result myOperation()
  28. {
  29. if (doSomeKindOfFoobar())
  30. return Result::ok();
  31. else
  32. return Result::fail ("foobar didn't work!");
  33. }
  34. const Result result (myOperation());
  35. if (result.wasOk())
  36. {
  37. ...it's all good...
  38. }
  39. else
  40. {
  41. warnUserAboutFailure ("The foobar operation failed! Error message was: "
  42. + result.getErrorMessage());
  43. }
  44. @endcode
  45. */
  46. class JUCE_API Result
  47. {
  48. public:
  49. //==============================================================================
  50. /** Creates and returns a 'successful' result. */
  51. static Result ok() noexcept;
  52. /** Creates a 'failure' result.
  53. If you pass a blank error message in here, a default "Unknown Error" message
  54. will be used instead.
  55. */
  56. static Result fail (const String& errorMessage) noexcept;
  57. //==============================================================================
  58. /** Returns true if this result indicates a success. */
  59. bool wasOk() const noexcept;
  60. /** Returns true if this result indicates a failure.
  61. You can use getErrorMessage() to retrieve the error message associated
  62. with the failure.
  63. */
  64. bool failed() const noexcept;
  65. /** Returns true if this result indicates a success.
  66. This is equivalent to calling wasOk().
  67. */
  68. operator bool() const noexcept;
  69. /** Returns true if this result indicates a failure.
  70. This is equivalent to calling failed().
  71. */
  72. bool operator!() const noexcept;
  73. /** Returns the error message that was set when this result was created.
  74. For a successful result, this will be an empty string;
  75. */
  76. const String& getErrorMessage() const noexcept;
  77. //==============================================================================
  78. Result (const Result& other);
  79. Result& operator= (const Result& other);
  80. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  81. Result (Result&& other) noexcept;
  82. Result& operator= (Result&& other) noexcept;
  83. #endif
  84. bool operator== (const Result& other) const noexcept;
  85. bool operator!= (const Result& other) const noexcept;
  86. private:
  87. String errorMessage;
  88. explicit Result (const String& errorMessage) noexcept;
  89. // These casts are private to prevent people trying to use the Result object in numeric contexts
  90. operator int() const;
  91. operator void*() const;
  92. };
  93. #endif // __JUCE_RESULT_JUCEHEADER__