Object.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef HEADER_Object
  2. #define HEADER_Object
  3. /*
  4. htop - Object.h
  5. (C) 2004-2012 Hisham H. Muhammad
  6. (C) 2020 Red Hat, Inc. All Rights Reserved.
  7. Released under the GNU GPLv2+, see the COPYING file
  8. in the source distribution for its full text.
  9. */
  10. #include <assert.h>
  11. #include <stdbool.h>
  12. #include "RichString.h"
  13. #include "XUtils.h" // IWYU pragma: keep
  14. struct Object_;
  15. typedef struct Object_ Object;
  16. typedef void(*Object_Display)(const Object*, RichString*);
  17. typedef int(*Object_Compare)(const void*, const void*);
  18. typedef void(*Object_Delete)(Object*);
  19. #define Object_getClass(obj_) ((const Object*)(obj_))->klass
  20. #define Object_setClass(obj_, class_) (((Object*)(obj_))->klass = (const ObjectClass*) (class_))
  21. #define Object_delete(obj_) (assert(Object_getClass(obj_)->delete), Object_getClass(obj_)->delete((Object*)(obj_)))
  22. #define Object_displayFn(obj_) Object_getClass(obj_)->display
  23. #define Object_display(obj_, str_) (assert(Object_getClass(obj_)->display), Object_getClass(obj_)->display((const Object*)(obj_), str_))
  24. #define Object_compare(obj_, other_) (assert(Object_getClass(obj_)->compare), Object_getClass(obj_)->compare((const void*)(obj_), other_))
  25. #define Class(class_) ((const ObjectClass*)(&(class_ ## _class)))
  26. #define AllocThis(class_) (class_*) xMalloc(sizeof(class_)); Object_setClass(this, Class(class_))
  27. typedef struct ObjectClass_ {
  28. const void* const extends;
  29. const Object_Display display;
  30. const Object_Delete delete;
  31. const Object_Compare compare;
  32. } ObjectClass;
  33. struct Object_ {
  34. const ObjectClass* klass;
  35. };
  36. typedef union {
  37. int i;
  38. void* v;
  39. } Arg;
  40. extern const ObjectClass Object_class;
  41. bool Object_isA(const Object* o, const ObjectClass* klass);
  42. #endif