Added a few C++ operators to Direct3D structures, and made some unions
[wine] / dlls / ole32 / bindctx.c
1 /***************************************************************************************
2  *                            BindCtx implementation
3  *
4  *  Copyright 1999  Noomen Hamza
5  ***************************************************************************************/
6
7 #include <string.h>
8 #include <assert.h>
9 #include "winerror.h"
10 #include "wine/obj_moniker.h"
11 #include "debugtools.h"
12 #include "heap.h"
13
14 DEFAULT_DEBUG_CHANNEL(ole)
15
16 /* represent the first size table and it's increment block size */
17 #define  BLOCK_TAB_SIZE 10 
18 #define  MAX_TAB_SIZE   0xFFFFFFFF
19
20 /* data structure of the BindCtx table elements */
21 typedef struct BindCtxObject{
22
23     IUnknown*   pObj; /* point on a bound object */
24
25     LPOLESTR  pkeyObj; /* key associated to this bound object */
26
27     BYTE regType; /* registration type: 1 if RegisterObjectParam and 0 if RegisterObjectBound */
28
29 } BindCtxObject;
30
31 /* BindCtx data strucrture */
32 typedef struct BindCtxImpl{
33
34     ICOM_VFIELD(IBindCtx); /* VTable relative to the IBindCtx interface.*/
35                                      
36     ULONG ref; /* reference counter for this object */
37
38     BindCtxObject* bindCtxTable; /* this is a table in witch all bounded objects are stored*/
39     DWORD          bindCtxTableLastIndex;  /* first free index in the table */
40     DWORD          bindCtxTableSize;   /* size table */
41
42     BIND_OPTS2 bindOption2; /* a structure witch contains the bind options*/
43
44 } BindCtxImpl;
45
46 /* IBindCtx prototype functions : */
47
48 /* IUnknown functions*/
49 static HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject);
50 static ULONG   WINAPI BindCtxImpl_AddRef(IBindCtx* iface);
51 static ULONG   WINAPI BindCtxImpl_Release(IBindCtx* iface);
52 /* IBindCtx functions */
53 static HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk);
54 static HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk);
55 static HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface);
56 static HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
57 static HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts);
58 static HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot);
59 static HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk);
60 static HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk);
61 static HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** ppenum);
62 static HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR pszkey);
63 /* Local functions*/
64 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This);
65 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This);
66 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,IUnknown* punk,LPOLESTR pszkey,DWORD *index);
67
68 /* Virtual function table for the BindCtx class. */
69 static ICOM_VTABLE(IBindCtx) VT_BindCtxImpl =
70     {
71     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
72     BindCtxImpl_QueryInterface,
73     BindCtxImpl_AddRef,
74     BindCtxImpl_Release,
75     BindCtxImpl_RegisterObjectBound,
76     BindCtxImpl_RevokeObjectBound,
77     BindCtxImpl_ReleaseBoundObjects,
78     BindCtxImpl_SetBindOptions,
79     BindCtxImpl_GetBindOptions,
80     BindCtxImpl_GetRunningObjectTable,
81     BindCtxImpl_RegisterObjectParam,
82     BindCtxImpl_GetObjectParam,
83     BindCtxImpl_EnumObjectParam,
84     BindCtxImpl_RevokeObjectParam
85 };
86
87 /*******************************************************************************
88  *        BindCtx_QueryInterface
89  *******************************************************************************/
90 HRESULT WINAPI BindCtxImpl_QueryInterface(IBindCtx* iface,REFIID riid,void** ppvObject)
91 {
92   ICOM_THIS(BindCtxImpl,iface);
93
94   TRACE("(%p,%p,%p)\n",This,riid,ppvObject);
95
96   /* Perform a sanity check on the parameters.*/
97   if ( (This==0) || (ppvObject==0) )
98       return E_INVALIDARG;
99   
100   /* Initialize the return parameter.*/
101   *ppvObject = 0;
102
103   /* Compare the riid with the interface IDs implemented by this object.*/
104   if (IsEqualIID(&IID_IUnknown, riid))
105       *ppvObject = (IBindCtx*)This;
106   else
107       if (IsEqualIID(&IID_IBindCtx, riid))
108           *ppvObject = (IBindCtx*)This;
109
110   /* Check that we obtained an interface.*/
111   if ((*ppvObject)==0)
112       return E_NOINTERFACE;
113   
114    /* Query Interface always increases the reference count by one when it is successful */
115   BindCtxImpl_AddRef(iface);
116
117   return S_OK;
118 }
119
120 /******************************************************************************
121  *       BindCtx_AddRef
122  ******************************************************************************/
123 ULONG WINAPI BindCtxImpl_AddRef(IBindCtx* iface)
124 {
125     ICOM_THIS(BindCtxImpl,iface);
126
127     TRACE("(%p)\n",This);
128
129     return ++(This->ref);
130 }
131
132 /******************************************************************************
133  *        BindCtx_Release
134  ******************************************************************************/
135 ULONG WINAPI BindCtxImpl_Release(IBindCtx* iface)
136 {
137     ICOM_THIS(BindCtxImpl,iface);
138
139     TRACE("(%p)\n",This);
140
141     This->ref--;
142
143     if (This->ref==0){
144
145         /* release all registered objects */
146         BindCtxImpl_ReleaseBoundObjects((IBindCtx*)This);
147
148         BindCtxImpl_Destroy(This);
149
150         return 0;
151     }
152     return This->ref;;
153 }
154
155
156 /******************************************************************************
157  *         BindCtx_Construct (local function)
158  *******************************************************************************/
159 HRESULT WINAPI BindCtxImpl_Construct(BindCtxImpl* This)
160 {
161     TRACE("(%p)\n",This);
162
163     /* Initialize the virtual function table.*/
164     ICOM_VTBL(This)    = &VT_BindCtxImpl;
165     This->ref          = 0;
166
167     /* Initialize the BIND_OPTS2 structure */
168     This->bindOption2.cbStruct  = sizeof(BIND_OPTS2);
169     This->bindOption2.grfFlags = 0;
170     This->bindOption2.grfMode = STGM_READWRITE;
171     This->bindOption2.dwTickCountDeadline = 0;
172
173     This->bindOption2.dwTrackFlags = 0;
174     This->bindOption2.dwClassContext = CLSCTX_SERVER;
175     This->bindOption2.locale = 1033;
176     This->bindOption2.pServerInfo = 0;
177
178     /* Initialize the bindctx table */
179     This->bindCtxTableSize=BLOCK_TAB_SIZE;
180     This->bindCtxTableLastIndex=0;
181     This->bindCtxTable= HeapAlloc(GetProcessHeap(), 0,This->bindCtxTableSize*sizeof(BindCtxObject));
182
183     if (This->bindCtxTable==NULL)
184         return E_OUTOFMEMORY;
185
186     return S_OK;
187 }
188
189 /******************************************************************************
190  *        BindCtx_Destroy    (local function)
191  *******************************************************************************/
192 HRESULT WINAPI BindCtxImpl_Destroy(BindCtxImpl* This)
193 {
194     TRACE("(%p)\n",This);
195
196     /* free the table space memory */
197     HeapFree(GetProcessHeap(),0,This->bindCtxTable);
198
199     /* free the bindctx structure */
200     HeapFree(GetProcessHeap(),0,This);
201
202     return S_OK;
203 }
204
205
206 /******************************************************************************
207  *        BindCtx_RegisterObjectBound
208  ******************************************************************************/
209 HRESULT WINAPI BindCtxImpl_RegisterObjectBound(IBindCtx* iface,IUnknown* punk)
210 {
211
212     ICOM_THIS(BindCtxImpl,iface);
213     DWORD lastIndex=This->bindCtxTableLastIndex;
214     BindCtxObject cell;
215
216     TRACE("(%p,%p)\n",This,punk);
217
218     if (punk==NULL)
219         return E_POINTER;
220     
221     IUnknown_AddRef(punk);
222     
223     /* put the object in the first free element in the table */
224     This->bindCtxTable[lastIndex].pObj = punk;
225     This->bindCtxTable[lastIndex].pkeyObj = NULL;
226     This->bindCtxTable[lastIndex].regType = 0;
227     cell=This->bindCtxTable[lastIndex];
228     lastIndex= ++This->bindCtxTableLastIndex;
229
230     if (lastIndex == This->bindCtxTableSize){ /* the table is full so it must be resized */
231
232         if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
233             FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
234             return E_FAIL;
235 }
236
237         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
238
239         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
240                                          This->bindCtxTableSize * sizeof(BindCtxObject));
241         if (!This->bindCtxTable)
242             return E_OUTOFMEMORY;
243     }
244     return S_OK;
245 }
246
247 /******************************************************************************
248  *        BindCtx_RevokeObjectBound
249  ******************************************************************************/
250 HRESULT WINAPI BindCtxImpl_RevokeObjectBound(IBindCtx* iface, IUnknown* punk)
251 {
252     DWORD index,j;
253
254     ICOM_THIS(BindCtxImpl,iface);
255
256     TRACE("(%p,%p)\n",This,punk);
257
258     /* check if the object was registred or not */
259     if (BindCtxImpl_GetObjectIndex(This,punk,NULL,&index)==S_FALSE)
260         
261         return MK_E_NOTBOUND;
262
263     IUnknown_Release(This->bindCtxTable[index].pObj);
264     
265     /* left-shift all elements in the rigth side of the curent revoked object */
266     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
267         This->bindCtxTable[j]= This->bindCtxTable[j+1];
268     
269     This->bindCtxTableLastIndex--;
270
271     return S_OK;
272 }
273
274 /******************************************************************************
275  *        BindCtx_ReleaseBoundObjects
276  ******************************************************************************/
277 HRESULT WINAPI BindCtxImpl_ReleaseBoundObjects(IBindCtx* iface)
278 {
279     DWORD i;
280
281     ICOM_THIS(BindCtxImpl,iface);
282
283     TRACE("(%p)\n",This);
284
285     for(i=0;i<This->bindCtxTableLastIndex;i++)
286        IUnknown_Release(This->bindCtxTable[i].pObj);
287
288     This->bindCtxTableLastIndex = 0;
289
290     return S_OK;
291 }
292
293 /******************************************************************************
294  *        BindCtx_SetBindOptions
295  ******************************************************************************/
296 HRESULT WINAPI BindCtxImpl_SetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
297 {
298     ICOM_THIS(BindCtxImpl,iface);
299
300     TRACE("(%p,%p)\n",This,pbindopts);
301
302     if (pbindopts==NULL)
303         return E_POINTER;
304     
305     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
306     {
307         WARN("invalid size");
308         return E_INVALIDARG; /* FIXME : not verified */
309     }
310     memcpy(&This->bindOption2, pbindopts, pbindopts->cbStruct);
311     return S_OK;
312 }
313
314 /******************************************************************************
315  *        BindCtx_GetBindOptions
316  ******************************************************************************/
317 HRESULT WINAPI BindCtxImpl_GetBindOptions(IBindCtx* iface,LPBIND_OPTS2 pbindopts)
318 {
319     ICOM_THIS(BindCtxImpl,iface);
320
321     TRACE("(%p,%p)\n",This,pbindopts);
322
323     if (pbindopts==NULL)
324         return E_POINTER;
325
326     if (pbindopts->cbStruct > sizeof(BIND_OPTS2))
327     {
328         WARN("invalid size");
329         return E_INVALIDARG; /* FIXME : not verified */
330     }
331     memcpy(pbindopts, &This->bindOption2, pbindopts->cbStruct);
332     return S_OK;
333 }
334
335 /******************************************************************************
336  *        BindCtx_GetRunningObjectTable
337  ******************************************************************************/
338 HRESULT WINAPI BindCtxImpl_GetRunningObjectTable(IBindCtx* iface,IRunningObjectTable** pprot)
339 {
340     HRESULT res;
341
342     ICOM_THIS(BindCtxImpl,iface);
343
344     TRACE("(%p,%p)\n",This,pprot);
345
346     if (pprot==NULL)
347         return E_POINTER;
348     
349     res=GetRunningObjectTable(0, pprot);
350
351     return res;
352 }
353
354 /******************************************************************************
355  *        BindCtx_RegisterObjectParam
356  ******************************************************************************/
357 HRESULT WINAPI BindCtxImpl_RegisterObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown* punk)
358 {
359     ICOM_THIS(BindCtxImpl,iface);
360
361     TRACE("(%p,%p,%p)\n",This,pszkey,punk);
362
363     if (punk==NULL)
364         return E_INVALIDARG;
365     
366     IUnknown_AddRef(punk);
367
368     This->bindCtxTable[This->bindCtxTableLastIndex].pObj = punk;
369     This->bindCtxTable[This->bindCtxTableLastIndex].regType = 1;
370
371     if (pszkey==NULL)
372
373         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=NULL;
374
375     else{
376
377         This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj=
378             HeapAlloc(GetProcessHeap(),0,(sizeof(WCHAR)*(1+lstrlenW(pszkey))));
379
380         if (This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj==NULL)
381             return E_OUTOFMEMORY;
382         lstrcpyW(This->bindCtxTable[This->bindCtxTableLastIndex].pkeyObj,pszkey);
383 }
384
385     This->bindCtxTableLastIndex++;
386     
387     if (This->bindCtxTableLastIndex == This->bindCtxTableSize){ /* table is full ! must be resized */
388
389         This->bindCtxTableSize+=BLOCK_TAB_SIZE; /* new table size */
390
391         if (This->bindCtxTableSize > (MAX_TAB_SIZE-BLOCK_TAB_SIZE)){
392             FIXME("This->bindCtxTableSize: %ld is out of data limite \n",This->bindCtxTableSize);
393             return E_FAIL;
394         }
395         This->bindCtxTable = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,This->bindCtxTable,
396                                          This->bindCtxTableSize * sizeof(BindCtxObject));
397         if (!This->bindCtxTable)
398             return E_OUTOFMEMORY;
399     }
400     return S_OK;
401 }
402 /******************************************************************************
403  *        BindCtx_GetObjectParam
404  ******************************************************************************/
405 HRESULT WINAPI BindCtxImpl_GetObjectParam(IBindCtx* iface,LPOLESTR pszkey, IUnknown** punk)
406 {
407     DWORD index;
408     ICOM_THIS(BindCtxImpl,iface);
409
410     TRACE("(%p,%p,%p)\n",This,pszkey,punk);
411
412     if (punk==NULL)
413         return E_POINTER;
414
415     *punk=0;
416     
417     if (BindCtxImpl_GetObjectIndex(This,NULL,pszkey,&index)==S_FALSE)
418         return E_FAIL;
419
420     IUnknown_AddRef(This->bindCtxTable[index].pObj);
421     
422     *punk = This->bindCtxTable[index].pObj;
423
424     return S_OK;
425 }
426
427 /******************************************************************************
428  *        BindCtx_RevokeObjectParam
429  ******************************************************************************/
430 HRESULT WINAPI BindCtxImpl_RevokeObjectParam(IBindCtx* iface,LPOLESTR ppenum)
431 {
432     DWORD index,j;
433
434     ICOM_THIS(BindCtxImpl,iface);
435
436     TRACE("(%p,%p)\n",This,ppenum);
437
438     if (BindCtxImpl_GetObjectIndex(This,NULL,ppenum,&index)==S_FALSE)
439         return E_FAIL;
440
441     /* release the object if it's found */
442     IUnknown_Release(This->bindCtxTable[index].pObj);
443     
444     /* remove the object from the table with a left-shifting of all objects in the right side */
445     for(j=index; j<This->bindCtxTableLastIndex-1; j++)
446         This->bindCtxTable[j]= This->bindCtxTable[j+1];
447     
448     This->bindCtxTableLastIndex--;
449
450     return S_OK;
451 }
452
453 /******************************************************************************
454  *        BindCtx_EnumObjectParam
455  ******************************************************************************/
456 HRESULT WINAPI BindCtxImpl_EnumObjectParam(IBindCtx* iface,IEnumString** pszkey)
457 {
458     FIXME("(%p,%p),stub!\n",iface,pszkey);
459     return E_NOTIMPL;
460 }
461
462 /********************************************************************************
463  *        GetObjectIndex (local function)
464  ********************************************************************************/
465 HRESULT WINAPI BindCtxImpl_GetObjectIndex(BindCtxImpl* This,
466                                           IUnknown* punk,
467                                           LPOLESTR pszkey,
468                                           DWORD *index)
469 {
470
471     DWORD i;
472     BYTE found=0;
473     
474     TRACE("(%p,%p,%p,%p)\n",This,punk,pszkey,index);
475
476     if (punk==NULL)
477         /* search object identified by a register key */
478         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++){
479
480             if(This->bindCtxTable[i].regType==1){
481
482                 if ( ( (This->bindCtxTable[i].pkeyObj==NULL) && (pszkey==NULL) ) ||
483                      ( (This->bindCtxTable[i].pkeyObj!=NULL) &&
484                        (pszkey!=NULL) &&
485                        (lstrcmpW(This->bindCtxTable[i].pkeyObj,pszkey)==0)
486                      )
487                    )
488
489                     found=1;
490             }
491         }
492     else
493         /* search object identified by a moniker*/
494         for(i=0; ( (i<This->bindCtxTableLastIndex) && (!found));i++)
495             if(This->bindCtxTable[i].pObj==punk)
496                 found=1;
497
498     if (index != NULL)
499         *index=i-1;
500
501     if (found)
502         return S_OK;
503     else
504         return S_FALSE;
505 }
506
507 /******************************************************************************
508  *        CreateBindCtx16
509  ******************************************************************************/
510 HRESULT WINAPI CreateBindCtx16(DWORD reserved, LPBC * ppbc)
511 {
512     FIXME("(%ld,%p),stub!\n",reserved,ppbc);
513     return E_NOTIMPL;
514 }
515
516 /******************************************************************************
517  *        CreateBindCtx
518  ******************************************************************************/
519 HRESULT WINAPI CreateBindCtx(DWORD reserved, LPBC * ppbc)
520 {
521     BindCtxImpl* newBindCtx = 0;
522     HRESULT hr;
523     IID riid=IID_IBindCtx;
524
525     TRACE("(%ld,%p)\n",reserved,ppbc);
526
527     newBindCtx = HeapAlloc(GetProcessHeap(), 0, sizeof(BindCtxImpl));
528
529     if (newBindCtx == 0)
530         return E_OUTOFMEMORY;
531
532     hr = BindCtxImpl_Construct(newBindCtx);
533
534     if (FAILED(hr)){
535
536         HeapFree(GetProcessHeap(),0,newBindCtx);
537         return hr;
538     }
539
540     hr = BindCtxImpl_QueryInterface((IBindCtx*)newBindCtx,&riid,(void**)ppbc);
541
542     return hr;
543 }