xref: /AOO41X/main/connectivity/source/inc/ado/Aolewrap.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef _CONNECTIVITY_ADO_AOLEWRAP_HXX_
28*cdf0e10cSrcweir #define _CONNECTIVITY_ADO_AOLEWRAP_HXX_
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <osl/diagnose.h>
31*cdf0e10cSrcweir #include <osl/thread.h>
32*cdf0e10cSrcweir #include <map>
33*cdf0e10cSrcweir #include <vector>
34*cdf0e10cSrcweir #include "connectivity/StdTypeDefs.hxx"
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir namespace rtl
37*cdf0e10cSrcweir {
38*cdf0e10cSrcweir 	class OUString;
39*cdf0e10cSrcweir }
40*cdf0e10cSrcweir namespace connectivity
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir 	namespace ado
43*cdf0e10cSrcweir 	{
44*cdf0e10cSrcweir 		class OLEVariant;
45*cdf0e10cSrcweir 		class WpBase
46*cdf0e10cSrcweir 		{
47*cdf0e10cSrcweir 		protected:
48*cdf0e10cSrcweir 			IDispatch* pIUnknown;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir 			void setIDispatch(IDispatch* _pIUnknown);
51*cdf0e10cSrcweir 		public:
52*cdf0e10cSrcweir 			WpBase();
53*cdf0e10cSrcweir 			WpBase(IDispatch* pInt);
54*cdf0e10cSrcweir 			//inline
55*cdf0e10cSrcweir 			WpBase& operator=(const WpBase& rhs);
56*cdf0e10cSrcweir 			WpBase& operator=(IDispatch* rhs);
57*cdf0e10cSrcweir 			WpBase(const WpBase& aWrapper);
58*cdf0e10cSrcweir 			virtual ~WpBase();
59*cdf0e10cSrcweir 			void clear();
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 			sal_Bool IsValid() const;
63*cdf0e10cSrcweir 			operator IDispatch*();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 		};
66*cdf0e10cSrcweir 		//////////////////////////////////////////////////////////////////////////
67*cdf0e10cSrcweir 		//
68*cdf0e10cSrcweir 		// Template-Klasse WpOLEBase<class T>
69*cdf0e10cSrcweir 		// ==================================
70*cdf0e10cSrcweir 		//
71*cdf0e10cSrcweir         // Objekte dieser Klasse haelt einen Zeiger auf ein Interface vom Typ T.
72*cdf0e10cSrcweir 		// Es gibt Konstruktoren und Zuweisungsoperator die sicherstellen, dass
73*cdf0e10cSrcweir 		// AddRef() und Release() entsprechend den COM-Konventionen gerufen werden.
74*cdf0e10cSrcweir 		// Ein Objekt kann auch keinen Zeiger halten (Nullzeiger), dann ergibt
75*cdf0e10cSrcweir 		// der Aufruf von IsValid() FALSE.
76*cdf0e10cSrcweir 		//
77*cdf0e10cSrcweir 		// Um effizientes pass-by-value machen zu koennen, ist diese (ebenso wie die
78*cdf0e10cSrcweir 		// abgeleiteten Klassen) eine ganz schmale Wrapper-Klasse unter Vermeidung
79*cdf0e10cSrcweir 		// virtueller Methoden und mit Inlining.
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 		//------------------------------------------------------------------------
82*cdf0e10cSrcweir 		template<class T> class WpOLEBase : public WpBase
83*cdf0e10cSrcweir 		{
84*cdf0e10cSrcweir 		protected:
85*cdf0e10cSrcweir 			T* pInterface;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 		public:
88*cdf0e10cSrcweir 			WpOLEBase(T* pInt = NULL) : WpBase(pInt),pInterface(pInt){}
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 			//inline
92*cdf0e10cSrcweir 			WpOLEBase<T>& operator=(const WpOLEBase<T>& rhs)
93*cdf0e10cSrcweir 			{
94*cdf0e10cSrcweir 				WpBase::operator=(rhs);
95*cdf0e10cSrcweir 				pInterface = rhs.pInterface;
96*cdf0e10cSrcweir 				return *this;
97*cdf0e10cSrcweir 			};
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			WpOLEBase<T>& operator=(T* rhs)
100*cdf0e10cSrcweir 			{
101*cdf0e10cSrcweir 				WpBase::operator=(rhs);
102*cdf0e10cSrcweir 				pInterface = rhs.pInterface;
103*cdf0e10cSrcweir 				return *this;
104*cdf0e10cSrcweir 			}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 			WpOLEBase(const WpOLEBase<T>& aWrapper)
107*cdf0e10cSrcweir 			{
108*cdf0e10cSrcweir 				operator=(aWrapper);
109*cdf0e10cSrcweir 			}
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 			virtual ~WpOLEBase()
112*cdf0e10cSrcweir 			{
113*cdf0e10cSrcweir 			}
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir 			operator T*() const { return static_cast<T*>(pInterface); }
116*cdf0e10cSrcweir 			void setWithOutAddRef(T* _pInterface)
117*cdf0e10cSrcweir 			{
118*cdf0e10cSrcweir 				pInterface = _pInterface;
119*cdf0e10cSrcweir 				WpBase::setIDispatch(_pInterface);
120*cdf0e10cSrcweir 			}
121*cdf0e10cSrcweir 		};
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		//////////////////////////////////////////////////////////////////////////
125*cdf0e10cSrcweir 		//
126*cdf0e10cSrcweir 		// Template-Klasse WpOLECollection<class Ts, class T, class WrapT>
127*cdf0e10cSrcweir 		// ===============================================================
128*cdf0e10cSrcweir 		//
129*cdf0e10cSrcweir 		// Diese Klasse, welche sich von WpOLEBase<Ts> ableitet, abstrahiert die
130*cdf0e10cSrcweir 		// den DAO-Collections gemeinsamen Eigenschaften:
131*cdf0e10cSrcweir 		//
132*cdf0e10cSrcweir         // Sie werden ueber ein Interface Ts (etwa: DAOFields) angesprochen
133*cdf0e10cSrcweir 		// und koennen ueber get_Item (hier:GetItem) Items des Typs T (genauer:
134*cdf0e10cSrcweir 		// mit Interface T, etwa DAOField) herausgeben.
135*cdf0e10cSrcweir 		//
136*cdf0e10cSrcweir 		// Diese Wrapperklasse gibt aber nicht ein Interface T heraus,
137*cdf0e10cSrcweir         // sondern ein Objekt der Klasse WrapT. Dieses muss eine Konstruktion
138*cdf0e10cSrcweir 		// durch T zulassen, vorzugsweise ist es von WpOLEBase<T> abgeleitet.
139*cdf0e10cSrcweir 		//
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 		//------------------------------------------------------------------------
142*cdf0e10cSrcweir 		template<class Ts, class T, class WrapT> class WpOLECollection : public WpOLEBase<Ts>
143*cdf0e10cSrcweir 		{
144*cdf0e10cSrcweir 		public:
145*cdf0e10cSrcweir 			using WpOLEBase<Ts>::pInterface;
146*cdf0e10cSrcweir 			using WpOLEBase<Ts>::IsValid;
147*cdf0e10cSrcweir 			// Konstruktoren, operator=
148*cdf0e10cSrcweir 			// diese rufen nur die Oberklasse
149*cdf0e10cSrcweir 			WpOLECollection(Ts* pInt=NULL):WpOLEBase<Ts>(pInt){}
150*cdf0e10cSrcweir 			WpOLECollection(const WpOLECollection& rhs){operator=(rhs);}
151*cdf0e10cSrcweir 			inline WpOLECollection& operator=(const WpOLECollection& rhs)
152*cdf0e10cSrcweir 				{WpOLEBase<Ts>::operator=(rhs); return *this;};
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 			//////////////////////////////////////////////////////////////////////
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 			inline void Refresh(){pInterface->Refresh();}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 			inline sal_Int32 GetItemCount()	const
159*cdf0e10cSrcweir 			{
160*cdf0e10cSrcweir 				sal_Int32 nCount = 0;
161*cdf0e10cSrcweir 				return pInterface ? (SUCCEEDED(pInterface->get_Count(&nCount)) ? nCount : sal_Int32(0)) : sal_Int32(0);
162*cdf0e10cSrcweir 			}
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 			inline WrapT GetItem(sal_Int32 index) const
165*cdf0e10cSrcweir 			{
166*cdf0e10cSrcweir 				OSL_ENSURE(index >= 0 && index<GetItemCount(),"Wrong index for field!");
167*cdf0e10cSrcweir 				T* pT = NULL;
168*cdf0e10cSrcweir 				WrapT aRet(NULL);
169*cdf0e10cSrcweir 				if(SUCCEEDED(pInterface->get_Item(OLEVariant(index), &pT)))
170*cdf0e10cSrcweir 					aRet.setWithOutAddRef(pT);
171*cdf0e10cSrcweir 				return aRet;
172*cdf0e10cSrcweir 			}
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 			inline WrapT GetItem(const OLEVariant& index) const
175*cdf0e10cSrcweir 			{
176*cdf0e10cSrcweir 				T* pT = NULL;
177*cdf0e10cSrcweir 				WrapT aRet(NULL);
178*cdf0e10cSrcweir 				if(SUCCEEDED(pInterface->get_Item(index, &pT)))
179*cdf0e10cSrcweir 					aRet.setWithOutAddRef(pT);
180*cdf0e10cSrcweir 				return aRet;
181*cdf0e10cSrcweir 			}
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 			inline WrapT GetItem(const ::rtl::OUString& sStr) const
184*cdf0e10cSrcweir 			{
185*cdf0e10cSrcweir 				WrapT aRet(NULL);
186*cdf0e10cSrcweir 				T* pT = NULL;
187*cdf0e10cSrcweir 				if (FAILED(pInterface->get_Item(OLEVariant(sStr), &pT)))
188*cdf0e10cSrcweir 				{
189*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
190*cdf0e10cSrcweir 					::rtl::OString sTemp("Unknown Item: ");
191*cdf0e10cSrcweir 					sTemp += ::rtl::OString(sStr.getStr(),sStr.getLength(),osl_getThreadTextEncoding());
192*cdf0e10cSrcweir 					OSL_ENSURE(0,sTemp);
193*cdf0e10cSrcweir #endif
194*cdf0e10cSrcweir 				}
195*cdf0e10cSrcweir 				else
196*cdf0e10cSrcweir 					aRet.setWithOutAddRef(pT);
197*cdf0e10cSrcweir 				return aRet;
198*cdf0e10cSrcweir 			}
199*cdf0e10cSrcweir 			inline void fillElementNames(TStringVector& _rVector)
200*cdf0e10cSrcweir 			{
201*cdf0e10cSrcweir 				if(IsValid())
202*cdf0e10cSrcweir 				{
203*cdf0e10cSrcweir 					Refresh();
204*cdf0e10cSrcweir 					sal_Int32 nCount = GetItemCount();
205*cdf0e10cSrcweir 					_rVector.reserve(nCount);
206*cdf0e10cSrcweir 					for(sal_Int32 i=0;i< nCount;++i)
207*cdf0e10cSrcweir 					{
208*cdf0e10cSrcweir 						WrapT aElement = GetItem(i);
209*cdf0e10cSrcweir 						if(aElement.IsValid())
210*cdf0e10cSrcweir 							_rVector.push_back(aElement.get_Name());
211*cdf0e10cSrcweir 					}
212*cdf0e10cSrcweir 				}
213*cdf0e10cSrcweir 			}
214*cdf0e10cSrcweir 		};
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 		template<class Ts, class T, class WrapT> class WpOLEAppendCollection:
217*cdf0e10cSrcweir 				public WpOLECollection<Ts,T,WrapT>
218*cdf0e10cSrcweir 		{
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 		public:
221*cdf0e10cSrcweir 			// Konstruktoren, operator=
222*cdf0e10cSrcweir 			// diese rufen nur die Oberklasse
223*cdf0e10cSrcweir 			using WpOLEBase<Ts>::pInterface;
224*cdf0e10cSrcweir 			WpOLEAppendCollection(Ts* pInt=NULL):WpOLECollection<Ts,T,WrapT>(pInt){}
225*cdf0e10cSrcweir 			WpOLEAppendCollection(const WpOLEAppendCollection& rhs){ operator=(rhs); }
226*cdf0e10cSrcweir 			inline WpOLEAppendCollection& operator=(const WpOLEAppendCollection& rhs)
227*cdf0e10cSrcweir 				{WpOLEBase<Ts>::operator=(rhs); return *this;};
228*cdf0e10cSrcweir 			//////////////////////////////////////////////////////////////////////
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 			inline sal_Bool Append(const WrapT& aWrapT)
231*cdf0e10cSrcweir 			{
232*cdf0e10cSrcweir 				return SUCCEEDED(pInterface->Append(OLEVariant((T*)aWrapT)));
233*cdf0e10cSrcweir 			};
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 			inline sal_Bool Delete(const ::rtl::OUString& sName)
236*cdf0e10cSrcweir 			{
237*cdf0e10cSrcweir 				return SUCCEEDED(pInterface->Delete(OLEVariant(sName)));
238*cdf0e10cSrcweir 			};
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 		};
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir #endif // _CONNECTIVITY_ADO_AOLEWRAP_HXX_
245*cdf0e10cSrcweir 
246