Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Begone realloc #966

Merged
merged 4 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions client/cityrepdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ static const struct city_report_spec base_city_report_specs[] = {
{true, 0, 1, N_("Currently Building"), N_("?Stock/Target:(Have/Need)"),
N_("Currently Building"), nullptr, FUNC_TAG(building)}};

struct city_report_spec *city_report_specs;
std::vector<city_report_spec> city_report_specs;
static int num_creport_cols;

/**
Expand Down Expand Up @@ -828,8 +828,7 @@ void init_city_report_game_data()

num_creport_cols =
ARRAY_SIZE(base_city_report_specs) + specialist_count() + 1;
city_report_specs = static_cast<city_report_spec *>(fc_realloc(
city_report_specs, num_creport_cols * sizeof(*city_report_specs)));
city_report_specs = std::vector<city_report_spec>(num_creport_cols);
p = &city_report_specs[0];

fc_snprintf(sp_explanations, sizeof(sp_explanations), "%s",
Expand Down
2 changes: 1 addition & 1 deletion client/cityrepdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct city_report_spec {
const char *tagname; // for save_options
};

extern struct city_report_spec *city_report_specs;
extern std::vector<city_report_spec> city_report_specs;

int num_city_report_spec();
bool *city_report_spec_show_ptr(int i);
Expand Down
43 changes: 21 additions & 22 deletions client/gui-qt/cityrep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ void city_item_delegate::paint(QPainter *painter,
QString txt;
QFont font;
QPalette palette;
struct city_report_spec *spec;
spec = city_report_specs + index.column();
txt = spec->tagname;
struct city_report_spec spec;
spec = city_report_specs[index.column()];
txt = spec.tagname;
if (txt == QLatin1String("cityname")) {
font.setCapitalization(QFont::SmallCaps);
font.setBold(true);
Expand Down Expand Up @@ -138,16 +138,16 @@ bool city_item::setData(int column, const QVariant &value, int role)
*/
QVariant city_item::data(int column, int role) const
{
struct city_report_spec *spec;
struct city_report_spec spec;

if (role == Qt::UserRole && column == 0) {
return QVariant::fromValue((void *) i_city);
}
if (role != Qt::DisplayRole) {
return QVariant();
}
spec = city_report_specs + column;
QString buf = QStringLiteral("%1").arg(spec->func(i_city, spec->data));
spec = city_report_specs[column];
QString buf = QStringLiteral("%1").arg(spec.func(i_city, spec.data));
return buf.trimmed();
}

Expand Down Expand Up @@ -219,24 +219,23 @@ bool city_model::setData(const QModelIndex &index, const QVariant &value,
QVariant city_model::headerData(int section, Qt::Orientation orientation,
int role) const
{
struct city_report_spec *spec = city_report_specs + section;
struct city_report_spec spec = city_report_specs[section];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: const auto &


if (orientation == Qt::Horizontal && section < NUM_CREPORT_COLS) {
if (role == Qt::DisplayRole) {
QString buf =
QStringLiteral("%1\n%2").arg(spec->title1 ? spec->title1 : "",
spec->title2 ? spec->title2 : "");
QIcon i = hIcon::i()->get(spec->tagname);
QString buf = QStringLiteral("%1\n%2").arg(
spec.title1 ? spec.title1 : "", spec.title2 ? spec.title2 : "");
QIcon i = hIcon::i()->get(spec.tagname);
if (!i.isNull()) { // icon exists for that header
return QString();
}
return buf.trimmed();
}
if (role == Qt::ToolTipRole) {
return QString(spec->explanation);
return QString(spec.explanation);
}
if (role == Qt::DecorationRole) {
QIcon i = hIcon::i()->get(spec->tagname);
QIcon i = hIcon::i()->get(spec.tagname);
if (!i.isNull()) {
return i;
}
Expand All @@ -250,11 +249,11 @@ QVariant city_model::headerData(int section, Qt::Orientation orientation,
*/
QVariant city_model::menu_data(int section) const
{
struct city_report_spec *spec;
struct city_report_spec spec;

if (section < NUM_CREPORT_COLS) {
spec = city_report_specs + section;
return QString(spec->explanation);
spec = city_report_specs[section];
return QString(spec.explanation);
}
return QVariant();
}
Expand All @@ -264,11 +263,11 @@ QVariant city_model::menu_data(int section) const
*/
QVariant city_model::hide_data(int section) const
{
struct city_report_spec *spec;
struct city_report_spec spec;

if (section < NUM_CREPORT_COLS) {
spec = city_report_specs + section;
return spec->show;
spec = city_report_specs[section];
return spec.show;
}
return QVariant();
}
Expand Down Expand Up @@ -1117,16 +1116,16 @@ void city_widget::display_header_menu(const QPoint)
hideshow_column->setAttribute(Qt::WA_DeleteOnClose);
connect(hideshow_column, &QMenu::triggered, this, [=](QAction *act) {
int col;
struct city_report_spec *spec;
struct city_report_spec spec;
if (!act) {
return;
}

col = actions.indexOf(act);
fc_assert_ret(col >= 0);
setColumnHidden(col, !isColumnHidden(col));
spec = city_report_specs + col;
spec->show = !spec->show;
spec = city_report_specs[col];
spec.show = !spec.show;
if (!isColumnHidden(col) && columnWidth(col) <= 5) {
setColumnWidth(col, 100);
}
Expand Down