summaryrefslogtreecommitdiff
path: root/Source/DirectFB/lib/direct/interface.h
blob: 5e6200d4c4572b41efefe91fcd08006b84d2a52d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
   (c) Copyright 2001-2009  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Hundt <andi@fischlustig.de>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#ifndef __DIRECT__INTERFACE_H__
#define __DIRECT__INTERFACE_H__

#include <direct/debug.h>
#include <direct/mem.h>

/*
 * Forward declaration macro for interfaces.
 */
#define DECLARE_INTERFACE( IFACE )                \
     typedef struct _##IFACE IFACE;

/*
 * Macro for an interface definition.
 */
#define DEFINE_INTERFACE( IFACE, IDATA... )       \
     struct _##IFACE {                            \
          void          *priv;                    \
          int            magic;                   \
                                                  \
          DirectResult (*AddRef)( IFACE *thiz );  \
          DirectResult (*Release)( IFACE *thiz ); \
                                                  \
          IDATA                                   \
     };

/*
 * Declare base interface
 */
DECLARE_INTERFACE( IAny )

/*
 * Define base interface
 */
DEFINE_INTERFACE( IAny, )

/*
 * Function type for probing of interface implementations
 */
typedef DirectResult (*DirectInterfaceGenericProbeFunc)( void *ctx, ... );

/*
 * Function type for initialization of interface instances
 */
typedef DirectResult (*DirectInterfaceGenericConstructFunc)( void *interface, ... );

/*
 * Function table for interface implementations
 */
typedef struct {
     const char * (*GetType)(void);
     const char * (*GetImplementation)(void);
     DirectResult (*Allocate)( void **interface );
     
     DirectInterfaceGenericProbeFunc     Probe;
     DirectInterfaceGenericConstructFunc Construct;
} DirectInterfaceFuncs;

/*
 * Callback type for user probing interface implementations
 */
typedef DirectResult (*DirectInterfaceProbeFunc)( DirectInterfaceFuncs *impl, void *ctx );

/*
 * Loads an interface of a specific 'type'.
 * Optionally an 'implementation' can be chosen.
 * A 'probe' function can be used to check available implementations.
 *
 * After success 'funcs' is set.
 */
DirectResult DirectGetInterface( DirectInterfaceFuncs     **funcs,
                                 const char                *type,
                                 const char                *implementation,
                                 DirectInterfaceProbeFunc   probe,
                                 void                      *probe_ctx );

/*
 * Default probe function. Calls "funcs->Probe(ctx)".
 * Can be used as the 'probe' argument to DirectGetInterface.
 * 'probe_ctx' should then be set to the interface specific probe context.
 */
DirectResult DirectProbeInterface( DirectInterfaceFuncs *funcs, void *ctx );

/*
 * Called by implementation modules during 'dlopen'ing or at startup if linked
 * into the executable.
 */
void DirectRegisterInterface( DirectInterfaceFuncs *funcs );

void DirectUnregisterInterface( DirectInterfaceFuncs *funcs );

void direct_print_interface_leaks(void);

#if DIRECT_BUILD_DEBUGS
void direct_dbg_interface_add   ( const char *func,
                                  const char *file,
                                  int         line,
                                  const char *what,
                                  const void *interface,
                                  const char *name );

void direct_dbg_interface_remove( const char *func,
                                  const char *file,
                                  int         line,
                                  const char *what,
                                  const void *interface );
#endif

#if DIRECT_BUILD_DEBUG || defined(DIRECT_ENABLE_DEBUG) || defined(DIRECT_FORCE_DEBUG)

#if !DIRECT_BUILD_DEBUGS
#error Building with debug, but library headers suggest that debug is not supported.
#endif

#define DIRECT_DBG_INTERFACE_ADD        direct_dbg_interface_add
#define DIRECT_DBG_INTERFACE_REMOVE     direct_dbg_interface_remove

#else

#define DIRECT_DBG_INTERFACE_ADD(func,file,line,what,interface,name)  do {} while (0)
#define DIRECT_DBG_INTERFACE_REMOVE(func,file,line,what,interface)    do {} while (0)

#endif



#define DIRECT_ALLOCATE_INTERFACE(p,i)                                               \
     do {                                                                            \
          (p) = (__typeof__(p))D_CALLOC( 1, sizeof(i) );                             \
                                                                                     \
          D_MAGIC_SET( (IAny*)(p), DirectInterface );                                \
                                                                                     \
          DIRECT_DBG_INTERFACE_ADD( __FUNCTION__, __FILE__, __LINE__, #p, p, #i );   \
     } while (0)


#define DIRECT_ALLOCATE_INTERFACE_DATA(p,i)                                          \
     i##_data *data;                                                                 \
                                                                                     \
     D_MAGIC_ASSERT( (IAny*)(p), DirectInterface );                                  \
                                                                                     \
     if (!(p)->priv)                                                                 \
          (p)->priv = D_CALLOC( 1, sizeof(i##_data) );                               \
                                                                                     \
     data = (i##_data*)((p)->priv);


#define DIRECT_DEALLOCATE_INTERFACE(p)                                               \
     DIRECT_DBG_INTERFACE_REMOVE( __FUNCTION__, __FILE__, __LINE__, #p, p );         \
                                                                                     \
     if ((p)->priv) {                                                                \
          D_FREE( (p)->priv );                                                       \
          (p)->priv = NULL;                                                          \
     }                                                                               \
                                                                                     \
     D_MAGIC_CLEAR( (IAny*)(p) );                                                    \
                                                                                     \
     D_FREE( (p) );


#define DIRECT_INTERFACE_GET_DATA(i)                                                 \
     i##_data *data;                                                                 \
                                                                                     \
     if (!thiz)                                                                      \
          return DR_THIZNULL;                                                        \
                                                                                     \
     D_MAGIC_ASSERT( (IAny*)thiz, DirectInterface );                                 \
                                                                                     \
     data = (i##_data*) thiz->priv;                                                  \
                                                                                     \
     if (!data)                                                                      \
          return DR_DEAD;


#define DIRECT_INTERFACE_GET_DATA_FROM(interface,data,prefix)                        \
     do {                                                                            \
          D_MAGIC_ASSERT( (IAny*)(interface), DirectInterface );                     \
                                                                                     \
          (data) = (prefix##_data*) (interface)->priv;                               \
                                                                                     \
          if (!(data))                                                               \
               return DR_DEAD;                                                       \
     } while (0)


#endif