mitteLib
Loading...
Searching...
No Matches
result_macro.hpp
Go to the documentation of this file.
1//
2// Created by spak on 7/10/23.
3//
4
5#ifndef MLAB_RESULT_MACRO_HPP
6#define MLAB_RESULT_MACRO_HPP
7
8#include <mlab/log.hpp>
9
10#ifndef MLAB_NO_RESULT_MACROS
11
12#ifndef MLAB_RESULT_LOG_PREFIX
13#define MLAB_RESULT_LOG_PREFIX "MLAB"
14#endif
15
16#define MLAB_FAIL_MSG(CMD_STR, RESULT) \
17 LOGW(MLAB_RESULT_LOG_PREFIX, "%s:%d failed %s with %s", \
18 __FILE__, __LINE__, CMD_STR, to_string((RESULT).error()));
19
20#define MLAB_FAIL_CMD(CMD_STR, RESULT) \
21 MLAB_FAIL_MSG(CMD_STR, RESULT) \
22 return (RESULT).error();
23
24#define MLAB_FAIL_CAST_CMD(CMD_STR, RESULT) \
25 MLAB_FAIL_MSG(CMD_STR, RESULT) \
26 return cast_error((RESULT).error());
27
28#define MLAB_CMD_WITH_NAMED_RESULT(CMD, RESULT_NAME) \
29 if (auto RESULT_NAME = (CMD); not RESULT_NAME) { \
30 MLAB_FAIL_CMD(#CMD, RESULT_NAME) \
31 }
32
33#define MLAB_CAST_CMD_WITH_NAMED_RESULT(CMD, RESULT_NAME) \
34 if (auto RESULT_NAME = (CMD); not RESULT_NAME) { \
35 MLAB_FAIL_CAST_CMD(#CMD, RESULT_NAME) \
36 }
37
38#define MLAB_CMD_WITH_NAMED_RESULT_SILENT(CMD, RESULT_NAME) \
39 if (auto RESULT_NAME = (CMD); not RESULT_NAME) { \
40 return RESULT_NAME.error(); \
41 }
42
43#define TRY(CMD) MLAB_CMD_WITH_NAMED_RESULT(CMD, _r)
44
45#define TRY_SILENT(CMD) MLAB_CMD_WITH_NAMED_RESULT_SILENT(CMD, _r)
46
47#define TRY_RESULT(CMD) \
48 MLAB_CMD_WITH_NAMED_RESULT(CMD, r) \
49 else
50
51#define TRY_RESULT_SILENT(CMD) \
52 MLAB_CMD_WITH_NAMED_RESULT_SILENT(CMD, r) \
53 else
54
55#define TRY_RESULT_AS(CMD, RES_VAR) \
56 MLAB_CMD_WITH_NAMED_RESULT(CMD, RES_VAR) \
57 else
58
59#define TRY_RESULT_AS_SILENT(CMD, RES_VAR) \
60 MLAB_CMD_WITH_NAMED_RESULT_SILENT(CMD, RES_VAR) \
61 else
62
63#define TRY_CAST(CMD) MLAB_CAST_CMD_WITH_NAMED_RESULT(CMD, _r)
64
65#define TRY_CAST_SILENT(CMD) MLAB_CAST_CMD_WITH_NAMED_RESULT_SILENT(CMD, _r)
66
67#define TRY_CAST_RESULT(CMD) \
68 MLAB_CAST_CMD_WITH_NAMED_RESULT(CMD, r) \
69 else
70
71#define TRY_CAST_RESULT_SILENT(CMD) \
72 MLAB_CAST_CMD_WITH_NAMED_RESULT_SILENT(CMD, r) \
73 else
74
75#define TRY_CAST_RESULT_AS(CMD, RES_VAR) \
76 MLAB_CAST_CMD_WITH_NAMED_RESULT(CMD, RES_VAR) \
77 else
78
79#define TRY_CAST_RESULT_AS_SILENT(CMD, RES_VAR) \
80 MLAB_CAST_CMD_WITH_NAMED_RESULT_SILENT(CMD, RES_VAR) \
81 else
82
83#endif
84
85#endif//MLAB_RESULT_MACRO_HPP