interface.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 __DIRECT__INTERFACE_H__
  24. #define __DIRECT__INTERFACE_H__
  25. #include <direct/debug.h>
  26. #include <direct/mem.h>
  27. /*
  28. * Forward declaration macro for interfaces.
  29. */
  30. #define DECLARE_INTERFACE( IFACE ) \
  31. typedef struct _##IFACE IFACE;
  32. /*
  33. * Macro for an interface definition.
  34. */
  35. #define DEFINE_INTERFACE( IFACE, IDATA... ) \
  36. struct _##IFACE { \
  37. void *priv; \
  38. int magic; \
  39. \
  40. DirectResult (*AddRef)( IFACE *thiz ); \
  41. DirectResult (*Release)( IFACE *thiz ); \
  42. \
  43. IDATA \
  44. };
  45. /*
  46. * Declare base interface
  47. */
  48. DECLARE_INTERFACE( IAny )
  49. /*
  50. * Define base interface
  51. */
  52. DEFINE_INTERFACE( IAny, )
  53. /*
  54. * Function type for probing of interface implementations
  55. */
  56. typedef DirectResult (*DirectInterfaceGenericProbeFunc)( void *ctx, ... );
  57. /*
  58. * Function type for initialization of interface instances
  59. */
  60. typedef DirectResult (*DirectInterfaceGenericConstructFunc)( void *interface, ... );
  61. /*
  62. * Function table for interface implementations
  63. */
  64. typedef struct {
  65. const char * (*GetType)(void);
  66. const char * (*GetImplementation)(void);
  67. DirectResult (*Allocate)( void **interface );
  68. DirectInterfaceGenericProbeFunc Probe;
  69. DirectInterfaceGenericConstructFunc Construct;
  70. } DirectInterfaceFuncs;
  71. /*
  72. * Callback type for user probing interface implementations
  73. */
  74. typedef DirectResult (*DirectInterfaceProbeFunc)( DirectInterfaceFuncs *impl, void *ctx );
  75. /*
  76. * Loads an interface of a specific 'type'.
  77. * Optionally an 'implementation' can be chosen.
  78. * A 'probe' function can be used to check available implementations.
  79. *
  80. * After success 'funcs' is set.
  81. */
  82. DirectResult DirectGetInterface( DirectInterfaceFuncs **funcs,
  83. const char *type,
  84. const char *implementation,
  85. DirectInterfaceProbeFunc probe,
  86. void *probe_ctx );
  87. /*
  88. * Default probe function. Calls "funcs->Probe(ctx)".
  89. * Can be used as the 'probe' argument to DirectGetInterface.
  90. * 'probe_ctx' should then be set to the interface specific probe context.
  91. */
  92. DirectResult DirectProbeInterface( DirectInterfaceFuncs *funcs, void *ctx );
  93. /*
  94. * Called by implementation modules during 'dlopen'ing or at startup if linked
  95. * into the executable.
  96. */
  97. void DirectRegisterInterface( DirectInterfaceFuncs *funcs );
  98. void DirectUnregisterInterface( DirectInterfaceFuncs *funcs );
  99. void direct_print_interface_leaks(void);
  100. #if DIRECT_BUILD_DEBUGS
  101. void direct_dbg_interface_add ( const char *func,
  102. const char *file,
  103. int line,
  104. const char *what,
  105. const void *interface,
  106. const char *name );
  107. void direct_dbg_interface_remove( const char *func,
  108. const char *file,
  109. int line,
  110. const char *what,
  111. const void *interface );
  112. #endif
  113. #if DIRECT_BUILD_DEBUG || defined(DIRECT_ENABLE_DEBUG) || defined(DIRECT_FORCE_DEBUG)
  114. #if !DIRECT_BUILD_DEBUGS
  115. #error Building with debug, but library headers suggest that debug is not supported.
  116. #endif
  117. #define DIRECT_DBG_INTERFACE_ADD direct_dbg_interface_add
  118. #define DIRECT_DBG_INTERFACE_REMOVE direct_dbg_interface_remove
  119. #else
  120. #define DIRECT_DBG_INTERFACE_ADD(func,file,line,what,interface,name) do {} while (0)
  121. #define DIRECT_DBG_INTERFACE_REMOVE(func,file,line,what,interface) do {} while (0)
  122. #endif
  123. #define DIRECT_ALLOCATE_INTERFACE(p,i) \
  124. do { \
  125. (p) = (__typeof__(p))D_CALLOC( 1, sizeof(i) ); \
  126. \
  127. D_MAGIC_SET( (IAny*)(p), DirectInterface ); \
  128. \
  129. DIRECT_DBG_INTERFACE_ADD( __FUNCTION__, __FUNCTION__, __LINE__, #p, p, #i ); \
  130. } while (0)
  131. #define DIRECT_ALLOCATE_INTERFACE_DATA(p,i) \
  132. i##_data *data; \
  133. \
  134. D_MAGIC_ASSERT( (IAny*)(p), DirectInterface ); \
  135. \
  136. if (!(p)->priv) \
  137. (p)->priv = D_CALLOC( 1, sizeof(i##_data) ); \
  138. \
  139. data = (i##_data*)((p)->priv);
  140. #define DIRECT_DEALLOCATE_INTERFACE(p) \
  141. DIRECT_DBG_INTERFACE_REMOVE( __FUNCTION__, __FUNCTION__, __LINE__, #p, p ); \
  142. \
  143. if ((p)->priv) { \
  144. D_FREE( (p)->priv ); \
  145. (p)->priv = NULL; \
  146. } \
  147. \
  148. D_MAGIC_CLEAR( (IAny*)(p) ); \
  149. \
  150. D_FREE( (p) );
  151. #define DIRECT_INTERFACE_GET_DATA(i) \
  152. i##_data *data; \
  153. \
  154. if (!thiz) \
  155. return DR_THIZNULL; \
  156. \
  157. D_MAGIC_ASSERT( (IAny*)thiz, DirectInterface ); \
  158. \
  159. data = (i##_data*) thiz->priv; \
  160. \
  161. if (!data) \
  162. return DR_DEAD;
  163. #define DIRECT_INTERFACE_GET_DATA_FROM(interface,data,prefix) \
  164. do { \
  165. D_MAGIC_ASSERT( (IAny*)(interface), DirectInterface ); \
  166. \
  167. (data) = (prefix##_data*) (interface)->priv; \
  168. \
  169. if (!(data)) \
  170. return DR_DEAD; \
  171. } while (0)
  172. #endif