Add initial bits for Qt6 support
[carla.git] / source / modules / water / misc / Result.h
blob7275427bf6eebf8dd944ec30e014ff1f6ca3da00
1 /*
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
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #ifndef WATER_RESULT_H_INCLUDED
27 #define WATER_RESULT_H_INCLUDED
29 #include "../water.h"
31 #include <string>
33 namespace water {
35 //==============================================================================
36 /**
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.
40 E.g.
41 @code
42 Result myOperation()
44 if (doSomeKindOfFoobar())
45 return Result::ok();
46 else
47 return Result::fail ("foobar didn't work!");
50 const Result result (myOperation());
52 if (result.wasOk())
54 ...it's all good...
56 else
58 warnUserAboutFailure ("The foobar operation failed! Error message was: "
59 + result.getErrorMessage());
61 @endcode
63 class Result
65 public:
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
72 will be used instead.
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
82 with the failure.
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;
108 private:
109 std::string errorMessage;
111 // The default constructor is not for public use!
112 // Instead, use Result::ok() or Result::fail()
113 Result() noexcept;
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