Skip to content

Commit

Permalink
Added utils::reversed_view
Browse files Browse the repository at this point in the history
This is a wrapper around either std::views::reversed (C++20) or boost::adaptors::reverse (anything else).
  • Loading branch information
Vultraz committed Jan 23, 2021
1 parent 204d3b4 commit 7311b19
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 33 deletions.
5 changes: 2 additions & 3 deletions src/ai/composite/aspect.hpp
Expand Up @@ -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 <functional>

#include <boost/range/adaptor/reversed.hpp>

namespace ai {

class aspect : public readonly_context_proxy, public events::observer, public component {
Expand Down Expand Up @@ -286,7 +285,7 @@ class composite_aspect : public typesafe_aspect<T> {

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;
Expand Down
13 changes: 6 additions & 7 deletions src/events.cpp
Expand Up @@ -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
Expand All @@ -36,8 +37,6 @@

#include <SDL2/SDL.h>

#include <boost/range/adaptor/reversed.hpp>

#define ERR_GEN LOG_STREAM(err, lg::general)

namespace
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/game_data.cpp
Expand Up @@ -22,8 +22,7 @@
#include "log.hpp" //LOG_STREAM
#include "variable.hpp" //scoped_wml_variable
#include "serialization/string_utils.hpp"

#include <boost/range/adaptor/reversed.hpp>
#include "utils/ranges.hpp"

static lg::log_domain log_engine("engine");
#define ERR_NG LOG_STREAM(err, log_engine)
Expand Down Expand Up @@ -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()) {
Expand Down
5 changes: 3 additions & 2 deletions src/gui/core/event/dispatcher_private.hpp
Expand Up @@ -17,10 +17,11 @@
#include "gui/core/event/dispatcher.hpp"

#include "gui/widgets/widget.hpp"
#include "utils/ranges.hpp"

#include <SDL2/SDL_events.h>

#include <boost/range/adaptor/reversed.hpp>
#include <cassert>

namespace gui2
{
Expand Down Expand Up @@ -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<T>(*ritor_widget.first, ritor_widget.second);

for(auto& pre_func : signal.pre_child) {
Expand Down
15 changes: 7 additions & 8 deletions src/gui/core/event/handler.cpp
Expand Up @@ -26,11 +26,10 @@
#include "video.hpp"
#include "serialization/unicode_cast.hpp"
#include "sdl/userevent.hpp"
#include "utils/ranges.hpp"

#include <cassert>

#include <boost/range/adaptor/reversed.hpp>

/**
* @todo The items below are not implemented yet.
*
Expand Down Expand Up @@ -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<widget&>(*dispatcher), position);
break;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<widget&>(*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<widget&>(*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<widget&>(*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<widget&>(*dispatcher), center, dTheta, dDist, numFingers);
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/quit_confirmation.cpp
Expand Up @@ -21,14 +21,13 @@
#include "gui/dialogs/surrender_quit.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/retval.hpp"

#include <boost/range/adaptor/reversed.hpp>
#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;
Expand Down
36 changes: 36 additions & 0 deletions 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 <ranges>
#else
#include <boost/range/adaptor/reversed.hpp>
#endif

namespace utils
{
template<typename T>
inline auto reversed_view(T& container)
{
#ifdef HAVE_CXX20
return std::views::reverse(container);
#else
return boost::adaptors::reverse(container);
#endif
}

} // namespace utils
5 changes: 2 additions & 3 deletions src/whiteboard/highlighter.cpp
Expand Up @@ -18,8 +18,6 @@

#include <algorithm>
#include <iterator>
#include <boost/range/adaptor/reversed.hpp>

#include <functional>

#include "whiteboard/highlighter.hpp"
Expand All @@ -45,6 +43,7 @@
#include "units/unit.hpp"
#include "units/animation_component.hpp"
#include "units/map.hpp"
#include "utils/ranges.hpp"

namespace wb
{
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 3 additions & 4 deletions src/whiteboard/mapbuilder.cpp
Expand Up @@ -28,8 +28,7 @@
#include "resources.hpp"
#include "units/unit.hpp"
#include "units/map.hpp"

#include <boost/range/adaptor/reversed.hpp>
#include "utils/ranges.hpp"

namespace wb
{
Expand Down Expand Up @@ -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<class move>(action);
if(move) {
move->set_turn_number(0);
Expand All @@ -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_);
}
}
Expand Down

0 comments on commit 7311b19

Please sign in to comment.