rtgui_object.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * File : rtgui_object.h
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-04 Bernard first version
  13. */
  14. #ifndef __RTGUI_OBJECT_H__
  15. #define __RTGUI_OBJECT_H__
  16. #include <rtthread.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* rtgui object type */
  21. #define RTGUI_CONTAINER_OF(obj, type, member) \
  22. ((type *)((char *)(obj) - (unsigned long)(&((type *)0)->member)))
  23. /** Casts the function pointer to an rtgui_constructor */
  24. #define RTGUI_CONSTRUCTOR(c) ((rtgui_constructor_t)(c))
  25. /** Casts the function pointer to an rtgui_constructor */
  26. #define RTGUI_DESTRUCTOR(d) ((rtgui_destructor_t)(d))
  27. /* pre-definetion */
  28. typedef struct rtgui_type rtgui_type_t;
  29. typedef struct rtgui_object rtgui_object_t;
  30. typedef void (*rtgui_constructor_t)(rtgui_object_t *object);
  31. typedef void (*rtgui_destructor_t)(rtgui_object_t *object);
  32. /* rtgui type structure */
  33. struct rtgui_type
  34. {
  35. /* type name */
  36. char* name;
  37. /* hierarchy and depth */
  38. rtgui_type_t **hierarchy;
  39. int hierarchy_depth;
  40. /* constructor and destructor */
  41. rtgui_constructor_t constructor;
  42. rtgui_destructor_t destructor;
  43. /* size of type */
  44. int size;
  45. };
  46. typedef struct rtgui_type rtgui_type_t;
  47. rtgui_type_t *rtgui_type_create(const char *type_name, rtgui_type_t *parent_type,
  48. int type_size, rtgui_constructor_t c,
  49. rtgui_destructor_t d);
  50. void rtgui_type_destroy(rtgui_type_t *type);
  51. void rtgui_type_object_construct(rtgui_type_t *type, rtgui_object_t *object);
  52. void rtgui_type_destructors_call(rtgui_type_t *type, rtgui_object_t *object);
  53. rt_bool_t rtgui_type_inherits_from(rtgui_type_t *type, rtgui_type_t *parent);
  54. rtgui_type_t *rtgui_type_parent_type_get(rtgui_type_t *type);
  55. const char *rtgui_type_name_get(rtgui_type_t *type);
  56. rtgui_type_t *GetTypeFromName(const char *name);
  57. #ifdef RTGUI_USING_CAST_CHECK
  58. #define RTGUI_OBJECT_CAST(obj, rtgui_type_t, c_type) \
  59. ((c_type *)rtgui_object_check_cast((rtgui_object_t *)(obj), (rtgui_type_t)))
  60. #else
  61. #define RTGUI_OBJECT_CAST(obj, rtgui_type_t, c_type) ((c_type *)(obj))
  62. #endif
  63. #define RTGUI_OBJECT_CHECK_TYPE(_obj, _type) \
  64. (rtgui_type_inherits_from(((rtgui_object_t *)(_obj))->type, (_type)))
  65. /** Gets the type of an object */
  66. #define RTGUI_OBJECT_TYPE (rtgui_object_type_get())
  67. /** Casts the object to an rtgui_object_t */
  68. #define RTGUI_OBJECT(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_OBJECT_TYPE, rtgui_object_t))
  69. /** Checks if the object is an rtgui_Object */
  70. #define RTGUI_IS_OBJECT(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_OBJECT_TYPE))
  71. /* rtgui base object */
  72. struct rtgui_object
  73. {
  74. /* object type */
  75. rtgui_type_t* type;
  76. rt_bool_t is_static;
  77. };
  78. rtgui_type_t *rtgui_object_type_get(void);
  79. rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type);
  80. void rtgui_object_destroy(rtgui_object_t *object);
  81. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, rtgui_type_t *type);
  82. #ifdef __cplusplus
  83. }
  84. #endif
  85. #endif