Audio plugin host https://kx.studio/carla
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.

Result.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_RESULT_H_INCLUDED
  21. #define WATER_RESULT_H_INCLUDED
  22. #include "../text/String.h"
  23. namespace water {
  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 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. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  84. Result (Result&&) noexcept;
  85. Result& operator= (Result&&) 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. // The default constructor is not for public use!
  92. // Instead, use Result::ok() or Result::fail()
  93. Result() noexcept;
  94. explicit Result (const String&) noexcept;
  95. // These casts are private to prevent people trying to use the Result object in numeric contexts
  96. operator int() const;
  97. operator void*() const;
  98. };
  99. }
  100. #endif // WATER_RESULT_H_INCLUDED