From c3f200fee459e71d6710ea739b661b182538ad47 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Thu, 9 Feb 2023 19:41:11 +0100 Subject: [PATCH] Set up clang format & tidy and format all the files --- .clang-format | 17 ++++- .vscode/settings.json | 172 +++++++++++++++++++++--------------------- inc/IPrinter.hpp | 4 +- inc/IUsb.hpp | 4 +- inc/P700Driver.hpp | 6 +- inc/P700Printer.hpp | 3 +- src/P700Driver.cpp | 22 +++--- src/Usb.cpp | 14 ++-- src/main.cpp | 4 +- 9 files changed, 127 insertions(+), 119 deletions(-) diff --git a/.clang-format b/.clang-format index 6f7ba0a..fddab64 100644 --- a/.clang-format +++ b/.clang-format @@ -41,14 +41,13 @@ BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon -ColumnLimit: 80 +ColumnLimit: 120 CompactNamespaces: false ContinuationIndentWidth: 4 Cpp11BracedListStyle: true DerivePointerAlignment: false EmptyLineBeforeAccessModifier: LogicalBlock FixNamespaceComments: true -IncludeBlocks: Preserve IndentCaseLabels: true IndentPPDirectives: None IndentWidth: 4 @@ -80,3 +79,17 @@ SpacesInSquareBrackets: false Standard: c++17 TabWidth: 4 UseTab: Never +QualifierAlignment: Left +SortIncludes: true +IncludeBlocks: Regroup +IncludeCategories: + # Classes own header is always priority 0 + # System C/C++ headers in <> with extension. + - Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>' + Priority: 1 + # System Headers in <> without extension. + - Regex: '<([A-Za-z0-9.\Q/-_\E])+>' + Priority: 2 + # c Headers in "" with extension. + - Regex: '"([A-Za-z0-9.\Q/-_\E])+"' + Priority: 3 diff --git a/.vscode/settings.json b/.vscode/settings.json index a54cf27..f79f6ca 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,86 +1,88 @@ { - "C_Cpp.codeAnalysis.clangTidy.args":[ - "-extra-arg=-std=c++17" - ], - "files.associations": { - "cctype": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "array": "cpp", - "atomic": "cpp", - "strstream": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "bitset": "cpp", - "cfenv": "cpp", - "chrono": "cpp", - "codecvt": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdint": "cpp", - "deque": "cpp", - "forward_list": "cpp", - "list": "cpp", - "map": "cpp", - "set": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "unordered_set": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "source_location": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "semaphore": "cpp", - "shared_mutex": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "cinttypes": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "valarray": "cpp", - "variant": "cpp", - "csignal": "cpp", - "any": "cpp", - "regex": "cpp", - "future": "cpp", - "charconv": "cpp", - "*.ipp": "cpp" - } -} \ No newline at end of file + "clangd.arguments": ["-background-index", "-compile-commands-dir=builddir"], + "C_Cpp.codeAnalysis.clangTidy.args": ["-extra-arg=-std=c++17"], + "editor.formatOnType": false, + "editor.formatOnSave": true, + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "strstream": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cfenv": "cpp", + "chrono": "cpp", + "codecvt": "cpp", + "compare": "cpp", + "complex": "cpp", + "concepts": "cpp", + "condition_variable": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "forward_list": "cpp", + "list": "cpp", + "map": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "unordered_set": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "ratio": "cpp", + "source_location": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "mutex": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "semaphore": "cpp", + "shared_mutex": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "cinttypes": "cpp", + "typeindex": "cpp", + "typeinfo": "cpp", + "valarray": "cpp", + "variant": "cpp", + "csignal": "cpp", + "any": "cpp", + "regex": "cpp", + "future": "cpp", + "charconv": "cpp", + "*.ipp": "cpp" + }, + "clang-tidy.buildPath": "builddir/" +} diff --git a/inc/IPrinter.hpp b/inc/IPrinter.hpp index 20773fc..267fe90 100644 --- a/inc/IPrinter.hpp +++ b/inc/IPrinter.hpp @@ -1,9 +1,9 @@ -#include "PrinterTypes.hpp" - #include +#include #include "Bitmap.hpp" +#include "PrinterTypes.hpp" #pragma once diff --git a/inc/IUsb.hpp b/inc/IUsb.hpp index 4a8ab6f..5254b93 100644 --- a/inc/IUsb.hpp +++ b/inc/IUsb.hpp @@ -1,8 +1,8 @@ -#include "UsbTypes.hpp" - #include #include +#include "UsbTypes.hpp" + #pragma once namespace ptprnt::driver { diff --git a/inc/P700Driver.hpp b/inc/P700Driver.hpp index a905c8c..320fb3c 100644 --- a/inc/P700Driver.hpp +++ b/inc/P700Driver.hpp @@ -1,8 +1,7 @@ -#include "IDriver.hpp" - #include #include +#include "IDriver.hpp" #include "Usb.hpp" #pragma once @@ -11,8 +10,7 @@ namespace ptprnt::driver { class P700Driver : public IDriver { public: - P700Driver(std::shared_ptr usbDriver, uint16_t usbDevVendor = 0x04f9, - uint16_t usbDevId = 0x2061); + P700Driver(std::shared_ptr usbDriver, uint16_t usbDevVendor = 0x04f9, uint16_t usbDevId = 0x2061); ~P700Driver() override; driver::info getInfo() override; diff --git a/inc/P700Printer.hpp b/inc/P700Printer.hpp index 86c2466..84a32d2 100644 --- a/inc/P700Printer.hpp +++ b/inc/P700Printer.hpp @@ -1,7 +1,6 @@ -#include "IPrinter.hpp" - #include +#include "IPrinter.hpp" #include "P700Driver.hpp" #pragma once diff --git a/src/P700Driver.cpp b/src/P700Driver.cpp index 647804d..5a8bdc8 100644 --- a/src/P700Driver.cpp +++ b/src/P700Driver.cpp @@ -1,14 +1,13 @@ #include "P700Driver.hpp" +#include + #include #include -#include - namespace ptprnt::driver { -P700Driver::P700Driver(std::shared_ptr usbDriver, uint16_t usbDevVendor, - uint16_t usbProductId) +P700Driver::P700Driver(std::shared_ptr usbDriver, uint16_t usbDevVendor, uint16_t usbProductId) : mUsbDriver{std::move(usbDriver)}, mUsbDev{.vendorId = usbDevVendor, .productId = usbProductId} @@ -31,7 +30,7 @@ driver::info P700Driver::getInfo() { bool P700Driver::open() { auto maybeUsbDev = mUsbDriver->open(mUsbDev); - if(!maybeUsbDev.has_value()) { + if (!maybeUsbDev.has_value()) { spdlog::error("Can't open P700 printer"); return false; } @@ -40,7 +39,7 @@ bool P700Driver::open() { } bool P700Driver::close() { - if(!mUsbDriver->close(mUsbDev)) { + if (!mUsbDriver->close(mUsbDev)) { return false; } return true; @@ -60,15 +59,12 @@ bool P700Driver::init() { // TODO: There is the possibility, that two printers with the same ID are connected // We have to take that into account - auto devIt = - std::find_if(devs.value().begin(), devs.value().end(), [=](auto dev) { - return mUsbDev.vendorId == dev.vendorId && - mUsbDev.productId == dev.productId; - }); + auto devIt = std::find_if(devs.value().begin(), devs.value().end(), [=](auto dev) { + return mUsbDev.vendorId == dev.vendorId && mUsbDev.productId == dev.productId; + }); if (devIt == devs.value().end()) { - spdlog::error("No device with {0:04X}:{1:04X}", mUsbDev.vendorId, - mUsbDev.productId); + spdlog::error("No device with {0:04X}:{1:04X}", mUsbDev.vendorId, mUsbDev.productId); return false; } diff --git a/src/Usb.cpp b/src/Usb.cpp index 9bd4f58..c36de03 100644 --- a/src/Usb.cpp +++ b/src/Usb.cpp @@ -1,6 +1,7 @@ #include "Usb.hpp" #include + #include #include #include @@ -27,8 +28,7 @@ std::optional> Usb::getDevices() { mDevices.clear(); if (0 == (ret = libusb_get_device_list(NULL, &libUsbDevs))) { - spdlog::error("Could not find any USB devices: {}", - libusb_error_name(ret)); + spdlog::error("Could not find any USB devices: {}", libusb_error_name(ret)); return std::nullopt; } @@ -44,8 +44,7 @@ std::optional> Usb::getDevices() { newDev.vendorId = libUsbDesc.idVendor; newDev.productId = libUsbDesc.idProduct; newDev.device = libUsbDev; - newDev.hndl = - nullptr; // handle is only available after we opened the dev + newDev.hndl = nullptr; // handle is only available after we opened the dev mDevices.push_back(newDev); } @@ -58,8 +57,8 @@ std::optional Usb::open(UsbDevice dev) { int ret = 0; if ((ret = libusb_open(dev.device, &libUsbHandle) != 0)) { - spdlog::error("Could not open device {0:04X}:{1:04X}: {2}", - dev.vendorId, dev.productId, libusb_error_name(ret)); + spdlog::error("Could not open device {0:04X}:{1:04X}: {2}", dev.vendorId, dev.productId, + libusb_error_name(ret)); return std::nullopt; } @@ -89,13 +88,14 @@ std::optional Usb::open(UsbDevice dev) { bool Usb::close(UsbDevice dev) { int ret = 0; - if(0 != (ret = libusb_release_interface(dev.hndl, 0))) { + if (0 != (ret = libusb_release_interface(dev.hndl, 0))) { spdlog::error( "Could not close USB device {0:04X}:{1:04X}: " "{2}", dev.vendorId, dev.productId, libusb_error_name(ret)); } + std::cout << "test" << std::endl; libusb_close(dev.hndl); return true; } diff --git a/src/main.cpp b/src/main.cpp index ab5b9b2..01f21c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,12 @@ +#include + #include #include #include "IPrinter.hpp" #include "P700Printer.hpp" #include "Usb.hpp" - #include -#include using namespace ptprnt;