Update ooo320-m1
[ooovba.git] / connectivity / source / inc / ado / Aolewrap.hxx
blob5453fc508ab1dfbca63b24d4b2eef6df598b8fd3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Aolewrap.hxx,v $
10 * $Revision: 1.13 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #ifndef _CONNECTIVITY_ADO_AOLEWRAP_HXX_
31 #define _CONNECTIVITY_ADO_AOLEWRAP_HXX_
33 #include <osl/diagnose.h>
34 #include <osl/thread.h>
35 #include <map>
36 #include <vector>
37 #include "connectivity/StdTypeDefs.hxx"
39 namespace rtl
41 class OUString;
43 namespace connectivity
45 namespace ado
47 class OLEVariant;
48 class WpBase
50 protected:
51 IDispatch* pIUnknown;
53 void setIDispatch(IDispatch* _pIUnknown);
54 public:
55 WpBase();
56 WpBase(IDispatch* pInt);
57 //inline
58 WpBase& operator=(const WpBase& rhs);
59 WpBase& operator=(IDispatch* rhs);
60 WpBase(const WpBase& aWrapper);
61 virtual ~WpBase();
62 void clear();
65 sal_Bool IsValid() const;
66 operator IDispatch*();
69 //////////////////////////////////////////////////////////////////////////
71 // Template-Klasse WpOLEBase<class T>
72 // ==================================
74 // Objekte dieser Klasse haelt einen Zeiger auf ein Interface vom Typ T.
75 // Es gibt Konstruktoren und Zuweisungsoperator die sicherstellen, dass
76 // AddRef() und Release() entsprechend den COM-Konventionen gerufen werden.
77 // Ein Objekt kann auch keinen Zeiger halten (Nullzeiger), dann ergibt
78 // der Aufruf von IsValid() FALSE.
80 // Um effizientes pass-by-value machen zu koennen, ist diese (ebenso wie die
81 // abgeleiteten Klassen) eine ganz schmale Wrapper-Klasse unter Vermeidung
82 // virtueller Methoden und mit Inlining.
84 //------------------------------------------------------------------------
85 template<class T> class WpOLEBase : public WpBase
87 protected:
88 T* pInterface;
90 public:
91 WpOLEBase(T* pInt = NULL) : WpBase(pInt),pInterface(pInt){}
94 //inline
95 WpOLEBase<T>& operator=(const WpOLEBase<T>& rhs)
97 WpBase::operator=(rhs);
98 pInterface = rhs.pInterface;
99 return *this;
102 WpOLEBase<T>& operator=(T* rhs)
104 WpBase::operator=(rhs);
105 pInterface = rhs.pInterface;
106 return *this;
109 WpOLEBase(const WpOLEBase<T>& aWrapper)
111 operator=(aWrapper);
114 virtual ~WpOLEBase()
118 operator T*() const { return static_cast<T*>(pInterface); }
119 void setWithOutAddRef(T* _pInterface)
121 pInterface = _pInterface;
122 WpBase::setIDispatch(_pInterface);
127 //////////////////////////////////////////////////////////////////////////
129 // Template-Klasse WpOLECollection<class Ts, class T, class WrapT>
130 // ===============================================================
132 // Diese Klasse, welche sich von WpOLEBase<Ts> ableitet, abstrahiert die
133 // den DAO-Collections gemeinsamen Eigenschaften:
135 // Sie werden ueber ein Interface Ts (etwa: DAOFields) angesprochen
136 // und koennen ueber get_Item (hier:GetItem) Items des Typs T (genauer:
137 // mit Interface T, etwa DAOField) herausgeben.
139 // Diese Wrapperklasse gibt aber nicht ein Interface T heraus,
140 // sondern ein Objekt der Klasse WrapT. Dieses muss eine Konstruktion
141 // durch T zulassen, vorzugsweise ist es von WpOLEBase<T> abgeleitet.
144 //------------------------------------------------------------------------
145 template<class Ts, class T, class WrapT> class WpOLECollection : public WpOLEBase<Ts>
147 public:
148 using WpOLEBase<Ts>::pInterface;
149 using WpOLEBase<Ts>::IsValid;
150 // Konstruktoren, operator=
151 // diese rufen nur die Oberklasse
152 WpOLECollection(Ts* pInt=NULL):WpOLEBase<Ts>(pInt){}
153 WpOLECollection(const WpOLECollection& rhs){operator=(rhs);}
154 inline WpOLECollection& operator=(const WpOLECollection& rhs)
155 {WpOLEBase<Ts>::operator=(rhs); return *this;};
157 //////////////////////////////////////////////////////////////////////
159 inline void Refresh(){pInterface->Refresh();}
161 inline sal_Int32 GetItemCount() const
163 sal_Int32 nCount = 0;
164 return pInterface ? (SUCCEEDED(pInterface->get_Count(&nCount)) ? nCount : sal_Int32(0)) : sal_Int32(0);
167 inline WrapT GetItem(sal_Int32 index) const
169 OSL_ENSURE(index >= 0 && index<GetItemCount(),"Wrong index for field!");
170 T* pT = NULL;
171 WrapT aRet(NULL);
172 if(SUCCEEDED(pInterface->get_Item(OLEVariant(index), &pT)))
173 aRet.setWithOutAddRef(pT);
174 return aRet;
177 inline WrapT GetItem(const OLEVariant& index) const
179 T* pT = NULL;
180 WrapT aRet(NULL);
181 if(SUCCEEDED(pInterface->get_Item(index, &pT)))
182 aRet.setWithOutAddRef(pT);
183 return aRet;
186 inline WrapT GetItem(const ::rtl::OUString& sStr) const
188 WrapT aRet(NULL);
189 T* pT = NULL;
190 if (FAILED(pInterface->get_Item(OLEVariant(sStr), &pT)))
192 #if OSL_DEBUG_LEVEL > 0
193 ::rtl::OString sTemp("Unknown Item: ");
194 sTemp += ::rtl::OString(sStr.getStr(),sStr.getLength(),osl_getThreadTextEncoding());
195 OSL_ENSURE(0,sTemp);
196 #endif
198 else
199 aRet.setWithOutAddRef(pT);
200 return aRet;
202 inline void fillElementNames(TStringVector& _rVector)
204 if(IsValid())
206 Refresh();
207 sal_Int32 nCount = GetItemCount();
208 _rVector.reserve(nCount);
209 for(sal_Int32 i=0;i< nCount;++i)
211 WrapT aElement = GetItem(i);
212 if(aElement.IsValid())
213 _rVector.push_back(aElement.get_Name());
219 template<class Ts, class T, class WrapT> class WpOLEAppendCollection:
220 public WpOLECollection<Ts,T,WrapT>
223 public:
224 // Konstruktoren, operator=
225 // diese rufen nur die Oberklasse
226 using WpOLEBase<Ts>::pInterface;
227 WpOLEAppendCollection(Ts* pInt=NULL):WpOLECollection<Ts,T,WrapT>(pInt){}
228 WpOLEAppendCollection(const WpOLEAppendCollection& rhs){ operator=(rhs); }
229 inline WpOLEAppendCollection& operator=(const WpOLEAppendCollection& rhs)
230 {WpOLEBase<Ts>::operator=(rhs); return *this;};
231 //////////////////////////////////////////////////////////////////////
233 inline sal_Bool Append(const WrapT& aWrapT)
235 return SUCCEEDED(pInterface->Append(OLEVariant((T*)aWrapT)));
238 inline sal_Bool Delete(const ::rtl::OUString& sName)
240 return SUCCEEDED(pInterface->Delete(OLEVariant(sName)));
247 #endif // _CONNECTIVITY_ADO_AOLEWRAP_HXX_