From 4dbf35a510fd2bad2ed8c59c4dd523de4b3cf77b Mon Sep 17 00:00:00 2001 From: Samuel Debionne Date: Tue, 28 Jun 2022 08:35:27 +0200 Subject: [PATCH] Add pmr image typedefs (#529) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add pmr image typedefs * Swap allocators only if it propagate on container * Do not call propagate_on_container_swap for C++14 Co-authored-by: Mateusz Łoskot --- include/boost/gil/image.hpp | 7 ++ include/boost/gil/typedefs.hpp | 147 ++++++++++++++++++------------- test/core/image/image.cpp | 29 +++++- test/core/image/test_fixture.hpp | 20 +++++ 4 files changed, 143 insertions(+), 60 deletions(-) diff --git a/include/boost/gil/image.hpp b/include/boost/gil/image.hpp index e3f943a839..86675b97d5 100644 --- a/include/boost/gil/image.hpp +++ b/include/boost/gil/image.hpp @@ -238,7 +238,14 @@ class image swap(_align_in_bytes, img._align_in_bytes); swap(_memory, img._memory); swap(_view, img._view); +#ifdef BOOST_NO_CXX17_HDR_MEMORY_RESOURCE swap(_alloc, img._alloc); +#else + if constexpr (std::allocator_traits::propagate_on_container_swap::value) + swap(_alloc, img._alloc); + else + BOOST_ASSERT(_alloc == img._alloc); +#endif swap(_allocated_bytes, img._allocated_bytes ); } diff --git a/include/boost/gil/typedefs.hpp b/include/boost/gil/typedefs.hpp index 4ff10d52d7..44673954ce 100644 --- a/include/boost/gil/typedefs.hpp +++ b/include/boost/gil/typedefs.hpp @@ -18,71 +18,100 @@ #include #include +#if !defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE) +# include +#endif //!defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE) // B - bits size/signedness, CM - channel model, CS - colour space, LAYOUT - pixel layout // Example: B = '8', CM = 'uint8_t', CS = 'bgr, LAYOUT='bgr_layout_t' -#define BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, LAYOUT) \ - template struct pixel; \ - template struct planar_pixel_reference; \ - template struct planar_pixel_iterator; \ - template class memory_based_step_iterator; \ - template class point; \ - template class memory_based_2d_locator; \ - template class image_view; \ - template class image; \ - using CS##B##_pixel_t = pixel; \ - using CS##B##c_pixel_t = pixel const; \ - using CS##B##_ref_t = pixel&; \ - using CS##B##c_ref_t = pixel const&; \ - using CS##B##_ptr_t = CS##B##_pixel_t*; \ - using CS##B##c_ptr_t = CS##B##c_pixel_t*; \ - using CS##B##_step_ptr_t = memory_based_step_iterator; \ - using CS##B##c_step_ptr_t = memory_based_step_iterator; \ - using CS##B##_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##c_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##_step_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##c_step_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##_view_t = image_view; \ - using CS##B##c_view_t = image_view; \ - using CS##B##_step_view_t = image_view; \ - using CS##B##c_step_view_t = image_view; \ +#define BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, LAYOUT) \ + template \ + struct pixel; \ + template \ + struct planar_pixel_reference; \ + template \ + struct planar_pixel_iterator; \ + template \ + class memory_based_step_iterator; \ + template \ + class point; \ + template \ + class memory_based_2d_locator; \ + template \ + class image_view; \ + template \ + class image; \ + using CS##B##_pixel_t = pixel; \ + using CS##B##c_pixel_t = pixel const; \ + using CS##B##_ref_t = pixel&; \ + using CS##B##c_ref_t = pixel const&; \ + using CS##B##_ptr_t = CS##B##_pixel_t*; \ + using CS##B##c_ptr_t = CS##B##c_pixel_t*; \ + using CS##B##_step_ptr_t = memory_based_step_iterator; \ + using CS##B##c_step_ptr_t = memory_based_step_iterator; \ + using CS##B##_loc_t = memory_based_2d_locator>; \ + using CS##B##c_loc_t = memory_based_2d_locator>; \ + using CS##B##_step_loc_t \ + = memory_based_2d_locator>; \ + using CS##B##c_step_loc_t \ + = memory_based_2d_locator>; \ + using CS##B##_view_t = image_view; \ + using CS##B##c_view_t = image_view; \ + using CS##B##_step_view_t = image_view; \ + using CS##B##c_step_view_t = image_view; \ using CS##B##_image_t = image>; -// Example: B = '8', CM = 'uint8_t', CS = 'bgr' CS_FULL = 'rgb_t' LAYOUT='bgr_layout_t' -#define BOOST_GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(B, CM, CS, CS_FULL, LAYOUT) \ - BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, LAYOUT) \ - using CS##B##_planar_ref_t = planar_pixel_reference; \ - using CS##B##c_planar_ref_t = planar_pixel_reference; \ - using CS##B##_planar_ptr_t = planar_pixel_iterator; \ - using CS##B##c_planar_ptr_t = planar_pixel_iterator; \ - using CS##B##_planar_step_ptr_t = memory_based_step_iterator; \ - using CS##B##c_planar_step_ptr_t \ - = memory_based_step_iterator; \ - using CS##B##_planar_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##c_planar_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##_planar_step_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##c_planar_step_loc_t \ - = memory_based_2d_locator>; \ - using CS##B##_planar_view_t = image_view; \ - using CS##B##c_planar_view_t = image_view; \ - using CS##B##_planar_step_view_t = image_view; \ - using CS##B##c_planar_step_view_t = image_view; \ - using CS##B##_planar_image_t \ - = image>; - -#define BOOST_GIL_DEFINE_BASE_TYPEDEFS(B, CM, CS) \ - BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, CS##_layout_t) - -#define BOOST_GIL_DEFINE_ALL_TYPEDEFS(B, CM, CS) \ - BOOST_GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(B, CM, CS, CS##_t, CS##_layout_t) +#define BOOST_GIL_DEFINE_BASE_PMR_TYPEDEFS_INTERNAL(B, CM, CS, LAYOUT) \ + namespace pmr { \ + using CS##B##_image_t \ + = image>; \ + } +// Example: B = '8', CM = 'uint8_t', CS = 'bgr' CS_FULL = 'rgb_t' LAYOUT='bgr_layout_t' +#define BOOST_GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(B, CM, CS, CS_FULL, LAYOUT) \ + BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, LAYOUT) \ + using CS##B##_planar_ref_t = planar_pixel_reference; \ + using CS##B##c_planar_ref_t = planar_pixel_reference; \ + using CS##B##_planar_ptr_t = planar_pixel_iterator; \ + using CS##B##c_planar_ptr_t = planar_pixel_iterator; \ + using CS##B##_planar_step_ptr_t = memory_based_step_iterator; \ + using CS##B##c_planar_step_ptr_t = memory_based_step_iterator; \ + using CS##B##_planar_loc_t \ + = memory_based_2d_locator>; \ + using CS##B##c_planar_loc_t \ + = memory_based_2d_locator>; \ + using CS##B##_planar_step_loc_t \ + = memory_based_2d_locator>; \ + using CS##B##c_planar_step_loc_t \ + = memory_based_2d_locator>; \ + using CS##B##_planar_view_t = image_view; \ + using CS##B##c_planar_view_t = image_view; \ + using CS##B##_planar_step_view_t = image_view; \ + using CS##B##c_planar_step_view_t = image_view; \ + using CS##B##_planar_image_t = image>; + +#define BOOST_GIL_DEFINE_ALL_PMR_TYPEDEFS_INTERNAL(B, CM, CS, CS_FULL, LAYOUT) \ + BOOST_GIL_DEFINE_BASE_PMR_TYPEDEFS_INTERNAL(B, CM, CS, LAYOUT) \ + namespace pmr { \ + using CS##B##_planar_image_t \ + = image>; \ + } + +#if defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE) +# define BOOST_GIL_DEFINE_BASE_TYPEDEFS(B, CM, CS) \ + BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, CS##_layout_t) + +# define BOOST_GIL_DEFINE_ALL_TYPEDEFS(B, CM, CS) \ + BOOST_GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(B, CM, CS, CS##_t, CS##_layout_t) +#else +# define BOOST_GIL_DEFINE_BASE_TYPEDEFS(B, CM, CS) \ + BOOST_GIL_DEFINE_BASE_TYPEDEFS_INTERNAL(B, CM, CS, CS##_layout_t) \ + BOOST_GIL_DEFINE_BASE_PMR_TYPEDEFS_INTERNAL(B, CM, CS, CS##_layout_t) + +# define BOOST_GIL_DEFINE_ALL_TYPEDEFS(B, CM, CS) \ + BOOST_GIL_DEFINE_ALL_TYPEDEFS_INTERNAL(B, CM, CS, CS##_t, CS##_layout_t) \ + BOOST_GIL_DEFINE_ALL_PMR_TYPEDEFS_INTERNAL(B, CM, CS, CS##_t, CS##_layout_t) +#endif //!defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE) namespace boost { namespace gil { diff --git a/test/core/image/image.cpp b/test/core/image/image.cpp index b2cca68688..cf73f1c7db 100644 --- a/test/core/image/image.cpp +++ b/test/core/image/image.cpp @@ -35,6 +35,7 @@ struct test_constructor_with_dimensions_pixel static void run() { boost::mp11::mp_for_each(test_constructor_with_dimensions_pixel{}); + boost::mp11::mp_for_each(test_constructor_with_dimensions_pixel{}); } }; @@ -44,12 +45,13 @@ struct test_constructor_from_other_image void operator()(Image const &) { using image_t = Image; + using allocator_t = typename Image::allocator_type; gil::point_t const dimensions{256, 128}; using pixel_t = typename image_t::view_t::value_type; pixel_t const rnd_pixel = fixture::pixel_generator::random(); { //constructor interleaved from planar - gil::image image1(dimensions, rnd_pixel); + gil::image image1(dimensions, rnd_pixel); image_t image2(image1); BOOST_TEST_EQ(image2.dimensions(), dimensions); auto v1 = gil::const_view(image1); @@ -106,6 +108,27 @@ struct test_constructor_from_view } }; +struct test_copy_assignement +{ + template + void operator()(Image const&) + { + using image_t = Image; + gil::point_t const dimensions{ 256, 128 }; + { + image_t image = fixture::create_image(dimensions.x, dimensions.y, 0); + image_t image2; + image2 = image; + BOOST_TEST_EQ(image2.dimensions(), dimensions); + } + } + static void run() + { + boost::mp11::mp_for_each(test_copy_assignement{}); + boost::mp11::mp_for_each(test_copy_assignement{}); + } +}; + struct test_move_constructor { template @@ -124,6 +147,7 @@ struct test_move_constructor static void run() { boost::mp11::mp_for_each(test_move_constructor{}); + boost::mp11::mp_for_each(test_move_constructor{}); } }; @@ -145,6 +169,7 @@ struct test_move_assignement static void run() { boost::mp11::mp_for_each(test_move_assignement{}); + boost::mp11::mp_for_each(test_move_assignement{}); } }; @@ -154,6 +179,8 @@ int main() test_constructor_from_other_image::run(); test_constructor_from_view::run(); + test_copy_assignement::run(); + test_move_constructor::run(); test_move_assignement::run(); diff --git a/test/core/image/test_fixture.hpp b/test/core/image/test_fixture.hpp index 8b149cd630..0a7fc4d865 100644 --- a/test/core/image/test_fixture.hpp +++ b/test/core/image/test_fixture.hpp @@ -38,6 +38,26 @@ using image_types = std::tuple gil::rgba32_image_t >; +#if defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE) + using pmr_image_types = std::tuple<>; +#else + using pmr_image_types = std::tuple + < + gil::pmr::gray8_image_t, + gil::pmr::gray16_image_t, + gil::pmr::gray32_image_t, + gil::pmr::bgr8_image_t, + gil::pmr::bgr16_image_t, + gil::pmr::bgr32_image_t, + gil::pmr::rgb8_image_t, + gil::pmr::rgb16_image_t, + gil::pmr::rgb32_image_t, + gil::pmr::rgba8_image_t, + gil::pmr::rgba16_image_t, + gil::pmr::rgba32_image_t + >; +#endif //defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE) + using rgb_interleaved_image_types = std::tuple < gil::bgr8_image_t,