Skip to content

Commit

Permalink
Merge pull request #78460 from akrieger/keyboards_were_a_mistake
Browse files Browse the repository at this point in the history
Categorically fix type issues with restore_on_out_of_scope
  • Loading branch information
Night-Pryanik authored Dec 10, 2024
2 parents cf7ef9b + 67c1ad0 commit 40b3a32
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 85 deletions.
11 changes: 11 additions & 0 deletions src/cata_scope_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define CATA_SRC_CATA_SCOPE_HELPERS_H

#include <functional>
#include <type_traits>

class on_out_of_scope
{
Expand Down Expand Up @@ -40,11 +41,21 @@ class restore_on_out_of_scope
impl([this]() { t = std::move(orig_t); }) {
}

// Ideally this would be deleted, but it is needed to support unique_ptr<U>
explicit restore_on_out_of_scope(T&& t_in) : t(t_in), orig_t(std::move(t_in)),
impl([this]() { t = std::move(orig_t); }) {
}

// Don't allow restoring any variable that is not a T, because that means the caller
// would potentially induce a conversion which results in a temporary T.
template<typename U, typename = std::enable_if_t<!std::is_same_v<std::decay_t<T>, std::decay_t<U>>>>
restore_on_out_of_scope(U&&) = delete;
// *INDENT-ON*

void cancel() {
impl.cancel();
}

restore_on_out_of_scope( const restore_on_out_of_scope<T> & ) = delete;
restore_on_out_of_scope( restore_on_out_of_scope<T> && ) = delete;
restore_on_out_of_scope &operator=( const restore_on_out_of_scope<T> & ) = delete;
Expand Down
5 changes: 2 additions & 3 deletions src/construction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,7 @@ construction_id construction_menu( const bool blueprint )
tilecontext->set_disable_occlusion( true );
g->invalidate_main_ui_adaptor();
#endif
std::unique_ptr<restore_on_out_of_scope<tripoint_rel_ms>> restore_view
= std::make_unique<restore_on_out_of_scope<tripoint_rel_ms>>( player_character.view_offset );
restore_on_out_of_scope restore_view( player_character.view_offset );

const auto recalc_buffer = [&]() {
//leave room for top and bottom UI text
Expand Down Expand Up @@ -1062,7 +1061,7 @@ construction_id construction_menu( const bool blueprint )
add_msg( m_info, _( "It is too dark to construct right now." ) );
} else {
draw_preview.reset();
restore_view.reset();
restore_view.cancel();
restore_ui.reset();
ui.reset();
place_construction( { constructs[select] } );
Expand Down
40 changes: 20 additions & 20 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ shared_ptr_fast<ui_adaptor> editmap::create_or_get_ui_adaptor()
std::optional<tripoint_bub_ms> editmap::edit()
{
avatar &player_character = get_avatar();
restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( player_character.view_offset );
restore_on_out_of_scope view_offset_prev( player_character.view_offset );
target = player_character.pos_bub() + player_character.view_offset;
input_context ctxt( "EDITMAP" );
ctxt.set_iso( true );
Expand Down Expand Up @@ -381,8 +381,8 @@ std::optional<tripoint_bub_ms> editmap::edit()
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );

creature_tracker &creatures = get_creature_tracker();
do {
Expand Down Expand Up @@ -1129,8 +1129,8 @@ void editmap::edit_feature()
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );

blink = true;
bool quit = false;
Expand Down Expand Up @@ -1250,8 +1250,8 @@ void editmap::edit_fld()
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );
map &here = get_map();

blink = true;
Expand Down Expand Up @@ -1415,8 +1415,8 @@ void editmap::edit_itm()
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );

shared_ptr_fast<uilist_impl> ilmenu_ui = ilmenu.create_or_get_ui();

Expand Down Expand Up @@ -1700,8 +1700,8 @@ int editmap::select_shape( shapetype shape, int mode )
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );

do {
if( moveall ) {
Expand Down Expand Up @@ -1745,8 +1745,8 @@ int editmap::select_shape( shapetype shape, int mode )
on_out_of_scope invalidate_current_ui_2( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev_2( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev_2( info_title_curr );
restore_on_out_of_scope info_txt_prev_2( info_txt_curr );
restore_on_out_of_scope info_title_prev_2( info_title_curr );

do {
info_txt_curr.clear();
Expand Down Expand Up @@ -1855,9 +1855,9 @@ void editmap::mapgen_preview( const real_coords &tc, uilist &gmenu )
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<tinymap *> tinymap_ptr_prev( tmpmap_ptr );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope tinymap_ptr_prev( tmpmap_ptr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );
map &here = get_map();

int lastsel = gmenu.selected;
Expand Down Expand Up @@ -2063,8 +2063,8 @@ void editmap::mapgen_retarget()
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );

blink = true;
do {
Expand Down Expand Up @@ -2130,8 +2130,8 @@ void editmap::edit_mapgen()
on_out_of_scope invalidate_current_ui( [this]() {
do_ui_invalidation();
} );
restore_on_out_of_scope<std::string> info_txt_prev( info_txt_curr );
restore_on_out_of_scope<std::string> info_title_prev( info_title_curr );
restore_on_out_of_scope info_txt_prev( info_txt_curr );
restore_on_out_of_scope info_title_prev( info_title_curr );
map &here = get_map();

do {
Expand Down
14 changes: 7 additions & 7 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3359,7 +3359,7 @@ void game::load_packs( const std::string &msg, const std::vector<mod_id> &packs
for( const auto &e : available ) {
loading_ui::show( msg, e->name() );
const MOD_INFORMATION &mod = *e;
restore_on_out_of_scope<check_plural_t> restore_check_plural( check_plural );
restore_on_out_of_scope restore_check_plural( check_plural );
if( mod.ident.str() == "test_data" ) {
check_plural = check_plural_t::none;
}
Expand Down Expand Up @@ -7030,9 +7030,9 @@ void game::zones_manager()
{
ui.mark_resize();
} );
restore_on_out_of_scope<bool> show_prev( show );
restore_on_out_of_scope<std::optional<tripoint>> zone_start_prev( zone_start );
restore_on_out_of_scope<std::optional<tripoint>> zone_end_prev( zone_end );
restore_on_out_of_scope show_prev( show );
restore_on_out_of_scope zone_start_prev( zone_start );
restore_on_out_of_scope zone_end_prev( zone_end );
show = false;
zone_start = std::nullopt;
zone_end = std::nullopt;
Expand Down Expand Up @@ -7377,9 +7377,9 @@ void game::zones_manager()
on_out_of_scope invalidate_current_ui( [&]() {
ui.mark_resize();
} );
restore_on_out_of_scope<bool> show_prev( show );
restore_on_out_of_scope<std::optional<tripoint>> zone_start_prev( zone_start );
restore_on_out_of_scope<std::optional<tripoint>> zone_end_prev( zone_end );
restore_on_out_of_scope show_prev( show );
restore_on_out_of_scope zone_start_prev( zone_start );
restore_on_out_of_scope zone_end_prev( zone_end );
show = false;
zone_start = std::nullopt;
zone_end = std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion src/generic_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class generic_factory
jo.throw_error( string_format( "cannot specify both '%s' and '%s'",
abstract_member_name, id_member_name ) );
}
restore_on_out_of_scope<check_plural_t> restore_check_plural( check_plural );
restore_on_out_of_scope restore_check_plural( check_plural );
check_plural = check_plural_t::none;
const std::string abstract_id = jo.get_string( abstract_member_name );
def.id = string_id<T>( abstract_id );
Expand Down
2 changes: 1 addition & 1 deletion src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4193,7 +4193,7 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std:
{
bool strict = src == "dda";

restore_on_out_of_scope<check_plural_t> restore_check_plural( check_plural );
restore_on_out_of_scope restore_check_plural( check_plural );
if( jo.has_string( "abstract" ) ) {
check_plural = check_plural_t::none;
}
Expand Down
12 changes: 6 additions & 6 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ target_handler::trajectory target_handler::mode_select_only( avatar &you, int ra
ui.mode = target_ui::TargetMode::SelectOnly;
ui.range = range;

restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( you.view_offset );
restore_on_out_of_scope view_offset_prev( you.view_offset );
return ui.run();
}

Expand All @@ -483,7 +483,7 @@ target_handler::trajectory target_handler::mode_throw( avatar &you, item &releva
ui.relevant = &relevant;
ui.range = you.throw_range( relevant );

restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( you.view_offset );
restore_on_out_of_scope view_offset_prev( you.view_offset );
return ui.run();
}

Expand All @@ -495,7 +495,7 @@ target_handler::trajectory target_handler::mode_reach( avatar &you, item_locatio
ui.relevant = weapon.get_item();
ui.range = weapon ? weapon->current_reach_range( you ) : 1;

restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( you.view_offset );
restore_on_out_of_scope view_offset_prev( you.view_offset );
return ui.run();
}

Expand All @@ -509,7 +509,7 @@ target_handler::trajectory target_handler::mode_turret_manual( avatar &you, turr
ui.range = turret.range();
ui.ammo = turret.ammo_data();

restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( you.view_offset );
restore_on_out_of_scope view_offset_prev( you.view_offset );
return ui.run();
}

Expand Down Expand Up @@ -539,7 +539,7 @@ target_handler::trajectory target_handler::mode_turrets( avatar &you, vehicle &v
ui.vturrets = &turrets;
ui.range = range_total;

restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( you.view_offset );
restore_on_out_of_scope view_offset_prev( you.view_offset );
return ui.run();
}

Expand All @@ -554,7 +554,7 @@ target_handler::trajectory target_handler::mode_spell( avatar &you, spell &casti
ui.no_fail = no_fail;
ui.no_mana = no_mana;

restore_on_out_of_scope<tripoint_rel_ms> view_offset_prev( you.view_offset );
restore_on_out_of_scope view_offset_prev( you.view_offset );
return ui.run();
}

Expand Down
4 changes: 2 additions & 2 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3517,14 +3517,14 @@ static void CheckMessages()
}
bool resized = false;
if( resize_dims.has_value() ) {
restore_on_out_of_scope<input_event> prev_last_input( last_input );
restore_on_out_of_scope prev_last_input( last_input );
needupdate = resized = handle_resize( resize_dims.value().x, resize_dims.value().y );
}
// resizing already reinitializes the render target
if( !resized && render_target_reset ) {
throwErrorIf( !SetupRenderTarget(), "SetupRenderTarget failed" );
needupdate = true;
restore_on_out_of_scope<input_event> prev_last_input( last_input );
restore_on_out_of_scope prev_last_input( last_input );
// FIXME: SDL_RENDER_TARGETS_RESET only seems to be fired after the first redraw
// when restoring the window after system sleep, rather than immediately
// on focus gain. This seems to mess up the first redraw and
Expand Down
4 changes: 2 additions & 2 deletions src/ui_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ void ui_adaptor::redraw_invalidated( )
}
imgui_frame_started = true;

restore_on_out_of_scope<bool> prev_redraw_in_progress( redraw_in_progress );
restore_on_out_of_scope<bool> prev_restart_redrawing( restart_redrawing );
restore_on_out_of_scope prev_redraw_in_progress( redraw_in_progress );
restore_on_out_of_scope prev_restart_redrawing( restart_redrawing );
redraw_in_progress = true;

do {
Expand Down
Loading

0 comments on commit 40b3a32

Please sign in to comment.