code quality: more code cleanup

* removed unnecessary enum class `massivedynamic::Type`
* removed some not needed newlines
* cleaned up error handling by utilizing asserts more
pull/1/head
Michael Ochmann 2 years ago
parent 6f3827427b
commit 576bdf8430
  1. 10
      main.cpp
  2. 31
      src/QR.cpp
  3. 9
      src/QR.hpp
  4. 2
      src/renderers/ConsoleRenderer.hpp

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "qrcodegen.hpp"
#include "QR.hpp" #include "QR.hpp"
#define UNUSED(var) (void) var; #define UNUSED(var) (void) var;
@ -72,7 +73,7 @@ int main(int argc, char* argv[]) {
size_t segmentSize = 0; size_t segmentSize = 0;
bool fromStdin = true; bool fromStdin = true;
bool anyParameterSet = false; bool anyParameterSet = false;
massivedynamic::Type type = massivedynamic::Type::MEDIUM; qrcodegen::QrCode::Ecc type = qrcodegen::QrCode::Ecc::LOW;
massivedynamic::Format format = massivedynamic::Format::CONSOLE; massivedynamic::Format format = massivedynamic::Format::CONSOLE;
for(;;) { for(;;) {
@ -98,12 +99,13 @@ int main(int argc, char* argv[]) {
case 't': { case 't': {
std::string value = optarg; std::string value = optarg;
if (value == "small") if (value == "small")
type = massivedynamic::Type::SMALL; type = qrcodegen::QrCode::Ecc::LOW;
else if (value == "medium") else if (value == "medium")
type = massivedynamic::Type::MEDIUM; type = qrcodegen::QrCode::Ecc::MEDIUM;
else if (value == "large") else if (value == "large")
type = massivedynamic::Type::LARGE; type = qrcodegen::QrCode::Ecc::HIGH;
else { else {
std::cerr << "ERROR: type (-t, --type) has to be one of 'small', 'medium' or 'large'" << std::endl;
printHelp(); printHelp();
return 1; return 1;
} }

@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <assert.h>
#include "QR.hpp" #include "QR.hpp"
#include "qrcodegen.hpp"
#include "renderers/ConsoleRenderer.hpp" #include "renderers/ConsoleRenderer.hpp"
#include "renderers/PNGRenderer.hpp" #include "renderers/PNGRenderer.hpp"
@ -12,24 +12,8 @@
namespace massivedynamic { namespace massivedynamic {
QR::QR(const std::string& data, std::string outputFile, size_t size, Type type) : outputFile(std::move(outputFile)) { QR::QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type) : outputFile(std::move(outputFile)) {
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.c_str(), type);
qrcodegen::QrCode::Ecc errorCorrectionLevel = qrcodegen::QrCode::Ecc::HIGH;
switch(type) {
case Type::SMALL:
errorCorrectionLevel = qrcodegen::QrCode::Ecc::LOW;
break;
case Type::MEDIUM:
errorCorrectionLevel = qrcodegen::QrCode::Ecc::MEDIUM;
break;
default:
case Type::LARGE:
errorCorrectionLevel = qrcodegen::QrCode::Ecc::HIGH;
break;
}
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.c_str(), errorCorrectionLevel);
static_assert(FormatLength == 5, "exhaustive formats: did you miss to add something here?"); static_assert(FormatLength == 5, "exhaustive formats: did you miss to add something here?");
this->renderers.insert({Format::CONSOLE, std::make_unique<ConsoleRenderer>(this->pixels, qr.getSize(), size)}); this->renderers.insert({Format::CONSOLE, std::make_unique<ConsoleRenderer>(this->pixels, qr.getSize(), size)});
@ -38,8 +22,8 @@ QR::QR(const std::string& data, std::string outputFile, size_t size, Type type)
this->renderers.insert({Format::JPG, std::make_unique<JPGRenderer>(this->pixels, qr.getSize(), size)}); this->renderers.insert({Format::JPG, std::make_unique<JPGRenderer>(this->pixels, qr.getSize(), size)});
this->renderers.insert({Format::BMP, std::make_unique<BMPRenderer>(this->pixels, qr.getSize(), size)}); this->renderers.insert({Format::BMP, std::make_unique<BMPRenderer>(this->pixels, qr.getSize(), size)});
// this is inherently stupid, but "qrcodegen" does not give access to the // this is inherently stupid, but "qrcodegen::QrCode" does not give access to the
// `segments` vector // `segments` vector member and the class itself is marked final.
for (int y = 0; y < qr.getSize(); y++) { for (int y = 0; y < qr.getSize(); y++) {
for (int x = 0; x < qr.getSize(); x++) for (int x = 0; x < qr.getSize(); x++)
this->pixels.push_back(qr.getModule(x, y)); this->pixels.push_back(qr.getModule(x, y));
@ -48,10 +32,7 @@ QR::QR(const std::string& data, std::string outputFile, size_t size, Type type)
} }
void QR::render(Format format) { void QR::render(Format format) {
if (this->renderers.find(format) == this->renderers.end()) { assert(this->renderers.find(format) != this->renderers.end() && "ERROR: a non-valid renderer has been selected");
std::cerr << "ERROR: a non-valid renderer has been selected" << std::endl;
exit(1);
}
this->renderers.at(format)->render(this->outputFile); this->renderers.at(format)->render(this->outputFile);
} }

@ -11,12 +11,6 @@
namespace massivedynamic { namespace massivedynamic {
typedef uint32_t Color; typedef uint32_t Color;
enum class Type {
SMALL,
MEDIUM,
LARGE
};
enum class Format { enum class Format {
CONSOLE, CONSOLE,
SVG, SVG,
@ -27,14 +21,13 @@ namespace massivedynamic {
}; };
constexpr size_t FormatLength = static_cast<size_t>(Format::END); constexpr size_t FormatLength = static_cast<size_t>(Format::END);
class QR { class QR {
private: private:
std::string outputFile; std::string outputFile;
std::vector<bool> pixels; std::vector<bool> pixels;
std::unordered_map<Format, std::unique_ptr<Renderer>> renderers; std::unordered_map<Format, std::unique_ptr<Renderer>> renderers;
public: public:
QR(const std::string& data, std::string outputFile, size_t size, Type type); QR(const std::string& data, std::string outputFile, size_t size, qrcodegen::QrCode::Ecc type);
void render(Format format); void render(Format format);
}; };

@ -21,7 +21,7 @@ class ConsoleRenderer : public Renderer {
std::cout << '\n'; std::cout << '\n';
} }
std::cout << std::string(border, '\n'); std::cout << std::string(border, '\n');
std::cout << std::endl; std::cout.flush();
} }
}; };

Loading…
Cancel
Save