juce_Result.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_H_INCLUDED
  22. #define JUCE_RESULT_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Represents the 'success' or 'failure' of an operation, and holds an associated
  26. error message to describe the error when there's a failure.
  27. E.g.
  28. @code
  29. Result myOperation()
  30. {
  31. if (doSomeKindOfFoobar())
  32. return Result::ok();
  33. else
  34. return Result::fail ("foobar didn't work!");
  35. }
  36. const Result result (myOperation());
  37. if (result.wasOk())
  38. {
  39. ...it's all good...
  40. }
  41. else
  42. {
  43. warnUserAboutFailure ("The foobar operation failed! Error message was: "
  44. + result.getErrorMessage());
  45. }
  46. @endcode
  47. */
  48. class JUCE_API Result
  49. {
  50. public:
  51. //==============================================================================
  52. /** Creates and returns a 'successful' result. */
  53. static Result ok() noexcept { return Result(); }
  54. /** Creates a 'failure' result.
  55. If you pass a blank error message in here, a default "Unknown Error" message
  56. will be used instead.
  57. */
  58. static Result fail (const String& errorMessage) noexcept;
  59. //==============================================================================
  60. /** Returns true if this result indicates a success. */
  61. bool wasOk() const noexcept;
  62. /** Returns true if this result indicates a failure.
  63. You can use getErrorMessage() to retrieve the error message associated
  64. with the failure.
  65. */
  66. bool failed() const noexcept;
  67. /** Returns true if this result indicates a success.
  68. This is equivalent to calling wasOk().
  69. */
  70. operator bool() const noexcept;
  71. /** Returns true if this result indicates a failure.
  72. This is equivalent to calling failed().
  73. */
  74. bool operator!() const noexcept;
  75. /** Returns the error message that was set when this result was created.
  76. For a successful result, this will be an empty string;
  77. */
  78. const String& getErrorMessage() const noexcept;
  79. //==============================================================================
  80. Result (const Result&);
  81. Result& operator= (const Result&);
  82. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  83. Result (Result&&) noexcept;
  84. Result& operator= (Result&&) noexcept;
  85. #endif
  86. bool operator== (const Result& other) const noexcept;
  87. bool operator!= (const Result& other) const noexcept;
  88. private:
  89. String errorMessage;
  90. // The default constructor is not for public use!
  91. // Instead, use Result::ok() or Result::fail()
  92. Result() noexcept;
  93. explicit Result (const String&) noexcept;
  94. // These casts are private to prevent people trying to use the Result object in numeric contexts
  95. operator int() const;
  96. operator void*() const;
  97. };
  98. #endif // JUCE_RESULT_H_INCLUDED