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 .
21 #include <osl/diagnose.h>
22 #include <osl/thread.h>
23 #include <systools/win32/comtools.hxx>
28 #include "Aolevariant.hxx"
34 namespace connectivity::ado
36 // Template class WpOLEBase<class T>
37 // ==================================
39 // Objects of this class contain a pointer to an interface of the type T.
41 template<class T
> class WpOLEBase
44 sal::systools::COMReference
<T
> pInterface
;
47 WpOLEBase(T
* pInt
= nullptr) : pInterface(pInt
){}
49 WpOLEBase(const WpOLEBase
<T
>& aWrapper
)
50 : pInterface( aWrapper
.pInterface
)
55 WpOLEBase
<T
>& operator=(const WpOLEBase
<T
>& rhs
)
57 pInterface
= rhs
.pInterface
;
61 operator T
*() const { return pInterface
.get(); }
62 T
** operator&() { return &pInterface
; }
63 bool IsValid() const { return pInterface
.is(); }
64 void set(T
* p
) { pInterface
= p
; }
65 void clear() { pInterface
.clear(); }
69 // Template class WpOLECollection<class Ts, class WrapT>
70 // ===============================================================
72 // This class (derived from WpOLEBase<Ts>), abstracts away the properties
73 // common to DAO collections:
75 // They are accessed via an interface Ts (e.g. DAOFields) and can return
76 // Items of the type wrapped by WrapT (actually: with the interface, e.g.
77 // DAOField) via get_Item (here GetItem).
79 // This wrapper class exposes an object of the class WrapT.
81 template<class Ts
, class WrapT
> class WpOLECollection
: public WpOLEBase
<Ts
>
84 using WpOLEBase
<Ts
>::pInterface
;
85 using WpOLEBase
<Ts
>::IsValid
;
87 // They only call the superclass
88 WpOLECollection() = default;
89 WpOLECollection(const WpOLECollection
& rhs
) : WpOLEBase
<Ts
>(rhs
) {}
90 WpOLECollection
& operator=(const WpOLECollection
& rhs
)
91 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
94 void Refresh(){pInterface
->Refresh();}
96 sal_Int32
GetItemCount() const
99 return pInterface
? (SUCCEEDED(pInterface
->get_Count(&nCount
)) ? nCount
: sal_Int32(0)) : sal_Int32(0);
102 WrapT
GetItem(sal_Int32 index
) const
104 OSL_ENSURE(index
>= 0 && index
<GetItemCount(),"Wrong index for field!");
106 pInterface
->get_Item(OLEVariant(index
), &aRet
);
110 WrapT
GetItem(const OLEVariant
& index
) const
113 pInterface
->get_Item(index
, &aRet
);
117 WrapT
GetItem(const OUString
& sStr
) const
120 if (FAILED(pInterface
->get_Item(OLEVariant(sStr
), &aRet
)))
122 #if OSL_DEBUG_LEVEL > 0
123 OString
sTemp("Unknown Item: " + OString(sStr
.getStr(),sStr
.getLength(),osl_getThreadTextEncoding()));
124 OSL_FAIL(sTemp
.getStr());
129 void fillElementNames(::std::vector
< OUString
>& _rVector
)
134 sal_Int32 nCount
= GetItemCount();
135 _rVector
.reserve(nCount
);
136 for(sal_Int32 i
=0;i
< nCount
;++i
)
138 WrapT aElement
= GetItem(i
);
139 if(aElement
.IsValid())
140 _rVector
.push_back(aElement
.get_Name());
146 template<class Ts
, class WrapT
> class WpOLEAppendCollection
:
147 public WpOLECollection
<Ts
,WrapT
>
152 // They only call the superclass
153 using WpOLEBase
<Ts
>::pInterface
;
154 WpOLEAppendCollection() = default;
155 WpOLEAppendCollection(const WpOLEAppendCollection
& rhs
) : WpOLECollection
<Ts
, WrapT
>(rhs
) {}
156 WpOLEAppendCollection
& operator=(const WpOLEAppendCollection
& rhs
)
157 {WpOLEBase
<Ts
>::operator=(rhs
); return *this;};
160 bool Append(const WrapT
& aWrapT
)
162 return SUCCEEDED(pInterface
->Append(OLEVariant(aWrapT
)));
165 bool Delete(const OUString
& sName
)
167 return SUCCEEDED(pInterface
->Delete(OLEVariant(sName
)));
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */