1
0

rtgui_object.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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) return;
  19. object->is_static = RT_FALSE;
  20. }
  21. /* Destroys the object */
  22. static void _rtgui_object_destructor(rtgui_object_t *object)
  23. {
  24. /* nothing */
  25. }
  26. rtgui_type_t *rtgui_type_create(const char *type_name, rtgui_type_t *parent_type,
  27. int type_size, rtgui_constructor_t constructor,
  28. rtgui_destructor_t destructor)
  29. {
  30. rtgui_type_t *new_type;
  31. if (!type_name)
  32. return RT_NULL;
  33. new_type = rtgui_malloc(sizeof(rtgui_type_t));
  34. new_type->name = rt_strdup(type_name);
  35. new_type->size = type_size;
  36. new_type->constructor = constructor;
  37. new_type->destructor = destructor;
  38. if (!parent_type)
  39. {
  40. new_type->hierarchy_depth = 0;
  41. new_type->hierarchy = RT_NULL;
  42. }
  43. else
  44. {
  45. /* Build the type hierarchy */
  46. new_type->hierarchy_depth = parent_type->hierarchy_depth + 1;
  47. new_type->hierarchy = rtgui_malloc(sizeof(rtgui_type_t *) * new_type->hierarchy_depth);
  48. new_type->hierarchy[0] = parent_type;
  49. rt_memcpy(new_type->hierarchy + 1, parent_type->hierarchy,
  50. parent_type->hierarchy_depth * sizeof(rtgui_type_t *));
  51. }
  52. return new_type;
  53. }
  54. void rtgui_type_destroy(rtgui_type_t *type)
  55. {
  56. if (!type) return;
  57. if (type->hierarchy) rtgui_free(type->hierarchy);
  58. rtgui_free(type->name);
  59. rtgui_free(type);
  60. }
  61. void rtgui_type_object_construct(rtgui_type_t *type, rtgui_object_t *object)
  62. {
  63. int i;
  64. if (!type || !object) return;
  65. /* Call the constructors */
  66. for (i = type->hierarchy_depth - 1; i >= 0; i--)
  67. {
  68. if (type->hierarchy[i]->constructor)
  69. type->hierarchy[i]->constructor(object);
  70. }
  71. if (type->constructor) type->constructor(object);
  72. }
  73. void rtgui_type_destructors_call(rtgui_type_t *type, rtgui_object_t *object)
  74. {
  75. int i;
  76. if (!type || !object) return;
  77. if (type->destructor) type->destructor(object);
  78. for (i = 0; i < type->hierarchy_depth; i++)
  79. {
  80. if (type->hierarchy[i]->destructor)
  81. type->hierarchy[i]->destructor(object);
  82. }
  83. }
  84. rt_bool_t rtgui_type_inherits_from(rtgui_type_t *type, rtgui_type_t *parent)
  85. {
  86. int i;
  87. if (!type || !parent) return RT_FALSE;
  88. if (type == parent) return RT_TRUE;
  89. for (i = 0; i < type->hierarchy_depth; i++)
  90. {
  91. if (type->hierarchy[i] == parent) return RT_TRUE;
  92. }
  93. return RT_FALSE;
  94. }
  95. rtgui_type_t *rtgui_type_parent_type_get(rtgui_type_t *type)
  96. {
  97. if (!type || !type->hierarchy) return RT_NULL;
  98. return type->hierarchy[0];
  99. }
  100. const char *rtgui_type_name_get(rtgui_type_t *type)
  101. {
  102. if (!type) return RT_NULL;
  103. return type->name;
  104. }
  105. struct rtgui_object_information
  106. {
  107. rt_uint32_t objs_number;
  108. rt_uint32_t allocated_size;
  109. rt_uint32_t max_allocated;
  110. };
  111. struct rtgui_object_information obj_info = {0, 0, 0};
  112. /**
  113. * @brief Creates a new object: it calls the corresponding constructors (from the constructor of the base class to the
  114. * constructor of the more derived class) and then sets the values of the given properties
  115. * @param object_type the type of object to create
  116. * @return Returns the new Etk_Object of type @a object_type
  117. */
  118. rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type)
  119. {
  120. rtgui_object_t *new_object;
  121. if (!object_type)
  122. return RT_NULL;
  123. new_object = rtgui_malloc(object_type->size);
  124. if (new_object == RT_NULL) return RT_NULL;
  125. obj_info.objs_number ++;
  126. obj_info.allocated_size += object_type->size;
  127. if (obj_info.allocated_size > obj_info.max_allocated)
  128. obj_info.max_allocated = obj_info.allocated_size;
  129. new_object->type = object_type;
  130. new_object->is_static = RT_FALSE;
  131. rtgui_type_object_construct(object_type, new_object);
  132. return new_object;
  133. }
  134. /**
  135. * @brief Destroys the object: it first sets the weak-pointers to RT_NULL, emits the "destroyed" signal, and then
  136. * queues the object in the list of objects to free. Thus, the destructors will only be called at the beginning of the
  137. * next main loop iteration (from the destructor of the more derived class to the destructor of the ultimate base class).
  138. * @param object the object to destroy
  139. * @warning You should not assume that this function will call directly the destructors of the object!
  140. */
  141. void rtgui_object_destroy(rtgui_object_t *object)
  142. {
  143. if (!object || object->is_static == RT_TRUE) return;
  144. obj_info.objs_number --;
  145. obj_info.allocated_size -= object->type->size;
  146. /* call destructor */
  147. RT_ASSERT(object->type != RT_NULL);
  148. rtgui_type_destructors_call(object->type, object);
  149. /* release object */
  150. rtgui_free(object);
  151. }
  152. /**
  153. * @internal
  154. * @brief Gets the type of a rtgui_object
  155. * @return Returns the type of a rtgui_object
  156. */
  157. rtgui_type_t *rtgui_object_type_get(void)
  158. {
  159. static rtgui_type_t *object_type = RT_NULL;
  160. if (!object_type)
  161. {
  162. object_type = rtgui_type_create("object", RT_NULL,
  163. sizeof(rtgui_object_t), RTGUI_CONSTRUCTOR(_rtgui_object_constructor),
  164. RTGUI_DESTRUCTOR(_rtgui_object_destructor));
  165. }
  166. return object_type;
  167. }
  168. /**
  169. * @brief Checks if @a object can be cast to @a type.
  170. * If @a object doesn't inherit from @a type, a warning is displayed in the console but the object is returned anyway.
  171. * @param object the object to cast
  172. * @param type the type to which we cast the object
  173. * @return Returns the object
  174. * @note You usually do not need to call this function, use specific macros instead (ETK_IS_WIDGET() for example)
  175. */
  176. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, rtgui_type_t *type)
  177. {
  178. if (!object) return RT_NULL;
  179. if (!rtgui_type_inherits_from(object->type, type))
  180. {
  181. rt_kprintf("Invalid cast from \"%s\" to \"%s\"\n", rtgui_type_name_get(object->type), rtgui_type_name_get(type));
  182. }
  183. return object;
  184. }
  185. /**
  186. * @brief Gets the type of the object
  187. * @param object an object
  188. * @return Returns the type of @a object (RT_NULL on failure)
  189. */
  190. rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object)
  191. {
  192. if (!object) return RT_NULL;
  193. return object->type;
  194. }