Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jerrrrry
infinicore
Commits
e5bda616
Unverified
Commit
e5bda616
authored
Apr 02, 2025
by
PanZezhong1725
Committed by
GitHub
Apr 02, 2025
Browse files
Merge pull request #122 from YdrMaster/main
issue/121/feat: 添加 Result 类型
parents
beaf1e8c
fd5d90c9
Changes
21
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
0 deletions
+59
-0
src/utils/result.hpp
src/utils/result.hpp
+59
-0
No files found.
src/utils/result.hpp
0 → 100644
View file @
e5bda616
#ifndef __INFINIUTILS_RESULT_H__
#define __INFINIUTILS_RESULT_H__
#include "check.h"
#include <infinicore.h>
#include <variant>
#define CHECK_RESULT(RESULT) \
if (!RESULT) { \
return RESULT.status(); \
}
namespace
utils
{
template
<
typename
T
,
typename
=
std
::
enable_if_t
<!
std
::
is_same_v
<
T
,
infiniStatus_t
>
>>
class
Result
{
std
::
variant
<
infiniStatus_t
,
T
>
_result
;
public:
explicit
Result
(
T
value
)
:
_result
(
std
::
move
(
value
))
{}
Result
(
infiniStatus_t
status
)
:
_result
(
status
)
{
if
(
status
==
INFINI_STATUS_SUCCESS
)
{
std
::
cerr
<<
"Warning: Result created with success status but value is not set."
<<
std
::
endl
;
std
::
abort
();
}
}
infiniStatus_t
status
()
const
{
return
_result
.
index
()
==
0
?
std
::
get
<
0
>
(
_result
)
:
INFINI_STATUS_SUCCESS
;
}
T
take
()
{
return
std
::
move
(
std
::
get
<
1
>
(
_result
));
}
operator
bool
()
const
{
return
status
()
==
INFINI_STATUS_SUCCESS
;
}
T
*
operator
->
()
{
return
&
std
::
get
<
1
>
(
_result
);
}
const
T
*
operator
->
()
const
{
return
&
std
::
get
<
1
>
(
_result
);
}
T
&
operator
*
()
{
return
std
::
get
<
1
>
(
_result
);
}
const
T
&
operator
*
()
const
{
return
std
::
get
<
1
>
(
_result
);
}
};
}
// namespace utils
#endif // __INFINIUTILS_RESULT_H__
Prev
1
2
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment