summaryrefslogtreecommitdiffstats
path: root/Table.h
blob: d1bf25af31d47060fa7df5d4cf4e9b779d73bb1c (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#ifndef HEADER_Table
#define HEADER_Table
/*
htop - Table.h
(C) 2004,2005 Hisham H. Muhammad
(C) 2023 htop dev team
Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include <stdbool.h>

#include "Hashtable.h"
#include "Object.h"
#include "RichString.h"
#include "Settings.h"
#include "Vector.h"


struct Machine_;  // IWYU pragma: keep
struct Panel_;    // IWYU pragma: keep
struct Row_;      // IWYU pragma: keep

typedef struct Table_ {
   /* Super object for emulated OOP */
   Object super;

   Vector* rows;          /* all known; sort order can vary and differ from display order */
   Vector* displayList;   /* row tree flattened in display order (borrowed);
                             updated in Table_updateDisplayList when rebuilding panel */
   Hashtable* table;      /* fast known row lookup by identifier */

   struct Machine_* host;
   const char* incFilter;
   bool needsSort;
   int following;         /* -1 or row being visually tracked in the user interface */

   struct Panel_* panel;
} Table;

typedef Table* (*Table_New)(const struct Machine_*);
typedef void (*Table_ScanPrepare)(Table* this);
typedef void (*Table_ScanIterate)(Table* this);
typedef void (*Table_ScanCleanup)(Table* this);

typedef struct TableClass_ {
   const ObjectClass super;
   const Table_ScanPrepare prepare;
   const Table_ScanIterate iterate;
   const Table_ScanCleanup cleanup;
} TableClass;

#define As_Table(this_)  ((const TableClass*)((this_)->super.klass))

#define Table_scanPrepare(t_)  (As_Table(t_)->prepare ? (As_Table(t_)->prepare(t_)) : Table_prepareEntries(t_))
#define Table_scanIterate(t_)  (As_Table(t_)->iterate(t_))  /* mandatory; must have a custom iterate method */
#define Table_scanCleanup(t_)  (As_Table(t_)->cleanup ? (As_Table(t_)->cleanup(t_)) : Table_cleanupEntries(t_))

Table* Table_init(Table* this, const ObjectClass* klass, struct Machine_* host);

void Table_done(Table* this);

extern const TableClass Table_class;

void Table_setPanel(Table* this, struct Panel_* panel);

void Table_printHeader(const Settings* settings, RichString* header);

void Table_add(Table* this, struct Row_* row);

void Table_updateDisplayList(Table* this);

void Table_expandTree(Table* this);

void Table_collapseAllBranches(Table* this);

void Table_rebuildPanel(Table* this);

static inline struct Row_* Table_findRow(Table* this, int id) {
   return (struct Row_*) Hashtable_get(this->table, id);
}

void Table_prepareEntries(Table* this);

void Table_cleanupEntries(Table* this);

void Table_cleanupRow(Table* this, Row* row, int idx);

static inline void Table_compact(Table* this) {
   Vector_compact(this->rows);
   this->needsSort = true;
}

#endif