1*caf5cd79SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*caf5cd79SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*caf5cd79SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*caf5cd79SAndrew Rist  * distributed with this work for additional information
6*caf5cd79SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*caf5cd79SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*caf5cd79SAndrew Rist  * "License"); you may not use this file except in compliance
9*caf5cd79SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*caf5cd79SAndrew Rist  *
11*caf5cd79SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*caf5cd79SAndrew Rist  *
13*caf5cd79SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*caf5cd79SAndrew Rist  * software distributed under the License is distributed on an
15*caf5cd79SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*caf5cd79SAndrew Rist  * KIND, either express or implied.  See the License for the
17*caf5cd79SAndrew Rist  * specific language governing permissions and limitations
18*caf5cd79SAndrew Rist  * under the License.
19*caf5cd79SAndrew Rist  *
20*caf5cd79SAndrew Rist  *************************************************************/
21*caf5cd79SAndrew Rist 
22*caf5cd79SAndrew Rist 
23cdf0e10cSrcweir #ifndef _CONNECTIVITY_ADO_AOLEWRAP_HXX_
24cdf0e10cSrcweir #define _CONNECTIVITY_ADO_AOLEWRAP_HXX_
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <osl/diagnose.h>
27cdf0e10cSrcweir #include <osl/thread.h>
28cdf0e10cSrcweir #include <map>
29cdf0e10cSrcweir #include <vector>
30cdf0e10cSrcweir #include "connectivity/StdTypeDefs.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace rtl
33cdf0e10cSrcweir {
34cdf0e10cSrcweir 	class OUString;
35cdf0e10cSrcweir }
36cdf0e10cSrcweir namespace connectivity
37cdf0e10cSrcweir {
38cdf0e10cSrcweir 	namespace ado
39cdf0e10cSrcweir 	{
40cdf0e10cSrcweir 		class OLEVariant;
41cdf0e10cSrcweir 		class WpBase
42cdf0e10cSrcweir 		{
43cdf0e10cSrcweir 		protected:
44cdf0e10cSrcweir 			IDispatch* pIUnknown;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 			void setIDispatch(IDispatch* _pIUnknown);
47cdf0e10cSrcweir 		public:
48cdf0e10cSrcweir 			WpBase();
49cdf0e10cSrcweir 			WpBase(IDispatch* pInt);
50cdf0e10cSrcweir 			//inline
51cdf0e10cSrcweir 			WpBase& operator=(const WpBase& rhs);
52cdf0e10cSrcweir 			WpBase& operator=(IDispatch* rhs);
53cdf0e10cSrcweir 			WpBase(const WpBase& aWrapper);
54cdf0e10cSrcweir 			virtual ~WpBase();
55cdf0e10cSrcweir 			void clear();
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 			sal_Bool IsValid() const;
59cdf0e10cSrcweir 			operator IDispatch*();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 		};
62cdf0e10cSrcweir 		//////////////////////////////////////////////////////////////////////////
63cdf0e10cSrcweir 		//
64cdf0e10cSrcweir 		// Template-Klasse WpOLEBase<class T>
65cdf0e10cSrcweir 		// ==================================
66cdf0e10cSrcweir 		//
67cdf0e10cSrcweir         // Objekte dieser Klasse haelt einen Zeiger auf ein Interface vom Typ T.
68cdf0e10cSrcweir 		// Es gibt Konstruktoren und Zuweisungsoperator die sicherstellen, dass
69cdf0e10cSrcweir 		// AddRef() und Release() entsprechend den COM-Konventionen gerufen werden.
70cdf0e10cSrcweir 		// Ein Objekt kann auch keinen Zeiger halten (Nullzeiger), dann ergibt
71cdf0e10cSrcweir 		// der Aufruf von IsValid() FALSE.
72cdf0e10cSrcweir 		//
73cdf0e10cSrcweir 		// Um effizientes pass-by-value machen zu koennen, ist diese (ebenso wie die
74cdf0e10cSrcweir 		// abgeleiteten Klassen) eine ganz schmale Wrapper-Klasse unter Vermeidung
75cdf0e10cSrcweir 		// virtueller Methoden und mit Inlining.
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 		//------------------------------------------------------------------------
78cdf0e10cSrcweir 		template<class T> class WpOLEBase : public WpBase
79cdf0e10cSrcweir 		{
80cdf0e10cSrcweir 		protected:
81cdf0e10cSrcweir 			T* pInterface;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 		public:
WpOLEBase(T * pInt=NULL)84cdf0e10cSrcweir 			WpOLEBase(T* pInt = NULL) : WpBase(pInt),pInterface(pInt){}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 			//inline
operator =(const WpOLEBase<T> & rhs)88cdf0e10cSrcweir 			WpOLEBase<T>& operator=(const WpOLEBase<T>& rhs)
89cdf0e10cSrcweir 			{
90cdf0e10cSrcweir 				WpBase::operator=(rhs);
91cdf0e10cSrcweir 				pInterface = rhs.pInterface;
92cdf0e10cSrcweir 				return *this;
93cdf0e10cSrcweir 			};
94cdf0e10cSrcweir 
operator =(T * rhs)95cdf0e10cSrcweir 			WpOLEBase<T>& operator=(T* rhs)
96cdf0e10cSrcweir 			{
97cdf0e10cSrcweir 				WpBase::operator=(rhs);
98cdf0e10cSrcweir 				pInterface = rhs.pInterface;
99cdf0e10cSrcweir 				return *this;
100cdf0e10cSrcweir 			}
101cdf0e10cSrcweir 
WpOLEBase(const WpOLEBase<T> & aWrapper)102cdf0e10cSrcweir 			WpOLEBase(const WpOLEBase<T>& aWrapper)
103cdf0e10cSrcweir 			{
104cdf0e10cSrcweir 				operator=(aWrapper);
105cdf0e10cSrcweir 			}
106cdf0e10cSrcweir 
~WpOLEBase()107cdf0e10cSrcweir 			virtual ~WpOLEBase()
108cdf0e10cSrcweir 			{
109cdf0e10cSrcweir 			}
110cdf0e10cSrcweir 
operator T*() const111cdf0e10cSrcweir 			operator T*() const { return static_cast<T*>(pInterface); }
setWithOutAddRef(T * _pInterface)112cdf0e10cSrcweir 			void setWithOutAddRef(T* _pInterface)
113cdf0e10cSrcweir 			{
114cdf0e10cSrcweir 				pInterface = _pInterface;
115cdf0e10cSrcweir 				WpBase::setIDispatch(_pInterface);
116cdf0e10cSrcweir 			}
117cdf0e10cSrcweir 		};
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 		//////////////////////////////////////////////////////////////////////////
121cdf0e10cSrcweir 		//
122cdf0e10cSrcweir 		// Template-Klasse WpOLECollection<class Ts, class T, class WrapT>
123cdf0e10cSrcweir 		// ===============================================================
124cdf0e10cSrcweir 		//
125cdf0e10cSrcweir 		// Diese Klasse, welche sich von WpOLEBase<Ts> ableitet, abstrahiert die
126cdf0e10cSrcweir 		// den DAO-Collections gemeinsamen Eigenschaften:
127cdf0e10cSrcweir 		//
128cdf0e10cSrcweir         // Sie werden ueber ein Interface Ts (etwa: DAOFields) angesprochen
129cdf0e10cSrcweir 		// und koennen ueber get_Item (hier:GetItem) Items des Typs T (genauer:
130cdf0e10cSrcweir 		// mit Interface T, etwa DAOField) herausgeben.
131cdf0e10cSrcweir 		//
132cdf0e10cSrcweir 		// Diese Wrapperklasse gibt aber nicht ein Interface T heraus,
133cdf0e10cSrcweir         // sondern ein Objekt der Klasse WrapT. Dieses muss eine Konstruktion
134cdf0e10cSrcweir 		// durch T zulassen, vorzugsweise ist es von WpOLEBase<T> abgeleitet.
135cdf0e10cSrcweir 		//
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 		//------------------------------------------------------------------------
138cdf0e10cSrcweir 		template<class Ts, class T, class WrapT> class WpOLECollection : public WpOLEBase<Ts>
139cdf0e10cSrcweir 		{
140cdf0e10cSrcweir 		public:
141cdf0e10cSrcweir 			using WpOLEBase<Ts>::pInterface;
142cdf0e10cSrcweir 			using WpOLEBase<Ts>::IsValid;
143cdf0e10cSrcweir 			// Konstruktoren, operator=
144cdf0e10cSrcweir 			// diese rufen nur die Oberklasse
WpOLECollection(Ts * pInt=NULL)145cdf0e10cSrcweir 			WpOLECollection(Ts* pInt=NULL):WpOLEBase<Ts>(pInt){}
WpOLECollection(const WpOLECollection & rhs)146cdf0e10cSrcweir 			WpOLECollection(const WpOLECollection& rhs){operator=(rhs);}
operator =(const WpOLECollection & rhs)147cdf0e10cSrcweir 			inline WpOLECollection& operator=(const WpOLECollection& rhs)
148cdf0e10cSrcweir 				{WpOLEBase<Ts>::operator=(rhs); return *this;};
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 			//////////////////////////////////////////////////////////////////////
151cdf0e10cSrcweir 
Refresh()152cdf0e10cSrcweir 			inline void Refresh(){pInterface->Refresh();}
153cdf0e10cSrcweir 
GetItemCount() const154cdf0e10cSrcweir 			inline sal_Int32 GetItemCount()	const
155cdf0e10cSrcweir 			{
156cdf0e10cSrcweir 				sal_Int32 nCount = 0;
157cdf0e10cSrcweir 				return pInterface ? (SUCCEEDED(pInterface->get_Count(&nCount)) ? nCount : sal_Int32(0)) : sal_Int32(0);
158cdf0e10cSrcweir 			}
159cdf0e10cSrcweir 
GetItem(sal_Int32 index) const160cdf0e10cSrcweir 			inline WrapT GetItem(sal_Int32 index) const
161cdf0e10cSrcweir 			{
162cdf0e10cSrcweir 				OSL_ENSURE(index >= 0 && index<GetItemCount(),"Wrong index for field!");
163cdf0e10cSrcweir 				T* pT = NULL;
164cdf0e10cSrcweir 				WrapT aRet(NULL);
165cdf0e10cSrcweir 				if(SUCCEEDED(pInterface->get_Item(OLEVariant(index), &pT)))
166cdf0e10cSrcweir 					aRet.setWithOutAddRef(pT);
167cdf0e10cSrcweir 				return aRet;
168cdf0e10cSrcweir 			}
169cdf0e10cSrcweir 
GetItem(const OLEVariant & index) const170cdf0e10cSrcweir 			inline WrapT GetItem(const OLEVariant& index) const
171cdf0e10cSrcweir 			{
172cdf0e10cSrcweir 				T* pT = NULL;
173cdf0e10cSrcweir 				WrapT aRet(NULL);
174cdf0e10cSrcweir 				if(SUCCEEDED(pInterface->get_Item(index, &pT)))
175cdf0e10cSrcweir 					aRet.setWithOutAddRef(pT);
176cdf0e10cSrcweir 				return aRet;
177cdf0e10cSrcweir 			}
178cdf0e10cSrcweir 
GetItem(const::rtl::OUString & sStr) const179cdf0e10cSrcweir 			inline WrapT GetItem(const ::rtl::OUString& sStr) const
180cdf0e10cSrcweir 			{
181cdf0e10cSrcweir 				WrapT aRet(NULL);
182cdf0e10cSrcweir 				T* pT = NULL;
183cdf0e10cSrcweir 				if (FAILED(pInterface->get_Item(OLEVariant(sStr), &pT)))
184cdf0e10cSrcweir 				{
185cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
186cdf0e10cSrcweir 					::rtl::OString sTemp("Unknown Item: ");
187cdf0e10cSrcweir 					sTemp += ::rtl::OString(sStr.getStr(),sStr.getLength(),osl_getThreadTextEncoding());
188cdf0e10cSrcweir 					OSL_ENSURE(0,sTemp);
189cdf0e10cSrcweir #endif
190cdf0e10cSrcweir 				}
191cdf0e10cSrcweir 				else
192cdf0e10cSrcweir 					aRet.setWithOutAddRef(pT);
193cdf0e10cSrcweir 				return aRet;
194cdf0e10cSrcweir 			}
fillElementNames(TStringVector & _rVector)195cdf0e10cSrcweir 			inline void fillElementNames(TStringVector& _rVector)
196cdf0e10cSrcweir 			{
197cdf0e10cSrcweir 				if(IsValid())
198cdf0e10cSrcweir 				{
199cdf0e10cSrcweir 					Refresh();
200cdf0e10cSrcweir 					sal_Int32 nCount = GetItemCount();
201cdf0e10cSrcweir 					_rVector.reserve(nCount);
202cdf0e10cSrcweir 					for(sal_Int32 i=0;i< nCount;++i)
203cdf0e10cSrcweir 					{
204cdf0e10cSrcweir 						WrapT aElement = GetItem(i);
205cdf0e10cSrcweir 						if(aElement.IsValid())
206cdf0e10cSrcweir 							_rVector.push_back(aElement.get_Name());
207cdf0e10cSrcweir 					}
208cdf0e10cSrcweir 				}
209cdf0e10cSrcweir 			}
210cdf0e10cSrcweir 		};
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 		template<class Ts, class T, class WrapT> class WpOLEAppendCollection:
213cdf0e10cSrcweir 				public WpOLECollection<Ts,T,WrapT>
214cdf0e10cSrcweir 		{
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 		public:
217cdf0e10cSrcweir 			// Konstruktoren, operator=
218cdf0e10cSrcweir 			// diese rufen nur die Oberklasse
219cdf0e10cSrcweir 			using WpOLEBase<Ts>::pInterface;
WpOLEAppendCollection(Ts * pInt=NULL)220cdf0e10cSrcweir 			WpOLEAppendCollection(Ts* pInt=NULL):WpOLECollection<Ts,T,WrapT>(pInt){}
WpOLEAppendCollection(const WpOLEAppendCollection & rhs)221cdf0e10cSrcweir 			WpOLEAppendCollection(const WpOLEAppendCollection& rhs){ operator=(rhs); }
operator =(const WpOLEAppendCollection & rhs)222cdf0e10cSrcweir 			inline WpOLEAppendCollection& operator=(const WpOLEAppendCollection& rhs)
223cdf0e10cSrcweir 				{WpOLEBase<Ts>::operator=(rhs); return *this;};
224cdf0e10cSrcweir 			//////////////////////////////////////////////////////////////////////
225cdf0e10cSrcweir 
Append(const WrapT & aWrapT)226cdf0e10cSrcweir 			inline sal_Bool Append(const WrapT& aWrapT)
227cdf0e10cSrcweir 			{
228cdf0e10cSrcweir 				return SUCCEEDED(pInterface->Append(OLEVariant((T*)aWrapT)));
229cdf0e10cSrcweir 			};
230cdf0e10cSrcweir 
Delete(const::rtl::OUString & sName)231cdf0e10cSrcweir 			inline sal_Bool Delete(const ::rtl::OUString& sName)
232cdf0e10cSrcweir 			{
233cdf0e10cSrcweir 				return SUCCEEDED(pInterface->Delete(OLEVariant(sName)));
234cdf0e10cSrcweir 			};
235cdf0e10cSrcweir 
236cdf0e10cSrcweir 
237cdf0e10cSrcweir 		};
238cdf0e10cSrcweir 	}
239cdf0e10cSrcweir }
240cdf0e10cSrcweir #endif // _CONNECTIVITY_ADO_AOLEWRAP_HXX_
241cdf0e10cSrcweir 
242