rtgui_object.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * File : rtgui_object.c
  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. #include <rtgui/rtgui_object.h>
  15. #include <rtgui/rtgui_system.h>
  16. static void _rtgui_object_constructor(rtgui_object_t *object)
  17. {
  18. if (!object)
  19. return;
  20. object->flag = RTGUI_OBJECT_FLAG_NONE;
  21. }
  22. /* Destroys the object */
  23. static void _rtgui_object_destructor(rtgui_object_t *object)
  24. {
  25. /* nothing */
  26. }
  27. DEFINE_CLASS_TYPE(object, "object",
  28. RT_NULL,
  29. _rtgui_object_constructor,
  30. _rtgui_object_destructor,
  31. sizeof(struct rtgui_object));
  32. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object)
  33. {
  34. /* first call parent's type */
  35. if (type->parent != RT_NULL)
  36. rtgui_type_object_construct(type->parent, object);
  37. if (type->constructor) type->constructor(object);
  38. }
  39. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object)
  40. {
  41. const rtgui_type_t *t;
  42. t = type;
  43. while (t)
  44. {
  45. if (t->destructor) t->destructor(object);
  46. t = t->parent;
  47. }
  48. }
  49. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent)
  50. {
  51. const rtgui_type_t *t;
  52. t = type;
  53. while (t)
  54. {
  55. if (t == parent) return RT_TRUE;
  56. t = t->parent;
  57. }
  58. return RT_FALSE;
  59. }
  60. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type)
  61. {
  62. return type->parent;
  63. }
  64. const char *rtgui_type_name_get(const rtgui_type_t *type)
  65. {
  66. if (!type) return RT_NULL;
  67. return type->name;
  68. }
  69. #ifdef RTGUI_OBJECT_TRACE
  70. struct rtgui_object_information
  71. {
  72. rt_uint32_t objs_number;
  73. rt_uint32_t allocated_size;
  74. rt_uint32_t max_allocated;
  75. };
  76. struct rtgui_object_information obj_info = {0, 0, 0};
  77. #endif
  78. /**
  79. * @brief Creates a new object: it calls the corresponding constructors (from the constructor of the base class to the
  80. * constructor of the more derived class) and then sets the values of the given properties
  81. * @param object_type the type of object to create
  82. * @return Returns the new Etk_Object of type @a object_type
  83. */
  84. rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type)
  85. {
  86. rtgui_object_t *new_object;
  87. if (!object_type)
  88. return RT_NULL;
  89. new_object = rtgui_malloc(object_type->size);
  90. if (new_object == RT_NULL) return RT_NULL;
  91. #ifdef RTGUI_OBJECT_TRACE
  92. obj_info.objs_number ++;
  93. obj_info.allocated_size += object_type->size;
  94. if (obj_info.allocated_size > obj_info.max_allocated)
  95. obj_info.max_allocated = obj_info.allocated_size;
  96. #endif
  97. new_object->type = object_type;
  98. rtgui_type_object_construct(object_type, new_object);
  99. return new_object;
  100. }
  101. /**
  102. * @brief Destroys the object: it first sets the weak-pointers to RT_NULL, emits the "destroyed" signal, and then
  103. * queues the object in the list of objects to free. Thus, the destructors will only be called at the beginning of the
  104. * next main loop iteration (from the destructor of the more derived class to the destructor of the ultimate base class).
  105. * @param object the object to destroy
  106. * @warning You should not assume that this function will call directly the destructors of the object!
  107. */
  108. void rtgui_object_destroy(rtgui_object_t *object)
  109. {
  110. if (!object || object->flag & RTGUI_OBJECT_FLAG_STATIC)
  111. return;
  112. #ifdef RTGUI_OBJECT_TRACE
  113. obj_info.objs_number --;
  114. obj_info.allocated_size -= object->type->size;
  115. #endif
  116. /* call destructor */
  117. RT_ASSERT(object->type != RT_NULL);
  118. rtgui_type_destructors_call(object->type, object);
  119. /* release object */
  120. rtgui_free(object);
  121. }
  122. /**
  123. * @brief Checks if @a object can be cast to @a type.
  124. * If @a object doesn't inherit from @a type, a warning is displayed in the console but the object is returned anyway.
  125. * @param object the object to cast
  126. * @param type the type to which we cast the object
  127. * @return Returns the object
  128. * @note You usually do not need to call this function, use specific macros instead (ETK_IS_WIDGET() for example)
  129. */
  130. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *obj, rtgui_type_t *obj_type, const char* func, int line)
  131. {
  132. if (!obj) return RT_NULL;
  133. if (!rtgui_type_inherits_from(obj->type, obj_type))
  134. {
  135. rt_kprintf("%s[%d]: Invalid cast from \"%s\" to \"%s\"\n", func, line, rtgui_type_name_get(obj->type), rtgui_type_name_get(obj_type));
  136. }
  137. return obj;
  138. }
  139. /**
  140. * @brief Gets the type of the object
  141. * @param object an object
  142. * @return Returns the type of @a object (RT_NULL on failure)
  143. */
  144. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object)
  145. {
  146. if (!object) return RT_NULL;
  147. return object->type;
  148. }
  149. void rtgui_object_set_event_handler(struct rtgui_object *object, rtgui_event_handler_ptr handler)
  150. {
  151. RT_ASSERT(object != RT_NULL);
  152. object->event_handler = handler;
  153. }
  154. rt_bool_t rtgui_object_event_handler(struct rtgui_object *object, struct rtgui_event* event)
  155. {
  156. return RT_FALSE;
  157. }