rtgui_object.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_VALID;
  21. object->id = (rt_uint32_t)object;
  22. }
  23. /* Destroys the object */
  24. static void _rtgui_object_destructor(rtgui_object_t *object)
  25. {
  26. /* Any valid objest should both have valid flag _and_ valid type. Only use
  27. * flag is not enough because the chunk of memory may be reallocted to other
  28. * object and thus the flag will become valid. */
  29. object->flag = RTGUI_OBJECT_FLAG_NONE;
  30. object->type = RT_NULL;
  31. }
  32. DEFINE_CLASS_TYPE(object, "object",
  33. RT_NULL,
  34. _rtgui_object_constructor,
  35. _rtgui_object_destructor,
  36. sizeof(struct rtgui_object));
  37. RTM_EXPORT(_rtgui_object);
  38. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object)
  39. {
  40. /* construct from parent to children */
  41. if (type->parent != RT_NULL)
  42. rtgui_type_object_construct(type->parent, object);
  43. if (type->constructor)
  44. type->constructor(object);
  45. }
  46. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object)
  47. {
  48. /* destruct from children to parent */
  49. if (type->destructor)
  50. type->destructor(object);
  51. if (type->parent)
  52. rtgui_type_destructors_call(type->parent, object);
  53. }
  54. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent)
  55. {
  56. const rtgui_type_t *t;
  57. t = type;
  58. while (t)
  59. {
  60. if (t == parent) return RT_TRUE;
  61. t = t->parent;
  62. }
  63. return RT_FALSE;
  64. }
  65. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type)
  66. {
  67. return type->parent;
  68. }
  69. const char *rtgui_type_name_get(const rtgui_type_t *type)
  70. {
  71. if (!type) return RT_NULL;
  72. return type->name;
  73. }
  74. #ifdef RTGUI_OBJECT_TRACE
  75. struct rtgui_object_information
  76. {
  77. rt_uint32_t objs_number;
  78. rt_uint32_t allocated_size;
  79. rt_uint32_t max_allocated;
  80. };
  81. struct rtgui_object_information obj_info = {0, 0, 0};
  82. #endif
  83. /**
  84. * @brief Creates a new object: it calls the corresponding constructors
  85. * (from the constructor of the base class to the constructor of the more
  86. * derived class) and then sets the values of the given properties
  87. *
  88. * @param object_type the type of object to create
  89. * @return the created object
  90. */
  91. rtgui_object_t *rtgui_object_create(const rtgui_type_t *object_type)
  92. {
  93. rtgui_object_t *new_object;
  94. if (!object_type)
  95. return RT_NULL;
  96. new_object = rtgui_malloc(object_type->size);
  97. if (new_object == RT_NULL) return RT_NULL;
  98. #ifdef RTGUI_OBJECT_TRACE
  99. obj_info.objs_number ++;
  100. obj_info.allocated_size += object_type->size;
  101. if (obj_info.allocated_size > obj_info.max_allocated)
  102. obj_info.max_allocated = obj_info.allocated_size;
  103. #endif
  104. new_object->type = object_type;
  105. rtgui_type_object_construct(object_type, new_object);
  106. return new_object;
  107. }
  108. RTM_EXPORT(rtgui_object_create);
  109. /**
  110. * @brief Destroys the object.
  111. *
  112. * The object destructors will be called in inherited type order.
  113. *
  114. * @param object the object to destroy
  115. */
  116. void rtgui_object_destroy(rtgui_object_t *object)
  117. {
  118. if (!object || object->flag & RTGUI_OBJECT_FLAG_STATIC)
  119. return;
  120. #ifdef RTGUI_OBJECT_TRACE
  121. obj_info.objs_number --;
  122. obj_info.allocated_size -= object->type->size;
  123. #endif
  124. /* call destructor */
  125. RT_ASSERT(object->type != RT_NULL);
  126. rtgui_type_destructors_call(object->type, object);
  127. /* release object */
  128. rtgui_free(object);
  129. }
  130. RTM_EXPORT(rtgui_object_destroy);
  131. /**
  132. * @brief Checks if the object can be cast to the specified type.
  133. *
  134. * If the object doesn't inherit from the specified type, a warning
  135. * is displayed in the console but the object is returned anyway.
  136. *
  137. * @param object the object to cast
  138. * @param type the type to which we cast the object
  139. * @return Returns the object
  140. */
  141. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *obj, const rtgui_type_t *obj_type, const char *func, int line)
  142. {
  143. if (!obj) return RT_NULL;
  144. if (!rtgui_type_inherits_from(obj->type, obj_type))
  145. {
  146. rt_kprintf("%s[%d]: Invalid cast from \"%s\" to \"%s\"\n", func, line,
  147. rtgui_type_name_get(obj->type), rtgui_type_name_get(obj_type));
  148. }
  149. return obj;
  150. }
  151. RTM_EXPORT(rtgui_object_check_cast);
  152. /**
  153. * @brief Gets the type of the object
  154. *
  155. * @param object an object
  156. * @return the type of the object (RT_NULL on failure)
  157. */
  158. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object)
  159. {
  160. if (!object) return RT_NULL;
  161. return object->type;
  162. }
  163. RTM_EXPORT(rtgui_object_object_type_get);
  164. void rtgui_object_set_event_handler(struct rtgui_object *object, rtgui_event_handler_ptr handler)
  165. {
  166. RT_ASSERT(object != RT_NULL);
  167. object->event_handler = handler;
  168. }
  169. RTM_EXPORT(rtgui_object_set_event_handler);
  170. rt_bool_t rtgui_object_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  171. {
  172. return RT_FALSE;
  173. }
  174. RTM_EXPORT(rtgui_object_event_handler);
  175. void rtgui_object_set_id(struct rtgui_object *object, rt_uint32_t id)
  176. {
  177. #ifdef RTGUI_USING_ID_CHECK
  178. struct rtgui_object *obj = rtgui_get_self_object(id);
  179. RT_ASSERT(!obj);
  180. #endif
  181. object->id = id;
  182. }
  183. RTM_EXPORT(rtgui_object_set_id);
  184. rt_uint32_t rtgui_object_get_id(struct rtgui_object *object)
  185. {
  186. return object->id;
  187. }
  188. RTM_EXPORT(rtgui_object_get_id);