Fixes to subclassing Process.

This commit is contained in:
Hisham Muhammad 2015-03-31 23:23:10 -03:00
parent 5320bab202
commit 4c24a9b462
5 changed files with 51 additions and 13 deletions

View File

@ -192,6 +192,15 @@ typedef struct Process_ {
} Process; } Process;
typedef void (*Process_WriteField)(Process*, RichString*, ProcessField);
typedef struct ProcessClass_ {
const ObjectClass super;
const Process_WriteField writeField;
} ProcessClass;
#define As_Process(this_) ((ProcessClass*)((this_)->super.klass))
}*/ }*/
const char *Process_fieldNames[] = { const char *Process_fieldNames[] = {
@ -470,7 +479,7 @@ static inline void Process_outputRate(RichString* str, int attr, char* buffer, i
RichString_append(str, attr, buffer); RichString_append(str, attr, buffer);
} }
static void Process_writeField(Process* this, RichString* str, ProcessField field) { void Process_writeField(Process* this, RichString* str, ProcessField field) {
char buffer[256]; buffer[255] = '\0'; char buffer[256]; buffer[255] = '\0';
int attr = CRT_colors[DEFAULT_COLOR]; int attr = CRT_colors[DEFAULT_COLOR];
int baseattr = CRT_colors[PROCESS_BASENAME]; int baseattr = CRT_colors[PROCESS_BASENAME];
@ -630,12 +639,12 @@ static void Process_writeField(Process* this, RichString* str, ProcessField fiel
RichString_append(str, attr, buffer); RichString_append(str, attr, buffer);
} }
static void Process_display(Object* cast, RichString* out) { void Process_display(Object* cast, RichString* out) {
Process* this = (Process*) cast; Process* this = (Process*) cast;
ProcessField* fields = this->pl->fields; ProcessField* fields = this->pl->fields;
RichString_prune(out); RichString_prune(out);
for (int i = 0; fields[i]; i++) for (int i = 0; fields[i]; i++)
Process_writeField(this, out, fields[i]); As_Process(this)->writeField(this, out, fields[i]);
if (this->pl->shadowOtherUsers && (int)this->st_uid != Process_getuid) if (this->pl->shadowOtherUsers && (int)this->st_uid != Process_getuid)
RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]); RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
if (this->tag == true) if (this->tag == true)
@ -651,11 +660,14 @@ void Process_done(Process* this) {
#endif #endif
} }
ObjectClass Process_class = { ProcessClass Process_class = {
.extends = Class(Object), .super = {
.display = Process_display, .extends = Class(Object),
.delete = Process_delete, .display = Process_display,
.compare = Process_compare .delete = Process_delete,
.compare = Process_compare
},
.writeField = Process_writeField,
}; };
void Process_init(Process* this, struct ProcessList_* pl) { void Process_init(Process* this, struct ProcessList_* pl) {

View File

@ -171,6 +171,15 @@ typedef struct Process_ {
} Process; } Process;
typedef void (*Process_WriteField)(Process*, RichString*, ProcessField);
typedef struct ProcessClass_ {
const ObjectClass super;
const Process_WriteField writeField;
} ProcessClass;
#define As_Process(this_) ((ProcessClass*)((this_)->super.klass))
extern const char *Process_fieldNames[]; extern const char *Process_fieldNames[];
@ -189,9 +198,13 @@ void Process_setupColumnWidths();
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K) #define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K) #define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
void Process_writeField(Process* this, RichString* str, ProcessField field);
void Process_display(Object* cast, RichString* out);
void Process_done(Process* this); void Process_done(Process* this);
extern ObjectClass Process_class; extern ProcessClass Process_class;
void Process_init(Process* this, struct ProcessList_* pl); void Process_init(Process* this, struct ProcessList_* pl);

2
htop.c
View File

@ -694,7 +694,7 @@ int main(int argc, char** argv) {
CRT_init(settings->delay, settings->colorScheme); CRT_init(settings->delay, settings->colorScheme);
Panel* panel = Panel_new(0, headerHeight, COLS, LINES - headerHeight - 2, false, &Process_class); Panel* panel = Panel_new(0, headerHeight, COLS, LINES - headerHeight - 2, false, (ObjectClass*) &Process_class);
ProcessList_setPanel(pl, panel); ProcessList_setPanel(pl, panel);
FunctionBar* defaultBar = FunctionBar_new(defaultFunctions, NULL, NULL); FunctionBar* defaultBar = FunctionBar_new(defaultFunctions, NULL, NULL);

View File

@ -27,9 +27,19 @@ typedef struct LinuxProcess_ {
}*/ }*/
ProcessClass LinuxProcess_class = {
.super = {
.extends = Class(Process),
.display = Process_display,
.delete = Process_delete,
.compare = LinuxProcess_compare
},
.writeField = (Process_WriteField) LinuxProcess_writeField,
};
LinuxProcess* LinuxProcess_new(ProcessList* pl) { LinuxProcess* LinuxProcess_new(ProcessList* pl) {
LinuxProcess* this = calloc(sizeof(LinuxProcess), 1); LinuxProcess* this = calloc(sizeof(LinuxProcess), 1);
Object_setClass(this, Class(Process)); Object_setClass(this, Class(LinuxProcess));
Process_init(&this->super, pl); Process_init(&this->super, pl);
return this; return this;
} }
@ -85,7 +95,8 @@ void LinuxProcess_writeField(LinuxProcess* this, RichString* str, ProcessField f
break; break;
} }
default: default:
snprintf(buffer, n, "- "); Process_writeField((Process*)this, str, field);
return;
} }
RichString_append(str, attr, buffer); RichString_append(str, attr, buffer);
} }
@ -104,6 +115,6 @@ long LinuxProcess_compare(const void* v1, const void* v2) {
case IO_PRIORITY: case IO_PRIORITY:
return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2); return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2);
default: default:
return (p1->super.pid - p2->super.pid); return Process_compare(v1, v2);
} }
} }

View File

@ -20,6 +20,8 @@ typedef struct LinuxProcess_ {
#define Process_delete LinuxProcess_delete #define Process_delete LinuxProcess_delete
extern ProcessClass LinuxProcess_class;
LinuxProcess* LinuxProcess_new(ProcessList* pl); LinuxProcess* LinuxProcess_new(ProcessList* pl);
void LinuxProcess_delete(Object* cast); void LinuxProcess_delete(Object* cast);