Tweaked usages of C++ QrCode::Ecc class to be passed by value instead of const reference.

This commit is contained in:
Project Nayuki 2017-09-06 04:21:56 +00:00
parent 236a999637
commit 8bbfa3938b
3 changed files with 16 additions and 17 deletions

View File

@ -59,19 +59,19 @@ const QrCode::Ecc QrCode::Ecc::QUARTILE(2, 3);
const QrCode::Ecc QrCode::Ecc::HIGH (3, 2);
QrCode QrCode::encodeText(const char *text, const Ecc &ecl) {
QrCode QrCode::encodeText(const char *text, Ecc ecl) {
vector<QrSegment> segs(QrSegment::makeSegments(text));
return encodeSegments(segs, ecl);
}
QrCode QrCode::encodeBinary(const vector<uint8_t> &data, const Ecc &ecl) {
QrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) {
vector<QrSegment> segs{QrSegment::makeBytes(data)};
return encodeSegments(segs, ecl);
}
QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
int minVersion, int maxVersion, int mask, bool boostEcl) {
if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
throw "Invalid value";
@ -90,14 +90,13 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
throw "Assertion error";
// Increase the error correction level while the data still fits in the current version number
const Ecc *newEcl = &ecl;
for (const Ecc *anEcl : vector<const Ecc*>{&Ecc::MEDIUM, &Ecc::QUARTILE, &Ecc::HIGH}) {
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, *anEcl) * 8)
newEcl = anEcl;
for (Ecc newEcl : vector<Ecc>{Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) {
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
ecl = newEcl;
}
// Create the data bit string by concatenating all segments
size_t dataCapacityBits = getNumDataCodewords(version, *newEcl) * 8;
size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
BitBuffer bb;
for (const QrSegment &seg : segs) {
bb.appendBits(seg.getMode().getModeBits(), 4);
@ -116,11 +115,11 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
throw "Assertion error";
// Create the QR Code symbol
return QrCode(version, *newEcl, bb.getBytes(), mask);
return QrCode(version, ecl, bb.getBytes(), mask);
}
QrCode::QrCode(int ver, const Ecc &ecl, const vector<uint8_t> &dataCodewords, int mask) :
QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int mask) :
// Initialize scalar fields
version(ver),
size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
@ -558,7 +557,7 @@ int QrCode::getNumRawDataModules(int ver) {
}
int QrCode::getNumDataCodewords(int ver, const Ecc &ecl) {
int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
return getNumRawDataModules(ver) / 8

View File

@ -73,7 +73,7 @@ class QrCode final {
* QR Code version is automatically chosen for the output. The ECC level of the result may be higher than
* the ecl argument if it can be done without increasing the version.
*/
public: static QrCode encodeText(const char *text, const Ecc &ecl);
public: static QrCode encodeText(const char *text, Ecc ecl);
/*
@ -82,7 +82,7 @@ class QrCode final {
* bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
*/
public: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, const Ecc &ecl);
public: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);
/*
@ -92,7 +92,7 @@ class QrCode final {
* between modes (such as alphanumeric and binary) to encode text more efficiently.
* This function is considered to be lower level than simply encoding text or binary data.
*/
public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,
int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters
@ -129,7 +129,7 @@ class QrCode final {
* and mask number. This is a cumbersome low-level constructor that should not be invoked directly by the user.
* To go one level up, see the encodeSegments() function.
*/
public: QrCode(int ver, const Ecc &ecl, const std::vector<std::uint8_t> &dataCodewords, int mask);
public: QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int mask);
/*
@ -252,7 +252,7 @@ class QrCode final {
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
// QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table.
private: static int getNumDataCodewords(int ver, const Ecc &ecl);
private: static int getNumDataCodewords(int ver, Ecc ecl);
/*---- Private tables of constants ----*/

View File

@ -58,7 +58,7 @@ int main() {
// Creates a single QR Code, then prints it to the console.
static void doBasicDemo() {
const char *text = "Hello, world!"; // User-supplied text
const QrCode::Ecc &errCorLvl = QrCode::Ecc::LOW; // Error correction level
const QrCode::Ecc errCorLvl = QrCode::Ecc::LOW; // Error correction level
// Make and print the QR Code symbol
const QrCode qr = QrCode::encodeText(text, errCorLvl);