diff --git a/src/actions/undo_action.cpp b/src/actions/undo_action.cpp index 02fe8c69043c..10e4650f1f31 100644 --- a/src/actions/undo_action.cpp +++ b/src/actions/undo_action.cpp @@ -46,7 +46,7 @@ undo_action_container::undo_action_container() bool undo_action_container::undo(int side) { int last_unit_id = resources::gameboard->unit_id_manager().get_save_id(); - for(auto& p_step : utils::reversed_view(steps_)) { + for(auto& p_step : steps_ | utils::views::reverse) { p_step->undo(side); } if(last_unit_id - unit_id_diff_ < 0) { diff --git a/src/ai/composite/aspect.hpp b/src/ai/composite/aspect.hpp index d3ec33d99718..24ca04683ac5 100644 --- a/src/ai/composite/aspect.hpp +++ b/src/ai/composite/aspect.hpp @@ -285,7 +285,7 @@ class composite_aspect : public typesafe_aspect { virtual void recalculate() const { - for(const auto& f : utils::reversed_view(facets_)) { + for(const auto& f : facets_ | utils::views::reverse) { if (f->active()) { this->value_ = f->get_ptr(); this->valid_ = true; diff --git a/src/config.hpp b/src/config.hpp index 714f13d84b3c..02549ab2bb43 100644 --- a/src/config.hpp +++ b/src/config.hpp @@ -32,21 +32,7 @@ #include "exceptions.hpp" #include "utils/const_clone.hpp" #include "utils/optional_reference.hpp" - -#ifdef CONFIG_USE_STL_RANGES -#undef CONFIG_USE_STL_RANGES -#endif - -#ifdef __cpp_lib_ranges // C++20 -#define CONFIG_USE_STL_RANGES -#endif - -#ifdef CONFIG_USE_STL_RANGES -#include -#else -#include -#include -#endif +#include "utils/ranges.hpp" #include #include @@ -808,19 +794,11 @@ class config /** In-order iteration over all children. */ auto all_children_view() const -#ifdef CONFIG_USE_STL_RANGES - { return ordered_children | std::views::transform(&config::any_tag_view); } -#else - { return ordered_children | boost::adaptors::transformed(&config::any_tag_view); } -#endif + { return ordered_children | utils::views::transform(&config::any_tag_view); } /** In-order iteration over all children. */ auto all_children_view() -#ifdef CONFIG_USE_STL_RANGES - { return ordered_children | std::views::transform(&config::any_tag_view); } -#else - { return ordered_children | boost::adaptors::transformed(&config::any_tag_view); } -#endif + { return ordered_children | utils::views::transform(&config::any_tag_view); } #endif // __cpp_explicit_this_parameter @@ -926,11 +904,7 @@ class config /** A non-owning view over all child tag names. */ auto child_name_view() const { -#ifdef __cpp_lib_ranges - return children_ | std::views::keys; -#else - return children_ | boost::adaptors::map_keys; -#endif + return children_ | utils::views::keys; } private: diff --git a/src/events.cpp b/src/events.cpp index 1a82aa773e16..ce5fb3902574 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -260,7 +260,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 : utils::reversed_view(event_contexts)) { + for(auto &context : event_contexts | utils::views::reverse) { if(context.has_handler(&that)) { found_context = true; context.add_handler(this); @@ -279,7 +279,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 : utils::reversed_view(event_contexts)) { + for(auto &context : event_contexts | utils::views::reverse) { if(context.has_handler(&that)) { join(context); break; @@ -340,7 +340,7 @@ void sdl_handler::join_same(sdl_handler* parent) leave(); // should not be in multiple event contexts } - for(auto& context : utils::reversed_view(event_contexts)) { + for(auto& context : event_contexts | utils::views::reverse) { if(context.has_handler(parent)) { join(context); return; @@ -362,7 +362,7 @@ void sdl_handler::leave() member->leave(); } - for(auto& context : utils::reversed_view(event_contexts)) { + for(auto& context : event_contexts | utils::views::reverse) { if(context.remove_handler(this)) { break; } diff --git a/src/game_data.cpp b/src/game_data.cpp index 9b5bcdfb499e..6814f9a49478 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -162,7 +162,7 @@ void game_data::activate_scope_variable(std::string var_name) const var_name.erase(itor, var_name.end()); } - for (scoped_wml_variable* v : utils::reversed_view(scoped_variables)) { + for (scoped_wml_variable* v : scoped_variables | utils::views::reverse) { 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 5b39e3f33413..e5ae5ac1d98b 100644 --- a/src/gui/core/event/dispatcher_private.hpp +++ b/src/gui/core/event/dispatcher_private.hpp @@ -237,7 +237,7 @@ bool fire_event(const ui_event event, bool halt = false; /***** ***** ***** Pre ***** ***** *****/ - for(const auto& [chain_target, chain_event] : utils::reversed_view(event_chain)) { + for(const auto& [chain_target, chain_event] : event_chain | utils::views::reverse) { const auto& signal = dispatcher_implementation::event_signal(*chain_target, chain_event); for(const auto& pre_func : signal.pre_child) { diff --git a/src/gui/core/event/handler.cpp b/src/gui/core/event/handler.cpp index 20b35ec18208..a0edcf978ab8 100644 --- a/src/gui/core/event/handler.cpp +++ b/src/gui/core/event/handler.cpp @@ -607,7 +607,7 @@ void sdl_event_handler::mouse(const ui_event event, const point& position) return; } - for(auto& dispatcher : utils::reversed_view(dispatchers_)) { + for(auto& dispatcher : dispatchers_ | utils::views::reverse) { if(dispatcher->get_mouse_behavior() == dispatcher::mouse_behavior::all) { dispatcher->fire(event, dynamic_cast(*dispatcher), position); break; @@ -687,7 +687,7 @@ dispatcher* sdl_event_handler::keyboard_dispatcher() return keyboard_focus_; } - for(auto& dispatcher : utils::reversed_view(dispatchers_)) { + for(auto& dispatcher : dispatchers_ | utils::views::reverse) { if(dispatcher->get_want_keyboard_input()) { return dispatcher; } @@ -698,28 +698,28 @@ dispatcher* sdl_event_handler::keyboard_dispatcher() void sdl_event_handler::touch_motion(const point& position, const point& distance) { - for(auto& dispatcher : utils::reversed_view(dispatchers_)) { + for(auto& dispatcher : dispatchers_ | utils::views::reverse) { dispatcher->fire(SDL_TOUCH_MOTION , dynamic_cast(*dispatcher), position, distance); } } void sdl_event_handler::touch_up(const point& position) { - for(auto& dispatcher : utils::reversed_view(dispatchers_)) { + for(auto& dispatcher : dispatchers_ | utils::views::reverse) { dispatcher->fire(SDL_TOUCH_UP, dynamic_cast(*dispatcher), position); } } void sdl_event_handler::touch_down(const point& position) { - for(auto& dispatcher : utils::reversed_view(dispatchers_)) { + for(auto& dispatcher : dispatchers_ | utils::views::reverse) { 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 : utils::reversed_view(dispatchers_)) { + for(auto& dispatcher : dispatchers_ | utils::views::reverse) { 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 b26cff56bd98..132e503b1ce8 100644 --- a/src/quit_confirmation.cpp +++ b/src/quit_confirmation.cpp @@ -27,7 +27,7 @@ bool quit_confirmation::quit() { if(!open_) { open_ = true; - for(quit_confirmation* blocker : utils::reversed_view(blockers_)) + for(quit_confirmation* blocker : blockers_ | utils::views::reverse) { if(!blocker->prompt_()) { open_ = false; diff --git a/src/scripting/application_lua_kernel.cpp b/src/scripting/application_lua_kernel.cpp index 3c76c49c4020..180d036c14b2 100644 --- a/src/scripting/application_lua_kernel.cpp +++ b/src/scripting/application_lua_kernel.cpp @@ -36,6 +36,7 @@ #include "scripting/lua_preferences.hpp" #include "scripting/plugins/context.hpp" #include "scripting/plugins/manager.hpp" +#include "utils/ranges.hpp" #ifdef DEBUG_LUA #include "scripting/debug_lua.hpp" @@ -48,7 +49,6 @@ #include #include -#include #include "lua/wrapper_lauxlib.h" @@ -276,7 +276,7 @@ application_lua_kernel::request_list application_lua_kernel::thread::run_script( // Now we have to create the context object. It is arranged as a table of boost functions. auto this_context_backend = std::make_shared(); lua_newtable(T_); // this will be the context table - for (const std::string & key : ctxt.callbacks_ | boost::adaptors::map_keys ) { + for (const std::string & key : ctxt.callbacks_ | utils::views::keys ) { lua_pushstring(T_, key.c_str()); lua_cpp::push_function(T_, std::bind(&impl_context_backend, std::placeholders::_1, this_context_backend, key)); lua_settable(T_, -3); diff --git a/src/utils/ranges.hpp b/src/utils/ranges.hpp index d6379e74bb0c..8708058ba961 100644 --- a/src/utils/ranges.hpp +++ b/src/utils/ranges.hpp @@ -17,19 +17,29 @@ #ifdef __cpp_lib_ranges #include #else +#include +#include #include +#include #endif -namespace utils -{ -template -inline auto reversed_view(T& container) +namespace utils::views { #ifdef __cpp_lib_ranges - return std::ranges::reverse_view(container); + +using std::views::filter; +using std::views::keys; +using std::views::reverse; +using std::views::transform; +using std::views::values; + #else - return boost::adaptors::reverse(container); -#endif -} -} // namespace utils +constexpr auto filter = boost::adaptors::filtered; +constexpr auto keys = boost::adaptors::map_keys; +constexpr auto reverse = boost::adaptors::reversed; +constexpr auto transform = boost::adaptors::transformed; +constexpr auto values = boost::adaptors::map_values; + +#endif +} // namespace utils::views diff --git a/src/whiteboard/highlighter.cpp b/src/whiteboard/highlighter.cpp index f4e8f66377d5..72500da89973 100644 --- a/src/whiteboard/highlighter.cpp +++ b/src/whiteboard/highlighter.cpp @@ -105,7 +105,7 @@ void highlighter::set_mouseover_hex(const map_location& hex) if(side_actions_->empty()) { return; } - for(action_ptr act : utils::reversed_view(*side_actions_)) { + for(action_ptr act : *side_actions_ | utils::views::reverse) { /**@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 387d35496a90..258fcc388c42 100644 --- a/src/whiteboard/mapbuilder.cpp +++ b/src/whiteboard/mapbuilder.cpp @@ -180,7 +180,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 : utils::reversed_view(applied_actions_this_turn_)) { + for(action_ptr action : applied_actions_this_turn_ | utils::views::reverse) { move_ptr move = std::dynamic_pointer_cast(action); if(move) { move->set_turn_number(0); @@ -200,7 +200,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 : utils::reversed_view(applied_actions_)) { + for(action_ptr act : applied_actions_ | utils::views::reverse) { act->remove_temp_modifier(unit_map_); } }