object.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. (c) Copyright 2001-2009 The world wide DirectFB Open Source Community (directfb.org)
  3. (c) Copyright 2000-2004 Convergence (integrated media) GmbH
  4. All rights reserved.
  5. Written by Denis Oliver Kropp <dok@directfb.org>,
  6. Andreas Hundt <andi@fischlustig.de>,
  7. Sven Neumann <neo@directfb.org>,
  8. Ville Syrjälä <syrjala@sci.fi> and
  9. Claudio Ciccani <klan@users.sf.net>.
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 2 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, write to the
  20. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. Boston, MA 02111-1307, USA.
  22. */
  23. #ifndef __FUSION__OBJECT_H__
  24. #define __FUSION__OBJECT_H__
  25. #include <fusion/types.h>
  26. #include <fusion/lock.h>
  27. #include <direct/list.h>
  28. #include <fusion/ref.h>
  29. #include <fusion/reactor.h>
  30. #include <direct/debug.h>
  31. typedef void (*FusionObjectDestructor)( FusionObject *object, bool zombie, void *ctx );
  32. typedef bool (*FusionPropIterator)( char *key, void *value, void *ctx);
  33. typedef u32 FusionObjectID;
  34. typedef enum {
  35. FOS_INIT,
  36. FOS_ACTIVE,
  37. FOS_DEINIT
  38. } FusionObjectState;
  39. struct __Fusion_FusionObject {
  40. DirectLink link;
  41. FusionObjectPool *pool;
  42. int magic;
  43. FusionObjectID id;
  44. FusionID identity;
  45. FusionObjectState state;
  46. FusionRef ref;
  47. FusionReactor *reactor;
  48. FusionWorldShared *shared;
  49. FusionHash *properties;
  50. FusionID owner;
  51. };
  52. typedef bool (*FusionObjectCallback)( FusionObjectPool *pool,
  53. FusionObject *object,
  54. void *ctx );
  55. FusionObjectPool *fusion_object_pool_create ( const char *name,
  56. int object_size,
  57. int message_size,
  58. FusionObjectDestructor destructor,
  59. void *ctx,
  60. const FusionWorld *world );
  61. DirectResult fusion_object_pool_destroy( FusionObjectPool *pool,
  62. const FusionWorld *world );
  63. DirectResult fusion_object_pool_enum ( FusionObjectPool *pool,
  64. FusionObjectCallback callback,
  65. void *ctx );
  66. FusionObject *fusion_object_create ( FusionObjectPool *pool,
  67. const FusionWorld *world,
  68. FusionID identity );
  69. DirectResult fusion_object_get ( FusionObjectPool *pool,
  70. FusionObjectID object_id,
  71. FusionObject **ret_object );
  72. DirectResult fusion_object_set_lock( FusionObject *object,
  73. FusionSkirmish *lock );
  74. DirectResult fusion_object_activate( FusionObject *object );
  75. DirectResult fusion_object_destroy ( FusionObject *object );
  76. DirectResult fusion_object_set_property( FusionObject *object ,
  77. const char *key, void *value, void **old_value);
  78. DirectResult fusion_object_set_int_property( FusionObject *object ,
  79. const char *key,int value);
  80. DirectResult fusion_object_set_string_property( FusionObject *object ,
  81. const char *key,char *value);
  82. void *fusion_object_get_property( FusionObject *object ,const char *key);
  83. void fusion_object_remove_property( FusionObject *object ,const char *key,void **ret_val);
  84. #define FUSION_OBJECT_METHODS(type, prefix) \
  85. \
  86. static inline DirectResult \
  87. prefix##_attach( type *object, \
  88. ReactionFunc func, \
  89. void *ctx, \
  90. Reaction *ret_reaction ) \
  91. { \
  92. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  93. return fusion_reactor_attach( ((FusionObject*)object)->reactor, \
  94. func, ctx, ret_reaction ); \
  95. } \
  96. \
  97. static inline DirectResult \
  98. prefix##_attach_channel( type *object, \
  99. int channel, \
  100. ReactionFunc func, \
  101. void *ctx, \
  102. Reaction *ret_reaction ) \
  103. { \
  104. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  105. return fusion_reactor_attach_channel( ((FusionObject*)object)->reactor, \
  106. channel, func, ctx, ret_reaction ); \
  107. } \
  108. \
  109. static inline DirectResult \
  110. prefix##_detach( type *object, \
  111. Reaction *reaction ) \
  112. { \
  113. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  114. return fusion_reactor_detach( ((FusionObject*)object)->reactor, \
  115. reaction ); \
  116. } \
  117. \
  118. static inline DirectResult \
  119. prefix##_attach_global( type *object, \
  120. int index, \
  121. void *ctx, \
  122. GlobalReaction *reaction ) \
  123. { \
  124. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  125. return fusion_reactor_attach_global( ((FusionObject*)object)->reactor, \
  126. index, ctx, reaction ); \
  127. } \
  128. \
  129. static inline DirectResult \
  130. prefix##_detach_global( type *object, \
  131. GlobalReaction *reaction ) \
  132. { \
  133. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  134. return fusion_reactor_detach_global( ((FusionObject*)object)->reactor, \
  135. reaction ); \
  136. } \
  137. \
  138. static inline DirectResult \
  139. prefix##_dispatch( type *object, \
  140. void *message, \
  141. const ReactionFunc *globals ) \
  142. { \
  143. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  144. return fusion_reactor_dispatch( ((FusionObject*)object)->reactor, \
  145. message, true, globals ); \
  146. } \
  147. \
  148. static inline DirectResult \
  149. prefix##_dispatch_channel( type *object, \
  150. int channel, \
  151. void *message, \
  152. int size, \
  153. const ReactionFunc *globals ) \
  154. { \
  155. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  156. return fusion_reactor_dispatch_channel( ((FusionObject*)object)->reactor, \
  157. channel, message, size, true, globals ); \
  158. } \
  159. \
  160. static inline DirectResult \
  161. prefix##_ref( type *object ) \
  162. { \
  163. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  164. return fusion_ref_up( &((FusionObject*)object)->ref, false ); \
  165. } \
  166. \
  167. static inline DirectResult \
  168. prefix##_unref( type *object ) \
  169. { \
  170. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  171. return fusion_ref_down( &((FusionObject*)object)->ref, false ); \
  172. } \
  173. \
  174. static inline DirectResult \
  175. prefix##_ref_stat( type *object, int *refs ) \
  176. { \
  177. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  178. return fusion_ref_stat ( &((FusionObject*)object)->ref, refs ); \
  179. } \
  180. \
  181. static inline DirectResult \
  182. prefix##_link( type **link, \
  183. type *object ) \
  184. { \
  185. DirectResult ret; \
  186. \
  187. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  188. \
  189. ret = fusion_ref_up( &((FusionObject*)object)->ref, true ); \
  190. if (ret) \
  191. return ret; \
  192. \
  193. *link = object; \
  194. \
  195. return DR_OK; \
  196. } \
  197. \
  198. static inline DirectResult \
  199. prefix##_unlink( type **link ) \
  200. { \
  201. type *object = *link; \
  202. \
  203. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  204. \
  205. *link = NULL; \
  206. \
  207. return fusion_ref_down( &((FusionObject*)object)->ref, true ); \
  208. } \
  209. \
  210. static inline DirectResult \
  211. prefix##_inherit( type *object, \
  212. void *from ) \
  213. { \
  214. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  215. D_MAGIC_ASSERT( (FusionObject*) from, FusionObject ); \
  216. \
  217. return fusion_ref_inherit( &((FusionObject*)object)->ref, \
  218. &((FusionObject*)from)->ref ); \
  219. } \
  220. \
  221. static inline DirectResult \
  222. prefix##_globalize( type *object ) \
  223. { \
  224. DirectResult ret; \
  225. \
  226. D_MAGIC_ASSERT( (FusionObject*) object, FusionObject ); \
  227. \
  228. ret = fusion_ref_up( &((FusionObject*)object)->ref, true ); \
  229. if (ret) \
  230. return ret; \
  231. \
  232. ret = fusion_ref_down( &((FusionObject*)object)->ref, false ); \
  233. if (ret) \
  234. fusion_ref_down( &((FusionObject*)object)->ref, true ); \
  235. \
  236. return ret; \
  237. }
  238. FUSION_OBJECT_METHODS( void, fusion_object )
  239. #endif