Skip to content

Commit

Permalink
Redefine MeterModeId to uint8_t and use it throughout
Browse files Browse the repository at this point in the history
Change the `mode` and `defaultMode` members of Meter objects, `modes`
array of MeterColumnSetting object, and `mode` parameters of
Meter_setMode() and MeterClass.updateMode() to use the type
`MeterModeId`. Redefine the type `MeterModeId` to uint8_t from an enum.

Signed-off-by: Kang-Che Sung <[email protected]>
  • Loading branch information
Explorer09 committed Apr 16, 2024
1 parent 296b9e2 commit 6b81659
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 22 deletions.
10 changes: 5 additions & 5 deletions CPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ static void CPUMeterCommonInit(Meter* this) {
}
}

static void CPUMeterCommonUpdateMode(Meter* this, int mode, int ncol) {
static void CPUMeterCommonUpdateMode(Meter* this, MeterModeId mode, int ncol) {
CPUMeterData* data = this->meterData;
Meter** meters = data->meters;
this->mode = mode;
Expand All @@ -276,19 +276,19 @@ static void AllCPUsMeter_done(Meter* this) {
free(data);
}

static void SingleColCPUsMeter_updateMode(Meter* this, int mode) {
static void SingleColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 1);
}

static void DualColCPUsMeter_updateMode(Meter* this, int mode) {
static void DualColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 2);
}

static void QuadColCPUsMeter_updateMode(Meter* this, int mode) {
static void QuadColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 4);
}

static void OctoColCPUsMeter_updateMode(Meter* this, int mode) {
static void OctoColCPUsMeter_updateMode(Meter* this, MeterModeId mode) {
CPUMeterCommonUpdateMode(this, mode, 8);
}

Expand Down
4 changes: 2 additions & 2 deletions Header.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void Header_writeBackToSettings(const Header* this) {
const Vector* vec = this->columns[col];
int len = Vector_size(vec);

colSettings->names = len ? xCalloc(len + 1, sizeof(char*)) : NULL;
colSettings->modes = len ? xCalloc(len, sizeof(int)) : NULL;
colSettings->names = len ? xCalloc(len + 1, sizeof(*colSettings->names)) : NULL;
colSettings->modes = len ? xCalloc(len, sizeof(*colSetting->modes)) : NULL;
colSettings->len = len;

for (int i = 0; i < len; i++) {
Expand Down
2 changes: 1 addition & 1 deletion MemorySwapMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void MemorySwapMeter_init(Meter* this) {
}
}

static void MemorySwapMeter_updateMode(Meter* this, int mode) {
static void MemorySwapMeter_updateMode(Meter* this, MeterModeId mode) {
MemorySwapMeterData* data = this->meterData;

this->mode = mode;
Expand Down
2 changes: 1 addition & 1 deletion Meter.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static inline void Meter_displayBuffer(const Meter* this, RichString* out) {
}
}

void Meter_setMode(Meter* this, int modeIndex) {
void Meter_setMode(Meter* this, MeterModeId modeIndex) {
if (modeIndex > 0 && modeIndex == this->mode) {
return;
}
Expand Down
14 changes: 8 additions & 6 deletions Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ struct Machine_; // IWYU pragma: keep
struct Meter_;
typedef struct Meter_ Meter;

typedef uint8_t MeterModeId;

typedef void(*Meter_Init)(Meter*);
typedef void(*Meter_Done)(Meter*);
typedef void(*Meter_UpdateMode)(Meter*, int);
typedef void(*Meter_UpdateMode)(Meter*, MeterModeId);
typedef void(*Meter_UpdateValues)(Meter*);
typedef void(*Meter_Draw)(Meter*, int, int, int);
typedef const char* (*Meter_GetCaption)(const Meter*);
Expand All @@ -64,7 +66,7 @@ typedef struct MeterClass_ {
const Meter_Draw draw;
const Meter_GetCaption getCaption;
const Meter_GetUiName getUiName;
const int defaultMode;
const MeterModeId defaultMode;
const double total;
const int* const attributes;
const char* const name; /* internal name of the meter, must not contain any space */
Expand Down Expand Up @@ -105,7 +107,7 @@ struct Meter_ {
const struct Machine_* host;

char* caption;
int mode;
MeterModeId mode;
unsigned int param;
GraphData drawData;
int h;
Expand All @@ -124,14 +126,14 @@ typedef struct MeterMode_ {
int h;
} MeterMode;

typedef enum {
enum MeterModeId_ {
/* Meter mode 0 is reserved */
BAR_METERMODE = 1,
TEXT_METERMODE,
GRAPH_METERMODE,
LED_METERMODE,
LAST_METERMODE
} MeterModeId;
};

typedef enum {
RATESTATUS_DATA,
Expand All @@ -152,7 +154,7 @@ void Meter_delete(Object* cast);

void Meter_setCaption(Meter* this, const char* caption);

void Meter_setMode(Meter* this, int modeIndex);
void Meter_setMode(Meter* this, MeterModeId modeIndex);

ListItem* Meter_toListItem(const Meter* this, bool moving);

Expand Down
12 changes: 6 additions & 6 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ static void Settings_readMeterModes(Settings* this, const char* line, unsigned i
}
column = MINIMUM(column, HeaderLayout_getColumns(this->hLayout) - 1);
this->hColumns[column].len = len;
int* modes = len ? xCalloc(len, sizeof(int)) : NULL;
MeterModeId* modes = len ? xCalloc(len, sizeof(MeterModeId)) : NULL;
for (int i = 0; i < len; i++) {
modes[i] = atoi(ids[i]);
modes[i] = (MeterModeId) atoi(ids[i]);
}
String_freeArray(ids);
this->hColumns[column].modes = modes;
Expand All @@ -122,7 +122,7 @@ static bool Settings_validateMeters(Settings* this) {

for (size_t column = 0; column < colCount; column++) {
char** names = this->hColumns[column].names;
const int* modes = this->hColumns[column].modes;
const MeterModeId* modes = this->hColumns[column].modes;
const size_t len = this->hColumns[column].len;

if (!len)
Expand Down Expand Up @@ -162,8 +162,8 @@ static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount)
this->hLayout = HF_TWO_50_50;
this->hColumns = xCalloc(HeaderLayout_getColumns(this->hLayout), sizeof(MeterColumnSetting));
for (size_t i = 0; i < 2; i++) {
this->hColumns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
this->hColumns[i].modes = xCalloc(sizes[i], sizeof(int));
this->hColumns[i].names = xCalloc(sizes[i] + 1, sizeof(*this->hColumns[0].names));
this->hColumns[i].modes = xCalloc(sizes[i], sizeof(*this->hColumns[0].modes));
this->hColumns[i].len = sizes[i];
}

Expand Down Expand Up @@ -605,7 +605,7 @@ static void writeMeterModes(const Settings* this, FILE* fd, char separator, unsi
if (this->hColumns[column].len) {
const char* sep = "";
for (size_t i = 0; i < this->hColumns[column].len; i++) {
fprintf(fd, "%s%d", sep, this->hColumns[column].modes[i]);
fprintf(fd, "%s%d", sep, (int)this->hColumns[column].modes[i]);
sep = " ";
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ in the source distribution for its full text.

#include "Hashtable.h"
#include "HeaderLayout.h"
#include "Meter.h"
#include "Row.h"
#include "RowField.h"

Expand All @@ -34,7 +35,7 @@ typedef struct {
typedef struct {
size_t len;
char** names;
int* modes;
MeterModeId* modes;
} MeterColumnSetting;

typedef struct ScreenSettings_ {
Expand Down

0 comments on commit 6b81659

Please sign in to comment.