LibreOffice Module connectivity (master) 1
Aolewrap.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#pragma once
20
21#include <osl/diagnose.h>
22#include <osl/thread.h>
23#include <systools/win32/comtools.hxx>
24
25#include <map>
26#include <vector>
27
28#include "Aolevariant.hxx"
29
30namespace rtl
31{
32 class OUString;
33}
34namespace connectivity::ado
35{
36 // Template class WpOLEBase<class T>
37 // ==================================
38 //
39 // Objects of this class contain a pointer to an interface of the type T.
40
41 template<class T> class WpOLEBase
42 {
43 protected:
44 sal::systools::COMReference<T> pInterface;
45
46 public:
47 WpOLEBase(T* pInt = nullptr) : pInterface(pInt){}
48
49 WpOLEBase(const WpOLEBase<T>& aWrapper)
50 : pInterface( aWrapper.pInterface )
51 {
52 }
53
54 //inline
56 {
58 return *this;
59 };
60
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(); }
66 };
67
68
69 // Template class WpOLECollection<class Ts, class WrapT>
70 // ===============================================================
71 //
72 // This class (derived from WpOLEBase<Ts>), abstracts away the properties
73 // common to DAO collections:
74 //
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).
78 //
79 // This wrapper class exposes an object of the class WrapT.
80
81 template<class Ts, class WrapT> class WpOLECollection : public WpOLEBase<Ts>
82 {
83 public:
86 // Ctors, operator=
87 // They only call the superclass
88 WpOLECollection() = default;
89 WpOLECollection(const WpOLECollection& rhs) : WpOLEBase<Ts>(rhs) {}
91 {WpOLEBase<Ts>::operator=(rhs); return *this;};
92
93
94 void Refresh(){pInterface->Refresh();}
95
96 sal_Int32 GetItemCount() const
97 {
98 sal_Int32 nCount = 0;
99 return pInterface ? (SUCCEEDED(pInterface->get_Count(&nCount)) ? nCount : sal_Int32(0)) : sal_Int32(0);
100 }
101
102 WrapT GetItem(sal_Int32 index) const
103 {
104 OSL_ENSURE(index >= 0 && index<GetItemCount(),"Wrong index for field!");
105 WrapT aRet;
106 pInterface->get_Item(OLEVariant(index), &aRet);
107 return aRet;
108 }
109
110 WrapT GetItem(const OLEVariant& index) const
111 {
112 WrapT aRet;
113 pInterface->get_Item(index, &aRet);
114 return aRet;
115 }
116
117 WrapT GetItem(const OUString& sStr) const
118 {
119 WrapT aRet;
120 if (FAILED(pInterface->get_Item(OLEVariant(sStr), &aRet)))
121 {
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
126 }
127 return aRet;
128 }
129 void fillElementNames(::std::vector< OUString>& _rVector)
130 {
131 if(IsValid())
132 {
133 Refresh();
134 sal_Int32 nCount = GetItemCount();
135 _rVector.reserve(nCount);
136 for(sal_Int32 i=0;i< nCount;++i)
137 {
138 WrapT aElement = GetItem(i);
139 if(aElement.IsValid())
140 _rVector.push_back(aElement.get_Name());
141 }
142 }
143 }
144 };
145
146 template<class Ts, class WrapT> class WpOLEAppendCollection:
147 public WpOLECollection<Ts,WrapT>
148 {
149
150 public:
151 // Ctors, operator=
152 // They only call the superclass
157 {WpOLEBase<Ts>::operator=(rhs); return *this;};
158
159
160 bool Append(const WrapT& aWrapT)
161 {
162 return SUCCEEDED(pInterface->Append(OLEVariant(aWrapT)));
163 };
164
165 bool Delete(const OUString& sName)
166 {
167 return SUCCEEDED(pInterface->Delete(OLEVariant(sName)));
168 };
169
170
171 };
172}
173
174/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
WpOLEAppendCollection(const WpOLEAppendCollection &rhs)
Definition: Aolewrap.hxx:155
bool Append(const WrapT &aWrapT)
Definition: Aolewrap.hxx:160
bool Delete(const OUString &sName)
Definition: Aolewrap.hxx:165
WpOLEAppendCollection & operator=(const WpOLEAppendCollection &rhs)
Definition: Aolewrap.hxx:156
WpOLEBase(T *pInt=nullptr)
Definition: Aolewrap.hxx:47
WpOLEBase(const WpOLEBase< T > &aWrapper)
Definition: Aolewrap.hxx:49
sal::systools::COMReference< T > pInterface
Definition: Aolewrap.hxx:44
WpOLEBase< T > & operator=(const WpOLEBase< T > &rhs)
Definition: Aolewrap.hxx:55
WrapT GetItem(const OUString &sStr) const
Definition: Aolewrap.hxx:117
sal_Int32 GetItemCount() const
Definition: Aolewrap.hxx:96
void fillElementNames(::std::vector< OUString > &_rVector)
Definition: Aolewrap.hxx:129
WpOLECollection & operator=(const WpOLECollection &rhs)
Definition: Aolewrap.hxx:90
WrapT GetItem(sal_Int32 index) const
Definition: Aolewrap.hxx:102
WpOLECollection(const WpOLECollection &rhs)
Definition: Aolewrap.hxx:89
WrapT GetItem(const OLEVariant &index) const
Definition: Aolewrap.hxx:110
int nCount
OUString sName
void * p
int i
index