mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-26 23:13:13 +02:00
Refactor setButtonLabels() to use std::vector for safer memory management
- Replaced manual dynamic memory allocation with std::vector in setButtonLabels(const button_label*...) and setButtonLabels(const std::vector<button_label_cc>&...). - Reserved vector capacity upfront to match label_count, optimizing memory usage. - Improved safety and readability by removing manual delete[] calls and handling memory cleanup through vector's automatic management.
This commit is contained in:
@@ -319,39 +319,36 @@ void CComponentsFooter::setButtonLabels(const struct button_label_cc * const con
|
|||||||
void CComponentsFooter::setButtonLabels(const struct button_label * const content, const size_t& label_count, const int& chain_width, const int& label_width)
|
void CComponentsFooter::setButtonLabels(const struct button_label * const content, const size_t& label_count, const int& chain_width, const int& label_width)
|
||||||
{
|
{
|
||||||
//conversion for compatibility with older paintButtons() methode, find in /gui/widget/buttons.h
|
//conversion for compatibility with older paintButtons() methode, find in /gui/widget/buttons.h
|
||||||
button_label_cc *buttons = new button_label_cc[label_count];
|
std::vector<button_label_cc> buttons;
|
||||||
for (size_t i = 0; i< label_count; i++){
|
buttons.reserve(label_count);
|
||||||
buttons[i].button = content[i].button;
|
|
||||||
buttons[i].locale = content[i].locale;
|
for (size_t i = 0; i < label_count; i++) {
|
||||||
|
button_label_cc button;
|
||||||
|
button.button = content[i].button;
|
||||||
|
button.locale = content[i].locale;
|
||||||
//NOTE: here are used default values, because old button label struct don't know about this,
|
//NOTE: here are used default values, because old button label struct don't know about this,
|
||||||
//if it possible, don't use this methode!
|
//if it possible, don't use this methode!
|
||||||
buttons[i].directKeys.push_back(CRCInput::RC_nokey);
|
button.directKeys.push_back(CRCInput::RC_nokey);
|
||||||
buttons[i].btn_result = -1;
|
button.btn_result = -1;
|
||||||
buttons[i].btn_alias = -1;
|
button.btn_alias = -1;
|
||||||
|
buttons.emplace_back(std::move(button));
|
||||||
}
|
}
|
||||||
setButtonLabels(buttons, label_count, chain_width, label_width);
|
|
||||||
delete[] buttons;
|
setButtonLabels(buttons.data(), buttons.size(), chain_width, label_width);
|
||||||
buttons = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CComponentsFooter::setButtonLabels(const vector<button_label_cc> &v_content, const int& chain_width, const int& label_width)
|
void CComponentsFooter::setButtonLabels(const std::vector<button_label_cc> &v_content, const int &chain_width, const int &label_width)
|
||||||
{
|
{
|
||||||
size_t label_count = v_content.size();
|
size_t label_count = v_content.size();
|
||||||
button_label_cc *buttons = new button_label_cc[label_count];
|
std::vector<button_label_cc> buttons;
|
||||||
|
buttons.reserve(label_count);
|
||||||
|
|
||||||
for (size_t i= 0; i< label_count; i++){
|
for (size_t i = 0; i < label_count; i++) {
|
||||||
buttons[i].button = v_content[i].button;
|
button_label_cc button = v_content[i]; // Kopiere alle Felder
|
||||||
buttons[i].text = v_content[i].text;
|
buttons.emplace_back(std::move(button));
|
||||||
buttons[i].locale = v_content[i].locale;
|
|
||||||
for (size_t j= 0; j< v_content[i].directKeys.size(); j++)
|
|
||||||
buttons[i].directKeys.push_back(v_content[i].directKeys[j]);
|
|
||||||
buttons[i].btn_result = v_content[i].btn_result;
|
|
||||||
buttons[i].btn_alias = v_content[i].btn_alias;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setButtonLabels(buttons, label_count, chain_width, label_width);
|
setButtonLabels(buttons.data(), buttons.size(), chain_width, label_width);
|
||||||
delete[] buttons;
|
|
||||||
buttons = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CComponentsFooter::setButtonLabel( const char *button_icon,
|
void CComponentsFooter::setButtonLabel( const char *button_icon,
|
||||||
|
@@ -91,7 +91,7 @@ class CComponentsFooter : public CComponentsHeader, public CCButtonSelect
|
|||||||
///add button labels with string label type as content, count as size_t, chain_width as int, label width as int
|
///add button labels with string label type as content, count as size_t, chain_width as int, label width as int
|
||||||
void setButtonLabels(const struct button_label_cc * const content, const size_t& label_count, const int& chain_width = 0, const int& label_width = 0);
|
void setButtonLabels(const struct button_label_cc * const content, const size_t& label_count, const int& chain_width = 0, const int& label_width = 0);
|
||||||
///add button labels with string label type as content, parameter 1 as vector, chain_width as int, label width as int
|
///add button labels with string label type as content, parameter 1 as vector, chain_width as int, label width as int
|
||||||
void setButtonLabels(const std::vector<button_label_cc> &v_content, const int& chain_width, const int& label_width);
|
void setButtonLabels(const std::vector<button_label_cc> &v_content, const int &chain_width, const int &label_width);
|
||||||
|
|
||||||
///enable/disable button frame in icon color, predefined for red, green, yellow and blue
|
///enable/disable button frame in icon color, predefined for red, green, yellow and blue
|
||||||
void enableButtonFrameColor(bool enable = true){btn_auto_frame_col = enable;}
|
void enableButtonFrameColor(bool enable = true){btn_auto_frame_col = enable;}
|
||||||
|
Reference in New Issue
Block a user