add const

This commit is contained in:
proddy
2026-05-16 15:45:14 +02:00
parent a1f24b38fa
commit 85a12bf581
35 changed files with 384 additions and 370 deletions

View File

@@ -146,7 +146,7 @@ class AnalogSensor {
bool updated_values();
// return back reference to the sensor list, used by other classes
std::vector<Sensor, AllocatorPSRAM<Sensor>> sensors() const {
const std::vector<Sensor, AllocatorPSRAM<Sensor>> & sensors() const {
return sensors_;
}
@@ -178,7 +178,7 @@ class AnalogSensor {
bool get_value_info(JsonObject output, const char * cmd, const int8_t id = -1);
void store_counters();
std::string get_metrics_prometheus();
static std::vector<uint8_t> exclude_types() {
static const std::vector<uint8_t> & exclude_types() {
return exclude_types_;
}

View File

@@ -98,7 +98,7 @@ class Command {
}
};
static std::vector<CmdFunction, AllocatorPSRAM<CmdFunction>> commands() {
static const std::vector<CmdFunction, AllocatorPSRAM<CmdFunction>> & commands() {
return cmdfunctions_;
}

View File

@@ -34,7 +34,7 @@ class EMSdevice {
public:
virtual ~EMSdevice() = default; // destructor of base class must always be virtual because it's a polymorphic class
using process_function_p = std::function<void(std::shared_ptr<const Telegram>)>;
using process_function_p = std::function<void(const std::shared_ptr<const Telegram> &)>;
// device_type defines which derived class to use, e.g. BOILER, THERMOSTAT etc..
EMSdevice(uint8_t device_type, uint8_t device_id, uint8_t product_id, const char * version, const char * default_name, uint8_t flags, uint8_t brand)
@@ -64,6 +64,10 @@ class EMSdevice {
bool has_tags(const int8_t tag) const;
bool has_cmd(const char * cmd, const int8_t id) const;
std::string brand_to_char();
// Same as brand_to_char() but returns a const char* (flash literal or
// custom_brand_.c_str()) without allocating a std::string. Lifetime is
// tied to this EMSdevice instance.
const char * brand_to_cstr() const;
std::string to_string();
std::string to_string_short();
std::string to_string_version();
@@ -125,7 +129,7 @@ class EMSdevice {
custom_name_ = custom_name;
}
std::string custom_name() const {
const std::string & custom_name() const {
return custom_name_;
}
@@ -134,7 +138,7 @@ class EMSdevice {
custom_brand_ = custom_brand;
}
std::string custom_brand() const {
const std::string & custom_brand() const {
return custom_brand_;
}
// set device model
@@ -142,7 +146,7 @@ class EMSdevice {
model_ = model;
}
std::string model() const {
const std::string & model() const {
return model_;
}
@@ -207,14 +211,14 @@ class EMSdevice {
}
}
void has_enumupdate(std::shared_ptr<const Telegram> telegram, uint8_t & value, const uint8_t index, int8_t s = 0) {
void has_enumupdate(const std::shared_ptr<const Telegram> & telegram, uint8_t & value, const uint8_t index, int8_t s = 0) {
if (telegram->read_enumvalue(value, index, s)) {
has_update_ = true;
publish_value((void *)&value);
}
}
void has_enumupdate(std::shared_ptr<const Telegram> telegram, uint8_t & value, const uint8_t index, const std::vector<uint8_t> & maskIn) {
void has_enumupdate(const std::shared_ptr<const Telegram> & telegram, uint8_t & value, const uint8_t index, const std::vector<uint8_t> & maskIn) {
uint8_t val = value < maskIn.size() ? maskIn[value] : EMS_VALUE_UINT8_NOTSET;
if (telegram->read_value(val, index)) {
for (uint8_t i = 0; i < maskIn.size(); i++) {
@@ -229,7 +233,7 @@ class EMSdevice {
}
template <typename Value>
void has_update(std::shared_ptr<const Telegram> telegram, Value & value, const uint8_t index, uint8_t s = 0) {
void has_update(const std::shared_ptr<const Telegram> & telegram, Value & value, const uint8_t index, uint8_t s = 0) {
if (telegram->read_value(value, index, s)) {
has_update_ = true;
publish_value((void *)&value);
@@ -237,7 +241,7 @@ class EMSdevice {
}
template <typename BitValue>
void has_bitupdate(std::shared_ptr<const Telegram> telegram, BitValue & value, const uint8_t index, uint8_t b) {
void has_bitupdate(const std::shared_ptr<const Telegram> & telegram, BitValue & value, const uint8_t index, uint8_t b) {
if (telegram->read_bitvalue(value, index, b)) {
has_update_ = true;
publish_value((void *)&value);
@@ -260,7 +264,7 @@ class EMSdevice {
void getCustomizationEntities(std::vector<std::string> & entity_ids);
void register_telegram_type(const uint16_t telegram_type_id, const char * telegram_type_name, bool fetch, const process_function_p cb, uint8_t length = 0);
bool handle_telegram(std::shared_ptr<const Telegram> telegram);
bool handle_telegram(const std::shared_ptr<const Telegram> & telegram);
std::string get_value_uom(const std::string & shortname) const;
bool get_value_info(JsonObject root, const char * cmd, const int8_t id);
@@ -359,7 +363,7 @@ class EMSdevice {
void publish_value(void * value_p) const;
void publish_all_values();
void mqtt_ha_entity_config_create();
const char * telegram_type_name(std::shared_ptr<const Telegram> telegram);
const char * telegram_type_name(const std::shared_ptr<const Telegram> & telegram);
void fetch_values();
void toggle_fetch(uint16_t telegram_id, bool toggle);
bool is_fetch(uint16_t telegram_id, uint8_t len = 0) const;
@@ -518,13 +522,17 @@ class EMSdevice {
uint8_t count_entities_fav();
bool has_entities() const;
// void reserve_device_values(uint8_t elements) {
// devicevalues_.reserve(elements);
// }
// Pre-allocate vector capacity to avoid realloc storms during device
// construction. Realloc here is especially expensive because each entry
// contains a std::function (heap-allocated functor) and DeviceValue
// (with std::string member), so growing copies a lot.
void reserve_device_values(uint16_t elements) {
devicevalues_.reserve(elements);
}
// void reserve_telegram_functions(uint8_t elements) {
// telegram_functions_.reserve(elements);
// }
void reserve_telegram_functions(uint8_t elements) {
telegram_functions_.reserve(elements);
}
#if defined(EMSESP_STANDALONE)
struct TelegramFunctionDump {

View File

@@ -701,7 +701,7 @@ void EMSESP::publish_sensor_values(const bool time, const bool force) {
}
// MQTT publish a telegram as raw data to the topic 'response'
void EMSESP::publish_response(std::shared_ptr<const Telegram> telegram) {
void EMSESP::publish_response(const std::shared_ptr<const Telegram> & telegram) {
static char * buffer = nullptr;
static uint8_t offset = 0;
static uint16_t type = 0;
@@ -815,7 +815,7 @@ std::string EMSESP::device_tostring(const uint8_t device_id) {
// create a pretty print telegram as a text string
// e.g. Boiler(0x08) -> Me(0x0B), Version(0x02), data: 7B 06 01 00 00 00 00 00 00 04 (offset 1)
std::string EMSESP::pretty_telegram(std::shared_ptr<const Telegram> telegram) {
std::string EMSESP::pretty_telegram(const std::shared_ptr<const Telegram> & telegram) {
uint8_t src = telegram->src & 0x7F;
uint8_t dest = telegram->dest & 0x7F;
uint8_t offset = telegram->offset;
@@ -975,7 +975,7 @@ std::string EMSESP::pretty_telegram(std::shared_ptr<const Telegram> telegram) {
* e.g. in example above 1st byte = x0B = b1011 so we have deviceIDs 0x08, 0x09, 0x011
* and 2nd byte = x80 = b1000 b0000 = deviceID 0x17
*/
void EMSESP::process_UBADevices(std::shared_ptr<const Telegram> telegram) {
void EMSESP::process_UBADevices(const std::shared_ptr<const Telegram> & telegram) {
// exit it length is incorrect (must be 13 or 15 bytes long)
if (telegram->message_length > 15) {
return;
@@ -1001,7 +1001,7 @@ void EMSESP::process_UBADevices(std::shared_ptr<const Telegram> telegram) {
}
// read deviceName from telegram 0x01 offset 27 and set it to custom name
void EMSESP::process_deviceName(std::shared_ptr<const Telegram> telegram) {
void EMSESP::process_deviceName(const std::shared_ptr<const Telegram> & telegram) {
// exit if only part of name fields
if (telegram->offset > 27 || (telegram->offset + telegram->message_length) < 29) {
return;
@@ -1029,7 +1029,7 @@ void EMSESP::process_deviceName(std::shared_ptr<const Telegram> telegram) {
// process the Version telegram (type 0x02), which is a common type
// e.g. 09 0B 02 00 PP V1 V2
void EMSESP::process_version(std::shared_ptr<const Telegram> telegram) {
void EMSESP::process_version(const std::shared_ptr<const Telegram> & telegram) {
// check for valid telegram, just in case
if (telegram->offset != 0) {
return;
@@ -1087,7 +1087,7 @@ void EMSESP::process_version(std::shared_ptr<const Telegram> telegram) {
// but only process if the telegram is sent to us or it's a broadcast (dest=0x00=all)
// We also check for common telegram types, like the Version(0x02)
// returns false if there are none found
bool EMSESP::process_telegram(std::shared_ptr<const Telegram> telegram) {
bool EMSESP::process_telegram(const std::shared_ptr<const Telegram> & telegram) {
// if watching or reading...
if ((telegram->type_id == read_id_ || telegram->type_id == response_id_) && (telegram->dest == EMSbus::ems_bus_id())) {
if (telegram->type_id == response_id_) {

View File

@@ -146,11 +146,11 @@ class Mqtt {
mqtt_enabled_ = mqtt_enabled;
}
static std::string base() {
static const std::string & base() {
return mqtt_base_;
}
static std::string basename() {
static const std::string & basename() {
return mqtt_basename_;
}
@@ -227,7 +227,7 @@ class Mqtt {
ha_enabled_ = ha_enabled;
}
static std::string get_response() {
static const std::string & get_response() {
return lastresponse_;
}

View File

@@ -99,7 +99,7 @@ class TemperatureSensor {
std::string get_metrics_prometheus();
// return back reference to the sensor list, used by other classes
std::vector<Sensor, AllocatorPSRAM<Sensor>> sensors() const {
const std::vector<Sensor, AllocatorPSRAM<Sensor>> & sensors() const {
return sensors_;
}