Add utils::span wrapper (#9318)

This commit is contained in:
Charles Dang 2024-09-08 02:38:24 -04:00 committed by GitHub
parent 7c60174ad1
commit 686eb30d41
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 22 deletions

View file

@ -21,22 +21,15 @@
#include "color_range.hpp"
#include "utils/span.hpp"
#include <array>
#include <cassert>
#include <sstream>
#ifdef __cpp_lib_span
#include <span>
#endif
namespace
{
#ifdef __cpp_lib_span
std::vector<color_t> recolor_range_impl(const color_range& new_range, std::span<const color_t> old_rgb)
#else
template<typename Container>
std::vector<color_t> recolor_range_impl(const color_range& new_range, const Container& old_rgb)
#endif
std::vector<color_t> recolor_range_impl(const color_range& new_range, utils::span<const color_t> old_rgb)
{
std::vector<color_t> clist;
clist.reserve(old_rgb.size());

View file

@ -16,6 +16,7 @@
#include "sdl/surface.hpp"
#include "sdl/utils.hpp"
#include "utils/span.hpp"
#include <algorithm>
#include <array>
@ -61,16 +62,6 @@ surface array_to_surface(const std::array<uint32_t, w * h>& arr)
return surf;
}
static std::vector<uint32_t> surface_to_vec(const surface& surf)
{
const_surface_lock lock{surf};
const uint32_t* const pixels = lock.pixels();
std::vector<uint32_t> pixel_vec;
const int surf_size = surf->w * surf->h;
std::copy(pixels, pixels + surf_size, std::back_inserter(pixel_vec));
return pixel_vec;
}
BOOST_AUTO_TEST_SUITE(sdl)
BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
@ -91,7 +82,8 @@ BOOST_AUTO_TEST_CASE(test_scale_sharp_round)
{
surface src = array_to_surface<4, 4>(img_4x4);
surface result = scale_surface_sharp(src, 2, 2);
std::vector<uint32_t> result_pixels = surface_to_vec(result);
const_surface_lock lock{result};
auto result_pixels = utils::span(lock.pixels(), result.area());
BOOST_CHECK_EQUAL_COLLECTIONS(
result_pixels.begin(), result_pixels.end(), img_4x4_to_2x2_result.begin(), img_4x4_to_2x2_result.end());
}
@ -100,7 +92,8 @@ BOOST_AUTO_TEST_CASE(test_scale_sharp_fractional)
{
surface src = array_to_surface<4, 4>(img_4x4);
surface result = scale_surface_sharp(src, 3, 2);
std::vector<uint32_t> result_pixels = surface_to_vec(result);
const_surface_lock lock{result};
auto result_pixels = utils::span(lock.pixels(), result.area());
BOOST_CHECK_EQUAL_COLLECTIONS(
result_pixels.begin(), result_pixels.end(), img_4x4_to_3x2_result.begin(), img_4x4_to_3x2_result.end());
}

29
src/utils/span.hpp Normal file
View file

@ -0,0 +1,29 @@
/*
Copyright (C) 2024 by the Battle for Wesnoth Project http://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
#ifdef __cpp_lib_span
#include <span>
#else
#include <boost/core/span.hpp>
#endif
namespace utils
{
#ifdef __cpp_lib_span
using std::span;
#else
using boost::span;
#endif
} // end namespace utils