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
10template <class Type>
11class Result
12{
13public:
15 Result() { }
16
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
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
58 ~Result() { Clear(); }
59
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
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
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
131 bool IsEmpty() const { return mState == EState::Invalid; }
132
134 bool IsValid() const { return mState == EState::Valid; }
135
137 const Type & Get() const { JPH_ASSERT(IsValid()); return mResult; }
138
140 void Set(const Type &inResult) { Clear(); ::new (&mResult) Type(inResult); mState = EState::Valid; }
141
143 void Set(Type &&inResult) { Clear(); ::new (&mResult) Type(std::move(inResult)); mState = EState::Valid; }
144
146 bool HasError() const { return mState == EState::Error; }
147
149 const String & GetError() const { JPH_ASSERT(HasError()); return mError; }
150
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
156private:
157 union
158 {
159 Type mResult;
161 };
162
164 enum class EState : uint8
165 {
166 Invalid,
167 Valid,
168 Error
169 };
170
171 EState mState = EState::Invalid;
172};
173
std::uint8_t uint8
Definition: Core.h:453
#define JPH_NAMESPACE_END
Definition: Core.h:378
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
std::basic_string< char, std::char_traits< char >, STLAllocator< char > > String
Definition: STLAllocator.h:107
@ 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:12
bool IsValid() const
Checks if the result is valid.
Definition: Result.h:134
void SetError(String &&inError)
Definition: Result.h:154
Result(const Result< Type > &inRHS)
Copy constructor.
Definition: Result.h:18
void Clear()
Clear result or error.
Definition: Result.h:111
void SetError(const char *inError)
Set an error value.
Definition: Result.h:152
Type mResult
The actual result object.
Definition: Result.h:159
void Set(const Type &inResult)
Set the result value.
Definition: Result.h:140
bool HasError() const
Check if we had an error.
Definition: Result.h:146
void Set(Type &&inResult)
Set the result value (move value)
Definition: Result.h:143
const String & GetError() const
Get the error value.
Definition: Result.h:149
Result()
Default constructor.
Definition: Result.h:15
bool IsEmpty() const
Checks if the result is still uninitialized.
Definition: Result.h:131
const Type & Get() const
Get the result value.
Definition: Result.h:137
Result< Type > & operator=(const Result< Type > &inRHS)
Copy assignment.
Definition: Result.h:61
~Result()
Destructor.
Definition: Result.h:58
void SetError(const string_view &inError)
Definition: Result.h:153
Result(Result< Type > &&inRHS) noexcept
Move constructor.
Definition: Result.h:37
String mError
The error description if the result failed.
Definition: Result.h:160