obj.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef obj_H_INCLUDED
  2. #define obj_H_INCLUDED
  3. #include "mem.h"
  4. /** OBJ imlementa a small OOP-like system with
  5. reference counted memory that can still be used,
  6. if neccesary like plain pointers. */
  7. /** OOP style class info. The most important
  8. values are done aqnd free. Done should release all
  9. references the object holds, and may be called several times
  10. recursively. Free should deallocate the memory used by
  11. the object itself. The fiel up may be NULL, but if it is not,
  12. it should contain a pointer to a parent class. done and free
  13. will be looked up recursively in the parent class if not defined
  14. in the child class */
  15. struct ObjClass_;
  16. typedef struct ObjClass_ ObjClass;
  17. /** ObjMethod is a generic object method pointer. It's assumed that as
  18. required by the ANSI C standard that all function pointers have the
  19. same size, so they can be cast to the required real method signature */
  20. typedef ObjClass * (ObjMethod)(ObjClass * self, ...);
  21. /** This macro can be used to implement your own object classes.
  22. Alternatively, include a member ObjClass as the first member of your
  23. own claqss. */
  24. #define IMPLEMENT_OBJCLASS \
  25. struct ObjClass_ * up; \
  26. ObjMethod * done; \
  27. ObjMethod * free;
  28. /** The object class structure. */
  29. struct ObjClass_ {
  30. struct ObjClass_ * up;
  31. ObjMethod * done;
  32. ObjMethod * free;
  33. };
  34. /** OBJCLASS_GETFUNCTION_AT looks up a function pointer in the given
  35. klasspointer. */
  36. #define OBJCLASS_GETFUNCTION_AT(KLASSPTR, FUNCTYPE, OFFSET) \
  37. ((FUNCTYPE)(((char *)(KLASSPTR))+(OFFSET)))
  38. /** OBJCLASS_GETFUNCTION uses offsetof to look up the named function pointer
  39. * of the given klass. KLASSTYPE should be the name of the struct that
  40. * KLASSPTR points to.
  41. */
  42. #define OBJCLASS_GETFUNCTION(KLASSPTR, KLASSTYPE, FUNCTYPE, NAME) \
  43. OBJCLASS_GETFUNCTION_AT((KLASSPTR), (FUNCTYPE), \
  44. offsetof(KLASSTYPE, NAME))
  45. /** An object pool allows several objects to be allocated in turn, and then
  46. all be unreferenced at once. */
  47. struct ObjPool_;
  48. typedef struct ObjPool_ ObjPool;
  49. struct ObjPoolNode_;
  50. typedef struct ObjPoolNode_ ObjPoolNode;
  51. /** Object pool struct. Use only as opaque object. */
  52. struct ObjPool_ {
  53. ObjPoolNode * last;
  54. // last object registered to the pool, forms a linked list.
  55. };
  56. /* This file was generated with:
  57. 'cfunctions -c -aoff -n -w obj_proto src/obj.c' */
  58. #ifndef CFH_OBJ_PROTO
  59. #define CFH_OBJ_PROTO
  60. /* From 'src/obj.c': */
  61. ObjPool * objpool_init (ObjPool * pool );
  62. void * objpool_register_data (ObjPool * pool , void * data );
  63. void * obj_alloc_in_pool (ObjPool * pool , size_t size , ObjClass * klass );
  64. void * obj_alloc (size_t size , ObjClass * klass );
  65. void * obj_ref (void * ptr );
  66. ObjClass * obj_class (void * ptr );
  67. ObjMethod * objclass_method_at (ObjClass * klass , size_t offset );
  68. ObjMethod * objclass_getfree (ObjClass * klass );
  69. void * obj_done (void * ptr );
  70. void * obj_free (void * ptr );
  71. void * obj_unref (void * ptr );
  72. ObjPool * objpool_unref (ObjPool * pool );
  73. #endif /* CFH_OBJ_PROTO */
  74. #endif