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 _CONNECTIVITY_ADO_AOLEWRAP_HXX_
20 #define _CONNECTIVITY_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
*();
58 //////////////////////////////////////////////////////////////////////////
60 // Template class WpOLEBase<class T>
61 // ==================================
63 // Objects of this class contain a pointer to an interface of the type T.
64 // The ctors and operator= make sure, that AddRef() and Release() are being
65 // called adhering to COM conventions.
66 // An object can also hold no pointer (null pointer), calling IsValid() then
69 // In order to do efficient pass-by-value, this class (as all derived classes)
70 // is a thin wrapper class, avoiding virtual methods and inlining.
71 //------------------------------------------------------------------------
72 template<class T
> class WpOLEBase
: public WpBase
78 WpOLEBase(T
* pInt
= NULL
) : WpBase(pInt
),pInterface(pInt
){}
82 WpOLEBase
<T
>& operator=(const WpOLEBase
<T
>& rhs
)
84 WpBase::operator=(rhs
);
85 pInterface
= rhs
.pInterface
;
89 WpOLEBase
<T
>& operator=(T
* rhs
)
91 WpBase::operator=(rhs
);
92 pInterface
= rhs
.pInterface
;
96 WpOLEBase(const WpOLEBase
<T
>& aWrapper
)
98 , pInterface( aWrapper
.pInterface
)
106 operator T
*() const { return static_cast<T
*>(pInterface
); }
107 void setWithOutAddRef(T
* _pInterface
)
109 pInterface
= _pInterface
;
110 WpBase::setIDispatch(_pInterface
);
115 //////////////////////////////////////////////////////////////////////////
117 // Template class WpOLECollection<class Ts, class T, class WrapT>
118 // ===============================================================
120 // This class (derived from WpOLEBase<Ts>), abstracts away the properties
121 // common to DAO collections:
123 // They are accessed via an interface Ts (e.g. DAOFields) and can return
124 // Items of the Type T (actually: with the interface T, e.g. DAOField)
125 // via get_Item (here GetItem).
127 // This wrapper class does not expose an interface T, however,
128 // it exposes an object of the class WrapT. This must allow a construction
129 // by T, preferably it is derived from WpOLEBase<T>.
131 //------------------------------------------------------------------------
132 template<class Ts
, class T
, class WrapT
> class WpOLECollection
: public WpOLEBase
<Ts
>
135 using WpOLEBase
<Ts
>::pInterface
;
136 using WpOLEBase
<Ts
>::IsValid
;
138 // They only call the superclass
139 WpOLECollection(Ts
* pInt
=NULL
):WpOLEBase
<Ts
>(pInt
){}
140 WpOLECollection(const WpOLECollection
& rhs
) : WpOLEBase
<Ts
>(rhs
) {}
141 inline WpOLECollection
& operator=(const WpOLECollection
& rhs
)
142 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
144 //////////////////////////////////////////////////////////////////////
146 inline void Refresh(){pInterface
->Refresh();}
148 inline sal_Int32
GetItemCount() const
150 sal_Int32 nCount
= 0;
151 return pInterface
? (SUCCEEDED(pInterface
->get_Count(&nCount
)) ? nCount
: sal_Int32(0)) : sal_Int32(0);
154 inline WrapT
GetItem(sal_Int32 index
) const
156 OSL_ENSURE(index
>= 0 && index
<GetItemCount(),"Wrong index for field!");
159 if(SUCCEEDED(pInterface
->get_Item(OLEVariant(index
), &pT
)))
160 aRet
.setWithOutAddRef(pT
);
164 inline WrapT
GetItem(const OLEVariant
& index
) const
168 if(SUCCEEDED(pInterface
->get_Item(index
, &pT
)))
169 aRet
.setWithOutAddRef(pT
);
173 inline WrapT
GetItem(const OUString
& sStr
) const
177 if (FAILED(pInterface
->get_Item(OLEVariant(sStr
), &pT
)))
179 #if OSL_DEBUG_LEVEL > 0
180 OString
sTemp("Unknown Item: ");
181 sTemp
+= OString(sStr
.getStr(),sStr
.getLength(),osl_getThreadTextEncoding());
182 OSL_FAIL(sTemp
.getStr());
186 aRet
.setWithOutAddRef(pT
);
189 inline void fillElementNames(TStringVector
& _rVector
)
194 sal_Int32 nCount
= GetItemCount();
195 _rVector
.reserve(nCount
);
196 for(sal_Int32 i
=0;i
< nCount
;++i
)
198 WrapT aElement
= GetItem(i
);
199 if(aElement
.IsValid())
200 _rVector
.push_back(aElement
.get_Name());
206 template<class Ts
, class T
, class WrapT
> class WpOLEAppendCollection
:
207 public WpOLECollection
<Ts
,T
,WrapT
>
212 // They only call the superclass
213 using WpOLEBase
<Ts
>::pInterface
;
214 WpOLEAppendCollection(Ts
* pInt
=NULL
):WpOLECollection
<Ts
,T
,WrapT
>(pInt
){}
215 WpOLEAppendCollection(const WpOLEAppendCollection
& rhs
) : WpOLECollection
<Ts
, T
, WrapT
>(rhs
) {}
216 inline WpOLEAppendCollection
& operator=(const WpOLEAppendCollection
& rhs
)
217 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
218 //////////////////////////////////////////////////////////////////////
220 inline sal_Bool
Append(const WrapT
& aWrapT
)
222 return SUCCEEDED(pInterface
->Append(OLEVariant((T
*)aWrapT
)));
225 inline sal_Bool
Delete(const OUString
& sName
)
227 return SUCCEEDED(pInterface
->Delete(OLEVariant(sName
)));
234 #endif // _CONNECTIVITY_ADO_AOLEWRAP_HXX_
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */