rtgui_object.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(constructor) ((rtgui_constructor_t)(constructor))
  25. /** Casts the function pointer to an rtgui_constructor */
  26. #define RTGUI_DESTRUCTOR(destructor) ((rtgui_destructor_t)(destructor))
  27. /* pre-definetion */
  28. struct rtgui_object;
  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. /* parent type link */
  38. struct rtgui_type *parent;
  39. /* constructor and destructor */
  40. rtgui_constructor_t constructor;
  41. rtgui_destructor_t destructor;
  42. /* size of type */
  43. int size;
  44. };
  45. typedef struct rtgui_type rtgui_type_t;
  46. #define RTGUI_TYPE(type) (struct rtgui_type*)&(_rtgui_##type)
  47. #define DECLARE_CLASS_TYPE(type) extern const struct rtgui_type _rtgui_##type
  48. #define DEFINE_CLASS_TYPE(type, name, parent, constructor, destructor, size) \
  49. const struct rtgui_type _rtgui_##type = { \
  50. name, \
  51. parent, \
  52. RTGUI_CONSTRUCTOR(constructor), \
  53. RTGUI_DESTRUCTOR(destructor), \
  54. size }
  55. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object);
  56. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object);
  57. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent);
  58. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type);
  59. const char *rtgui_type_name_get(const rtgui_type_t *type);
  60. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object);
  61. #ifdef RTGUI_USING_CAST_CHECK
  62. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) \
  63. ((c_type *)rtgui_object_check_cast((rtgui_object_t *)(obj), (obj_type)))
  64. #else
  65. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) ((c_type *)(obj))
  66. #endif
  67. #define RTGUI_OBJECT_CHECK_TYPE(_obj, _type) \
  68. (rtgui_type_inherits_from(((rtgui_object_t *)(_obj))->type, (_type)))
  69. DECLARE_CLASS_TYPE(type);
  70. /** Gets the type of an object */
  71. #define RTGUI_OBJECT_TYPE RTGUI_TYPE(type)
  72. /** Casts the object to an rtgui_object_t */
  73. #define RTGUI_OBJECT(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_OBJECT_TYPE, rtgui_object_t))
  74. /** Checks if the object is an rtgui_Object */
  75. #define RTGUI_IS_OBJECT(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_OBJECT_TYPE))
  76. /* rtgui base object */
  77. struct rtgui_object
  78. {
  79. /* object type */
  80. const rtgui_type_t* type;
  81. rt_bool_t is_static;
  82. };
  83. rtgui_type_t *rtgui_object_type_get(void);
  84. rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type);
  85. void rtgui_object_destroy(rtgui_object_t *object);
  86. void rtgui_object_name_set(rtgui_object_t *object, const char *name);
  87. const char *rtgui_object_name_get(rtgui_object_t *object);
  88. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, rtgui_type_t *type);
  89. rtgui_type_t *rtk_object_object_type_get(rtgui_object_t *object);
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif