Added rvalue reference (xvalue) constructor version for C++ QrSegment, updated code to use it.

This commit is contained in:
Project Nayuki 2017-08-18 03:43:28 +00:00
parent 2984aa504d
commit 3305d230c1
2 changed files with 20 additions and 4 deletions

View File

@ -23,6 +23,7 @@
#include <climits>
#include <cstring>
#include <utility>
#include "QrSegment.hpp"
using std::uint8_t;
@ -59,7 +60,7 @@ QrSegment QrSegment::makeBytes(const vector<uint8_t> &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<bool> &dt) :
}
QrSegment::QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt) :
mode(md),
numChars(numCh),
data(std::move(dt)) {
if (numCh < 0)
throw "Invalid value";
}
int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
if (version < 1 || version > 40)
throw "Version number out of range";

View File

@ -149,6 +149,12 @@ class QrSegment final {
public: QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);
/*
* Creates a new QR Code data segment with the given parameters and data.
*/
public: QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);
// Package-private helper function.
public: static int getTotalBits(const std::vector<QrSegment> &segs, int version);