Skip to content

Commit 9aa4875

Browse files
authored
Merge pull request #3 from Adrian104/fix-20240829
Fix compilation errors in VS, add move constructor and assignment operator
2 parents 11fb900 + 25a5203 commit 9aa4875

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

BitmapPlusPlus.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <cstddef> // std::size_t
99
#include <string> // std::string
1010
#include <cstring> // std::memcmp
11-
#include <exception> // std::exception
11+
#include <stdexcept> // std::runtime_error
12+
#include <utility> // std::exchange
1213

1314
namespace bmp {
1415
// Magic number for Bitmap .bmp 24 bpp files (24/8 = 3 = rgb colors only)
@@ -50,10 +51,10 @@ namespace bmp {
5051
constexpr Pixel(std::uint8_t red, std::uint8_t green, std::uint8_t blue) noexcept: r(red), g(green), b(blue) {}
5152

5253
constexpr bool operator==(const Pixel other) const noexcept {
53-
return r == other.r and g == other.g and b == other.b;
54+
return r == other.r && g == other.g && b == other.b;
5455
}
5556

56-
constexpr bool operator!=(const Pixel other) const noexcept { return not((*this) == other); }
57+
constexpr bool operator!=(const Pixel other) const noexcept { return !((*this) == other); }
5758
};
5859

5960
static_assert(sizeof(Pixel) == 3, "Bitmap Pixel size must be 3 bytes");
@@ -124,6 +125,12 @@ namespace bmp {
124125

125126
Bitmap(const Bitmap &other) = default; // Copy Constructor
126127

128+
Bitmap(Bitmap &&other) noexcept
129+
: m_pixels(std::move(other.m_pixels)),
130+
m_width(std::exchange(other.m_width, 0)),
131+
m_height(std::exchange(other.m_height, 0)) {
132+
}
133+
127134
virtual ~Bitmap() noexcept {
128135
m_pixels.clear();
129136
}
@@ -400,7 +407,7 @@ namespace bmp {
400407

401408
bool operator!=(const Bitmap &image) const { return !(*this == image); }
402409

403-
Bitmap &operator=(const Bitmap &image) // Move assignment operator
410+
Bitmap &operator=(const Bitmap &image) // Copy assignment operator
404411
{
405412
if (this != &image) {
406413
m_width = image.m_width;
@@ -410,6 +417,15 @@ namespace bmp {
410417
return *this;
411418
}
412419

420+
Bitmap &operator=(Bitmap &&image) noexcept {
421+
if (this != &image) {
422+
m_pixels = std::move(image.m_pixels);
423+
m_width = std::exchange(image.m_width, 0);
424+
m_height = std::exchange(image.m_height, 0);
425+
}
426+
return *this;
427+
}
428+
413429
public: /** foreach iterators access */
414430
std::vector<Pixel>::iterator begin() noexcept { return m_pixels.begin(); }
415431

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
cmake_minimum_required(VERSION 3.10)
22
project(BitmapPlusPlus VERSION "1.0.1" DESCRIPTION "Simple and Fast header only Bitmap (BMP) C++ library" LANGUAGES CXX)
3+
set(CMAKE_CXX_STANDARD 17)
34

45
# Examples
56
option(BPP_BUILD_EXAMPLES "Requires BPP to build all examples inside examples/ folder." ON)

0 commit comments

Comments
 (0)