From 65cf8021c9152a5817b25d1c1d3f44f4eff51a17 Mon Sep 17 00:00:00 2001 From: mniip Date: Thu, 4 May 2023 19:38:46 +0200 Subject: [PATCH] Reorganize the constructors of non-owning PlaneAdapter --- src/common/Plane.h | 66 ++++++++++++++++++++++++++------------- src/graphics/Graphics.cpp | 6 ++-- src/graphics/Graphics.h | 1 - 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/common/Plane.h b/src/common/Plane.h index c79ff701b..ab75344eb 100644 --- a/src/common/Plane.h +++ b/src/common/Plane.h @@ -1,7 +1,10 @@ #pragma once #include +#include #include +#include +#include #include "common/Vec2.h" @@ -54,6 +57,24 @@ struct yExtent: extentStorage using extentStorage::extentStorage; }; +template +struct baseStorage +{ + using type = T; +}; + +template +struct baseStorage +{ + using type = std::reference_wrapper; +}; + +template +struct baseStorage +{ + using type = std::reference_wrapper; +}; + // A class that contains some container T and lets you index into it as if it // were a 2D array of size Width x Height, in row-major order. template @@ -73,8 +94,18 @@ class PlaneAdapter: xExtent, yExtent return yExtent::getExtent(); } + std::remove_reference_t &getBase() + { + return Base; + } + + std::remove_reference_t const &getBase() const + { + return Base; + } + public: - T Base; + typename baseStorage::type Base; PlaneAdapter(): xExtent(0), @@ -82,18 +113,6 @@ public: Base() {} - PlaneAdapter(PlaneAdapter const &) = default; - - PlaneAdapter(PlaneAdapter &&) = default; - - // PlaneAdapter can be constructed from (Vec2, T &&) - // PlaneAdapter can be constructed from (Vec2, T &) - PlaneAdapter(Vec2 size, T &&base): - xExtent(size.X), - yExtent(size.Y), - Base(std::forward(base)) - {} - template PlaneAdapter(Vec2 size, Args&&... args): xExtent(size.X), @@ -101,9 +120,12 @@ public: Base(getWidth() * getHeight(), std::forward(args)...) {} - PlaneAdapter &operator=(PlaneAdapter const &) = default; - - PlaneAdapter &operator=(PlaneAdapter &&) = default; + template + PlaneAdapter(Vec2 size, std::in_place_t, Args&&... args): + xExtent(size.X), + yExtent(size.Y), + Base(std::forward(args)...) + {} Vec2 Size() const { @@ -118,31 +140,31 @@ public: iterator RowIterator(Vec2 p) { - return std::begin(Base) + (p.X + p.Y * getWidth()); + return std::begin(getBase()) + (p.X + p.Y * getWidth()); } const_iterator RowIterator(Vec2 p) const { - return std::begin(Base) + (p.X + p.Y * getWidth()); + return std::begin(getBase()) + (p.X + p.Y * getWidth()); } value_type *data() { - return std::data(Base); + return std::data(getBase()); } value_type const *data() const { - return std::data(Base); + return std::data(getBase()); } value_type &operator[](Vec2 p) { - return Base[p.X + p.Y * getWidth()]; + return getBase()[p.X + p.Y * getWidth()]; } value_type const &operator[](Vec2 p) const { - return Base[p.X + p.Y * getWidth()]; + return getBase()[p.X + p.Y * getWidth()]; } }; diff --git a/src/graphics/Graphics.cpp b/src/graphics/Graphics.cpp index 13c9c907c..8b7828b71 100644 --- a/src/graphics/Graphics.cpp +++ b/src/graphics/Graphics.cpp @@ -36,15 +36,15 @@ void VideoBuffer::Crop(Rect rect) if (rect == Size().OriginRect()) return; - PlaneAdapter &> newVideo(rect.Size(), video.Base); + PlaneAdapter &> newVideo(rect.Size(), std::in_place, video.Base); for (auto y = 0; y < newVideo.Size().Y; y++) std::copy_n( video.RowIterator(rect.TopLeft + Vec2(0, y)), newVideo.Size().X, newVideo.RowIterator(Vec2(0, y)) ); - newVideo.Base.resize(newVideo.Size().X * newVideo.Size().Y); - newVideo.Base.shrink_to_fit(); + video.Base.resize(newVideo.Size().X * newVideo.Size().Y); + video.Base.shrink_to_fit(); video.SetSize(newVideo.Size()); } diff --git a/src/graphics/Graphics.h b/src/graphics/Graphics.h index bbc7eb0ce..638ae4996 100644 --- a/src/graphics/Graphics.h +++ b/src/graphics/Graphics.h @@ -22,7 +22,6 @@ class VideoBuffer: public RasterDrawMethods friend struct RasterDrawMethods; public: - VideoBuffer(VideoBuffer const &) = default; VideoBuffer(pixel const *data, Vec2 size); VideoBuffer(pixel const *data, Vec2 size, size_t rowStride); VideoBuffer(Vec2 size);