OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
oleutil.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectComponents
3// Copyright (c) 1994, 1996 by Borland International, All Rights Reserved
4/// \file
5/// Low level OLE Utility class definitions
6//----------------------------------------------------------------------------
7
8#if !defined(OCF_OLEUTIL_H)
9#define OCF_OLEUTIL_H
10
11#include <owl/private/defs.h>
12#if defined(BI_HAS_PRAGMA_ONCE)
13# pragma once
14#endif
15
16#include <ocf/defs.h>
17#include <ocf/except.h>
18
19#include <owl/pointer.h>
20
21#include <owl/string.h>
22
23
24#if !defined(_ICLASS)
25# define _ICLASS _OCFCLASS
26#endif
27
28#define _IFUNC STDMETHODCALLTYPE
29
30
31namespace owl {class _OWLCLASS TRegList;};
32
33namespace ocf {
34
35#if 1
36
37//
38/// BASIC global string encapsulation
39//
41
42#else
43
44//
45/// \class TBSTR
46// ~~~~~ ~~~~~
47/// BASIC global string encapsulation
48//
49class TBSTR {
50 public:
51 TBSTR() : P(0) {}
52 TBSTR(BSTR p) : P(p) {}
53 // NOTE: OK to pass 0 to 'SysFreeString'
54 //
55 ~TBSTR(){ ::SysFreeString(P); }
56
57
58 // Operators
59 //
60 BSTR operator =(BSTR p) { ::SysFreeString(P); return P = p; }
61 TBSTR& operator =(const TBSTR& p){::SysFreeString(P); P = p.P; return *this;}
62 operator BSTR() { return P;}
63 operator BSTR*() { if(P) ::SysFreeString(P); return &P;}
64 int operator !() const { return P==0;}
65 void operator ~() { ::SysFreeString(P); P = 0; }
66
67 protected:
68 BSTR P;
69
70 private:
71
72 // Prohibit use of new/delete operators
73 //
74 void* operator new(size_t) {return 0;}
75 void operator delete(void*) {}
76};
77#endif
78
79
80
82{
83 public:
84 static HRESULT SafeArrayGetVartype(SAFEARRAY * psa, VARTYPE * pvt);
85};
86
87inline HRESULT
88TOleAutoDll::SafeArrayGetVartype(SAFEARRAY * psa, VARTYPE * pvt)
89{
90 return ::SafeArrayGetVartype(psa,pvt);
91}
92
93
94
95
96// Conversion routines
97//
98void OcHiMetricToPixel(const SIZEL& sizeHM, SIZEL& sizePix);
99void OcPixelToHiMetric(const SIZEL& sizePix, SIZEL& sizeHM);
100
101//
102/// \class TOleAllocator
103// ~~~~~ ~~~~~~~~~~~~~
104/// Object to initialize OLE and access memory manager
105//
107 public:
108 TOleAllocator(); // Gets allocator, does not initialize OLE
109 TOleAllocator(IMalloc* mem); // Use 0 to initialize with OLE task allocator
111 void* Alloc(unsigned long size);
112 void Free(void * pv);
113 IMalloc* Mem;
114 private:
115 bool Initialized;
116};
117
118//
119//
120//
121# if defined(BI_COMP_BORLANDC)
122# pragma warn -inl
123# endif
124inline
125TOleAllocator::TOleAllocator(IMalloc* mem) : Initialized(true)
126{
127 OLECALL(OleInitialize(mem), _T("OleInitialize"));
128 OLECALL(CoGetMalloc(MEMCTX_TASK, &Mem), _T("CoGetMalloc"));
129}
130
131//
132//
133inline
134TOleAllocator::TOleAllocator() : Mem(0), Initialized(false)
135{
136 OLECALL(CoGetMalloc(MEMCTX_TASK, &Mem), _T("CoGetMalloc"));
137}
138
139//
140//
141//
142inline
144{
145 if (Mem)
146 Mem->Release();
147 Mem = 0;
148 if (Initialized)
149 ::OleUninitialize();
150}
151
152//
153/// \class TOleInit
154// ~~~~~ ~~~~~~~~
155/// Simple wrapper of TOleAllocator used exclusively for initializing OLE
156//
157class TOleInit : public TOleAllocator {
158 public:
160};
161
162// To maintain compatibility with initial release of v5.0 of OCF
163//
165
166//
167/// \class TClassId
168// ~~~~~ ~~~~~~~~
169/// GUID/IID/CLSID management
170//
171class TClassId {
172 public:
175 };
176 TClassId(const GUID& guid);
177 TClassId(LPCTSTR idtxt);
178 TClassId(const TClassId& copy);
180 TClassId& operator =(const TClassId& copy);
181 TClassId(const GUID & guid, int offset);
182 ~TClassId();
183 operator GUID&();
184 operator LPCTSTR();
185
186 protected:
187 TClassId();
188 GUID Guid;
189 LPCTSTR Text;
190};
191
192//
193//
194inline
195TClassId::TClassId(const GUID& guid) : Text(0) {
196 Guid = guid;
197}
198
199//
200//
201inline
202TClassId::TClassId(const TClassId& copy) : Guid(copy.Guid), Text(0)
203{
204}
205
206/// Default constructor - Derived class must set GUID
207//
208inline
210{
211}
212
213//
214//
215inline
216TClassId::operator GUID&()
217{
218 return Guid;
219}
220
221//
222//
223inline TClassId&
225{
226 Guid = copy.Guid;
227 Text=0;
228 return *this;
229}
230
231//
232/// \class TBaseClassId
233// ~~~~~ ~~~~~~~~~~~~
234/// Base GUID for a components sequence of GUIDs
235//
236class TBaseClassId : public TClassId {
237 public:
238 TBaseClassId(owl::TRegList& regInfo, int maxAlloc = 256);
239 TBaseClassId(const GUID& guid, int maxAlloc = 256);
240 TBaseClassId(LPCTSTR idtxt, int maxAlloc = 256);
241 TBaseClassId(TGenerate, int maxAlloc = 256);
242 TClassId operator [](int offset);
243 int AllocId();
244 int GetOffset(const GUID & guid); // returns -1 if not within range
245
246 private:
247 int Offset; // Last offset given out by AllocId(), creator reserves 0
248 int MaxAlloc;
249};
250
251//
252// Fast GUID compare functions, assume that low word has already matched OK
253//
254void CmpGuid12 (IUnknown* This, REFIID req, REFIID ref, void** pif);
255void CmpGuidOle(IUnknown* This, REFIID req, void** pif);
256
257//
258/// \class TUnknown
259// ~~~~~ ~~~~~~~~
260/// Standard implementation of a controlling IUnknown for an object, to be
261/// inherited with other COM interfaces into an implementation class whose
262/// IUnknown implementation delegates to TUnknown::Outer.
263//
265 public:
266 operator IUnknown&() {return *Outer;}
267 operator IUnknown*() {Outer->AddRef(); return Outer;}
268 IUnknown* SetOuter(IUnknown* outer = 0);
269 IUnknown* GetOuter() {return Outer;}
270 unsigned long GetRefCount();
271 unsigned long AdjustRefCount(int i);
272 IUnknown& Aggregate(TUnknown& inner); // returns outermost IUnknown
273
274 protected:
275 TUnknown();
276 virtual ~TUnknown();
277
278 IUnknown& ThisUnknown() {return I;}
279 IUnknown* Outer;
280 virtual HRESULT QueryObject(const GUID& iid, void** pif);
281
282 private:
283 class _ICLASS TUnknownI : public IUnknown {
284 public:
285 HRESULT _IFUNC QueryInterface(const GUID & iid, void** pif);
286 unsigned long _IFUNC AddRef();
287 unsigned long _IFUNC Release();
288 TUnknownI() : RefCnt(0), Inner(0) {}
289 ~TUnknownI();
290 unsigned RefCnt; ///< Object's reference count, remains 1 if aggregated
291 TUnknown* Inner; ///< Chain of TUnknown-aggregated objects
292 private:
293 TUnknown& Host();
294 } I;
295 friend class TUnknownI;
296};
297
298inline IUnknown* TUnknown::SetOuter(IUnknown* outer)
299{
300 if (outer) {
301 Outer = outer;
302 if (I.Inner)
303 I.Inner->SetOuter(outer);
304 }
305 return &I;
306}
307
308// is this portable?????????
309inline TUnknown& TUnknown::TUnknownI::Host()
310{
311 return *(TUnknown*)((char*)this-(int)(&((TUnknown*)0)->I));
312}
313
314inline unsigned long TUnknown::AdjustRefCount(int i)
315{
316 return (unsigned long)(I.RefCnt += i);
317}
318# if defined(BI_COMP_BORLANDC)
319# pragma warn .inl
320# endif
321
322
323//------------------------------------------------------------------------
324//
325// Macros to generate COM mixin classes for use with TUnknown
326// Each base class must have an inline QueryInterface wrapper defined.
327//
328// DECLARE_COMBASES1(classname, base) // declares support for 1 interface
329// DECLARE_COMBASES2(classname, base, base) // mixes in for 2 interfaces
330// .......
331// DEFINE_COMBASES1(classname, base) // implements IUnknown for 1 interface
332// DEFINE_COMBASES2(classname, base, base) // implements for 2 interfaces
333// ......
334//
335// Macros to generate inline QueryInterface wrappers for interfaces/delegates
336// (low GUID word may specify an explicit integer or an IID_whatever.Data1)
337// For efficiency they are void functions; check result pointer for success
338//
339// DEFINE_QI_BASE(cls, low) // Inherited COM interface with IUnknown base
340// DEFINE_QI_OLEBASE(cls, low) // Inherited OLE interface using an OLE GUID
341// DEFINE_QI_DERIVED(cls, base, low) // COM interface derived from another
342// DEFINE_QI_OLEDERIVED(cls, base, low) // OLE interface derived from another
343// DEFINE_QI_DELEGATE(cls, member) // Inherited object with COM member
344// DEFINE_QI_DELEGATE_TEST(cls, member) // Object whose COM member may be 0
345// DEFINE_QI_MEMBER(name, member) // Used inside object with COM member
346// DEFINE_QI_MEMBER_TEST(name, member) // Where object's COM member may be 0
347//
348// Macro to invoke inline QueryInterface wrappers and return if successful
349// A list of these is generated by the DEFINE_COMBASESn(...) macro
350//
351// COMBASEQUERY(name) // Calls name_QueryInterface(), returns OK if success
352//
353// Macro to provide standard IUnknown implementation which forwards to Outer
354// This macro is automatically invoked by the DEFINE_COMBASESn(...) macro
355//
356// IUNKNOWN_FORWARD(cls, outer) // cls IUnknown implementation forwards to outer
357//
358// Example of user-written COM class inheriting from macro-generated mixin
359//
360// class TSample : public classname {
361// public:
362// TSample(IUnknown* outer, IUnknown** pif) { *pif = SetOuter(outer); }
363// };
364//
365//------------------------------------------------------------------------
366
367#define COMBASES_D(cls, bases) \
368 class cls : public TUnknown, bases { \
369 protected: \
370 virtual HRESULT _IFUNC QueryInterface(const GUID & iid, void ** pif); \
371 virtual unsigned long _IFUNC AddRef(); \
372 virtual unsigned long _IFUNC Release(); \
373 virtual HRESULT QueryObject(const GUID & iid, void ** pif); \
374 HRESULT QueryBases(const GUID & iid, void ** pif) \
375 {return cls::QueryObject(iid, pif);} \
376 }; // QueryBases() is an inline wrapper to this QueryObject() implementation
377
378#define COMQRY1BASES(i1) public i1
379#define COMQRY2BASES(i1,i2) public i1, COMQRY1BASES(i2)
380#define COMQRY3BASES(i1,i2,i3) public i1, COMQRY2BASES(i2,i3)
381#define COMQRY4BASES(i1,i2,i3,i4) public i1, COMQRY3BASES(i2,i3,i4)
382#define COMQRY5BASES(i1,i2,i3,i4,i5) public i1, COMQRY4BASES(i2,i3,i4,i5)
383
384#define DECLARE_COMBASES1(cls,i1) \
385 COMBASES_D(cls,COMQRY1BASES(i1))
386#define DECLARE_COMBASES2(cls,i1,i2) \
387 COMBASES_D(cls,COMQRY2BASES(i1,i2))
388#define DECLARE_COMBASES3(cls,i1,i2,i3) \
389 COMBASES_D(cls,COMQRY3BASES(i1,i2,i3))
390#define DECLARE_COMBASES4(cls,i1,i2,i3,i4) \
391 COMBASES_D(cls,COMQRY4BASES(i1,i2,i3,i4))
392#define DECLARE_COMBASES5(cls,i1,i2,i3,i4,i5) \
393 COMBASES_D(cls,COMQRY5BASES(i1,i2,i3,i4,i5))
394
395// Macro which can be used by classes which inherit from TUnknown for
396// IUnknown methods support - This macro defines the methods inline
397//
398#define TUNKNOWN_STANDARD_IUNKNOWN_METHODS \
399 owl::ulong _IFUNC AddRef() {return GetOuter()->AddRef();} \
400 owl::ulong _IFUNC Release() {return GetOuter()->Release();} \
401 HRESULT _IFUNC QueryInterface(const GUID& iid, void** iface) \
402 {return GetOuter()->QueryInterface(iid, iface);}
403
404
405// Macro which can be used by classes which inherit from TUnknown for
406// IUnknown methods support - This macros defines the methods out-of-line
407//
408#define IUNKNOWN_FORWARD(cls, outer) \
409 HRESULT _IFUNC cls::QueryInterface(const GUID & iid, void * * pif)\
410 { return outer->QueryInterface(iid, pif); } \
411 owl::ulong _IFUNC cls::AddRef() { return outer->AddRef(); } \
412 owl::ulong _IFUNC cls::Release() { return outer->Release(); }
413
414#define COMBASES_I(cls, tests) \
415 IUNKNOWN_FORWARD(cls, Outer) \
416 HRESULT cls::QueryObject(const GUID & iid, void ** pif) \
417 { *pif = 0; tests return ResultFromScode(E_NOINTERFACE); }
418
419#define COMBASEQUERY(cls) \
420 cls##_QueryInterface(this,iid,pif); if(*pif) return NOERROR;
421
422#define COMQRY1TEST(i1) COMBASEQUERY(i1)
423#define COMQRY2TEST(i1,i2) COMBASEQUERY(i1) COMQRY1TEST(i2)
424#define COMQRY3TEST(i1,i2,i3) COMBASEQUERY(i1) COMQRY2TEST(i2,i3)
425#define COMQRY4TEST(i1,i2,i3,i4) COMBASEQUERY(i1) COMQRY3TEST(i2,i3,i4)
426#define COMQRY5TEST(i1,i2,i3,i4,i5) COMBASEQUERY(i1) COMQRY3TEST(i2,i3,i4,i5)
427
428#define DEFINE_COMBASES1(cls,i1) \
429 COMBASES_I(cls,COMQRY1TEST(i1))
430#define DEFINE_COMBASES2(cls,i1,i2) \
431 COMBASES_I(cls,COMQRY2TEST(i1,i2))
432#define DEFINE_COMBASES3(cls,i1,i2,i3) \
433 COMBASES_I(cls,COMQRY3TEST(i1,i2,i3))
434#define DEFINE_COMBASES4(cls,i1,i2,i3,i4) \
435 COMBASES_I(cls,COMQRY4TEST(i1,i2,i3,i4))
436#define DEFINE_COMBASES5(cls,i1,i2,i3,i4,i5) \
437 COMBASES_I(cls,COMQRY5TEST(i1,i2,i3,i4,i5))
438
439#define DEFINE_QI_BASE(cls, low) \
440 inline void cls##_QueryInterface(cls* obj, REFIID iid, void * * pif) \
441 {if (iid.Data1==low) CmpGuid12(obj, iid, IID_##cls, pif);}
442
443#define DEFINE_QI_OLEBASE(cls, low) \
444 inline void cls##_QueryInterface(cls* obj, REFIID iid, void * * pif) \
445 {if (iid.Data1==low) CmpGuidOle(obj, iid, pif);}
446
447#define DEFINE_QI_DERIVED(cls, base, low) \
448 inline void cls##_QueryInterface(cls* obj, REFIID iid, void * * pif) \
449 {if (iid.Data1==low) CmpGuid12(obj, iid, IID_##cls, pif); \
450 else base##_QueryInterface(obj, iid, pif);}
451
452#define DEFINE_QI_OLEDERIVED(cls, base, low) \
453 inline void cls##_QueryInterface(cls* obj, REFIID iid, void * * pif) \
454 {if (iid.Data1==low) CmpGuidOle(obj, iid, pif); \
455 else base##_QueryInterface(obj, iid, pif);}
456
457#define DEFINE_QI_DELEGATE(cls, member) \
458 inline void cls##_QueryInterface(cls* obj, REFIID iid, void * * pif) \
459 {obj->member->QueryInterface(iid, pif);}
460
461#define DEFINE_QI_DELEGATE_TEST(cls, member) \
462 inline void cls##_QueryInterface(cls* obj, REFIID iid, void * * pif) \
463 {if (obj->member) obj->member->QueryInterface(iid, pif);}
464
465#define DEFINE_QI_MEMBER(name, member) \
466 void name##_QueryInterface(void *, REFIID iid, void * * pif) \
467 {member->QueryInterface(iid, pif);}
468
469#define DEFINE_QI_MEMBER_TEST(name, member) \
470 void name##_QueryInterface(void *, REFIID iid, void * * pif) \
471 {if (member) member->QueryInterface(iid, pif);}
472
473#define DEFINE_COMBASE_DELEGATE(cls, ifc, member) \
474 struct _ICLASS cls { \
475 cls() : member(0) {} \
476 ifc* member; \
477 DEFINE_QI_MEMBER_TEST(cls, member) \
478 };
479
480} // OCF namespace
481
482#endif // OCF_OLEUTIL_H
Base GUID for a components sequence of GUIDs.
Definition oleutil.h:236
int GetOffset(const GUID &guid)
Definition oleutil.cpp:168
TClassId operator[](int offset)
Definition oleutil.cpp:145
TBaseClassId(owl::TRegList &regInfo, int maxAlloc=256)
Definition oleutil.cpp:131
GUID/IID/CLSID management.
Definition oleutil.h:171
LPCTSTR Text
Definition oleutil.h:189
TClassId & operator=(const TClassId &copy)
Definition oleutil.h:224
TClassId()
Default constructor - Derived class must set GUID.
Definition oleutil.h:209
Object to initialize OLE and access memory manager.
Definition oleutil.h:106
void * Alloc(unsigned long size)
Definition oleutil.cpp:27
IMalloc * Mem
Definition oleutil.h:113
void Free(void *pv)
Definition oleutil.cpp:38
static HRESULT SafeArrayGetVartype(SAFEARRAY *psa, VARTYPE *pvt)
Definition oleutil.h:88
Simple wrapper of TOleAllocator used exclusively for initializing OLE.
Definition oleutil.h:157
Standard implementation of a controlling IUnknown for an object, to be inherited with other COM inter...
Definition oleutil.h:264
IUnknown * SetOuter(IUnknown *outer=0)
Definition oleutil.h:298
IUnknown * GetOuter()
Definition oleutil.h:269
unsigned long AdjustRefCount(int i)
Definition oleutil.h:314
IUnknown * Outer
Definition oleutil.h:279
IUnknown & ThisUnknown()
Definition oleutil.h:278
A registration parameter table, composed of a list of TRegItems.
Definition registry.h:531
System string (BSTR) encapsulation.
Definition string.h:33
#define _T(x)
Definition cygwin.h:51
Object Component Framework (COM encapsulation)
Definition appdesc.h:22
void OcHiMetricToPixel(const SIZEL &sizeHM, SIZEL &sizePix)
Definition oleutil.cpp:378
void CmpGuidOle(IUnknown *This, REFIID req, void **pif)
Definition oleutil.cpp:204
TOleInit TOleStart
Definition oleutil.h:164
const GUID & iid
Definition appdesc.h:328
owl::TSysStr TBSTR
BASIC global string encapsulation.
Definition oleutil.h:40
void CmpGuid12(IUnknown *This, REFIID req, REFIID ref, void **pif)
Definition oleutil.cpp:190
void OcPixelToHiMetric(const SIZEL &sizePix, SIZEL &sizeHM)
Definition oleutil.cpp:391
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
ObjectComponents fundamental definitions.
#define _OCFCLASS
Definition defs.h:45
OCF Exception classes.
#define OLECALL(func, msg)
Definition except.h:78
#define _IFUNC
Definition oleutil.h:28
#define _ICLASS
Definition oleutil.h:25
#define _OWLCLASS
Definition defs.h:338
Various types of smart pointer templatized classes.
Definition of class TString, a flexible universal string envelope class.