rtgui_object.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * File : rtgui_object.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-04 Bernard first version
  23. */
  24. #include <rtgui/rtgui_object.h>
  25. #include <rtgui/rtgui_system.h>
  26. static void _rtgui_object_constructor(rtgui_object_t *object)
  27. {
  28. if (!object)
  29. return;
  30. object->flag = RTGUI_OBJECT_FLAG_VALID;
  31. object->id = (rt_uint32_t)object;
  32. }
  33. /* Destroys the object */
  34. static void _rtgui_object_destructor(rtgui_object_t *object)
  35. {
  36. /* Any valid objest should both have valid flag _and_ valid type. Only use
  37. * flag is not enough because the chunk of memory may be reallocted to other
  38. * object and thus the flag will become valid. */
  39. object->flag = RTGUI_OBJECT_FLAG_NONE;
  40. object->type = RT_NULL;
  41. }
  42. DEFINE_CLASS_TYPE(object, "object",
  43. RT_NULL,
  44. _rtgui_object_constructor,
  45. _rtgui_object_destructor,
  46. sizeof(struct rtgui_object));
  47. RTM_EXPORT(_rtgui_object);
  48. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object)
  49. {
  50. /* construct from parent to children */
  51. if (type->parent != RT_NULL)
  52. rtgui_type_object_construct(type->parent, object);
  53. if (type->constructor)
  54. type->constructor(object);
  55. }
  56. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object)
  57. {
  58. /* destruct from children to parent */
  59. if (type->destructor)
  60. type->destructor(object);
  61. if (type->parent)
  62. rtgui_type_destructors_call(type->parent, object);
  63. }
  64. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent)
  65. {
  66. const rtgui_type_t *t;
  67. t = type;
  68. while (t)
  69. {
  70. if (t == parent) return RT_TRUE;
  71. t = t->parent;
  72. }
  73. return RT_FALSE;
  74. }
  75. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type)
  76. {
  77. return type->parent;
  78. }
  79. const char *rtgui_type_name_get(const rtgui_type_t *type)
  80. {
  81. if (!type) return RT_NULL;
  82. return type->name;
  83. }
  84. #ifdef RTGUI_OBJECT_TRACE
  85. struct rtgui_object_information
  86. {
  87. rt_uint32_t objs_number;
  88. rt_uint32_t allocated_size;
  89. rt_uint32_t max_allocated;
  90. };
  91. struct rtgui_object_information obj_info = {0, 0, 0};
  92. #endif
  93. /**
  94. * @brief Creates a new object: it calls the corresponding constructors
  95. * (from the constructor of the base class to the constructor of the more
  96. * derived class) and then sets the values of the given properties
  97. *
  98. * @param object_type the type of object to create
  99. * @return the created object
  100. */
  101. rtgui_object_t *rtgui_object_create(const rtgui_type_t *object_type)
  102. {
  103. rtgui_object_t *new_object;
  104. if (!object_type)
  105. return RT_NULL;
  106. new_object = rtgui_malloc(object_type->size);
  107. if (new_object == RT_NULL) return RT_NULL;
  108. #ifdef RTGUI_OBJECT_TRACE
  109. obj_info.objs_number ++;
  110. obj_info.allocated_size += object_type->size;
  111. if (obj_info.allocated_size > obj_info.max_allocated)
  112. obj_info.max_allocated = obj_info.allocated_size;
  113. #endif
  114. new_object->type = object_type;
  115. rtgui_type_object_construct(object_type, new_object);
  116. return new_object;
  117. }
  118. RTM_EXPORT(rtgui_object_create);
  119. /**
  120. * @brief Destroys the object.
  121. *
  122. * The object destructors will be called in inherited type order.
  123. *
  124. * @param object the object to destroy
  125. */
  126. void rtgui_object_destroy(rtgui_object_t *object)
  127. {
  128. if (!object || object->flag & RTGUI_OBJECT_FLAG_STATIC)
  129. return;
  130. #ifdef RTGUI_OBJECT_TRACE
  131. obj_info.objs_number --;
  132. obj_info.allocated_size -= object->type->size;
  133. #endif
  134. /* call destructor */
  135. RT_ASSERT(object->type != RT_NULL);
  136. rtgui_type_destructors_call(object->type, object);
  137. /* release object */
  138. rtgui_free(object);
  139. }
  140. RTM_EXPORT(rtgui_object_destroy);
  141. /**
  142. * @brief Checks if the object can be cast to the specified type.
  143. *
  144. * If the object doesn't inherit from the specified type, a warning
  145. * is displayed in the console but the object is returned anyway.
  146. *
  147. * @param object the object to cast
  148. * @param type the type to which we cast the object
  149. * @return Returns the object
  150. */
  151. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *obj, const rtgui_type_t *obj_type, const char *func, int line)
  152. {
  153. if (!obj) return RT_NULL;
  154. if (!rtgui_type_inherits_from(obj->type, obj_type))
  155. {
  156. rt_kprintf("%s[%d]: Invalid cast from \"%s\" to \"%s\"\n", func, line,
  157. rtgui_type_name_get(obj->type), rtgui_type_name_get(obj_type));
  158. }
  159. return obj;
  160. }
  161. RTM_EXPORT(rtgui_object_check_cast);
  162. /**
  163. * @brief Gets the type of the object
  164. *
  165. * @param object an object
  166. * @return the type of the object (RT_NULL on failure)
  167. */
  168. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object)
  169. {
  170. if (!object) return RT_NULL;
  171. return object->type;
  172. }
  173. RTM_EXPORT(rtgui_object_object_type_get);
  174. void rtgui_object_set_event_handler(struct rtgui_object *object, rtgui_event_handler_ptr handler)
  175. {
  176. RT_ASSERT(object != RT_NULL);
  177. object->event_handler = handler;
  178. }
  179. RTM_EXPORT(rtgui_object_set_event_handler);
  180. rt_bool_t rtgui_object_event_handler(struct rtgui_object *object, struct rtgui_event *event)
  181. {
  182. return RT_FALSE;
  183. }
  184. RTM_EXPORT(rtgui_object_event_handler);
  185. void rtgui_object_set_id(struct rtgui_object *object, rt_uint32_t id)
  186. {
  187. #ifdef RTGUI_USING_ID_CHECK
  188. struct rtgui_object *obj = rtgui_get_self_object(id);
  189. RT_ASSERT(!obj);
  190. #endif
  191. object->id = id;
  192. }
  193. RTM_EXPORT(rtgui_object_set_id);
  194. rt_uint32_t rtgui_object_get_id(struct rtgui_object *object)
  195. {
  196. return object->id;
  197. }
  198. RTM_EXPORT(rtgui_object_get_id);