1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_CONNECTIVITY_SOURCE_INC_ADO_AOLEWRAP_HXX
20 #define INCLUDED_CONNECTIVITY_SOURCE_INC_ADO_AOLEWRAP_HXX
22 #include <osl/diagnose.h>
23 #include <osl/thread.h>
26 #include <connectivity/StdTypeDefs.hxx>
32 namespace connectivity
42 void setIDispatch(IDispatch
* _pIUnknown
);
45 WpBase(IDispatch
* pInt
);
47 WpBase
& operator=(const WpBase
& rhs
);
48 WpBase
& operator=(IDispatch
* rhs
);
49 WpBase(const WpBase
& aWrapper
);
54 sal_Bool
IsValid() const;
55 operator IDispatch
*();
59 // Template class WpOLEBase<class T>
60 // ==================================
62 // Objects of this class contain a pointer to an interface of the type T.
63 // The ctors and operator= make sure, that AddRef() and Release() are being
64 // called adhering to COM conventions.
65 // An object can also hold no pointer (null pointer), calling IsValid() then
68 // In order to do efficient pass-by-value, this class (as all derived classes)
69 // is a thin wrapper class, avoiding virtual methods and inlining.
71 template<class T
> class WpOLEBase
: public WpBase
77 WpOLEBase(T
* pInt
= NULL
) : WpBase(pInt
),pInterface(pInt
){}
81 WpOLEBase
<T
>& operator=(const WpOLEBase
<T
>& rhs
)
83 WpBase::operator=(rhs
);
84 pInterface
= rhs
.pInterface
;
88 WpOLEBase
<T
>& operator=(T
* rhs
)
90 WpBase::operator=(rhs
);
91 pInterface
= rhs
.pInterface
;
95 WpOLEBase(const WpOLEBase
<T
>& aWrapper
)
97 , pInterface( aWrapper
.pInterface
)
105 operator T
*() const { return static_cast<T
*>(pInterface
); }
106 void setWithOutAddRef(T
* _pInterface
)
108 pInterface
= _pInterface
;
109 WpBase::setIDispatch(_pInterface
);
114 // Template class WpOLECollection<class Ts, class T, class WrapT>
115 // ===============================================================
117 // This class (derived from WpOLEBase<Ts>), abstracts away the properties
118 // common to DAO collections:
120 // They are accessed via an interface Ts (e.g. DAOFields) and can return
121 // Items of the Type T (actually: with the interface T, e.g. DAOField)
122 // via get_Item (here GetItem).
124 // This wrapper class does not expose an interface T, however,
125 // it exposes an object of the class WrapT. This must allow a construction
126 // by T, preferably it is derived from WpOLEBase<T>.
128 template<class Ts
, class T
, class WrapT
> class WpOLECollection
: public WpOLEBase
<Ts
>
131 using WpOLEBase
<Ts
>::pInterface
;
132 using WpOLEBase
<Ts
>::IsValid
;
134 // They only call the superclass
135 WpOLECollection(Ts
* pInt
=NULL
):WpOLEBase
<Ts
>(pInt
){}
136 WpOLECollection(const WpOLECollection
& rhs
) : WpOLEBase
<Ts
>(rhs
) {}
137 inline WpOLECollection
& operator=(const WpOLECollection
& rhs
)
138 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
142 inline void Refresh(){pInterface
->Refresh();}
144 inline sal_Int32
GetItemCount() const
146 sal_Int32 nCount
= 0;
147 return pInterface
? (SUCCEEDED(pInterface
->get_Count(&nCount
)) ? nCount
: sal_Int32(0)) : sal_Int32(0);
150 inline WrapT
GetItem(sal_Int32 index
) const
152 OSL_ENSURE(index
>= 0 && index
<GetItemCount(),"Wrong index for field!");
155 if(SUCCEEDED(pInterface
->get_Item(OLEVariant(index
), &pT
)))
156 aRet
.setWithOutAddRef(pT
);
160 inline WrapT
GetItem(const OLEVariant
& index
) const
164 if(SUCCEEDED(pInterface
->get_Item(index
, &pT
)))
165 aRet
.setWithOutAddRef(pT
);
169 inline WrapT
GetItem(const OUString
& sStr
) const
173 if (FAILED(pInterface
->get_Item(OLEVariant(sStr
), &pT
)))
175 #if OSL_DEBUG_LEVEL > 0
176 OString
sTemp("Unknown Item: ");
177 sTemp
+= OString(sStr
.getStr(),sStr
.getLength(),osl_getThreadTextEncoding());
178 OSL_FAIL(sTemp
.getStr());
182 aRet
.setWithOutAddRef(pT
);
185 inline void fillElementNames(TStringVector
& _rVector
)
190 sal_Int32 nCount
= GetItemCount();
191 _rVector
.reserve(nCount
);
192 for(sal_Int32 i
=0;i
< nCount
;++i
)
194 WrapT aElement
= GetItem(i
);
195 if(aElement
.IsValid())
196 _rVector
.push_back(aElement
.get_Name());
202 template<class Ts
, class T
, class WrapT
> class WpOLEAppendCollection
:
203 public WpOLECollection
<Ts
,T
,WrapT
>
208 // They only call the superclass
209 using WpOLEBase
<Ts
>::pInterface
;
210 WpOLEAppendCollection(Ts
* pInt
=NULL
):WpOLECollection
<Ts
,T
,WrapT
>(pInt
){}
211 WpOLEAppendCollection(const WpOLEAppendCollection
& rhs
) : WpOLECollection
<Ts
, T
, WrapT
>(rhs
) {}
212 inline WpOLEAppendCollection
& operator=(const WpOLEAppendCollection
& rhs
)
213 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
216 inline sal_Bool
Append(const WrapT
& aWrapT
)
218 return SUCCEEDED(pInterface
->Append(OLEVariant((T
*)aWrapT
)));
221 inline sal_Bool
Delete(const OUString
& sName
)
223 return SUCCEEDED(pInterface
->Delete(OLEVariant(sName
)));
230 #endif // INCLUDED_CONNECTIVITY_SOURCE_INC_ADO_AOLEWRAP_HXX
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */