From 6998b2830421574f6eff8374c1f364fbb60d0442 Mon Sep 17 00:00:00 2001 From: Norbert Takacs Date: Fri, 4 Jul 2025 07:33:48 +0200 Subject: [PATCH] Use constexp instead of simple const or macro definitions Signed-off-by: Norbert Takacs --- src/core/GenericDisplay.cpp | 2 +- src/core/GenericDisplay.h | 8 +++--- .../ArduinoHomeCockpit.cpp | 4 +-- src/devices/fip/FIPFonts.h | 12 ++++----- src/devices/fip/FIPImageLayer.cpp | 2 +- src/devices/fip/FIPImageLayer.h | 3 +++ src/devices/fip/FIPScreen.h | 6 ++--- src/devices/fip/FIPTextLayer.h | 4 +-- src/devices/saitek-multi/SaitekMultiPanel.cpp | 4 +-- src/devices/saitek-radio/SaitekRadioPanel.cpp | 4 +-- .../saitek-switch/SaitekSwitchPanel.cpp | 4 +-- src/devices/trc-1000/TRC1000Audio.cpp | 21 +++++++--------- src/devices/trc-1000/TRC1000PFD.cpp | 25 ++++++++----------- 13 files changed, 48 insertions(+), 51 deletions(-) diff --git a/src/core/GenericDisplay.cpp b/src/core/GenericDisplay.cpp index b37a3d5..b75aa37 100644 --- a/src/core/GenericDisplay.cpp +++ b/src/core/GenericDisplay.cpp @@ -10,7 +10,7 @@ #include "LuaHelper.h" #include "Logger.h" -const double GenericDisplay::MAX_VALUE = 10000000; +// MAX_VALUE now defined as constexpr in header GenericDisplay::GenericDisplay(bool _use_bcd) { diff --git a/src/core/GenericDisplay.h b/src/core/GenericDisplay.h index 3cd1610..314b9c6 100644 --- a/src/core/GenericDisplay.h +++ b/src/core/GenericDisplay.h @@ -21,7 +21,7 @@ class GenericDisplay GenericDisplay(bool _use_bcd); // bcd: binary encoded decimal GenericDisplay(GenericDisplay* other); - static const double MAX_VALUE; + static constexpr double MAX_VALUE = 10000000.0; void set_nr_bytes(int _nr_of_bytes); // called from UsbHidDevice worker thread @@ -54,9 +54,9 @@ class GenericDisplay double const_value; int nr_of_bytes; private: - const unsigned char BLANK_CHAR = 0xFF; - const unsigned char ZERO_CHAR = 0x00; - const unsigned char PERIOD_CHAR = 0xD0; + static constexpr unsigned char BLANK_CHAR = 0xFF; + static constexpr unsigned char ZERO_CHAR = 0x00; + static constexpr unsigned char PERIOD_CHAR = 0xD0; int dataref_index; bool get_decimal_components(int number, unsigned char* buffer, int _minimum_number_of_digits, int _dot_position); diff --git a/src/devices/arduino-homecockpit/ArduinoHomeCockpit.cpp b/src/devices/arduino-homecockpit/ArduinoHomeCockpit.cpp index 8c9a7e0..0b463c2 100644 --- a/src/devices/arduino-homecockpit/ArduinoHomeCockpit.cpp +++ b/src/devices/arduino-homecockpit/ArduinoHomeCockpit.cpp @@ -15,8 +15,8 @@ #include "core/UsbHidDevice.h" #include "core/Logger.h" -#define WRITE_BUFFER_SIZE 64 -#define READ_BUFFER_SIZE 9 +constexpr int WRITE_BUFFER_SIZE = 64; +constexpr int READ_BUFFER_SIZE = 9; std::filesystem::path get_plugin_path(); diff --git a/src/devices/fip/FIPFonts.h b/src/devices/fip/FIPFonts.h index 0ce5f84..abf97e8 100644 --- a/src/devices/fip/FIPFonts.h +++ b/src/devices/fip/FIPFonts.h @@ -114,9 +114,9 @@ void init_fip_fonts(){ fip_font_positions[125] = Font(174,34,5,16); fip_font_positions[126] = Font(77,17,8,16); } -const int FIP_FONT_FILE_WIDTH=256; -const int FIP_FONT_FILE_HEIGHT=256; -const int FIP_FONT_HEIGHT=16; -const int FIP_MAX_FONT_WIDTH=13; -const int FIP_MAX_FONT_ASCII_ID=126; -const int FIP_MIN_FONT_ASCII_ID=32; +constexpr int FIP_FONT_FILE_WIDTH = 256; +constexpr int FIP_FONT_FILE_HEIGHT = 256; +constexpr int FIP_FONT_HEIGHT = 16; +constexpr int FIP_MAX_FONT_WIDTH = 13; +constexpr int FIP_MAX_FONT_ASCII_ID = 126; +constexpr int FIP_MIN_FONT_ASCII_ID = 32; diff --git a/src/devices/fip/FIPImageLayer.cpp b/src/devices/fip/FIPImageLayer.cpp index 7c4a1bb..fdddcb9 100644 --- a/src/devices/fip/FIPImageLayer.cpp +++ b/src/devices/fip/FIPImageLayer.cpp @@ -16,7 +16,7 @@ bool ImageBuffer::load_from_bmp_file(std::string file_name) file.read((char*)&bmp_header, sizeof(bmp_header)); - if (bmp_header.type != 0x4d42) // the BMP magic number + if (bmp_header.type != BMP_MAGIC_NUMBER) { Logger(logERROR) << file_name << " bad magic number in BMP header: " << bmp_header.type << std::endl; return false; diff --git a/src/devices/fip/FIPImageLayer.h b/src/devices/fip/FIPImageLayer.h index 8cf547b..edf183b 100644 --- a/src/devices/fip/FIPImageLayer.h +++ b/src/devices/fip/FIPImageLayer.h @@ -9,6 +9,9 @@ #include #include "FIPLayer.h" +// BMP file format constants +constexpr uint16_t BMP_MAGIC_NUMBER = 0x4d42; + #pragma pack(push) #pragma pack(1) typedef struct { diff --git a/src/devices/fip/FIPScreen.h b/src/devices/fip/FIPScreen.h index 72197bf..73aab32 100644 --- a/src/devices/fip/FIPScreen.h +++ b/src/devices/fip/FIPScreen.h @@ -14,9 +14,9 @@ class FIPScreen : public GenericScreen { private: std::map pages; - const int SCREEN_WIDTH = 320; - const int SCREEN_HEIGHT = 240; - const int BIT_PER_PIXEL = 24; + static constexpr int SCREEN_WIDTH = 320; + static constexpr int SCREEN_HEIGHT = 240; + static constexpr int BIT_PER_PIXEL = 24; std::mutex guard; public: FIPScreen(); diff --git a/src/devices/fip/FIPTextLayer.h b/src/devices/fip/FIPTextLayer.h index 28eec6f..8656307 100644 --- a/src/devices/fip/FIPTextLayer.h +++ b/src/devices/fip/FIPTextLayer.h @@ -11,8 +11,8 @@ class FIPTextLayer : public FIPImageLayer private: std::string text; ImageBuffer font_image_buffer; - const int MAX_CHAR_COUNT = 32; - const int CHAR_SPACE = 2; + static constexpr int MAX_CHAR_COUNT = 32; + static constexpr int CHAR_SPACE = 2; public: FIPTextLayer(); FIPTextLayer(FIPTextLayer* other); diff --git a/src/devices/saitek-multi/SaitekMultiPanel.cpp b/src/devices/saitek-multi/SaitekMultiPanel.cpp index 6b68daf..7dbaab1 100644 --- a/src/devices/saitek-multi/SaitekMultiPanel.cpp +++ b/src/devices/saitek-multi/SaitekMultiPanel.cpp @@ -10,8 +10,8 @@ #include "saitek-multi/SaitekMultiPanel.h" #include "core/Logger.h" -#define WRITE_BUFFER_SIZE 13 -#define READ_BUFFER_SIZE 4 +constexpr int WRITE_BUFFER_SIZE = 13; +constexpr int READ_BUFFER_SIZE = 4; SaitekMultiPanel::SaitekMultiPanel(ClassConfiguration& config) :UsbHidDevice(config, READ_BUFFER_SIZE, WRITE_BUFFER_SIZE) { diff --git a/src/devices/saitek-radio/SaitekRadioPanel.cpp b/src/devices/saitek-radio/SaitekRadioPanel.cpp index a460bb2..83430f6 100644 --- a/src/devices/saitek-radio/SaitekRadioPanel.cpp +++ b/src/devices/saitek-radio/SaitekRadioPanel.cpp @@ -11,8 +11,8 @@ #include "saitek-radio/SaitekRadioPanel.h" #include "core/Logger.h" -#define WRITE_BUFFER_SIZE 23 -#define READ_BUFFER_SIZE 5 +constexpr int WRITE_BUFFER_SIZE = 23; +constexpr int READ_BUFFER_SIZE = 5; SaitekRadioPanel::SaitekRadioPanel(ClassConfiguration& config) :UsbHidDevice(config, READ_BUFFER_SIZE, WRITE_BUFFER_SIZE) { diff --git a/src/devices/saitek-switch/SaitekSwitchPanel.cpp b/src/devices/saitek-switch/SaitekSwitchPanel.cpp index eacd297..806d918 100644 --- a/src/devices/saitek-switch/SaitekSwitchPanel.cpp +++ b/src/devices/saitek-switch/SaitekSwitchPanel.cpp @@ -10,8 +10,8 @@ #include "saitek-switch/SaitekSwitchPanel.h" #include "core/Logger.h" -#define WRITE_BUFFER_SIZE 2 -#define READ_BUFFER_SIZE 4 +constexpr int WRITE_BUFFER_SIZE = 2; +constexpr int READ_BUFFER_SIZE = 4; SaitekSwitchPanel::SaitekSwitchPanel(ClassConfiguration& config) :UsbHidDevice(config, READ_BUFFER_SIZE, WRITE_BUFFER_SIZE) { diff --git a/src/devices/trc-1000/TRC1000Audio.cpp b/src/devices/trc-1000/TRC1000Audio.cpp index c9c86b3..b6ef9d3 100644 --- a/src/devices/trc-1000/TRC1000Audio.cpp +++ b/src/devices/trc-1000/TRC1000Audio.cpp @@ -11,18 +11,15 @@ #include "trc-1000/TRC1000Audio.h" #include "core/UsbHidDevice.h" #include "core/Logger.h" - -#define WRITE_BUFFER_SIZE (8+1) // +1 for the hid report id at position 0 -#define READ_BUFFER_SIZE 8 - -#define CMD_REQUEST_BUTTON_SATE 0x42 -#define CMD_REQUEST_ENCODER_0 0x41 - -#define RESPONSE_BUTTON_STATE 0xc2 -#define RESPONSE_ENCODER_0 0xc1 - -#define BUTTON_BUFFER_OFFSET 0 -#define ENCODER_0_BUFFER_OFFSET 5 + +constexpr int WRITE_BUFFER_SIZE = 8 + 1; // +1 for the hid report id at position 0 +constexpr int READ_BUFFER_SIZE = 8; +constexpr uint8_t CMD_REQUEST_BUTTON_SATE = 0x42; +constexpr uint8_t CMD_REQUEST_ENCODER_0 = 0x41; +constexpr uint8_t RESPONSE_BUTTON_STATE = 0xc2; +constexpr uint8_t RESPONSE_ENCODER_0 = 0xc1; +constexpr int BUTTON_BUFFER_OFFSET = 0; +constexpr int ENCODER_0_BUFFER_OFFSET = 5; TRC1000Audio::TRC1000Audio(ClassConfiguration& config) :TRC1000(config, READ_BUFFER_SIZE, WRITE_BUFFER_SIZE) { diff --git a/src/devices/trc-1000/TRC1000PFD.cpp b/src/devices/trc-1000/TRC1000PFD.cpp index acb0496..5045783 100644 --- a/src/devices/trc-1000/TRC1000PFD.cpp +++ b/src/devices/trc-1000/TRC1000PFD.cpp @@ -11,20 +11,17 @@ #include "core/UsbHidDevice.h" #include "core/Logger.h" -#define WRITE_BUFFER_SIZE (8+1) // +1 for the hid report id at position 0 -#define READ_BUFFER_SIZE 23 - -#define CMD_REQUEST_BUTTON_SATE 0x41 -#define CMD_REQUEST_ENCODER_0 0x42 -#define CMD_REQUEST_ENCODER_1 0x43 - -#define RESPONSE_BUTTON_STATE 0xc1 -#define RESPONSE_ENCODER_0 0xc2 -#define RESPONSE_ENCODER_1 0xc3 - -#define BUTTON_BUFFER_OFFSET 0 -#define ENCODER_0_BUFFER_OFFSET 7 -#define ENCODER_1_BUFFER_OFFSET 15 +constexpr int WRITE_BUFFER_SIZE = 8 + 1; // +1 for the hid report id at position 0 +constexpr int READ_BUFFER_SIZE = 23; +constexpr uint8_t CMD_REQUEST_BUTTON_SATE = 0x41; +constexpr uint8_t CMD_REQUEST_ENCODER_0 = 0x42; +constexpr uint8_t CMD_REQUEST_ENCODER_1 = 0x43; +constexpr uint8_t RESPONSE_BUTTON_STATE = 0xc1; +constexpr uint8_t RESPONSE_ENCODER_0 = 0xc2; +constexpr uint8_t RESPONSE_ENCODER_1 = 0xc3; +constexpr int BUTTON_BUFFER_OFFSET = 0; +constexpr int ENCODER_0_BUFFER_OFFSET = 7; +constexpr int ENCODER_1_BUFFER_OFFSET = 15; TRC1000PFD::TRC1000PFD(ClassConfiguration& config) :TRC1000(config, READ_BUFFER_SIZE, WRITE_BUFFER_SIZE) {