bump product version to 5.0.4.1
[LibreOffice.git] / connectivity / source / inc / ado / Aolewrap.hxx
blobfba73837c02dbc6bd1f17b1eef4a9e1a18002fd4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
24 #include <map>
25 #include <vector>
26 #include <connectivity/StdTypeDefs.hxx>
28 namespace rtl
30 class OUString;
32 namespace connectivity
34 namespace ado
36 class OLEVariant;
37 class WpBase
39 protected:
40 IDispatch* pIUnknown;
42 void setIDispatch(IDispatch* _pIUnknown);
43 public:
44 WpBase();
45 WpBase(IDispatch* pInt);
46 //inline
47 WpBase& operator=(const WpBase& rhs);
48 WpBase& operator=(IDispatch* rhs);
49 WpBase(const WpBase& aWrapper);
50 virtual ~WpBase();
51 void clear();
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
66 // returns false.
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
73 protected:
74 T* pInterface;
76 public:
77 WpOLEBase(T* pInt = NULL) : WpBase(pInt),pInterface(pInt){}
80 //inline
81 WpOLEBase<T>& operator=(const WpOLEBase<T>& rhs)
83 WpBase::operator=(rhs);
84 pInterface = rhs.pInterface;
85 return *this;
88 WpOLEBase<T>& operator=(T* rhs)
90 WpBase::operator=(rhs);
91 pInterface = rhs.pInterface;
92 return *this;
95 WpOLEBase(const WpOLEBase<T>& aWrapper)
96 : WpBase( aWrapper )
97 , pInterface( aWrapper.pInterface )
101 virtual ~WpOLEBase()
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>
130 public:
131 using WpOLEBase<Ts>::pInterface;
132 using WpOLEBase<Ts>::IsValid;
133 // Ctors, operator=
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!");
153 T* pT = NULL;
154 WrapT aRet(NULL);
155 if(SUCCEEDED(pInterface->get_Item(OLEVariant(index), &pT)))
156 aRet.setWithOutAddRef(pT);
157 return aRet;
160 inline WrapT GetItem(const OLEVariant& index) const
162 T* pT = NULL;
163 WrapT aRet(NULL);
164 if(SUCCEEDED(pInterface->get_Item(index, &pT)))
165 aRet.setWithOutAddRef(pT);
166 return aRet;
169 inline WrapT GetItem(const OUString& sStr) const
171 WrapT aRet(NULL);
172 T* pT = NULL;
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());
179 #endif
181 else
182 aRet.setWithOutAddRef(pT);
183 return aRet;
185 inline void fillElementNames(TStringVector& _rVector)
187 if(IsValid())
189 Refresh();
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>
206 public:
207 // Ctors, operator=
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: */