Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/Result.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 JPH_NAMESPACE_BEGIN
8
9 /// Helper class that either contains a valid result or an error
10 template <class Type>
11 class Result
12 {
13 public:
14 /// Default constructor
15 Result() { }
16
17 /// Copy constructor
18 Result(const Result<Type> &inRHS) :
19 mState(inRHS.mState)
20 {
21 switch (inRHS.mState)
22 {
23 case EState::Valid:
24 new (&mResult) Type (inRHS.mResult);
25 break;
26
27 case EState::Error:
28 new (&mError) String(inRHS.mError);
29 break;
30
31 case EState::Invalid:
32 break;
33 }
34 }
35
36 /// Move constructor
37 Result(Result<Type> &&inRHS) noexcept :
38 mState(inRHS.mState)
39 {
40 switch (inRHS.mState)
41 {
42 case EState::Valid:
43 new (&mResult) Type (std::move(inRHS.mResult));
44 break;
45
46 case EState::Error:
47 new (&mError) String(std::move(inRHS.mError));
48 break;
49
50 case EState::Invalid:
51 break;
52 }
53
54 // Don't reset the state of inRHS, the destructors still need to be called after a move operation
55 }
56
57 /// Destructor
58 ~Result() { Clear(); }
59
60 /// Copy assignment
61 Result<Type> & operator = (const Result<Type> &inRHS)
62 {
63 Clear();
64
65 mState = inRHS.mState;
66
67 switch (inRHS.mState)
68 {
69 case EState::Valid:
70 new (&mResult) Type (inRHS.mResult);
71 break;
72
73 case EState::Error:
74 new (&mError) String(inRHS.mError);
75 break;
76
77 case EState::Invalid:
78 break;
79 }
80
81 return *this;
82 }
83
84 /// Move assignment
85 Result<Type> & operator = (Result<Type> &&inRHS) noexcept
86 {
87 Clear();
88
89 mState = inRHS.mState;
90
91 switch (inRHS.mState)
92 {
93 case EState::Valid:
94 new (&mResult) Type (std::move(inRHS.mResult));
95 break;
96
97 case EState::Error:
98 new (&mError) String(std::move(inRHS.mError));
99 break;
100
101 case EState::Invalid:
102 break;
103 }
104
105 // Don't reset the state of inRHS, the destructors still need to be called after a move operation
106
107 return *this;
108 }
109
110 /// Clear result or error
111 void Clear()
112 {
113 switch (mState)
114 {
115 case EState::Valid:
116 mResult.~Type();
117 break;
118
119 case EState::Error:
120 mError.~String();
121 break;
122
123 case EState::Invalid:
124 break;
125 }
126
127 mState = EState::Invalid;
128 }
129
130 /// Checks if the result is still uninitialized
131 bool IsEmpty() const { return mState == EState::Invalid; }
132
133 /// Checks if the result is valid
134 bool IsValid() const { return mState == EState::Valid; }
135
136 /// Get the result value
137 const Type & Get() const { JPH_ASSERT(IsValid()); return mResult; }
138
139 /// Set the result value
140 void Set(const Type &inResult) { Clear(); new (&mResult) Type(inResult); mState = EState::Valid; }
141
142 /// Set the result value (move value)
143 void Set(Type &&inResult) { Clear(); new (&mResult) Type(std::move(inResult)); mState = EState::Valid; }
144
145 /// Check if we had an error
146 bool HasError() const { return mState == EState::Error; }
147
148 /// Get the error value
149 const String & GetError() const { JPH_ASSERT(HasError()); return mError; }
150
151 /// Set an error value
152 void SetError(const char *inError) { Clear(); new (&mError) String(inError); mState = EState::Error; }
153 void SetError(const string_view &inError) { Clear(); new (&mError) String(inError); mState = EState::Error; }
154 void SetError(String &&inError) { Clear(); new (&mError) String(std::move(inError)); mState = EState::Error; }
155
156 private:
157 union
158 {
159 Type mResult; ///< The actual result object
160 String mError; ///< The error description if the result failed
161 };
162
163 /// State of the result
164 enum class EState : uint8
165 {
166 Invalid,
167 Valid,
168 Error
169 };
170
171 EState mState = EState::Invalid;
172 };
173
174 JPH_NAMESPACE_END