tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / connectivity / source / inc / ado / Aolewrap.hxx
blob9d402fcf21b11c71f06cfc043e69b46601f06545
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 #pragma once
21 #include <osl/diagnose.h>
22 #include <osl/thread.h>
23 #include <systools/win32/comtools.hxx>
25 #include <map>
26 #include <vector>
28 #include "Aolevariant.hxx"
30 namespace rtl
32 class OUString;
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
43 protected:
44 sal::systools::COMReference<T> pInterface;
46 public:
47 WpOLEBase(T* pInt = nullptr) : pInterface(pInt){}
49 WpOLEBase(const WpOLEBase<T>& aWrapper)
50 : pInterface( aWrapper.pInterface )
54 //inline
55 WpOLEBase<T>& operator=(const WpOLEBase<T>& rhs)
57 pInterface = rhs.pInterface;
58 return *this;
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>
83 public:
84 using WpOLEBase<Ts>::pInterface;
85 using WpOLEBase<Ts>::IsValid;
86 // Ctors, operator=
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
98 sal_Int32 nCount = 0;
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!");
105 WrapT aRet;
106 pInterface->get_Item(OLEVariant(index), &aRet);
107 return aRet;
110 WrapT GetItem(const OLEVariant& index) const
112 WrapT aRet;
113 pInterface->get_Item(index, &aRet);
114 return aRet;
117 WrapT GetItem(const OUString& sStr) const
119 WrapT aRet;
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());
125 #endif
127 return aRet;
129 void fillElementNames(::std::vector< OUString>& _rVector)
131 if(IsValid())
133 Refresh();
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>
150 public:
151 // Ctors, operator=
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: */