2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 ==============================================================================
26 #ifndef WATER_RESULT_H_INCLUDED
27 #define WATER_RESULT_H_INCLUDED
35 //==============================================================================
37 Represents the 'success' or 'failure' of an operation, and holds an associated
38 error message to describe the error when there's a failure.
44 if (doSomeKindOfFoobar())
47 return Result::fail ("foobar didn't work!");
50 const Result result (myOperation());
58 warnUserAboutFailure ("The foobar operation failed! Error message was: "
59 + result.getErrorMessage());
66 //==============================================================================
67 /** Creates and returns a 'successful' result. */
68 static Result
ok() noexcept
{ return Result(); }
70 /** Creates a 'failure' result.
71 If you pass a blank error message in here, a default "Unknown Error" message
74 static Result
fail (const std::string
& errorMessage
) noexcept
;
76 //==============================================================================
77 /** Returns true if this result indicates a success. */
78 bool wasOk() const noexcept
;
80 /** Returns true if this result indicates a failure.
81 You can use getErrorMessage() to retrieve the error message associated
84 bool failed() const noexcept
;
86 /** Returns true if this result indicates a success.
87 This is equivalent to calling wasOk().
89 operator bool() const noexcept
;
91 /** Returns true if this result indicates a failure.
92 This is equivalent to calling failed().
94 bool operator!() const noexcept
;
96 /** Returns the error message that was set when this result was created.
97 For a successful result, this will be an empty string;
99 const std::string
& getErrorMessage() const noexcept
;
101 //==============================================================================
102 Result (const Result
&);
103 Result
& operator= (const Result
&);
105 bool operator== (const Result
& other
) const noexcept
;
106 bool operator!= (const Result
& other
) const noexcept
;
109 std::string errorMessage
;
111 // The default constructor is not for public use!
112 // Instead, use Result::ok() or Result::fail()
114 explicit Result (const std::string
&) noexcept
;
116 // These casts are private to prevent people trying to use the Result object in numeric contexts
117 operator int() const;
118 operator void*() const;
123 #endif // WATER_RESULT_H_INCLUDED