MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_result_wrapper.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_RESULT_WRAPPER_H_1611140530
12#define INCGUARD_MGL_RESULT_WRAPPER_H_1611140530
13
14#include <type_traits>
15
16namespace MGL
17{
18/* ------------------------------------------------------------------------- */
25/* ------------------------------------------------------------------------- */
26template <typename ErrorType, ErrorType noErrorValue = ErrorType(), ErrorType defaultErrorValue = noErrorValue>
28{
29public:
30 static constexpr ErrorType kNoError = noErrorValue;
31 static constexpr ErrorType kDefaultError = defaultErrorValue;
32
33 /* ------------------------------------------------------------------------- */
38 /* ------------------------------------------------------------------------- */
39 constexpr ResultWrapper(ErrorType error) noexcept
40 : _error(error)
41 {}
42
43 /* ------------------------------------------------------------------------- */
47 /* ------------------------------------------------------------------------- */
48 constexpr ResultWrapper() noexcept
49 : _error(defaultErrorValue)
50 {}
51
52 /* ------------------------------------------------------------------------- */
57 /* ------------------------------------------------------------------------- */
58 [[nodiscard]] constexpr ErrorType GetError() const noexcept
59 {
60 return _error;
61 }
62
63 /* ------------------------------------------------------------------------- */
68 /* ------------------------------------------------------------------------- */
69 [[nodiscard]] constexpr std::underlying_type_t<ErrorType> GetErrorCode() const noexcept
70 {
71 return static_cast<std::underlying_type_t<ErrorType>>(_error);
72 }
73
74 /* ------------------------------------------------------------------------- */
79 /* ------------------------------------------------------------------------- */
80 constexpr void SetError(ErrorType error) noexcept
81 {
82 _error = error;
83 }
84
85 /* ------------------------------------------------------------------------- */
89 /* ------------------------------------------------------------------------- */
90 constexpr void Success() noexcept
91 {
92 _error = noErrorValue;
93 }
94
95 /* ------------------------------------------------------------------------- */
100 /* ------------------------------------------------------------------------- */
101 static constexpr ResultWrapper Succeeded() noexcept
102 {
103 return ResultWrapper(noErrorValue);
104 }
105
106 /* ------------------------------------------------------------------------- */
111 /* ------------------------------------------------------------------------- */
112 constexpr ResultWrapper &operator=(const ErrorType &rhs) noexcept
113 {
114 _error = rhs;
115 return *this;
116 }
117
118 /* ------------------------------------------------------------------------- */
125 /* ------------------------------------------------------------------------- */
126 constexpr bool operator==(const ErrorType &rhs) const noexcept
127 {
128 return _error == rhs;
129 }
130
131 /* ------------------------------------------------------------------------- */
138 /* ------------------------------------------------------------------------- */
139 constexpr bool operator!=(const ErrorType &rhs) const noexcept
140 {
141 return _error != rhs;
142 }
143
144 /* ------------------------------------------------------------------------- */
150 /* ------------------------------------------------------------------------- */
151 [[nodiscard]] constexpr bool HasError() const noexcept
152 {
153 return _error != noErrorValue;
154 }
155
156 /* ------------------------------------------------------------------------- */
163 /* ------------------------------------------------------------------------- */
164 [[nodiscard]] constexpr bool IsSucceeded() const noexcept
165 {
166 return !HasError();
167 }
168
169private:
170 ErrorType _error;
171};
172} // namespace MGL
173
174#endif // INCGUARD_MGL_RESULT_WRAPPER_H_1611140530
175
176// vim: et ts=4 sw=4 sts=4
エラーをラップするテンプレートクラス
Definition mgl_result_wrapper.h:28
constexpr void Success() noexcept
エラーを無効化
Definition mgl_result_wrapper.h:90
constexpr bool operator==(const ErrorType &rhs) const noexcept
エラーの等価演算子
Definition mgl_result_wrapper.h:126
constexpr ResultWrapper() noexcept
コンストラクタ
Definition mgl_result_wrapper.h:48
constexpr bool HasError() const noexcept
エラーが発生しているかを取得
Definition mgl_result_wrapper.h:151
constexpr ResultWrapper & operator=(const ErrorType &rhs) noexcept
エラーの代入演算子
Definition mgl_result_wrapper.h:112
static constexpr ErrorType kNoError
エラーが発生していない事を表す値
Definition mgl_result_wrapper.h:30
constexpr bool operator!=(const ErrorType &rhs) const noexcept
エラーの不等価演算子
Definition mgl_result_wrapper.h:139
constexpr std::underlying_type_t< ErrorType > GetErrorCode() const noexcept
エラーコードを取得
Definition mgl_result_wrapper.h:69
constexpr ResultWrapper(ErrorType error) noexcept
エラー設定用コンストラクタ
Definition mgl_result_wrapper.h:39
static constexpr ErrorType kDefaultError
エラーの初期値
Definition mgl_result_wrapper.h:31
constexpr ErrorType GetError() const noexcept
エラーを取得
Definition mgl_result_wrapper.h:58
constexpr void SetError(ErrorType error) noexcept
エラーを設定
Definition mgl_result_wrapper.h:80
static constexpr ResultWrapper Succeeded() noexcept
エラーのないオブジェクトを取得
Definition mgl_result_wrapper.h:101
constexpr bool IsSucceeded() const noexcept
エラーが発生していないかを取得
Definition mgl_result_wrapper.h:164