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.

117 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Represents the 'success' or 'failure' of an operation, and holds an associated
  22. error message to describe the error when there's a failure.
  23. E.g.
  24. @code
  25. Result myOperation()
  26. {
  27. if (doSomeKindOfFoobar())
  28. return Result::ok();
  29. else
  30. return Result::fail ("foobar didn't work!");
  31. }
  32. const Result result (myOperation());
  33. if (result.wasOk())
  34. {
  35. ...it's all good...
  36. }
  37. else
  38. {
  39. warnUserAboutFailure ("The foobar operation failed! Error message was: "
  40. + result.getErrorMessage());
  41. }
  42. @endcode
  43. @tags{Core}
  44. */
  45. class JUCE_API Result
  46. {
  47. public:
  48. //==============================================================================
  49. /** Creates and returns a 'successful' result. */
  50. static Result ok() noexcept { return Result(); }
  51. /** Creates a 'failure' result.
  52. If you pass a blank error message in here, a default "Unknown Error" message
  53. will be used instead.
  54. */
  55. static Result fail (const String& errorMessage) noexcept;
  56. //==============================================================================
  57. /** Returns true if this result indicates a success. */
  58. bool wasOk() const noexcept;
  59. /** Returns true if this result indicates a failure.
  60. You can use getErrorMessage() to retrieve the error message associated
  61. with the failure.
  62. */
  63. bool failed() const noexcept;
  64. /** Returns true if this result indicates a success.
  65. This is equivalent to calling wasOk().
  66. */
  67. operator bool() const noexcept;
  68. /** Returns true if this result indicates a failure.
  69. This is equivalent to calling failed().
  70. */
  71. bool operator!() const noexcept;
  72. /** Returns the error message that was set when this result was created.
  73. For a successful result, this will be an empty string;
  74. */
  75. const String& getErrorMessage() const noexcept;
  76. //==============================================================================
  77. Result (const Result&);
  78. Result& operator= (const Result&);
  79. Result (Result&&) noexcept;
  80. Result& operator= (Result&&) noexcept;
  81. bool operator== (const Result& other) const noexcept;
  82. bool operator!= (const Result& other) const noexcept;
  83. private:
  84. String errorMessage;
  85. // The default constructor is not for public use!
  86. // Instead, use Result::ok() or Result::fail()
  87. Result() noexcept;
  88. explicit Result (const String&) 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. } // namespace juce