summaryrefslogtreecommitdiffstats
path: root/Object.h
blob: 4d7d653ee9535e40e32d08cdbac15beb8286bd69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef HEADER_Object
#define HEADER_Object
/*
htop - Object.h
(C) 2004-2012 Hisham H. Muhammad
(C) 2020 Red Hat, Inc.  All Rights Reserved.
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include <assert.h>
#include <stdbool.h>

#include "RichString.h"
#include "XUtils.h" // IWYU pragma: keep


struct Object_;
typedef struct Object_ Object;

typedef void(*Object_Display)(const Object*, RichString*);
typedef int(*Object_Compare)(const void*, const void*);
typedef void(*Object_Delete)(Object*);

#define Object_getClass(obj_)         ((const Object*)(obj_))->klass
#define Object_setClass(obj_, class_) (((Object*)(obj_))->klass = (const ObjectClass*) (class_))

#define Object_delete(obj_)           (assert(Object_getClass(obj_)->delete), Object_getClass(obj_)->delete((Object*)(obj_)))
#define Object_displayFn(obj_)        Object_getClass(obj_)->display
#define Object_display(obj_, str_)    (assert(Object_getClass(obj_)->display), Object_getClass(obj_)->display((const Object*)(obj_), str_))
#define Object_compare(obj_, other_)  (assert(Object_getClass(obj_)->compare), Object_getClass(obj_)->compare((const void*)(obj_), other_))

#define Class(class_)                 ((const ObjectClass*)(&(class_ ## _class)))

#define AllocThis(class_) (class_*)   xMalloc(sizeof(class_)); Object_setClass(this, Class(class_))

typedef struct ObjectClass_ {
   const void* const extends;
   const Object_Display display;
   const Object_Delete delete;
   const Object_Compare compare;
} ObjectClass;

struct Object_ {
   const ObjectClass* klass;
};

typedef union {
   int i;
   void* v;
} Arg;

extern const ObjectClass Object_class;

bool Object_isA(const Object* o, const ObjectClass* klass);

#endif