diff --git a/cpp/QrSegment.cpp b/cpp/QrSegment.cpp index e65c276..4694ee4 100644 --- a/cpp/QrSegment.cpp +++ b/cpp/QrSegment.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "QrSegment.hpp" using std::uint8_t; @@ -59,7 +60,7 @@ QrSegment QrSegment::makeBytes(const vector &data) { BitBuffer bb; for (uint8_t b : data) bb.appendBits(b, 8); - return QrSegment(Mode::BYTE, (int)data.size(), bb); + return QrSegment(Mode::BYTE, (int)data.size(), std::move(bb)); } @@ -82,7 +83,7 @@ QrSegment QrSegment::makeNumeric(const char *digits) { } if (accumCount > 0) // 1 or 2 digits remaining bb.appendBits(accumData, accumCount * 3 + 1); - return QrSegment(Mode::NUMERIC, charCount, bb); + return QrSegment(Mode::NUMERIC, charCount, std::move(bb)); } @@ -105,7 +106,7 @@ QrSegment QrSegment::makeAlphanumeric(const char *text) { } if (accumCount > 0) // 1 character remaining bb.appendBits(accumData, 6); - return QrSegment(Mode::ALPHANUMERIC, charCount, bb); + return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb)); } @@ -139,7 +140,7 @@ QrSegment QrSegment::makeEci(long assignVal) { bb.appendBits(assignVal, 21); } else throw "ECI assignment value out of range"; - return QrSegment(Mode::ECI, 0, bb); + return QrSegment(Mode::ECI, 0, std::move(bb)); } @@ -152,6 +153,15 @@ QrSegment::QrSegment(const Mode &md, int numCh, const std::vector &dt) : } +QrSegment::QrSegment(const Mode &md, int numCh, std::vector &&dt) : + mode(md), + numChars(numCh), + data(std::move(dt)) { + if (numCh < 0) + throw "Invalid value"; +} + + int QrSegment::getTotalBits(const vector &segs, int version) { if (version < 1 || version > 40) throw "Version number out of range"; diff --git a/cpp/QrSegment.hpp b/cpp/QrSegment.hpp index e6bf82e..2c5a908 100644 --- a/cpp/QrSegment.hpp +++ b/cpp/QrSegment.hpp @@ -149,6 +149,12 @@ class QrSegment final { public: QrSegment(const Mode &md, int numCh, const std::vector &dt); + /* + * Creates a new QR Code data segment with the given parameters and data. + */ + public: QrSegment(const Mode &md, int numCh, std::vector &&dt); + + // Package-private helper function. public: static int getTotalBits(const std::vector &segs, int version);