yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
macro.h
Go to the documentation of this file.
1#ifndef YAZE_UTIL_MACRO_H
2#define YAZE_UTIL_MACRO_H
3
4using uint = unsigned int;
5
6#define TAB_ITEM(w) if (ImGui::BeginTabItem(w)) {
7#define END_TAB_ITEM() \
8 ImGui::EndTabItem(); \
9 }
10
11#define BEGIN_TABLE(l, n, f) if (ImGui::BeginTable(l, n, f, ImVec2(0, 0))) {
12#define SETUP_COLUMN(l) ImGui::TableSetupColumn(l);
13
14#define TABLE_HEADERS() \
15 ImGui::TableHeadersRow(); \
16 ImGui::TableNextRow();
17
18#define NEXT_COLUMN() ImGui::TableNextColumn();
19
20#define END_TABLE() \
21 ImGui::EndTable(); \
22 }
23
24#define HOVER_HINT(string) \
25 if (ImGui::IsItemHovered()) \
26 ImGui::SetTooltip(string)
27
28#define PRINT_IF_ERROR(expression) \
29 { \
30 auto error = expression; \
31 if (!error.ok()) { \
32 std::cout << error.ToString() << std::endl; \
33 } \
34 }
35
36#define EXIT_IF_ERROR(expression) \
37 { \
38 auto error = expression; \
39 if (!error.ok()) { \
40 std::cout << error.ToString() << std::endl; \
41 return EXIT_FAILURE; \
42 } \
43 }
44
45#define RETURN_VOID_IF_ERROR(expression) \
46 { \
47 auto error = expression; \
48 if (!error.ok()) { \
49 std::cout << error.ToString() << std::endl; \
50 return; \
51 } \
52 }
53
54#define RETURN_IF_ERROR(expression) \
55 { \
56 auto error = expression; \
57 if (!error.ok()) { \
58 return error; \
59 } \
60 }
61
62#define ASSIGN_OR_RETURN(type_variable_name, expression) \
63 ASSIGN_OR_RETURN_IMPL(APPEND_NUMBER(error_or_value, __LINE__), \
64 type_variable_name, expression)
65
66#define ASSIGN_OR_RETURN_IMPL(error_or_value, type_variable_name, expression) \
67 auto error_or_value = expression; \
68 if (!error_or_value.ok()) { \
69 return error_or_value.status(); \
70 } \
71 type_variable_name = std::move(*error_or_value)
72
73#define ASSIGN_OR_LOG_ERROR(type_variable_name, expression) \
74 ASSIGN_OR_LOG_ERROR_IMPL(APPEND_NUMBER(error_or_value, __LINE__), \
75 type_variable_name, expression)
76
77#define ASSIGN_OR_LOG_ERROR_IMPL(error_or_value, type_variable_name, \
78 expression) \
79 auto error_or_value = expression; \
80 if (!error_or_value.ok()) { \
81 std::cout << error_or_value.status().ToString() << std::endl; \
82 } \
83 type_variable_name = std::move(*error_or_value);
84
85#define APPEND_NUMBER(expression, number) \
86 APPEND_NUMBER_INNER(expression, number)
87
88#define APPEND_NUMBER_INNER(expression, number) expression##number
89
90#define TEXT_WITH_SEPARATOR(text) \
91 ImGui::Text(text); \
92 ImGui::Separator();
93
94#define TABLE_BORDERS_RESIZABLE \
95 ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable
96
97#define CLEAR_AND_RETURN_STATUS(status) \
98 if (!status.ok()) { \
99 auto temp = status; \
100 status = absl::OkStatus(); \
101 return temp; \
102 }
103
104#define RETURN_IF_EXCEPTION(expression) \
105 try { \
106 expression; \
107 } catch (const std::exception& e) { \
108 std::cerr << e.what() << std::endl; \
109 return EXIT_FAILURE; \
110 }
111
112#define SDL_RETURN_IF_ERROR() \
113 if (SDL_GetError() != nullptr) { \
114 return absl::InternalError(SDL_GetError()); \
115 }
116
117// ===========================================================================
118// ROM Bounds Checking Macros
119// ===========================================================================
120// These macros provide consistent bounds checking for ROM operations,
121// ensuring all ROM reads and writes are validated against the ROM size.
122
132#define RETURN_IF_ROM_OUT_OF_RANGE(rom, offset, size) \
133 do { \
134 if (((offset) + (size)) > (rom)->size()) { \
135 return absl::OutOfRangeError( \
136 absl::StrFormat("ROM access out of range: offset 0x%X + size " \
137 "0x%X exceeds ROM size 0x%X", \
138 (offset), (size), (rom)->size())); \
139 } \
140 } while (0)
141
151#define RETURN_FALSE_IF_ROM_OUT_OF_RANGE(rom, offset, size) \
152 do { \
153 if (((offset) + (size)) > (rom)->size()) { \
154 return false; \
155 } \
156 } while (0)
157
165#define RETURN_IF_ROOM_OUT_OF_RANGE(room_id) \
166 do { \
167 constexpr int kMaxRoomId = 296; \
168 if ((room_id) < 0 || (room_id) >= kMaxRoomId) { \
169 return absl::OutOfRangeError(absl::StrFormat( \
170 "Room ID %d out of range [0, %d)", (room_id), kMaxRoomId)); \
171 } \
172 } while (0)
173
181#define RETURN_IF_MAP_OUT_OF_RANGE(map_id) \
182 do { \
183 constexpr int kMaxMapId = 160; \
184 if ((map_id) < 0 || (map_id) >= kMaxMapId) { \
185 return absl::OutOfRangeError(absl::StrFormat( \
186 "Map ID %d out of range [0, %d)", (map_id), kMaxMapId)); \
187 } \
188 } while (0)
189
198#define RETURN_IF_PALETTE_OUT_OF_RANGE(palette_id, max_palettes) \
199 do { \
200 if ((palette_id) < 0 || (palette_id) >= (max_palettes)) { \
201 return absl::OutOfRangeError(absl::StrFormat( \
202 "Palette ID %d out of range [0, %d)", \
203 (palette_id), (max_palettes))); \
204 } \
205 } while (0)
206
207#endif // YAZE_UTIL_MACRO_H
unsigned int uint
Definition macro.h:4