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.

124 lines
4.1KB

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