rtgui_object.c 5.0 KB

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