Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Result.h
Go to the documentation of this file.
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
8
9// GCC doesn't properly detect that mState is used to ensure that mResult is initialized
10JPH_GCC_SUPPRESS_WARNING("-Wmaybe-uninitialized")
11
12
13template <class Type>
14class Result
15{
16public:
18 Result() { }
19
21 Result(const Result<Type> &inRHS) :
22 mState(inRHS.mState)
23 {
24 switch (inRHS.mState)
25 {
26 case EState::Valid:
27 ::new (&mResult) Type (inRHS.mResult);
28 break;
29
30 case EState::Error:
31 ::new (&mError) String(inRHS.mError);
32 break;
33
34 case EState::Invalid:
35 break;
36 }
37 }
38
40 Result(Result<Type> &&inRHS) noexcept :
41 mState(inRHS.mState)
42 {
43 switch (inRHS.mState)
44 {
45 case EState::Valid:
46 ::new (&mResult) Type (std::move(inRHS.mResult));
47 break;
48
49 case EState::Error:
50 ::new (&mError) String(std::move(inRHS.mError));
51 break;
52
53 case EState::Invalid:
54 break;
55 }
56
57 // Don't reset the state of inRHS, the destructors still need to be called after a move operation
58 }
59
61 ~Result() { Clear(); }
62
64 Result<Type> & operator = (const Result<Type> &inRHS)
65 {
66 Clear();
67
68 mState = inRHS.mState;
69
70 switch (inRHS.mState)
71 {
72 case EState::Valid:
73 ::new (&mResult) Type (inRHS.mResult);
74 break;
75
76 case EState::Error:
77 ::new (&mError) String(inRHS.mError);
78 break;
79
80 case EState::Invalid:
81 break;
82 }
83
84 return *this;
85 }
86
88 Result<Type> & operator = (Result<Type> &&inRHS) noexcept
89 {
90 Clear();
91
92 mState = inRHS.mState;
93
94 switch (inRHS.mState)
95 {
96 case EState::Valid:
97 ::new (&mResult) Type (std::move(inRHS.mResult));
98 break;
99
100 case EState::Error:
101 ::new (&mError) String(std::move(inRHS.mError));
102 break;
103
104 case EState::Invalid:
105 break;
106 }
107
108 // Don't reset the state of inRHS, the destructors still need to be called after a move operation
109
110 return *this;
111 }
112
114 void Clear()
115 {
116 switch (mState)
117 {
118 case EState::Valid:
119 mResult.~Type();
120 break;
121
122 case EState::Error:
123 mError.~String();
124 break;
125
126 case EState::Invalid:
127 break;
128 }
129
130 mState = EState::Invalid;
131 }
132
134 bool IsEmpty() const { return mState == EState::Invalid; }
135
137 bool IsValid() const { return mState == EState::Valid; }
138
140 const Type & Get() const { JPH_ASSERT(IsValid()); return mResult; }
141
143 void Set(const Type &inResult) { Clear(); ::new (&mResult) Type(inResult); mState = EState::Valid; }
144
146 void Set(Type &&inResult) { Clear(); ::new (&mResult) Type(std::move(inResult)); mState = EState::Valid; }
147
149 bool HasError() const { return mState == EState::Error; }
150
152 const String & GetError() const { JPH_ASSERT(HasError()); return mError; }
153
155 void SetError(const char *inError) { Clear(); ::new (&mError) String(inError); mState = EState::Error; }
156 void SetError(const string_view &inError) { Clear(); ::new (&mError) String(inError); mState = EState::Error; }
157 void SetError(String &&inError) { Clear(); ::new (&mError) String(std::move(inError)); mState = EState::Error; }
158
159private:
160 union
161 {
162 Type mResult;
164 };
165
167 enum class EState : uint8
168 {
169 Invalid,
170 Valid,
171 Error
172 };
173
174 EState mState = EState::Invalid;
175};
176
std::uint8_t uint8
Definition: Core.h:440
#define JPH_GCC_SUPPRESS_WARNING(w)
Definition: Core.h:264
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
std::basic_string< char, std::char_traits< char >, STLAllocator< char > > String
Definition: STLAllocator.h:82
@ Invalid
Next token on the stream was not a valid data type.
Helper class that either contains a valid result or an error.
Definition: Result.h:15
bool IsValid() const
Checks if the result is valid.
Definition: Result.h:137
void SetError(String &&inError)
Definition: Result.h:157
Result(const Result< Type > &inRHS)
Copy constructor.
Definition: Result.h:21
void Clear()
Clear result or error.
Definition: Result.h:114
void SetError(const char *inError)
Set an error value.
Definition: Result.h:155
Type mResult
The actual result object.
Definition: Result.h:162
void Set(const Type &inResult)
Set the result value.
Definition: Result.h:143
bool HasError() const
Check if we had an error.
Definition: Result.h:149
void Set(Type &&inResult)
Set the result value (move value)
Definition: Result.h:146
const String & GetError() const
Get the error value.
Definition: Result.h:152
Result()
Default constructor.
Definition: Result.h:18
bool IsEmpty() const
Checks if the result is still uninitialized.
Definition: Result.h:134
const Type & Get() const
Get the result value.
Definition: Result.h:140
~Result()
Destructor.
Definition: Result.h:61
void SetError(const string_view &inError)
Definition: Result.h:156
Result(Result< Type > &&inRHS) noexcept
Move constructor.
Definition: Result.h:40
String mError
The error description if the result failed.
Definition: Result.h:163