diff --git a/common/arch/sdl/event.cpp b/common/arch/sdl/event.cpp index 2c4155604..57e146fe3 100644 --- a/common/arch/sdl/event.cpp +++ b/common/arch/sdl/event.cpp @@ -103,7 +103,7 @@ window_event_result event_poll() // Send the idle event if there were no other events (or they were ignored) if (state.highest_result == window_event_result::ignored) { - const d_event ievent{EVENT_IDLE}; + const d_event ievent{event_type::idle}; state.highest_result = std::max(event_send(ievent), state.highest_result); } else @@ -170,8 +170,7 @@ void event_poll_state::process_event_batch(const ranges::subrangeis_visible()) diff --git a/common/arch/sdl/joy.cpp b/common/arch/sdl/joy.cpp index c4aa87355..233c70497 100644 --- a/common/arch/sdl/joy.cpp +++ b/common/arch/sdl/joy.cpp @@ -197,10 +197,10 @@ window_event_result joy_button_handler(const SDL_JoyButtonEvent *const jbe) Joystick.button_state[button] = jbe->state; const d_event_joystickbutton event{ - (jbe->type == SDL_JOYBUTTONDOWN) ? EVENT_JOYSTICK_BUTTON_DOWN : EVENT_JOYSTICK_BUTTON_UP, + (jbe->type == SDL_JOYBUTTONDOWN) ? event_type::joystick_button_down : event_type::joystick_button_up, button }; - con_printf(CON_DEBUG, "Sending event %s, button %d", (jbe->type == SDL_JOYBUTTONDOWN) ? "EVENT_JOYSTICK_BUTTON_DOWN" : "EVENT_JOYSTICK_JOYSTICK_UP", event.button); + con_printf(CON_DEBUG, "Sending event %s, button %d", (jbe->type == SDL_JOYBUTTONDOWN) ? "event_type::joystick_button_down" : "EVENT_JOYSTICK_JOYSTICK_UP", event.button); return event_send(event); } #endif @@ -236,14 +236,14 @@ window_event_result joy_hat_handler(const SDL_JoyHatEvent *const jhe) // Same state as before continue; saved_button_state = current_button_state; - const d_event_joystickbutton event{current_button_state ? EVENT_JOYSTICK_BUTTON_DOWN : EVENT_JOYSTICK_BUTTON_UP, hat + i}; + const d_event_joystickbutton event{current_button_state ? event_type::joystick_button_down : event_type::joystick_button_up, hat + i}; if (current_button_state) //last_state up, current state down { - con_printf(CON_DEBUG, "Sending event EVENT_JOYSTICK_BUTTON_DOWN, button %d", event.button); + con_printf(CON_DEBUG, "Sending event event_type::joystick_button_down, button %d", event.button); } else //last_state down, current state up { - con_printf(CON_DEBUG, "Sending event EVENT_JOYSTICK_BUTTON_UP, button %d", event.button); + con_printf(CON_DEBUG, "Sending event event_type::joystick_button_up, button %d", event.button); } highest_result = std::max(event_send(event), highest_result); } @@ -258,9 +258,9 @@ namespace { static window_event_result send_axis_button_event(unsigned button, event_type e) { - Joystick.button_state[button] = (e == EVENT_JOYSTICK_BUTTON_UP) ? 0 : 1; + Joystick.button_state[button] = (e == event_type::joystick_button_up) ? 0 : 1; const d_event_joystickbutton event{ e, button }; - con_printf(CON_DEBUG, "Sending event %s, button %d", (e == EVENT_JOYSTICK_BUTTON_UP) ? "EVENT_JOYSTICK_BUTTON_UP" : "EVENT_JOYSTICK_BUTTON_DOWN", event.button); + con_printf(CON_DEBUG, "Sending event %s, button %d", (e == event_type::joystick_button_up) ? "event_type::joystick_button_up" : "event_type::joystick_button_down", event.button); return event_send(event); } @@ -283,16 +283,16 @@ window_event_result joy_axisbutton_handler(const SDL_JoyAxisEvent *const jae) if (prev_value <= 0 && new_value >= 0) // positive pressed { if (prev_value < 0) // Do previous direction release first if the case - highest_result = std::max(send_axis_button_event(button + 1, EVENT_JOYSTICK_BUTTON_UP), highest_result); + highest_result = std::max(send_axis_button_event(button + 1, event_type::joystick_button_up), highest_result); if (new_value > 0) - highest_result = std::max(send_axis_button_event(button, EVENT_JOYSTICK_BUTTON_DOWN), highest_result); + highest_result = std::max(send_axis_button_event(button, event_type::joystick_button_down), highest_result); } else if (prev_value >= 0 && new_value <= 0) // negative pressed { if (prev_value > 0) // Do previous direction release first if the case - highest_result = std::max(send_axis_button_event(button, EVENT_JOYSTICK_BUTTON_UP), highest_result); + highest_result = std::max(send_axis_button_event(button, event_type::joystick_button_up), highest_result); if (new_value < 0) - highest_result = std::max(send_axis_button_event(button + 1, EVENT_JOYSTICK_BUTTON_DOWN), highest_result); + highest_result = std::max(send_axis_button_event(button + 1, event_type::joystick_button_down), highest_result); } return highest_result; @@ -308,10 +308,10 @@ window_event_result joy_axis_handler(const SDL_JoyAxisEvent *const jae) if (axis_value == jae->value/256) return window_event_result::ignored; - d_event_joystick_moved event{EVENT_JOYSTICK_MOVED}; + d_event_joystick_moved event{event_type::joystick_moved}; event.value = axis_value = jae->value/256; event.axis = axis; - con_printf(CON_DEBUG, "Sending event EVENT_JOYSTICK_MOVED, axis: %d, value: %d",event.axis, event.value); + con_printf(CON_DEBUG, "Sending event event_type::joystick_moved, axis: %d, value: %d",event.axis, event.value); return event_send(event); } @@ -455,7 +455,7 @@ void joy_close() const d_event_joystick_axis_value &event_joystick_get_axis(const d_event &event) { auto &e = static_cast(event); - Assert(e.type == EVENT_JOYSTICK_MOVED); + assert(e.type == event_type::joystick_moved); return e; } #endif @@ -479,7 +479,7 @@ void joy_flush() int event_joystick_get_button(const d_event &event) { auto &e = static_cast(event); - Assert(e.type == EVENT_JOYSTICK_BUTTON_DOWN || e.type == EVENT_JOYSTICK_BUTTON_UP); + assert(e.type == event_type::joystick_button_down || e.type == event_type::joystick_button_up); return e.button; } #endif @@ -496,7 +496,7 @@ int apply_deadzone(int value, int deadzone) #if DXX_MAX_BUTTONS_PER_JOYSTICK bool joy_translate_menu_key(const d_event &event) { - if (event.type != EVENT_JOYSTICK_BUTTON_DOWN) + if (event.type != event_type::joystick_button_down) return false; auto &e = static_cast(event); assert(e.button < joy_key_map.size()); diff --git a/common/arch/sdl/key.cpp b/common/arch/sdl/key.cpp index b78ac343f..6c4137cf1 100644 --- a/common/arch/sdl/key.cpp +++ b/common/arch/sdl/key.cpp @@ -535,9 +535,9 @@ window_event_result key_handler(const SDL_KeyboardEvent *const kevent) // We allowed the key to be added to the queue for now, // because there are still input loops without associated windows - const d_event_keycommand event{key_state ? EVENT_KEY_COMMAND : EVENT_KEY_RELEASE, keycode}; + const d_event_keycommand event{key_state ? event_type::key_command : event_type::key_release, keycode}; con_printf(CON_DEBUG, "Sending event %s: %s %s %s %s %s %s", - (key_state) ? "EVENT_KEY_COMMAND": "EVENT_KEY_RELEASE", + (key_state) ? "event_type::key_command": "event_type::key_release", (keycode & KEY_METAED) ? "META" : "", (keycode & KEY_DEBUGGED) ? "DEBUG" : "", (keycode & KEY_CTRLED) ? "CTRL" : "", @@ -597,14 +597,13 @@ void key_flush() } void event_keycommand_send(unsigned key) { - const d_event_keycommand event{EVENT_KEY_COMMAND, key}; - event_send(event); + event_send(d_event_keycommand{event_type::key_command, key}); } int event_key_get(const d_event &event) { auto &e = static_cast(event); - Assert(e.type == EVENT_KEY_COMMAND || e.type == EVENT_KEY_RELEASE); + assert(e.type == event_type::key_command || e.type == event_type::key_release); return e.keycode; } diff --git a/common/arch/sdl/mouse.cpp b/common/arch/sdl/mouse.cpp index 03d7b7a12..be115d67e 100644 --- a/common/arch/sdl/mouse.cpp +++ b/common/arch/sdl/mouse.cpp @@ -80,13 +80,12 @@ static window_event_result maybe_send_z_move(const mbtn button) } else return window_event_result::ignored; - const d_event_mouse_moved event{EVENT_MOUSE_MOVED, 0, 0, dz}; - return event_send(event); + return event_send(d_event_mouse_moved{event_type::mouse_moved, 0, 0, dz}); } static window_event_result send_singleclick(const bool pressed, const mbtn button) { - const d_event_mousebutton event{pressed ? EVENT_MOUSE_BUTTON_DOWN : EVENT_MOUSE_BUTTON_UP, button}; + const d_event_mousebutton event{pressed ? event_type::mouse_button_down : event_type::mouse_button_up, button}; con_printf(CON_DEBUG, "Sending event EVENT_MOUSE_BUTTON_%s, button %d, coords %d,%d,%d", pressed ? "DOWN" : "UP", underlying_value(event.button), Mouse.x, Mouse.y, Mouse.z); return event_send(event); @@ -99,8 +98,8 @@ static window_event_result maybe_send_doubleclick(const fix64 now, const mbtn bu when = now; if (now > then + F1_0/5) return window_event_result::ignored; - const d_event_mousebutton event{EVENT_MOUSE_DOUBLE_CLICKED, button}; - con_printf(CON_DEBUG, "Sending event EVENT_MOUSE_DOUBLE_CLICKED, button %d, coords %d,%d", underlying_value(button), Mouse.x, Mouse.y); + const d_event_mousebutton event{event_type::mouse_double_clicked, button}; + con_printf(CON_DEBUG, "Sending event event_type::mouse_double_clicked, button %d, coords %d,%d", underlying_value(button), Mouse.x, Mouse.y); return event_send(event); } @@ -163,9 +162,9 @@ window_event_result mouse_motion_handler(const SDL_MouseMotionEvent *const mme) Mouse.y += mme->yrel; // z handled in mouse_button_handler - const d_event_mouse_moved event{EVENT_MOUSE_MOVED, mme->xrel, mme->yrel, 0}; + const d_event_mouse_moved event{event_type::mouse_moved, mme->xrel, mme->yrel, 0}; - //con_printf(CON_DEBUG, "Sending event EVENT_MOUSE_MOVED, relative motion %d,%d,%d", + //con_printf(CON_DEBUG, "Sending event event_type::mouse_moved, relative motion %d,%d,%d", // event.dx, event.dy, event.dz); return event_send(event); } @@ -178,12 +177,14 @@ void mouse_flush() // clears all mice events... } //======================================================================== -void mouse_get_pos( int *x, int *y, int *z ) +std::tuple mouse_get_pos() { //event_poll(); // Have to assume this is called in event_process, because event_poll can cause a window to close (depending on what the user does) - *x=Mouse.x; - *y=Mouse.y; - *z=Mouse.z; + return { + Mouse.x, + Mouse.y, + Mouse.z, + }; } window_event_result mouse_in_window(window *wind) @@ -193,14 +194,22 @@ window_event_result mouse_in_window(window *wind) (static_cast(Mouse.y) - canv.cv_bitmap.bm_y <= canv.cv_bitmap.bm_h) ? window_event_result::handled : window_event_result::ignored; } -void mouse_get_delta( int *dx, int *dy, int *dz ) +#if 0 +std::tuple mouse_get_delta() { - *dz = Mouse.delta_z; + const int dz{Mouse.delta_z}; + int dx{}, dy{}; Mouse.delta_x = 0; Mouse.delta_y = 0; Mouse.delta_z = 0; - SDL_GetRelativeMouseState(dx, dy); + SDL_GetRelativeMouseState(&dx, &dy); + return { + dx, + dy, + dz, + }; } +#endif namespace { diff --git a/common/arch/sdl/window.cpp b/common/arch/sdl/window.cpp index 61908a579..ebe96a62c 100644 --- a/common/arch/sdl/window.cpp +++ b/common/arch/sdl/window.cpp @@ -20,6 +20,7 @@ #include "console.h" #include "dxxerror.h" #include "event.h" +#include "d_underlying_value.h" namespace dcx { @@ -43,9 +44,9 @@ void window::send_creation_events() FrontWindow->next = this; FrontWindow = this; if (prev_front) - prev_front->send_event(d_event{EVENT_WINDOW_DEACTIVATED}); + prev_front->send_event(d_event{event_type::window_deactivated}); this->send_event(d_create_event{}); - this->send_event(d_event{EVENT_WINDOW_ACTIVATED}); + this->send_event(d_event{event_type::window_activated}); } window::~window() @@ -69,14 +70,14 @@ mixin_trackable_window::~mixin_trackable_window() int window_close(window *wind) { if (wind == window_get_front()) - wind->send_event(d_event{EVENT_WINDOW_DEACTIVATED}); // Deactivate first + wind->send_event(d_event{event_type::window_deactivated}); // Deactivate first - const auto result = wind->send_event(d_event{EVENT_WINDOW_CLOSE}); + const auto result = wind->send_event(d_event{event_type::window_close}); if (result == window_event_result::handled) { // User 'handled' the event, cancelling close if (wind == window_get_front()) - wind->send_event(d_event{EVENT_WINDOW_ACTIVATED}); + wind->send_event(d_event{event_type::window_activated}); return 0; } @@ -86,7 +87,7 @@ int window_close(window *wind) delete wind; if (const auto prev = window_get_front()) - prev->send_event(d_event{EVENT_WINDOW_ACTIVATED}); + prev->send_event(d_event{event_type::window_activated}); return 1; } @@ -127,8 +128,8 @@ void window_select(window &wind) if (wind.is_visible()) { if (prev) - prev->send_event(d_event{EVENT_WINDOW_DEACTIVATED}); - wind.send_event(d_event{EVENT_WINDOW_ACTIVATED}); + prev->send_event(d_event{event_type::window_deactivated}); + wind.send_event(d_event{event_type::window_activated}); } } @@ -141,10 +142,10 @@ window *window::set_visible(uint8_t visible) return wind; if (prev) - prev->send_event(d_event{EVENT_WINDOW_DEACTIVATED}); + prev->send_event(d_event{event_type::window_deactivated}); if (wind) - wind->send_event(d_event{EVENT_WINDOW_ACTIVATED}); + wind->send_event(d_event{event_type::window_activated}); return wind; } @@ -155,7 +156,7 @@ window_event_result window::send_event(const d_event &event ) { #if DXX_HAVE_CXX_BUILTIN_FILE_LINE - con_printf(CON_DEBUG, "%s:%u: sending event %i to window of dimensions %dx%d", file, line, event.type, w_canv.cv_bitmap.bm_w, w_canv.cv_bitmap.bm_h); + con_printf(CON_DEBUG, "%s:%u: sending event %i to window of dimensions %dx%d", file, line, underlying_value(event.type), w_canv.cv_bitmap.bm_w, w_canv.cv_bitmap.bm_h); #endif const auto r = event_handler(event); if (r == window_event_result::close) diff --git a/common/arch/win32/messagebox.cpp b/common/arch/win32/messagebox.cpp index de231a2b5..d49d0000f 100644 --- a/common/arch/win32/messagebox.cpp +++ b/common/arch/win32/messagebox.cpp @@ -23,7 +23,7 @@ static void display_win32_alert(const char *message, int error) { // Handle Descent's windows properly if (const auto wind = window_get_front()) - wind->send_event(d_event{EVENT_WINDOW_DEACTIVATED}); + wind->send_event(d_event{event_type::window_deactivated}); int fullscreen = (grd_curscreen && gr_check_fullscreen()); if (fullscreen) @@ -32,8 +32,7 @@ static void display_win32_alert(const char *message, int error) MessageBox(NULL, message, error?"Sorry, a critical error has occurred.":"Attention!", error?MB_OK|MB_ICONERROR:MB_OK|MB_ICONWARNING); if (const auto wind = window_get_front()) - wind->send_event(d_event{EVENT_WINDOW_ACTIVATED}); - + wind->send_event(d_event{event_type::window_activated}); if (!error && fullscreen) gr_toggle_fullscreen(); } diff --git a/common/editor/func.cpp b/common/editor/func.cpp index cba19d442..a85967f9c 100644 --- a/common/editor/func.cpp +++ b/common/editor/func.cpp @@ -34,73 +34,11 @@ namespace dcx { #define MAX_PARAMS 10 -static const FUNCTION * func_table = NULL; -static int func_size = 0; -static int initialized = 0; static int func_params[MAX_PARAMS]; -void func_init( const FUNCTION * funtable, int size ) -{ - if (!initialized) - { - initialized = 1; - func_table = funtable; - func_size = size; - atexit( func_close ); - } -} - - -void func_close() -{ - if (initialized) - { - initialized = 0; - func_table = NULL; - func_size = 0; - } -} - -int (*func_get( char * name, int * numparams ))(void) -{ - for (int i=0; i namespace dcx { -typedef struct { + +struct FUNCTION +{ + using callable = int(*)(void); const char * name; int nparams; - int (*cfunction)(void); -} FUNCTION; + callable cfunction; +}; -void func_init( const FUNCTION * funtable, int size ); -void func_close(); -int (*func_get( char * name, int * numparams ))(void); -int (*func_nget( int func_number, int * numparams, const char **name ))(void); int func_get_param( int n ); -int func_get_index( char * name ); -} +FUNCTION::callable func_get(const char *name, int *numparams); +FUNCTION::callable func_nget(std::size_t func_number, int *numparams, const char **name); +int func_get_index(const char *name); -#endif +} diff --git a/common/include/fwd-event.h b/common/include/fwd-event.h index a99877548..118fc0653 100644 --- a/common/include/fwd-event.h +++ b/common/include/fwd-event.h @@ -16,7 +16,7 @@ struct d_event; struct d_change_event; struct d_select_event; -enum event_type : unsigned; +enum class event_type : uint8_t; enum class window_event_result : uint8_t; // Sends input events to event handlers diff --git a/common/include/gr.h b/common/include/gr.h index e6ea601d3..8bad2129b 100644 --- a/common/include/gr.h +++ b/common/include/gr.h @@ -98,7 +98,7 @@ struct grs_bitmap : prohibit_void_ptr short bm_rowsize; // unsigned char offset to next row std::array avg_color_rgb; // same as above but real rgb value to be used to textured objects that should emit light union { - const color_palette_index *bm_data; // ptr to pixel data... + const color_palette_index *bm_data{}; // ptr to pixel data... // Linear = *parent+(rowsize*y+x) // ModeX = *parent+(rowsize*y+x/4) // SVGA = *parent+(rowsize*y+x) @@ -106,9 +106,9 @@ struct grs_bitmap : prohibit_void_ptr }; const color_palette_index *get_bitmap_data() const { return bm_data; } color_palette_index *get_bitmap_data() { return bm_mdata; } - struct grs_bitmap *bm_parent; + struct grs_bitmap *bm_parent{}; #if DXX_USE_OGL - struct ogl_texture *gltexture; + struct ogl_texture *gltexture{}; #else uint8_t avg_color; // Average color of all pixels in texture map. #endif /* def OGL */ diff --git a/common/include/key.h b/common/include/key.h index a5a2f17e5..87e249c89 100644 --- a/common/include/key.h +++ b/common/include/key.h @@ -62,7 +62,7 @@ extern std::array unicode_frame_buffer; extern void key_flush(); // Clears the 256 char buffer extern void event_keycommand_send(unsigned key); // synthesize a key command event from a keycode -extern int event_key_get(const d_event &event); // Get the keycode from the EVENT_KEY_COMMAND event +int event_key_get(const d_event &event); // Get the keycode from the event_type::key_command event extern int event_key_get_raw(const d_event &event); // same as above but without mod states unsigned char key_ascii(); diff --git a/common/include/maths.h b/common/include/maths.h index 41130dbba..f2a96dd11 100644 --- a/common/include/maths.h +++ b/common/include/maths.h @@ -56,7 +56,7 @@ static constexpr int f2i(const fix &f) //Convert fix to float and float to fix static constexpr float f2fl(const fix &f) { - return static_cast(f) / static_cast(65536.0); + return static_cast(f) / 65536.0f; } static constexpr double f2db(const fix &f) diff --git a/common/include/mouse.h b/common/include/mouse.h index fa98027ab..fb16e2f9d 100644 --- a/common/include/mouse.h +++ b/common/include/mouse.h @@ -12,10 +12,10 @@ #pragma once +#include +#include #include "pstypes.h" #include "maths.h" - -#ifdef __cplusplus #include #include #include "event.h" @@ -56,9 +56,13 @@ enum class mbtn : uint8_t extern void mouse_flush(); // clears all mice events... extern void mouse_init(void); extern void mouse_close(void); -extern void mouse_get_pos( int *x, int *y, int *z ); +[[nodiscard]] +std::tuple mouse_get_pos(); window_event_result mouse_in_window(class window *wind); -extern void mouse_get_delta( int *dx, int *dy, int *dz ); +#if 0 +[[nodiscard]] +std::tuple mouse_get_delta(); +#endif void mouse_enable_cursor(); void mouse_disable_cursor(); window_event_result mouse_button_handler(const SDL_MouseButtonEvent *mbe); @@ -92,19 +96,20 @@ class d_event_mouse_moved : public d_event static inline mbtn event_mouse_get_button(const d_event &event) { auto &e = static_cast(event); - assert(e.type == EVENT_MOUSE_BUTTON_DOWN || e.type == EVENT_MOUSE_BUTTON_UP); + assert(e.type == event_type::mouse_button_down || e.type == event_type::mouse_button_up); return e.button; } -static inline void event_mouse_get_delta(const d_event &event, int *dx, int *dy, int *dz) +[[nodiscard]] +static inline std::array event_mouse_get_delta(const d_event &event) { auto &e = static_cast(event); - assert(e.type == EVENT_MOUSE_MOVED); - *dx = e.dx; - *dy = e.dy; - *dz = e.dz; + assert(e.type == event_type::mouse_moved); + return {{ + e.dx, + e.dy, + e.dz, + }}; } } - -#endif diff --git a/common/include/ntstring.h b/common/include/ntstring.h index ac049e9e8..fee0d8ec8 100644 --- a/common/include/ntstring.h +++ b/common/include/ntstring.h @@ -41,12 +41,12 @@ class ntstring : template std::size_t copy_if(I &&i) { - return copy_if(static_cast(0), static_cast(i)); + return copy_if(std::size_t{0}, static_cast(i)); } template std::size_t copy_if(I &&i, std::size_t N) { - return copy_if(static_cast(0), static_cast(i), N); + return copy_if(std::size_t{0}, static_cast(i), N); } template std::size_t copy_if(std::size_t out_offset, I &&i) diff --git a/common/include/rle.h b/common/include/rle.h index 2cffd8f8e..4de8a6ac8 100644 --- a/common/include/rle.h +++ b/common/include/rle.h @@ -189,71 +189,69 @@ class bm_rle_expand : bm_rle_src_stride end_src_bm(end(src)) { } - template - /* Decode one row of the bitmap, then return control to the - * caller. If the return value is `again`, then the caller - * should perform any per-row processing, then call step() - * again. If the return value is not `again`, then the - * destination buffer is undefined and the caller should not - * access it or call step(). - * - * `const T &` to ensure that t is only modified by the caller - * and that the caller does not accidentally provide an - * implementation of `get_begin_dbits` that moves the - * destination pointer. + /* Decode one row of the bitmap, then return control to the + * caller. If the return value is `again`, then the caller + * should perform any per-row processing, then call step() + * again. If the return value is not `again`, then the + * destination buffer is undefined and the caller should not + * access it or call step(). + * + * Use `const auto &` to ensure that t is only modified by the caller + * and that the caller does not accidentally provide an + * implementation of `get_begin_dbits` that moves the + * destination pointer. + */ + step_result step(const auto &t) + { + /* Poison the memory first, so that it is undefined even if + * the source is exhausted. */ - step_result step(const T &t) - { - /* Poison the memory first, so that it is undefined even if - * the source is exhausted. - */ - const auto b = t.get_begin_dbits(); - const auto e = t.get_end_dbits(); - DXX_MAKE_MEM_UNDEFINED(std::span(b, e)); - /* Check for source exhaustion, so that empty bitmaps are - * not read at all. This allows callers to treat - * src_exhausted as a definitive end-of-record with no data - * available. - */ - if (ptr_src_bit_lengths == end_src_bit_lengths) - return src_exhausted; - return step_internal(b, e); - } - template - /* Decode until source or destination is exhausted. The - * destination position is updated automatically as each row is - * decoded. There is no opportunity for callers to perform - * per-row processing. Callers should call step() directly if - * per-row processing is required. - * - * `T &&` since some callers may not care about the state of `t` - * afterward; `T &&` lets them pass an anonymous temporary. + const auto b = t.get_begin_dbits(); + const auto e = t.get_end_dbits(); + DXX_MAKE_MEM_UNDEFINED(std::span(b, e)); + /* Check for source exhaustion, so that empty bitmaps are + * not read at all. This allows callers to treat + * src_exhausted as a definitive end-of-record with no data + * available. */ - bool loop(const unsigned bm_w, T &&t) + if (ptr_src_bit_lengths == end_src_bit_lengths) + return src_exhausted; + return step_internal(b, e); + } + /* Decode until source or destination is exhausted. The + * destination position is updated automatically as each row is + * decoded. There is no opportunity for callers to perform + * per-row processing. Callers should call step() directly if + * per-row processing is required. + * + * Use `auto &&` since some callers may not care about the state of `t` + * afterward; `auto &&` lets them pass an anonymous temporary. + */ + bool loop(const unsigned bm_w, auto &&t) + { + for (;;) { - for (;;) + switch (step(t)) { - switch (step(t)) - { - case again: - /* Step succeeded. Notify `t` to update its - * dbits position, then loop around. - */ - t.consume_dbits(bm_w); - break; - case src_exhausted: - /* Success: source buffer exhausted and no error - * conditions detected. - */ - return true; - case dst_exhausted: - /* Failure: destination buffer too small to hold - * expanded source. - */ - return false; - } + case again: + /* Step succeeded. Notify `t` to update its + * dbits position, then loop around. + */ + t.consume_dbits(bm_w); + break; + case src_exhausted: + /* Success: source buffer exhausted and no error + * conditions detected. + */ + return true; + case dst_exhausted: + /* Failure: destination buffer too small to hold + * expanded source. + */ + return false; } } + } private: step_result step_internal(uint8_t *begin_dbits, uint8_t *end_dbits); }; diff --git a/common/include/serial.h b/common/include/serial.h index 90e714fb1..3b72f8a51 100644 --- a/common/include/serial.h +++ b/common/include/serial.h @@ -24,6 +24,9 @@ namespace serial { +template +concept check_equal_value = (Expected == Actual); + template class message; @@ -327,8 +330,11 @@ static inline detail::sign_extend_type sign_ #define DEFINE_SERIAL_MUTABLE_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) \ _DEFINE_SERIAL_UDT_TO_MESSAGE(TYPE, NAME, MEMBERLIST) +template +concept check_serial_udt_maximum_message_size = check_equal_value; + #define ASSERT_SERIAL_UDT_MESSAGE_SIZE(T, SIZE) \ - assert_equal(serial::class_type::maximum_size, SIZE, "sizeof(" #T ") is not " #SIZE) + static_assert(serial::check_serial_udt_maximum_message_size>) template ::type>::type, T1>> struct udt_message_compatible_same_type : base_type @@ -390,8 +396,8 @@ union unaligned_storage typename std::conditional, typename std::conditional>::type::type i; uint8_t u[N]; - assert_equal(sizeof(i), N, "sizeof(i) is not N"); - assert_equal(sizeof(a), sizeof(u), "sizeof(T) is not N"); + static_assert(check_equal_value); + static_assert(check_equal_value); }; template @@ -512,16 +518,19 @@ static constexpr T bswap(const T u) */ } -assert_equal(bswap(static_cast(1)), 1, ""); +template +concept check_bswap = check_equal_value; + +static_assert(check_bswap); -assert_equal(bswap(static_cast(0x12)), 0x1200, ""); -assert_equal(bswap(static_cast(0x92)), 0x9200, ""); -assert_equal(bswap(static_cast(0x9200)), 0x92, ""); -assert_equal(bswap(static_cast(0x102)), 0x201, ""); +static_assert(check_bswap); +static_assert(check_bswap); +static_assert(check_bswap); +static_assert(check_bswap); -assert_equal(bswap(static_cast(0x102)), 0x2010000, ""); +static_assert(check_bswap); -assert_equal(bswap(static_cast(0x102)), 0x201000000000000ull, ""); +static_assert(check_bswap); namespace reader { diff --git a/common/include/ui.h b/common/include/ui.h index 807d0cb02..f7ac0f78f 100644 --- a/common/include/ui.h +++ b/common/include/ui.h @@ -247,9 +247,9 @@ struct UI_DIALOG : window virtual window_event_result callback_handler(const d_event &) = 0; }; -#define B1_JUST_PRESSED (event.type == EVENT_MOUSE_BUTTON_DOWN && event_mouse_get_button(event) == mbtn::left) -#define B1_JUST_RELEASED (event.type == EVENT_MOUSE_BUTTON_UP && event_mouse_get_button(event) == mbtn::left) -#define B1_DOUBLE_CLICKED (event.type == EVENT_MOUSE_DOUBLE_CLICKED && event_mouse_get_button(event) == mbtn::left) +#define B1_JUST_PRESSED (event.type == event_type::mouse_button_down && event_mouse_get_button(event) == mbtn::left) +#define B1_JUST_RELEASED (event.type == event_type::mouse_button_up && event_mouse_get_button(event) == mbtn::left) +#define B1_DOUBLE_CLICKED (event.type == event_type::mouse_double_clicked && event_mouse_get_button(event) == mbtn::left) extern grs_font_ptr ui_small_font; @@ -275,7 +275,7 @@ int ui_messagebox( short xc, short yc, const char * text, const ui_messagebox_ti void ui_dialog_set_current_canvas(UI_DIALOG &dlg); void ui_close_dialog(UI_DIALOG &dlg); -#define GADGET_PRESSED(g) (event.type == EVENT_UI_GADGET_PRESSED && &ui_event_get_gadget(event) == g) +#define GADGET_PRESSED(g) (event.type == event_type::ui_gadget_pressed && &ui_event_get_gadget(event) == g) void ui_gadget_add(UI_DIALOG &dlg, short x1, short y1, short x2, short y2, UI_GADGET &); diff --git a/common/include/valptridx.h b/common/include/valptridx.h index d3e255895..c8fe9c57f 100644 --- a/common/include/valptridx.h +++ b/common/include/valptridx.h @@ -271,7 +271,7 @@ template class valptridx::partial_policy::require_valid { public: - static constexpr bool allow_nullptr = false; + static constexpr bool allow_nullptr{false}; [[nodiscard]] static constexpr std::false_type check_allowed_invalid_index(index_type) { return {}; } [[nodiscard]] @@ -285,7 +285,7 @@ template class valptridx::partial_policy::allow_invalid { public: - static constexpr bool allow_nullptr = true; + static constexpr bool allow_nullptr{true}; [[nodiscard]] static constexpr bool check_allowed_invalid_index(index_type i) { @@ -298,12 +298,6 @@ class valptridx::partial_policy::allow_invalid } }; -template -constexpr bool valptridx::partial_policy::require_valid::allow_nullptr; - -template -constexpr bool valptridx::partial_policy::allow_invalid::allow_nullptr; - template template