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.

121 lines
4.2KB

  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. #pragma once
  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 { return Result(); }
  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&);
  82. Result& operator= (const Result&);
  83. Result (Result&&) noexcept;
  84. Result& operator= (Result&&) noexcept;
  85. bool operator== (const Result& other) const noexcept;
  86. bool operator!= (const Result& other) const noexcept;
  87. private:
  88. String errorMessage;
  89. // The default constructor is not for public use!
  90. // Instead, use Result::ok() or Result::fail()
  91. Result() noexcept;
  92. explicit Result (const String&) noexcept;
  93. // These casts are private to prevent people trying to use the Result object in numeric contexts
  94. operator int() const;
  95. operator void*() const;
  96. };