1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
28 #ifndef _CONNECTIVITY_ADO_AOLEWRAP_HXX_
29 #define _CONNECTIVITY_ADO_AOLEWRAP_HXX_
31 #include <osl/diagnose.h>
32 #include <osl/thread.h>
35 #include "connectivity/StdTypeDefs.hxx"
41 namespace connectivity
51 void setIDispatch(IDispatch
* _pIUnknown
);
54 WpBase(IDispatch
* pInt
);
56 WpBase
& operator=(const WpBase
& rhs
);
57 WpBase
& operator=(IDispatch
* rhs
);
58 WpBase(const WpBase
& aWrapper
);
63 sal_Bool
IsValid() const;
64 operator IDispatch
*();
67 //////////////////////////////////////////////////////////////////////////
69 // Template class WpOLEBase<class T>
70 // ==================================
72 // Objects of this class contain a pointer to an interface of the type T.
73 // The ctors and operator= make sure, that AddRef() and Release() are being
74 // called adhering to COM conventions.
75 // An object can also hold no pointer (null pointer), calling IsValid() then
78 // In order to do efficient pass-by-value, this class (as all derived classes)
79 // is a thin wrapper class, avoiding virtual methods and inlining.
80 //------------------------------------------------------------------------
81 template<class T
> class WpOLEBase
: public WpBase
87 WpOLEBase(T
* pInt
= NULL
) : WpBase(pInt
),pInterface(pInt
){}
91 WpOLEBase
<T
>& operator=(const WpOLEBase
<T
>& rhs
)
93 WpBase::operator=(rhs
);
94 pInterface
= rhs
.pInterface
;
98 WpOLEBase
<T
>& operator=(T
* rhs
)
100 WpBase::operator=(rhs
);
101 pInterface
= rhs
.pInterface
;
105 WpOLEBase(const WpOLEBase
<T
>& aWrapper
)
114 operator T
*() const { return static_cast<T
*>(pInterface
); }
115 void setWithOutAddRef(T
* _pInterface
)
117 pInterface
= _pInterface
;
118 WpBase::setIDispatch(_pInterface
);
123 //////////////////////////////////////////////////////////////////////////
125 // Template class WpOLECollection<class Ts, class T, class WrapT>
126 // ===============================================================
128 // This class (derived from WpOLEBase<Ts>), abstracts away the properties
129 // common to DAO collections:
131 // They are accessed via an interface Ts (e.g. DAOFields) and can return
132 // Items of the Type T (actually: with the interface T, e.g. DAOField)
133 // via get_Item (here GetItem).
135 // This wrapper class does not expose an interface T, however,
136 // it exposes an object of the class WrapT. This must allow a construction
137 // by T, preferably it is derived from WpOLEBase<T>.
139 //------------------------------------------------------------------------
140 template<class Ts
, class T
, class WrapT
> class WpOLECollection
: public WpOLEBase
<Ts
>
143 using WpOLEBase
<Ts
>::pInterface
;
144 using WpOLEBase
<Ts
>::IsValid
;
146 // They only call the superclass
147 WpOLECollection(Ts
* pInt
=NULL
):WpOLEBase
<Ts
>(pInt
){}
148 WpOLECollection(const WpOLECollection
& rhs
) : WpOLEBase
<Ts
>(rhs
) {}
149 inline WpOLECollection
& operator=(const WpOLECollection
& rhs
)
150 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
152 //////////////////////////////////////////////////////////////////////
154 inline void Refresh(){pInterface
->Refresh();}
156 inline sal_Int32
GetItemCount() const
158 sal_Int32 nCount
= 0;
159 return pInterface
? (SUCCEEDED(pInterface
->get_Count(&nCount
)) ? nCount
: sal_Int32(0)) : sal_Int32(0);
162 inline WrapT
GetItem(sal_Int32 index
) const
164 OSL_ENSURE(index
>= 0 && index
<GetItemCount(),"Wrong index for field!");
167 if(SUCCEEDED(pInterface
->get_Item(OLEVariant(index
), &pT
)))
168 aRet
.setWithOutAddRef(pT
);
172 inline WrapT
GetItem(const OLEVariant
& index
) const
176 if(SUCCEEDED(pInterface
->get_Item(index
, &pT
)))
177 aRet
.setWithOutAddRef(pT
);
181 inline WrapT
GetItem(const ::rtl::OUString
& sStr
) const
185 if (FAILED(pInterface
->get_Item(OLEVariant(sStr
), &pT
)))
187 #if OSL_DEBUG_LEVEL > 0
188 ::rtl::OString
sTemp("Unknown Item: ");
189 sTemp
+= ::rtl::OString(sStr
.getStr(),sStr
.getLength(),osl_getThreadTextEncoding());
190 OSL_FAIL(sTemp
.getStr());
194 aRet
.setWithOutAddRef(pT
);
197 inline void fillElementNames(TStringVector
& _rVector
)
202 sal_Int32 nCount
= GetItemCount();
203 _rVector
.reserve(nCount
);
204 for(sal_Int32 i
=0;i
< nCount
;++i
)
206 WrapT aElement
= GetItem(i
);
207 if(aElement
.IsValid())
208 _rVector
.push_back(aElement
.get_Name());
214 template<class Ts
, class T
, class WrapT
> class WpOLEAppendCollection
:
215 public WpOLECollection
<Ts
,T
,WrapT
>
220 // They only call the superclass
221 using WpOLEBase
<Ts
>::pInterface
;
222 WpOLEAppendCollection(Ts
* pInt
=NULL
):WpOLECollection
<Ts
,T
,WrapT
>(pInt
){}
223 WpOLEAppendCollection(const WpOLEAppendCollection
& rhs
) : WpOLECollection
<Ts
, T
, WrapT
>(rhs
) {}
224 inline WpOLEAppendCollection
& operator=(const WpOLEAppendCollection
& rhs
)
225 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
226 //////////////////////////////////////////////////////////////////////
228 inline sal_Bool
Append(const WrapT
& aWrapT
)
230 return SUCCEEDED(pInterface
->Append(OLEVariant((T
*)aWrapT
)));
233 inline sal_Bool
Delete(const ::rtl::OUString
& sName
)
235 return SUCCEEDED(pInterface
->Delete(OLEVariant(sName
)));
242 #endif // _CONNECTIVITY_ADO_AOLEWRAP_HXX_
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */