From 4c24a9b462e2e55e9d2f6d24694d5408c886c556 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 31 Mar 2015 23:23:10 -0300 Subject: [PATCH] Fixes to subclassing Process. --- Process.c | 28 ++++++++++++++++++++-------- Process.h | 15 ++++++++++++++- htop.c | 2 +- linux/LinuxProcess.c | 17 ++++++++++++++--- linux/LinuxProcess.h | 2 ++ 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/Process.c b/Process.c index ff1baba2..9048cfe2 100644 --- a/Process.c +++ b/Process.c @@ -192,6 +192,15 @@ typedef struct 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[] = { @@ -470,7 +479,7 @@ static inline void Process_outputRate(RichString* str, int attr, char* buffer, i 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'; int attr = CRT_colors[DEFAULT_COLOR]; 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); } -static void Process_display(Object* cast, RichString* out) { +void Process_display(Object* cast, RichString* out) { Process* this = (Process*) cast; ProcessField* fields = this->pl->fields; RichString_prune(out); 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) RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]); if (this->tag == true) @@ -651,11 +660,14 @@ void Process_done(Process* this) { #endif } -ObjectClass Process_class = { - .extends = Class(Object), - .display = Process_display, - .delete = Process_delete, - .compare = Process_compare +ProcessClass Process_class = { + .super = { + .extends = Class(Object), + .display = Process_display, + .delete = Process_delete, + .compare = Process_compare + }, + .writeField = Process_writeField, }; void Process_init(Process* this, struct ProcessList_* pl) { diff --git a/Process.h b/Process.h index be54adab..a08ec92c 100644 --- a/Process.h +++ b/Process.h @@ -171,6 +171,15 @@ typedef struct 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[]; @@ -189,9 +198,13 @@ void Process_setupColumnWidths(); #define ONE_DECIMAL_M (ONE_DECIMAL_K * 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); -extern ObjectClass Process_class; +extern ProcessClass Process_class; void Process_init(Process* this, struct ProcessList_* pl); diff --git a/htop.c b/htop.c index 62e49c9c..ce173275 100644 --- a/htop.c +++ b/htop.c @@ -694,7 +694,7 @@ int main(int argc, char** argv) { 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); FunctionBar* defaultBar = FunctionBar_new(defaultFunctions, NULL, NULL); diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index b534d4da..269843a4 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -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* this = calloc(sizeof(LinuxProcess), 1); - Object_setClass(this, Class(Process)); + Object_setClass(this, Class(LinuxProcess)); Process_init(&this->super, pl); return this; } @@ -85,7 +95,8 @@ void LinuxProcess_writeField(LinuxProcess* this, RichString* str, ProcessField f break; } default: - snprintf(buffer, n, "- "); + Process_writeField((Process*)this, str, field); + return; } RichString_append(str, attr, buffer); } @@ -104,6 +115,6 @@ long LinuxProcess_compare(const void* v1, const void* v2) { case IO_PRIORITY: return LinuxProcess_effectiveIOPriority(p1) - LinuxProcess_effectiveIOPriority(p2); default: - return (p1->super.pid - p2->super.pid); + return Process_compare(v1, v2); } } diff --git a/linux/LinuxProcess.h b/linux/LinuxProcess.h index a162c5eb..f7a14764 100644 --- a/linux/LinuxProcess.h +++ b/linux/LinuxProcess.h @@ -20,6 +20,8 @@ typedef struct LinuxProcess_ { #define Process_delete LinuxProcess_delete +extern ProcessClass LinuxProcess_class; + LinuxProcess* LinuxProcess_new(ProcessList* pl); void LinuxProcess_delete(Object* cast);