ftobject.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include <ft2build.h>
  2. #include FT_INTERNAL_OBJECT_H
  3. #include FT_INTERNAL_DEBUG_H
  4. #include FT_INTERNAL_OBJECTS_H
  5. #define FT_MAGIC_DEATH 0xDEADdead
  6. #define FT_MAGIC_CLASS 0x12345678
  7. #define FT_TYPE_HASH(x) (( (FT_UInt32)(x) >> 2 )^( (FT_UInt32)(x) >> 10 ))
  8. #define FT_OBJECT_CHECK(o) \
  9. ( FT_OBJECT(o) != NULL && \
  10. FT_OBJECT(o)->clazz != NULL && \
  11. FT_OBJECT(o)->ref_count >= 1 && \
  12. FT_OBJECT(o)->clazz->magic == FT_MAGIC_CLASS )
  13. #define FT_CLASS_CHECK(c) \
  14. ( FT_CLASS(c) != NULL && FT_CLASS(c)->magic == FT_MAGIC_CLASS )
  15. #define FT_ASSERT_IS_CLASS(c) FT_ASSERT( FT_CLASS_CHECK(c) )
  16. /*******************************************************************/
  17. /*******************************************************************/
  18. /***** *****/
  19. /***** *****/
  20. /***** M E T A - C L A S S *****/
  21. /***** *****/
  22. /***** *****/
  23. /*******************************************************************/
  24. /*******************************************************************/
  25. /* forward declaration */
  26. FT_BASE_DEF( FT_Error )
  27. ft_metaclass_init( FT_MetaClass meta,
  28. FT_Library library );
  29. /* forward declaration */
  30. FT_BASE_DEF( void )
  31. ft_metaclass_done( FT_MetaClass meta );
  32. /* class type for the meta-class itself */
  33. static const FT_TypeRec ft_meta_class_type =
  34. {
  35. "FT2.MetaClass",
  36. NULL,
  37. sizeof( FT_MetaClassRec ),
  38. (FT_Object_InitFunc) ft_metaclass_init,
  39. (FT_Object_DoneFunc) ft_metaclass_done,
  40. sizeof( FT_ClassRec ),
  41. (FT_Object_InitFunc) NULL,
  42. (FT_Object_DoneFunc) NULL
  43. };
  44. /* destroy a given class */
  45. static void
  46. ft_class_hnode_destroy( FT_ClassHNode node )
  47. {
  48. FT_Class clazz = node->clazz;
  49. FT_Memory memory = clazz->memory;
  50. if ( clazz->class_done )
  51. clazz->class_done( (FT_Object) clazz );
  52. FT_FREE( clazz );
  53. node->clazz = NULL;
  54. node->type = NULL;
  55. FT_FREE( node );
  56. }
  57. static FT_Int
  58. ft_type_equal( FT_Type type1,
  59. FT_Type type2 )
  60. {
  61. if ( type1 == type2 )
  62. goto Ok;
  63. if ( type1 == NULL || type2 == NULL )
  64. goto Fail;
  65. /* compare parent types */
  66. if ( type1->super != type2->super )
  67. {
  68. if ( type1->super == NULL ||
  69. type2->super == NULL ||
  70. !ft_type_equal( type1, type2 ) )
  71. goto Fail;
  72. }
  73. /* compare type names */
  74. if ( type1->name != type2->name )
  75. {
  76. if ( type1->name == NULL ||
  77. type2->name == NULL ||
  78. ft_strcmp( type1->name, type2->name ) != 0 )
  79. goto Fail;
  80. }
  81. /* compare the other type fields */
  82. if ( type1->class_size != type2->class_size ||
  83. type1->class_init != type2->class_init ||
  84. type1->class_done != type2->class_done ||
  85. type1->obj_size != type2->obj_size ||
  86. type1->obj_init != type2->obj_init ||
  87. type1->obj_done != type2->obj_done )
  88. goto Fail;
  89. Ok:
  90. return 1;
  91. Fail:
  92. return 0;
  93. }
  94. static FT_Int
  95. ft_class_hnode_equal( const FT_ClassHNode node1,
  96. const FT_ClassHNode node2 )
  97. {
  98. FT_Type type1 = node1->type;
  99. FT_Type type2 = node2->type;
  100. /* comparing the pointers should work in 99% of cases */
  101. return ( type1 == type2 ) ? 1 : ft_type_equal( type1, type2 );
  102. }
  103. FT_BASE_DEF( void )
  104. ft_metaclass_done( FT_MetaClass meta )
  105. {
  106. /* clear all classes */
  107. ft_hash_done( &meta->type_to_class,
  108. (FT_Hash_ForeachFunc) ft_class_hnode_destroy,
  109. NULL );
  110. meta->clazz.object.clazz = NULL;
  111. meta->clazz.object.ref_count = 0;
  112. meta->clazz.magic = FT_MAGIC_DEATH;
  113. }
  114. FT_BASE_DEF( FT_Error )
  115. ft_metaclass_init( FT_MetaClass meta,
  116. FT_Library library )
  117. {
  118. FT_ClassRec* clazz = (FT_ClassRec*) &meta->clazz;
  119. /* the meta-class is its OWN class !! */
  120. clazz->object.clazz = (FT_Class) clazz;
  121. clazz->object.ref_count = 1;
  122. clazz->magic = FT_MAGIC_CLASS;
  123. clazz->library = library;
  124. clazz->memory = library->memory;
  125. clazz->type = &ft_meta_class_type;
  126. clazz->info = NULL;
  127. clazz->class_done = (FT_Object_DoneFunc) ft_metaclass_done;
  128. clazz->obj_size = sizeof( FT_ClassRec );
  129. clazz->obj_init = NULL;
  130. clazz->obj_done = NULL;
  131. return ft_hash_init( &meta->type_to_class,
  132. (FT_Hash_EqualFunc) ft_class_hnode_equal,
  133. library->memory );
  134. }
  135. /* find or create the class corresponding to a given type */
  136. /* note that this function will retunr NULL in case of */
  137. /* memory overflow */
  138. /* */
  139. static FT_Class
  140. ft_metaclass_get_class( FT_MetaClass meta,
  141. FT_Type ctype )
  142. {
  143. FT_ClassHNodeRec keynode, *node, **pnode;
  144. FT_Memory memory;
  145. FT_ClassRec* clazz;
  146. FT_Class parent;
  147. FT_Error error;
  148. keynode.hnode.hash = FT_TYPE_HASH( ctype );
  149. keynode.type = ctype;
  150. pnode = (FT_ClassHNode*) ft_hash_lookup( &meta->type_to_class,
  151. (FT_HashNode) &keynode );
  152. node = *pnode;
  153. if ( node != NULL )
  154. {
  155. clazz = (FT_ClassRec*) node->clazz;
  156. goto Exit;
  157. }
  158. memory = FT_CLASS__MEMORY(meta);
  159. clazz = NULL;
  160. parent = NULL;
  161. if ( ctype->super != NULL )
  162. {
  163. FT_ASSERT( ctype->super->class_size <= ctype->class_size );
  164. FT_ASSERT( ctype->super->obj_size <= ctype->obj_size );
  165. parent = ft_metaclass_get_class( meta, ctype->super );
  166. }
  167. if ( !FT_NEW( node ) )
  168. {
  169. if ( !FT_ALLOC( clazz, ctype->class_size ) )
  170. {
  171. if ( parent )
  172. FT_MEM_COPY( (FT_ClassRec*)clazz, parent, parent->type->class_size );
  173. clazz->object.clazz = (FT_Class) meta;
  174. clazz->object.ref_count = 1;
  175. clazz->memory = memory;
  176. clazz->library = FT_CLASS__LIBRARY(meta);
  177. clazz->super = parent;
  178. clazz->type = ctype;
  179. clazz->info = NULL;
  180. clazz->magic = FT_MAGIC_CLASS;
  181. clazz->class_done = ctype->class_done;
  182. clazz->obj_size = ctype->obj_size;
  183. clazz->obj_init = ctype->obj_init;
  184. clazz->obj_done = ctype->obj_done;
  185. if ( parent )
  186. {
  187. if ( clazz->class_done == NULL )
  188. clazz->class_done = parent->class_done;
  189. if ( clazz->obj_init == NULL )
  190. clazz->obj_init = parent->obj_init;
  191. if ( clazz->obj_done == NULL )
  192. clazz->obj_done = parent->obj_done;
  193. }
  194. /* find class initializer, if any */
  195. {
  196. FT_Type ztype = ctype;
  197. FT_Object_InitFunc cinit = NULL;
  198. do
  199. {
  200. cinit = ztype->class_init;
  201. if ( cinit != NULL )
  202. break;
  203. ztype = ztype->super;
  204. }
  205. while ( ztype != NULL );
  206. /* then call it when needed */
  207. if ( cinit != NULL )
  208. error = cinit( (FT_Object) clazz, NULL );
  209. }
  210. }
  211. if (error)
  212. {
  213. if ( clazz )
  214. {
  215. /* we always call the class destructor when */
  216. /* an error was detected in the constructor !! */
  217. if ( clazz->class_done )
  218. clazz->class_done( (FT_Object) clazz );
  219. FT_FREE( clazz );
  220. }
  221. FT_FREE( node );
  222. }
  223. }
  224. Exit:
  225. return (FT_Class) clazz;
  226. }
  227. FT_BASE_DEF( FT_Int )
  228. ft_object_check( FT_Pointer obj )
  229. {
  230. return FT_OBJECT_CHECK(obj);
  231. }
  232. FT_BASE_DEF( FT_Int )
  233. ft_object_is_a( FT_Pointer obj,
  234. FT_Class clazz )
  235. {
  236. if ( FT_OBJECT_CHECK(obj) )
  237. {
  238. FT_Class c = FT_OBJECT__CLASS(obj);
  239. do
  240. {
  241. if ( c == clazz )
  242. return 1;
  243. c = c->super;
  244. }
  245. while ( c == NULL );
  246. return (clazz == NULL);
  247. }
  248. return 0;
  249. }
  250. FT_BASE_DEF( FT_Error )
  251. ft_object_create( FT_Object *pobject,
  252. FT_Class clazz,
  253. FT_Pointer init_data )
  254. {
  255. FT_Memory memory;
  256. FT_Error error;
  257. FT_Object obj;
  258. FT_ASSERT_IS_CLASS(clazz);
  259. memory = FT_CLASS__MEMORY(clazz);
  260. if ( !FT_ALLOC( obj, clazz->obj_size ) )
  261. {
  262. obj->clazz = clazz;
  263. obj->ref_count = 1;
  264. if ( clazz->obj_init )
  265. {
  266. error = clazz->obj_init( obj, init_data );
  267. if ( error )
  268. {
  269. /* IMPORTANT: call the destructor when an error */
  270. /* was detected in the constructor !! */
  271. if ( clazz->obj_done )
  272. clazz->obj_done( obj );
  273. FT_FREE( obj );
  274. }
  275. }
  276. }
  277. *pobject = obj;
  278. return error;
  279. }
  280. FT_BASE_DEF( FT_Class )
  281. ft_class_find_by_type( FT_Type type,
  282. FT_Library library )
  283. {
  284. FT_MetaClass meta = &library->meta_class;
  285. return ft_metaclass_get_class( meta, type );
  286. }
  287. FT_BASE_DEF( FT_Error )
  288. ft_object_create_from_type( FT_Object *pobject,
  289. FT_Type type,
  290. FT_Pointer init_data,
  291. FT_Library library )
  292. {
  293. FT_Class clazz;
  294. FT_Error error;
  295. clazz = ft_class_find_by_type( type, library );
  296. if ( clazz )
  297. error = ft_object_create( pobject, clazz, init_data );
  298. else
  299. {
  300. *pobject = NULL;
  301. error = FT_Err_Out_Of_Memory;
  302. }
  303. return error;
  304. }