Table.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef HEADER_Table
  2. #define HEADER_Table
  3. /*
  4. htop - Table.h
  5. (C) 2004,2005 Hisham H. Muhammad
  6. (C) 2023 htop dev team
  7. Released under the GNU GPLv2+, see the COPYING file
  8. in the source distribution for its full text.
  9. */
  10. #include <stdbool.h>
  11. #include "Hashtable.h"
  12. #include "Object.h"
  13. #include "RichString.h"
  14. #include "Settings.h"
  15. #include "Vector.h"
  16. struct Machine_; // IWYU pragma: keep
  17. struct Panel_; // IWYU pragma: keep
  18. struct Row_; // IWYU pragma: keep
  19. typedef struct Table_ {
  20. /* Super object for emulated OOP */
  21. Object super;
  22. Vector* rows; /* all known; sort order can vary and differ from display order */
  23. Vector* displayList; /* row tree flattened in display order (borrowed);
  24. updated in Table_updateDisplayList when rebuilding panel */
  25. Hashtable* table; /* fast known row lookup by identifier */
  26. struct Machine_* host;
  27. const char* incFilter;
  28. bool needsSort;
  29. int following; /* -1 or row being visually tracked in the user interface */
  30. struct Panel_* panel;
  31. } Table;
  32. typedef Table* (*Table_New)(const struct Machine_*);
  33. typedef void (*Table_ScanPrepare)(Table* this);
  34. typedef void (*Table_ScanIterate)(Table* this);
  35. typedef void (*Table_ScanCleanup)(Table* this);
  36. typedef struct TableClass_ {
  37. const ObjectClass super;
  38. const Table_ScanPrepare prepare;
  39. const Table_ScanIterate iterate;
  40. const Table_ScanCleanup cleanup;
  41. } TableClass;
  42. #define As_Table(this_) ((const TableClass*)((this_)->super.klass))
  43. #define Table_scanPrepare(t_) (As_Table(t_)->prepare ? (As_Table(t_)->prepare(t_)) : Table_prepareEntries(t_))
  44. #define Table_scanIterate(t_) (As_Table(t_)->iterate(t_)) /* mandatory; must have a custom iterate method */
  45. #define Table_scanCleanup(t_) (As_Table(t_)->cleanup ? (As_Table(t_)->cleanup(t_)) : Table_cleanupEntries(t_))
  46. Table* Table_init(Table* this, const ObjectClass* klass, struct Machine_* host);
  47. void Table_done(Table* this);
  48. extern const TableClass Table_class;
  49. void Table_setPanel(Table* this, struct Panel_* panel);
  50. void Table_printHeader(const Settings* settings, RichString* header);
  51. void Table_add(Table* this, struct Row_* row);
  52. void Table_updateDisplayList(Table* this);
  53. void Table_expandTree(Table* this);
  54. void Table_collapseAllBranches(Table* this);
  55. void Table_rebuildPanel(Table* this);
  56. static inline struct Row_* Table_findRow(Table* this, int id) {
  57. return (struct Row_*) Hashtable_get(this->table, id);
  58. }
  59. void Table_prepareEntries(Table* this);
  60. void Table_cleanupEntries(Table* this);
  61. void Table_cleanupRow(Table* this, Row* row, int idx);
  62. static inline void Table_compact(Table* this) {
  63. Vector_compact(this->rows);
  64. this->needsSort = true;
  65. }
  66. #endif