From 7311b19d4d24ca0f54a309e033f2a767d56faeb4 Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Sat, 23 Jan 2021 21:38:02 +1100 Subject: [PATCH] Added utils::reversed_view This is a wrapper around either std::views::reversed (C++20) or boost::adaptors::reverse (anything else). --- src/ai/composite/aspect.hpp | 5 ++-- src/events.cpp | 13 ++++---- src/game_data.cpp | 5 ++-- src/gui/core/event/dispatcher_private.hpp | 5 ++-- src/gui/core/event/handler.cpp | 15 +++++----- src/quit_confirmation.cpp | 5 ++-- src/utils/ranges.hpp | 36 +++++++++++++++++++++++ src/whiteboard/highlighter.cpp | 5 ++-- src/whiteboard/mapbuilder.cpp | 7 ++--- 9 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 src/utils/ranges.hpp diff --git a/src/ai/composite/aspect.hpp b/src/ai/composite/aspect.hpp index fcb3beb12c8..a558af90a8e 100644 --- a/src/ai/composite/aspect.hpp +++ b/src/ai/composite/aspect.hpp @@ -23,13 +23,12 @@ #include "ai/lua/lua_object.hpp" #include "ai/lua/core.hpp" #include "scripting/game_lua_kernel.hpp" +#include "utils/ranges.hpp" #include "log.hpp" #include -#include - namespace ai { class aspect : public readonly_context_proxy, public events::observer, public component { @@ -286,7 +285,7 @@ public: virtual void recalculate() const { - for(const auto& f : boost::adaptors::reverse(facets_)) { + for(const auto& f : utils::reversed_view(facets_)) { if (f->active()) { this->value_ = f->get_ptr(); this->valid_ = true; diff --git a/src/events.cpp b/src/events.cpp index c41a664e331..11bda14102c 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -19,6 +19,7 @@ #include "log.hpp" #include "quit_confirmation.hpp" #include "sdl/userevent.hpp" +#include "utils/ranges.hpp" #include "video.hpp" #if defined _WIN32 @@ -36,8 +37,6 @@ #include -#include - #define ERR_GEN LOG_STREAM(err, lg::general) namespace @@ -249,7 +248,7 @@ sdl_handler::sdl_handler(const sdl_handler &that) event_contexts.front().add_handler(this); } else if(has_joined_) { bool found_context = false; - for(auto &context : boost::adaptors::reverse(event_contexts)) { + for(auto &context : utils::reversed_view(event_contexts)) { if(context.has_handler(&that)) { found_context = true; context.add_handler(this); @@ -268,7 +267,7 @@ sdl_handler &sdl_handler::operator=(const sdl_handler &that) if(that.has_joined_global_) { join_global(); } else if(that.has_joined_) { - for(auto &context : boost::adaptors::reverse(event_contexts)) { + for(auto &context : utils::reversed_view(event_contexts)) { if(context.has_handler(&that)) { join(context); break; @@ -329,7 +328,7 @@ void sdl_handler::join_same(sdl_handler* parent) leave(); // should not be in multiple event contexts } - for(auto& context : boost::adaptors::reverse(event_contexts)) { + for(auto& context : utils::reversed_view(event_contexts)) { if(context.has_handler(parent)) { join(context); return; @@ -351,7 +350,7 @@ void sdl_handler::leave() member->leave(); } - for(auto& context : boost::adaptors::reverse(event_contexts)) { + for(auto& context : utils::reversed_view(event_contexts)) { if(context.remove_handler(this)) { break; } @@ -522,7 +521,7 @@ void pump() } bool resize_found = false; - for(const SDL_Event& event : boost::adaptors::reverse(events)) { + for(const SDL_Event& event : utils::reversed_view(events)) { if(event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) { resize_found = true; last_resize_event = event; diff --git a/src/game_data.cpp b/src/game_data.cpp index 6a16d1bdd3e..e289c476414 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -22,8 +22,7 @@ #include "log.hpp" //LOG_STREAM #include "variable.hpp" //scoped_wml_variable #include "serialization/string_utils.hpp" - -#include +#include "utils/ranges.hpp" static lg::log_domain log_engine("engine"); #define ERR_NG LOG_STREAM(err, log_engine) @@ -156,7 +155,7 @@ void game_data::activate_scope_variable(std::string var_name) const var_name.erase(itor, var_name.end()); } - for (scoped_wml_variable* v : boost::adaptors::reverse(scoped_variables)) { + for (scoped_wml_variable* v : utils::reversed_view(scoped_variables)) { if (v->name() == var_name) { recursive_activation = true; if (!v->activated()) { diff --git a/src/gui/core/event/dispatcher_private.hpp b/src/gui/core/event/dispatcher_private.hpp index c38a6378437..75b3f5a1369 100644 --- a/src/gui/core/event/dispatcher_private.hpp +++ b/src/gui/core/event/dispatcher_private.hpp @@ -17,10 +17,11 @@ #include "gui/core/event/dispatcher.hpp" #include "gui/widgets/widget.hpp" +#include "utils/ranges.hpp" #include -#include +#include namespace gui2 { @@ -290,7 +291,7 @@ inline bool fire_event(const ui_event event, bool halt = false; /***** ***** ***** Pre ***** ***** *****/ - for(auto& ritor_widget : boost::adaptors::reverse(event_chain)) { + for(auto& ritor_widget : utils::reversed_view(event_chain)) { auto& signal = dispatcher_implementation::event_signal(*ritor_widget.first, ritor_widget.second); for(auto& pre_func : signal.pre_child) { diff --git a/src/gui/core/event/handler.cpp b/src/gui/core/event/handler.cpp index 42ea6a9edce..cfcdf9576e9 100644 --- a/src/gui/core/event/handler.cpp +++ b/src/gui/core/event/handler.cpp @@ -26,11 +26,10 @@ #include "video.hpp" #include "serialization/unicode_cast.hpp" #include "sdl/userevent.hpp" +#include "utils/ranges.hpp" #include -#include - /** * @todo The items below are not implemented yet. * @@ -639,7 +638,7 @@ void sdl_event_handler::mouse(const ui_event event, const point& position) return; } - for(auto& dispatcher : boost::adaptors::reverse(dispatchers_)) { + for(auto& dispatcher : utils::reversed_view(dispatchers_)) { if(dispatcher->get_mouse_behavior() == dispatcher::all) { dispatcher->fire(event, dynamic_cast(*dispatcher), position); break; @@ -719,7 +718,7 @@ dispatcher* sdl_event_handler::keyboard_dispatcher() return keyboard_focus_; } - for(auto& dispatcher : boost::adaptors::reverse(dispatchers_)) { + for(auto& dispatcher : utils::reversed_view(dispatchers_)) { if(dispatcher->get_want_keyboard_input()) { return dispatcher; } @@ -730,28 +729,28 @@ dispatcher* sdl_event_handler::keyboard_dispatcher() void sdl_event_handler::touch_motion(const point& position, const point& distance) { - for(auto& dispatcher : boost::adaptors::reverse(dispatchers_)) { + for(auto& dispatcher : utils::reversed_view(dispatchers_)) { dispatcher->fire(SDL_TOUCH_MOTION , dynamic_cast(*dispatcher), position, distance); } } void sdl_event_handler::touch_up(const point& position) { - for(auto& dispatcher : boost::adaptors::reverse(dispatchers_)) { + for(auto& dispatcher : utils::reversed_view(dispatchers_)) { dispatcher->fire(SDL_TOUCH_UP, dynamic_cast(*dispatcher), position); } } void sdl_event_handler::touch_down(const point& position) { - for(auto& dispatcher : boost::adaptors::reverse(dispatchers_)) { + for(auto& dispatcher : utils::reversed_view(dispatchers_)) { dispatcher->fire(SDL_TOUCH_DOWN, dynamic_cast(*dispatcher), position); } } void sdl_event_handler::touch_multi_gesture(const point& center, float dTheta, float dDist, uint8_t numFingers) { - for(auto& dispatcher : boost::adaptors::reverse(dispatchers_)) { + for(auto& dispatcher : utils::reversed_view(dispatchers_)) { dispatcher->fire(SDL_TOUCH_MULTI_GESTURE, dynamic_cast(*dispatcher), center, dTheta, dDist, numFingers); } } diff --git a/src/quit_confirmation.cpp b/src/quit_confirmation.cpp index d367328528d..429e5cb64b3 100644 --- a/src/quit_confirmation.cpp +++ b/src/quit_confirmation.cpp @@ -21,14 +21,13 @@ #include "gui/dialogs/surrender_quit.hpp" #include "gui/dialogs/message.hpp" #include "gui/widgets/retval.hpp" - -#include +#include "utils/ranges.hpp" bool quit_confirmation::quit() { if(!open_) { open_ = true; - for(quit_confirmation* blocker : boost::adaptors::reverse(blockers_)) + for(quit_confirmation* blocker : utils::reversed_view(blockers_)) { if(!blocker->prompt_()) { open_ = false; diff --git a/src/utils/ranges.hpp b/src/utils/ranges.hpp new file mode 100644 index 00000000000..cea36364c98 --- /dev/null +++ b/src/utils/ranges.hpp @@ -0,0 +1,36 @@ +/* + Copyright (C) 2021 by the Battle for Wesnoth Project https://www.wesnoth.org/ + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. + + See the COPYING file for more details. +*/ + +#pragma once + +#include "global.hpp" + +#ifdef HAVE_CXX20 +#include +#else +#include +#endif + +namespace utils +{ +template +inline auto reversed_view(T& container) +{ +#ifdef HAVE_CXX20 + return std::views::reverse(container); +#else + return boost::adaptors::reverse(container); +#endif +} + +} // namespace utils diff --git a/src/whiteboard/highlighter.cpp b/src/whiteboard/highlighter.cpp index 5e269c365c3..aafacbd8b9d 100644 --- a/src/whiteboard/highlighter.cpp +++ b/src/whiteboard/highlighter.cpp @@ -18,8 +18,6 @@ #include #include -#include - #include #include "whiteboard/highlighter.hpp" @@ -45,6 +43,7 @@ #include "units/unit.hpp" #include "units/animation_component.hpp" #include "units/map.hpp" +#include "utils/ranges.hpp" namespace wb { @@ -109,7 +108,7 @@ void highlighter::set_mouseover_hex(const map_location& hex) if(side_actions_->empty()) { return; } - for(action_ptr act : boost::adaptors::reverse(*side_actions_)) { + for(action_ptr act : utils::reversed_view(*side_actions_)) { /**@todo "is_numbering_hex" is not the "correct" criterion by which to * select the highlighted/selected action. It's just convenient for me * to use at the moment since it happens to coincide with the "correct" diff --git a/src/whiteboard/mapbuilder.cpp b/src/whiteboard/mapbuilder.cpp index c867747f839..4631901876c 100644 --- a/src/whiteboard/mapbuilder.cpp +++ b/src/whiteboard/mapbuilder.cpp @@ -28,8 +28,7 @@ #include "resources.hpp" #include "units/unit.hpp" #include "units/map.hpp" - -#include +#include "utils/ranges.hpp" namespace wb { @@ -180,7 +179,7 @@ void mapbuilder::post_visit_team(std::size_t turn) // Go backwards through the actions of this turn to identify // which ones are moves that end a turn. - for(action_ptr action : boost::adaptors::reverse(applied_actions_this_turn_)) { + for(action_ptr action : utils::reversed_view(applied_actions_this_turn_)) { move_ptr move = std::dynamic_pointer_cast(action); if(move) { move->set_turn_number(0); @@ -200,7 +199,7 @@ void mapbuilder::post_visit_team(std::size_t turn) void mapbuilder::restore_normal_map() { //applied_actions_ contain only the actions that we applied to the unit map - for(action_ptr act : boost::adaptors::reverse(applied_actions_)) { + for(action_ptr act : utils::reversed_view(applied_actions_)) { act->remove_temp_modifier(unit_map_); } }