Skip to content

Commit

Permalink
Clean up and expand ranges compat layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Dec 27, 2024
1 parent 6d97ac4 commit 2bbcd5f
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/actions/undo_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/ai/composite/aspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class composite_aspect : public typesafe_aspect<T> {

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;
Expand Down
34 changes: 4 additions & 30 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ranges>
#else
#include <boost/range/adaptor/map.hpp>
#include <boost/range/adaptor/transformed.hpp>
#endif
#include "utils/ranges.hpp"

#include <functional>
#include <iosfwd>
Expand Down Expand Up @@ -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<const config&>); }
#else
{ return ordered_children | boost::adaptors::transformed(&config::any_tag_view<const config&>); }
#endif
{ return ordered_children | utils::views::transform(&config::any_tag_view<const config&>); }

/** 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<config&>); }
#else
{ return ordered_children | boost::adaptors::transformed(&config::any_tag_view<config&>); }
#endif
{ return ordered_children | utils::views::transform(&config::any_tag_view<config&>); }

#endif // __cpp_explicit_this_parameter

Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/game_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/gui/core/event/dispatcher_private.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<C>(*chain_target, chain_event);

for(const auto& pre_func : signal.pre_child) {
Expand Down
12 changes: 6 additions & 6 deletions src/gui/core/event/handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<widget&>(*dispatcher), position);
break;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<widget&>(*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<widget&>(*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<widget&>(*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<widget&>(*dispatcher), center, dTheta, dDist, numFingers);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/quit_confirmation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/scripting/application_lua_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -48,7 +49,6 @@
#include <utility>

#include <functional>
#include <boost/range/adaptors.hpp>

#include "lua/wrapper_lauxlib.h"

Expand Down Expand Up @@ -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_context_backend>();
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);
Expand Down
28 changes: 19 additions & 9 deletions src/utils/ranges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@
#ifdef __cpp_lib_ranges
#include <ranges>
#else
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <boost/range/adaptor/transformed.hpp>
#endif

namespace utils
{
template<typename T>
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
2 changes: 1 addition & 1 deletion src/whiteboard/highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/whiteboard/mapbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<class move>(action);
if(move) {
move->set_turn_number(0);
Expand All @@ -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_);
}
}
Expand Down

0 comments on commit 2bbcd5f

Please sign in to comment.