@@ -28,70 +28,55 @@ enum class ErrorKind
2828 RendererError,
2929};
3030
31- template <typename T>
31+ template <typename T>
3232class [[nodiscard]] Result
3333{
34- public:
34+ public:
3535 // Factories
36- static Result ok (const T& v)
37- {
38- return Result (v);
39- }
40- static Result ok (T&& v)
41- {
42- return Result (std::move (v));
43- }
44- static Result error (ErrorKind e) noexcept
45- {
46- return Result (e);
47- }
36+ static Result ok (const T& v) { return Result (v); }
37+ static Result ok (T&& v) { return Result (std::move (v)); }
38+ static Result error (ErrorKind e) noexcept { return Result (e); }
4839
4940 // Constructors
50- Result (const T& v) noexcept (std::is_nothrow_copy_constructible<T>::value) : success_ (true )
41+ Result (const T& v) noexcept (std::is_nothrow_copy_constructible<T>::value)
42+ : success_ (true )
5143 {
5244 ::new (static_cast <void *>(std::addressof (value_))) T (v);
5345 }
5446
55- Result (T&& v) noexcept (std::is_nothrow_move_constructible<T>::value) : success_ (true )
47+ Result (T&& v) noexcept (std::is_nothrow_move_constructible<T>::value)
48+ : success_ (true )
5649 {
5750 ::new (static_cast <void *>(std::addressof (value_))) T (std::move (v));
5851 }
5952
60- Result (ErrorKind e) noexcept : success_ (false )
53+ Result (ErrorKind e) noexcept
54+ : success_ (false )
6155 {
6256 error_ = e;
6357 }
6458
6559 // Destructor
66- ~Result ()
67- {
68- reset ();
69- }
60+ ~Result () { reset (); }
7061
7162 // Copy ctor
7263 Result (const Result& other) noexcept (std::is_nothrow_copy_constructible<T>::value)
73- : success_ (other.success_ )
64+ : success_ (other.success_ )
7465 {
75- if (success_)
76- {
66+ if (success_) {
7767 ::new (static_cast <void *>(std::addressof (value_))) T (other.value_ );
78- }
79- else
80- {
68+ } else {
8169 error_ = other.error_ ;
8270 }
8371 }
8472
8573 // Move ctor
8674 Result (Result&& other) noexcept (std::is_nothrow_move_constructible<T>::value)
87- : success_ (other.success_ )
75+ : success_ (other.success_ )
8876 {
89- if (success_)
90- {
77+ if (success_) {
9178 ::new (static_cast <void *>(std::addressof (value_))) T (std::move (other.value_ ));
92- }
93- else
94- {
79+ } else {
9580 error_ = other.error_ ;
9681 }
9782 }
@@ -103,23 +88,16 @@ class [[nodiscard]] Result
10388 if (this == &other)
10489 return *this ;
10590
106- if (success_ && other.success_ )
107- {
91+ if (success_ && other.success_ ) {
10892 value_ = other.value_ ;
109- }
110- else if (success_ && !other.success_ )
111- {
93+ } else if (success_ && !other.success_ ) {
11294 value_.~T ();
11395 success_ = false ;
11496 error_ = other.error_ ;
115- }
116- else if (!success_ && other.success_ )
117- {
97+ } else if (!success_ && other.success_ ) {
11898 ::new (static_cast <void *>(std::addressof (value_))) T (other.value_ );
11999 success_ = true ;
120- }
121- else
122- { // both errors
100+ } else { // both errors
123101 error_ = other.error_ ;
124102 }
125103 return *this ;
@@ -132,75 +110,44 @@ class [[nodiscard]] Result
132110 if (this == &other)
133111 return *this ;
134112
135- if (success_ && other.success_ )
136- {
113+ if (success_ && other.success_ ) {
137114 value_ = std::move (other.value_ );
138- }
139- else if (success_ && !other.success_ )
140- {
115+ } else if (success_ && !other.success_ ) {
141116 value_.~T ();
142117 success_ = false ;
143118 error_ = other.error_ ;
144- }
145- else if (!success_ && other.success_ )
146- {
119+ } else if (!success_ && other.success_ ) {
147120 ::new (static_cast <void *>(std::addressof (value_))) T (std::move (other.value_ ));
148121 success_ = true ;
149- }
150- else
151- { // both errors
122+ } else { // both errors
152123 error_ = other.error_ ;
153124 }
154125 return *this ;
155126 }
156127
157128 // Observers
158- explicit operator bool () const noexcept
159- {
160- return success_;
161- }
129+ explicit operator bool () const noexcept { return success_; }
162130
163- T& operator *() &
164- {
165- return value_;
166- }
167- const T& operator *() const &
168- {
169- return value_;
170- }
171- T&& operator *() &&
172- {
173- return std::move (value_);
174- }
131+ T& operator *() & { return value_; }
132+ const T& operator *() const & { return value_; }
133+ T&& operator *() && { return std::move (value_); }
175134
176- T* operator ->()
177- {
178- return std::addressof (value_);
179- }
180- const T* operator ->() const
181- {
182- return std::addressof (value_);
183- }
135+ T* operator ->() { return std::addressof (value_); }
136+ const T* operator ->() const { return std::addressof (value_); }
184137
185- bool has_value () const noexcept
186- {
187- return success_;
188- }
138+ bool has_value () const noexcept { return success_; }
189139
190140 // If in error, returns default_value
191- template <typename U>
141+ template <typename U>
192142 T value_or (U&& default_value) const
193143 {
194144 return success_ ? value_ : static_cast <T>(std::forward<U>(default_value));
195145 }
196146
197147 // Returns ErrorKind::Undefined when holding a value
198- ErrorKind error () const noexcept
199- {
200- return success_ ? ErrorKind::Undefined : error_;
201- }
148+ ErrorKind error () const noexcept { return success_ ? ErrorKind::Undefined : error_; }
202149
203- private:
150+ private:
204151 // Active member is tracked by success_
205152 union
206153 {
@@ -211,46 +158,38 @@ class [[nodiscard]] Result
211158
212159 void reset () noexcept
213160 {
214- if (success_)
215- {
161+ if (success_) {
216162 value_.~T ();
217163 }
218164 }
219165};
220166
221167// Specialization for void
222- template <>
168+ template <>
223169class [[nodiscard]] Result<void >
224170{
225- public:
226- static Result ok () noexcept
171+ public:
172+ static Result ok () noexcept { return Result (true , ErrorKind::Undefined); }
173+ static Result error (ErrorKind e) noexcept { return Result (false , e); }
174+ Result (ErrorKind e) noexcept
175+ : success_ (false )
176+ , error_ (e)
227177 {
228- return Result (true , ErrorKind::Undefined);
229178 }
230- static Result error (ErrorKind e) noexcept
231- {
232- return Result (false , e);
233- }
234- Result (ErrorKind e) noexcept : success_ (false ), error_ (e) {}
235179
236- explicit operator bool () const noexcept
237- {
238- return success_;
239- }
240- bool has_value () const noexcept
241- {
242- return success_;
243- }
180+ explicit operator bool () const noexcept { return success_; }
181+ bool has_value () const noexcept { return success_; }
244182
245183 // Returns ErrorKind::Undefined when success
246- ErrorKind error () const noexcept
247- {
248- return success_ ? ErrorKind::Undefined : error_;
249- }
184+ ErrorKind error () const noexcept { return success_ ? ErrorKind::Undefined : error_; }
250185
251- private:
186+ private:
252187 bool success_;
253188 ErrorKind error_;
254189
255- explicit Result (bool s, ErrorKind e) noexcept : success_ (s), error_ (e) {}
190+ explicit Result (bool s, ErrorKind e) noexcept
191+ : success_ (s)
192+ , error_ (e)
193+ {
194+ }
256195};
0 commit comments