File: | home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx |
Warning: | line 292, column 16 Use of memory after it is freed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 | ||||
20 | #include <QueryTableView.hxx> | |||
21 | #include <TableFieldDescription.hxx> | |||
22 | #include <tools/diagnose_ex.h> | |||
23 | #include <osl/diagnose.h> | |||
24 | #include <helpids.h> | |||
25 | #include "QTableWindow.hxx" | |||
26 | #include "QTableConnection.hxx" | |||
27 | #include "QTableConnectionData.hxx" | |||
28 | #include <QueryDesignView.hxx> | |||
29 | #include "QueryAddTabConnUndoAction.hxx" | |||
30 | #include "QueryTabWinShowUndoAct.hxx" | |||
31 | #include <browserids.hxx> | |||
32 | #include <com/sun/star/sdbc/XConnection.hpp> | |||
33 | #include <com/sun/star/sdbcx/XColumnsSupplier.hpp> | |||
34 | #include <com/sun/star/accessibility/AccessibleEventId.hpp> | |||
35 | #include <JAccess.hxx> | |||
36 | #include <com/sun/star/sdbcx/KeyType.hpp> | |||
37 | #include <com/sun/star/container/XIndexAccess.hpp> | |||
38 | #include <com/sun/star/beans/XPropertySet.hpp> | |||
39 | #include <connectivity/dbtools.hxx> | |||
40 | #include <comphelper/sequence.hxx> | |||
41 | #include "querydlg.hxx" | |||
42 | #include <core_resource.hxx> | |||
43 | #include <strings.hrc> | |||
44 | #include <strings.hxx> | |||
45 | ||||
46 | using namespace dbaui; | |||
47 | using namespace ::com::sun::star::uno; | |||
48 | using namespace ::com::sun::star::sdbc; | |||
49 | using namespace ::com::sun::star::sdbcx; | |||
50 | using namespace ::com::sun::star::beans; | |||
51 | using namespace ::com::sun::star::container; | |||
52 | using namespace ::com::sun::star::accessibility; | |||
53 | ||||
54 | namespace | |||
55 | { | |||
56 | /** appends a new TabAdd Undo action at controller | |||
57 | @param _pView the view which we use | |||
58 | @param _pUndoAction the undo action which should be added | |||
59 | @param _pConnection the connection for which the undo action should be appended | |||
60 | @param _bOwner is the undo action the owner | |||
61 | */ | |||
62 | void addUndoAction( OQueryTableView const * _pView, | |||
63 | std::unique_ptr<OQueryTabConnUndoAction> _pUndoAction, | |||
64 | OQueryTableConnection* _pConnection, | |||
65 | bool _bOwner = false) | |||
66 | { | |||
67 | _pUndoAction->SetOwnership(_bOwner); | |||
68 | _pUndoAction->SetConnection(_pConnection); | |||
69 | _pView->getDesignView()->getController().addUndoActionAndInvalidate(std::move(_pUndoAction)); | |||
70 | } | |||
71 | /** openJoinDialog opens the join dialog with this connection data | |||
72 | @param _pView the view which we use | |||
73 | @param _pConnectionData the connection data | |||
74 | ||||
75 | @return true when OK was pressed otherwise false | |||
76 | */ | |||
77 | bool openJoinDialog(OQueryTableView* _pView,const TTableConnectionData::value_type& _pConnectionData,bool _bSelectableTables) | |||
78 | { | |||
79 | OQueryTableConnectionData* pData = static_cast< OQueryTableConnectionData*>(_pConnectionData.get()); | |||
80 | ||||
81 | DlgQryJoin aDlg(_pView,_pConnectionData,&_pView->GetTabWinMap(),_pView->getDesignView()->getController().getConnection(),_bSelectableTables); | |||
82 | bool bOk = aDlg.run() == RET_OK; | |||
83 | if( bOk ) | |||
84 | { | |||
85 | pData->SetJoinType(aDlg.GetJoinType()); | |||
86 | _pView->getDesignView()->getController().setModified(true); | |||
87 | } | |||
88 | ||||
89 | return bOk; | |||
90 | } | |||
91 | /** connectionModified adds an undo action for the modified connection and forces a redraw | |||
92 | @param _pView the view which we use | |||
93 | @param _pConnection the connection which was modified | |||
94 | @param _bAddUndo true when an undo action should be appended | |||
95 | */ | |||
96 | void connectionModified(OQueryTableView* _pView, | |||
97 | OTableConnection* _pConnection, | |||
98 | bool _bAddUndo) | |||
99 | { | |||
100 | OSL_ENSURE(_pConnection,"Invalid connection!")do { if (true && (!(_pConnection))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "100" ": "), "%s", "Invalid connection!"); } } while (false ); | |||
101 | _pConnection->UpdateLineList(); | |||
102 | ||||
103 | // add an undo action | |||
104 | if ( _bAddUndo ) | |||
105 | addUndoAction( _pView, | |||
106 | std::make_unique<OQueryAddTabConnUndoAction>(_pView), | |||
107 | static_cast< OQueryTableConnection*>(_pConnection)); | |||
108 | // redraw | |||
109 | _pConnection->RecalcLines(); | |||
110 | // force an invalidation of the bounding rectangle | |||
111 | _pConnection->InvalidateConnection(); | |||
112 | ||||
113 | _pView->Invalidate(InvalidateFlags::NoChildren); | |||
114 | } | |||
115 | void addConnections(OQueryTableView* _pView, | |||
116 | const OQueryTableWindow& _rSource, | |||
117 | const OQueryTableWindow& _rDest, | |||
118 | const Reference<XNameAccess>& _rxSourceForeignKeyColumns) | |||
119 | { | |||
120 | if ( _rSource.GetData()->isQuery() || _rDest.GetData()->isQuery() ) | |||
121 | // nothing to do if one of both denotes a query | |||
122 | return; | |||
123 | ||||
124 | // we found a table in our view where we can insert some connections | |||
125 | // the key columns have a property called RelatedColumn | |||
126 | // build OQueryTableConnectionData | |||
127 | auto xNewConnData = std::make_shared<OQueryTableConnectionData>( _rSource.GetData(), _rDest.GetData() ); | |||
128 | ||||
129 | OUString sRelatedColumn; | |||
130 | ||||
131 | // iterate through all foreignkey columns to create the connections | |||
132 | const Sequence<OUString> aKeyCols = _rxSourceForeignKeyColumns->getElementNames(); | |||
133 | for(const OUString& rElement : aKeyCols) | |||
134 | { | |||
135 | Reference<XPropertySet> xColumn; | |||
136 | if ( !( _rxSourceForeignKeyColumns->getByName(rElement) >>= xColumn ) ) | |||
137 | { | |||
138 | OSL_FAIL( "addConnections: invalid foreign key column!" )do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "138" ": "), "%s", "addConnections: invalid foreign key column!" ); } } while (false); | |||
139 | continue; | |||
140 | } | |||
141 | ||||
142 | xColumn->getPropertyValue(PROPERTY_RELATEDCOLUMN"RelatedColumn") >>= sRelatedColumn; | |||
143 | ||||
144 | { | |||
145 | sal_Int32 nFindIndex = ::comphelper::findValue(_rSource.GetOriginalColumns()->getElementNames(),rElement); | |||
146 | if(nFindIndex != -1) | |||
147 | xNewConnData->SetFieldIndex(JTCS_FROM,nFindIndex+1); | |||
148 | else | |||
149 | OSL_FAIL("Column not found!")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "149" ": "), "%s", "Column not found!"); } } while (false ); | |||
150 | } | |||
151 | // get the position inside the table | |||
152 | Reference<XNameAccess> xRefColumns = _rDest.GetOriginalColumns(); | |||
153 | if(xRefColumns.is()) | |||
154 | { | |||
155 | sal_Int32 nFindIndex = ::comphelper::findValue(xRefColumns->getElementNames(),sRelatedColumn); | |||
156 | if(nFindIndex != -1) | |||
157 | xNewConnData->SetFieldIndex(JTCS_TO,nFindIndex+1); | |||
158 | else | |||
159 | OSL_FAIL("Column not found!")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "159" ": "), "%s", "Column not found!"); } } while (false ); | |||
160 | } | |||
161 | xNewConnData->AppendConnLine(rElement,sRelatedColumn); | |||
162 | ||||
163 | // now add the Conn itself | |||
164 | ScopedVclPtrInstance< OQueryTableConnection > aNewConn(_pView, xNewConnData); | |||
165 | // referring to the local variable is not important, as NotifyQueryTabConn creates a new copy | |||
166 | // to add me (if not existent) | |||
167 | _pView->NotifyTabConnection(*aNewConn, false); | |||
168 | // don't create an Undo-Action for the new connection : the connection is | |||
169 | // covered by the Undo-Action for the tabwin, as the "Undo the insert" will | |||
170 | // automatically remove all connections adjacent to the win. | |||
171 | // (Because of this automatism we would have an ownership ambiguity for | |||
172 | // the connection data if we would insert the conn-Undo-Action) | |||
173 | } | |||
174 | } | |||
175 | } | |||
176 | ||||
177 | OQueryTableView::OQueryTableView( vcl::Window* pParent,OQueryDesignView* pView) | |||
178 | : OJoinTableView( pParent,pView) | |||
179 | { | |||
180 | SetHelpId(HID_CTL_QRYDGNTAB"DBACCESS_HID_CTL_QRYDGNTAB"); | |||
181 | } | |||
182 | ||||
183 | sal_Int32 OQueryTableView::CountTableAlias(const OUString& rName, sal_Int32& rMax) | |||
184 | { | |||
185 | sal_Int32 nRet = 0; | |||
186 | ||||
187 | OTableWindowMap::const_iterator aIter = GetTabWinMap().find(rName); | |||
188 | while(aIter != GetTabWinMap().end()) | |||
189 | { | |||
190 | OUString aNewName = rName + "_" + OUString::number(++nRet); | |||
191 | aIter = GetTabWinMap().find(aNewName); | |||
192 | } | |||
193 | ||||
194 | rMax = nRet; | |||
195 | ||||
196 | return nRet; | |||
197 | } | |||
198 | ||||
199 | void OQueryTableView::ReSync() | |||
200 | { | |||
201 | TTableWindowData& rTabWinDataList = m_pView->getController().getTableWindowData(); | |||
202 | OSL_ENSURE((getTableConnections().empty()) && (GetTabWinMap().empty()),do { if (true && (!((getTableConnections().empty()) && (GetTabWinMap().empty())))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN ), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "203" ": "), "%s", "before calling OQueryTableView::ReSync() please call ClearAll !" ); } } while (false) | |||
203 | "before calling OQueryTableView::ReSync() please call ClearAll !")do { if (true && (!((getTableConnections().empty()) && (GetTabWinMap().empty())))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN ), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "203" ": "), "%s", "before calling OQueryTableView::ReSync() please call ClearAll !" ); } } while (false); | |||
204 | ||||
205 | // I need a collection of all window names that cannot be created so that I do not initialize connections for them. | |||
206 | std::vector<OUString> arrInvalidTables; | |||
207 | ||||
208 | TTableWindowData::const_reverse_iterator aIter = rTabWinDataList.rbegin(); | |||
209 | // Create the window and add it | |||
210 | ||||
211 | for(;aIter != rTabWinDataList.rend();++aIter) | |||
212 | { | |||
213 | OQueryTableWindowData* pData = static_cast<OQueryTableWindowData*>(aIter->get()); | |||
214 | VclPtr<OTableWindow> pTabWin = createWindow(*aIter); | |||
215 | ||||
216 | // I don't use ShowTabWin as this adds the window data to the list of documents. | |||
217 | // This would be bad as I am getting them from there. | |||
218 | // Instead, I do it step by step | |||
219 | if (!pTabWin->Init()) | |||
220 | { | |||
221 | // The initialisation has gone wrong, this TabWin is not available, so | |||
222 | // I must clean up the data and the document | |||
223 | pTabWin->clearListBox(); | |||
224 | pTabWin.disposeAndClear(); | |||
225 | arrInvalidTables.push_back(pData->GetAliasName()); | |||
226 | ||||
227 | rTabWinDataList.erase( std::remove(rTabWinDataList.begin(), rTabWinDataList.end(), *aIter), rTabWinDataList.end()); | |||
228 | continue; | |||
229 | } | |||
230 | ||||
231 | GetTabWinMap()[pData->GetAliasName()] = pTabWin; // add at the beginning as I am going backwards through the DataList | |||
232 | // Use the default if there is no position or size | |||
233 | if (!pData->HasPosition() && !pData->HasSize()) | |||
234 | SetDefaultTabWinPosSize(pTabWin); | |||
235 | ||||
236 | pTabWin->Show(); | |||
237 | } | |||
238 | ||||
239 | // Add the connections | |||
240 | TTableConnectionData& rTabConnDataList = m_pView->getController().getTableConnectionData(); | |||
241 | TTableConnectionData::const_reverse_iterator aConIter = rTabConnDataList.rbegin(); | |||
242 | ||||
243 | for(;aConIter != rTabConnDataList.rend();++aConIter) | |||
244 | { | |||
245 | OQueryTableConnectionData* pTabConnData = static_cast<OQueryTableConnectionData*>(aConIter->get()); | |||
246 | ||||
247 | // do both tables for the connection exist ? | |||
248 | OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetWinName(); | |||
249 | bool bInvalid = std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end(); | |||
250 | strTabExistenceTest = pTabConnData->getReferencedTable()->GetWinName(); | |||
251 | bInvalid = bInvalid && std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end(); | |||
252 | ||||
253 | if (bInvalid) | |||
254 | { | |||
255 | // no -> bad luck, no connection | |||
256 | rTabConnDataList.erase( std::remove(rTabConnDataList.begin(), rTabConnDataList.end(), *aConIter), rTabConnDataList.end()); | |||
257 | continue; | |||
258 | } | |||
259 | ||||
260 | // adds a new connection to join view and notifies our accessible and invalidates the controller | |||
261 | addConnection(VclPtr<OQueryTableConnection>::Create(this, *aConIter)); | |||
262 | } | |||
263 | } | |||
264 | ||||
265 | void OQueryTableView::ClearAll() | |||
266 | { | |||
267 | OJoinTableView::ClearAll(); | |||
268 | ||||
269 | SetUpdateMode(true); | |||
270 | m_pView->getController().setModified(true); | |||
271 | } | |||
272 | ||||
273 | VclPtr<OTableWindow> OQueryTableView::createWindow(const TTableWindowData::value_type& _pData) | |||
274 | { | |||
275 | return VclPtr<OQueryTableWindow>::Create(this,_pData); | |||
276 | } | |||
277 | ||||
278 | void OQueryTableView::NotifyTabConnection(const OQueryTableConnection& rNewConn, bool _bCreateUndoAction) | |||
279 | { | |||
280 | // let's first check if I have the connection already | |||
281 | OQueryTableConnection* pTabConn = nullptr; | |||
282 | const auto& rConnections = getTableConnections(); | |||
283 | auto aEnd = rConnections.end(); | |||
284 | auto aIter = std::find( rConnections.begin(), | |||
285 | aEnd, | |||
286 | VclPtr<OTableConnection>(const_cast<OTableConnection*>(static_cast<const OTableConnection*>(&rNewConn))) | |||
287 | ); | |||
288 | if(aIter == aEnd) | |||
289 | { | |||
290 | for (auto const& connection : rConnections) | |||
291 | { | |||
292 | if(*static_cast<OQueryTableConnection*>(connection.get()) == rNewConn) | |||
| ||||
293 | { | |||
294 | pTabConn = static_cast<OQueryTableConnection*>(connection.get()); | |||
295 | break; | |||
296 | } | |||
297 | } | |||
298 | } | |||
299 | else | |||
300 | pTabConn = static_cast<OQueryTableConnection*>((*aIter).get()); | |||
301 | ||||
302 | // no -> insert | |||
303 | if (pTabConn == nullptr) | |||
304 | { | |||
305 | // the new data ... | |||
306 | auto pNewData = std::static_pointer_cast<OQueryTableConnectionData>(rNewConn.GetData()->NewInstance()); | |||
307 | pNewData->CopyFrom(*rNewConn.GetData()); | |||
308 | VclPtrInstance<OQueryTableConnection> pNewConn(this, pNewData); | |||
309 | GetConnection(pNewConn); | |||
310 | ||||
311 | connectionModified(this,pNewConn,_bCreateUndoAction); | |||
312 | } | |||
313 | } | |||
314 | ||||
315 | std::shared_ptr<OTableWindowData> OQueryTableView::CreateImpl(const OUString& _rComposedName | |||
316 | ,const OUString& _sTableName | |||
317 | ,const OUString& _rWinName) | |||
318 | { | |||
319 | return std::make_shared<OQueryTableWindowData>( _rComposedName, _sTableName,_rWinName ); | |||
320 | } | |||
321 | ||||
322 | void OQueryTableView::AddTabWin(const OUString& _rTableName, const OUString& _rAliasName, bool bNewTable) | |||
323 | { | |||
324 | // this method has been inherited from the base class, linking back to the parent and which constructs | |||
325 | // an Alias and which passes on to my other AddTabWin | |||
326 | ||||
327 | // pity _rTableName is fully qualified, OQueryDesignView expects a string which only | |||
328 | // contains schema and tables but no catalog. | |||
329 | Reference< XConnection> xConnection = m_pView->getController().getConnection(); | |||
330 | if(!xConnection.is()) | |||
331 | return; | |||
332 | try | |||
333 | { | |||
334 | Reference< XDatabaseMetaData > xMetaData = xConnection->getMetaData(); | |||
335 | OUString sCatalog, sSchema, sTable; | |||
336 | ::dbtools::qualifiedNameComponents(xMetaData, | |||
337 | _rTableName, | |||
338 | sCatalog, | |||
339 | sSchema, | |||
340 | sTable, | |||
341 | ::dbtools::EComposeRule::InDataManipulation); | |||
342 | OUString sRealName(sSchema); | |||
343 | if (!sRealName.isEmpty()) | |||
344 | sRealName += "."; | |||
345 | sRealName += sTable; | |||
346 | ||||
347 | AddTabWin(_rTableName, sRealName, _rAliasName, bNewTable); | |||
348 | } | |||
349 | catch(SQLException&) | |||
350 | { | |||
351 | OSL_FAIL("qualifiedNameComponents")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "351" ": "), "%s", "qualifiedNameComponents"); } } while (false); | |||
352 | } | |||
353 | } | |||
354 | ||||
355 | // find the table which has a foreign key with this referencedTable name | |||
356 | static Reference<XPropertySet> getKeyReferencedTo(const Reference<XIndexAccess>& _rxKeys,const OUString& _rReferencedTable) | |||
357 | { | |||
358 | if(!_rxKeys.is()) | |||
359 | return Reference<XPropertySet>(); | |||
360 | ||||
361 | // search the one and only primary key | |||
362 | const sal_Int32 nCount = _rxKeys->getCount(); | |||
363 | for(sal_Int32 i=0;i<nCount ;++i) | |||
364 | { | |||
365 | Reference<XPropertySet> xKey(_rxKeys->getByIndex(i),UNO_QUERY); | |||
366 | if(xKey.is()) | |||
367 | { | |||
368 | sal_Int32 nKeyType = 0; | |||
369 | xKey->getPropertyValue(PROPERTY_TYPE"Type") >>= nKeyType; | |||
370 | if(KeyType::FOREIGN == nKeyType) | |||
371 | { | |||
372 | OUString sReferencedTable; | |||
373 | xKey->getPropertyValue(PROPERTY_REFERENCEDTABLE"ReferencedTable") >>= sReferencedTable; | |||
374 | // TODO check case | |||
375 | if(sReferencedTable == _rReferencedTable) | |||
376 | return xKey; | |||
377 | } | |||
378 | } | |||
379 | } | |||
380 | return Reference<XPropertySet>(); | |||
381 | } | |||
382 | ||||
383 | void OQueryTableView::AddTabWin(const OUString& _rComposedName, const OUString& _rTableName, const OUString& strAlias, bool bNewTable) | |||
384 | { | |||
385 | OSL_ENSURE(!_rTableName.isEmpty() || !strAlias.isEmpty(), "OQueryTableView::AddTabWin : no tables or aliases !")do { if (true && (!(!_rTableName.isEmpty() || !strAlias .isEmpty()))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN ), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "385" ": "), "%s", "OQueryTableView::AddTabWin : no tables or aliases !" ); } } while (false); | |||
386 | // If the table is not set, then it is a dummy window, but at least the alias must be set | |||
387 | ||||
388 | // build a new data structure | |||
389 | // first check if this already has its data | |||
390 | bool bAppend = bNewTable; | |||
391 | TTableWindowData::value_type pNewTabWinData; | |||
392 | TTableWindowData& rWindowData = getDesignView()->getController().getTableWindowData(); | |||
393 | bool bFoundElem = false; | |||
394 | for (auto const& elem : rWindowData) | |||
395 | { | |||
396 | pNewTabWinData = elem; | |||
397 | if (pNewTabWinData && pNewTabWinData->GetWinName() == strAlias && pNewTabWinData->GetComposedName() == _rComposedName && pNewTabWinData->GetTableName() == _rTableName) | |||
398 | { | |||
399 | bFoundElem = true; | |||
400 | break; | |||
401 | } | |||
402 | } | |||
403 | if ( !bAppend ) | |||
404 | bAppend = !bFoundElem; | |||
405 | if ( bAppend ) | |||
406 | pNewTabWinData = createTableWindowData(_rComposedName, _rTableName, strAlias); | |||
407 | // I do not need to add TabWinData to the DocShell list, ShowTabWin does that. | |||
408 | ||||
409 | // Create a new window | |||
410 | VclPtr<OQueryTableWindow> pNewTabWin = static_cast<OQueryTableWindow*>(createWindow(pNewTabWinData).get()); | |||
411 | // No need to initialize, as that happens in ShowTabWin | |||
412 | ||||
413 | // New UndoAction | |||
414 | std::unique_ptr<OQueryTabWinShowUndoAct> pUndoAction(new OQueryTabWinShowUndoAct(this)); | |||
415 | pUndoAction->SetTabWin(pNewTabWin); // Window | |||
416 | bool bSuccess = ShowTabWin(pNewTabWin, pUndoAction.get(), bAppend); | |||
417 | if(!bSuccess) | |||
418 | { | |||
419 | // reset table window | |||
420 | pUndoAction->SetTabWin(nullptr); | |||
421 | pUndoAction->SetOwnership(false); | |||
422 | return; | |||
423 | } | |||
424 | ||||
425 | // Show the relations between the individual tables | |||
426 | OTableWindowMap& rTabWins = GetTabWinMap(); | |||
427 | if(bNewTable && !rTabWins.empty() && !_rTableName.isEmpty()) | |||
428 | { | |||
429 | modified(); | |||
430 | if ( m_pAccessible ) | |||
431 | m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD, | |||
432 | Any(), | |||
433 | makeAny(pNewTabWin->GetAccessible()) | |||
434 | ); | |||
435 | ||||
436 | do { | |||
437 | ||||
438 | if ( pNewTabWin->GetData()->isQuery() ) | |||
439 | break; | |||
440 | ||||
441 | try | |||
442 | { | |||
443 | // find relations between the table and the tables already inserted | |||
444 | Reference< XIndexAccess> xKeyIndex = pNewTabWin->GetData()->getKeys(); | |||
445 | if ( !xKeyIndex.is() ) | |||
446 | break; | |||
447 | ||||
448 | Reference<XNameAccess> xFKeyColumns; | |||
449 | OUString aReferencedTable; | |||
450 | Reference<XColumnsSupplier> xColumnsSupplier; | |||
451 | ||||
452 | const sal_Int32 nKeyCount = xKeyIndex->getCount(); | |||
453 | for ( sal_Int32 i=0; i<nKeyCount ; ++i ) | |||
454 | { | |||
455 | Reference< XPropertySet > xProp( xKeyIndex->getByIndex(i), UNO_QUERY_THROW ); | |||
456 | xColumnsSupplier.set( xProp, UNO_QUERY_THROW ); | |||
457 | xFKeyColumns.set( xColumnsSupplier->getColumns(), UNO_SET_THROW ); | |||
458 | ||||
459 | sal_Int32 nKeyType = 0; | |||
460 | xProp->getPropertyValue(PROPERTY_TYPE"Type") >>= nKeyType; | |||
461 | ||||
462 | switch ( nKeyType ) | |||
463 | { | |||
464 | case KeyType::FOREIGN: | |||
465 | { // our new table has a foreign key | |||
466 | // so look if the referenced table is already in our list | |||
467 | xProp->getPropertyValue(PROPERTY_REFERENCEDTABLE"ReferencedTable") >>= aReferencedTable; | |||
468 | OSL_ENSURE(!aReferencedTable.isEmpty(),"Foreign key without referencedTableName")do { if (true && (!(!aReferencedTable.isEmpty()))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "468" ": "), "%s", "Foreign key without referencedTableName" ); } } while (false); | |||
469 | ||||
470 | OTableWindowMap::const_iterator aIter = rTabWins.find(aReferencedTable); | |||
471 | OTableWindowMap::const_iterator aEnd = rTabWins.end(); | |||
472 | if(aIter == aEnd) | |||
473 | { | |||
474 | for(aIter = rTabWins.begin();aIter != aEnd;++aIter) | |||
475 | { | |||
476 | OQueryTableWindow* pTabWinTmp = static_cast<OQueryTableWindow*>(aIter->second.get()); | |||
477 | OSL_ENSURE( pTabWinTmp,"TableWindow is null!" )do { if (true && (!(pTabWinTmp))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "477" ": "), "%s", "TableWindow is null!"); } } while (false ); | |||
478 | if ( pTabWinTmp != pNewTabWin && pTabWinTmp->GetComposedName() == aReferencedTable ) | |||
479 | break; | |||
480 | } | |||
481 | } | |||
482 | if ( aIter != aEnd && pNewTabWin.get() != aIter->second.get() ) | |||
483 | addConnections( this, *pNewTabWin, *static_cast<OQueryTableWindow*>(aIter->second.get()), xFKeyColumns ); | |||
484 | } | |||
485 | break; | |||
486 | ||||
487 | case KeyType::PRIMARY: | |||
488 | { | |||
489 | // we have a primary key so look in our list if there exists a key which this is referred to | |||
490 | for (auto const& tabWin : rTabWins) | |||
491 | { | |||
492 | OQueryTableWindow* pTabWinTmp = static_cast<OQueryTableWindow*>(tabWin.second.get()); | |||
493 | if ( pTabWinTmp == pNewTabWin ) | |||
494 | continue; | |||
495 | ||||
496 | if ( pTabWinTmp->GetData()->isQuery() ) | |||
497 | continue; | |||
498 | ||||
499 | OSL_ENSURE(pTabWinTmp,"TableWindow is null!")do { if (true && (!(pTabWinTmp))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "499" ": "), "%s", "TableWindow is null!"); } } while (false ); | |||
500 | Reference< XPropertySet > xFKKey = getKeyReferencedTo( pTabWinTmp->GetData()->getKeys(), pNewTabWin->GetComposedName() ); | |||
501 | if ( !xFKKey.is() ) | |||
502 | continue; | |||
503 | ||||
504 | Reference<XColumnsSupplier> xFKColumnsSupplier( xFKKey, UNO_QUERY_THROW ); | |||
505 | Reference< XNameAccess > xTColumns( xFKColumnsSupplier->getColumns(), UNO_SET_THROW ); | |||
506 | addConnections( this, *pTabWinTmp, *pNewTabWin, xTColumns ); | |||
507 | } | |||
508 | } | |||
509 | break; | |||
510 | } | |||
511 | } | |||
512 | } | |||
513 | catch( const Exception& ) | |||
514 | { | |||
515 | DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "515" ": ", "dbaccess" );; | |||
516 | } | |||
517 | ||||
518 | } while ( false ); | |||
519 | } | |||
520 | ||||
521 | // My parent needs to be informed about the delete | |||
522 | m_pView->getController().addUndoActionAndInvalidate( std::move(pUndoAction) ); | |||
523 | } | |||
524 | ||||
525 | void OQueryTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest) | |||
526 | { | |||
527 | OQueryTableWindow* pSourceWin = static_cast< OQueryTableWindow*>(jxdSource.pListBox->GetTabWin()); | |||
528 | OQueryTableWindow* pDestWin = static_cast< OQueryTableWindow*>(jxdDest.pListBox->GetTabWin()); | |||
529 | ||||
530 | OUString aSourceFieldName, aDestFieldName; | |||
531 | weld::TreeView& rSourceTreeView = jxdSource.pListBox->get_widget(); | |||
532 | aSourceFieldName = rSourceTreeView.get_text(jxdSource.nEntry); | |||
533 | weld::TreeView& rDestTreeView = jxdDest.pListBox->get_widget(); | |||
534 | aDestFieldName = rDestTreeView.get_text(jxdDest.nEntry); | |||
535 | ||||
536 | OTableConnection* pConn = GetTabConn(pSourceWin,pDestWin,true); | |||
537 | if ( !pConn ) | |||
| ||||
538 | { | |||
539 | // new data object | |||
540 | auto xNewConnectionData = std::make_shared<OQueryTableConnectionData>(pSourceWin->GetData(), pDestWin->GetData()); | |||
541 | ||||
542 | sal_uInt32 nSourceFieldIndex, nDestFieldIndex; | |||
543 | ||||
544 | // Get name/position of both affected fields ... | |||
545 | // Source | |||
546 | nSourceFieldIndex = jxdSource.nEntry; | |||
547 | // Dest | |||
548 | nDestFieldIndex = jxdDest.nEntry; | |||
549 | ||||
550 | // ... and set them | |||
551 | xNewConnectionData->SetFieldIndex(JTCS_FROM, nSourceFieldIndex); | |||
552 | xNewConnectionData->SetFieldIndex(JTCS_TO, nDestFieldIndex); | |||
553 | ||||
554 | xNewConnectionData->AppendConnLine( aSourceFieldName,aDestFieldName ); | |||
555 | ||||
556 | ScopedVclPtrInstance< OQueryTableConnection > aNewConnection(this, xNewConnectionData); | |||
557 | NotifyTabConnection(*aNewConnection); | |||
558 | // As usual with NotifyTabConnection, using a local variable is fine because a copy is made | |||
559 | } | |||
560 | else | |||
561 | { | |||
562 | // the connection could point on the other side | |||
563 | if(pConn->GetSourceWin() == pDestWin) | |||
564 | { | |||
565 | OUString aTmp(aSourceFieldName); | |||
566 | aSourceFieldName = aDestFieldName; | |||
567 | aDestFieldName = aTmp; | |||
568 | } | |||
569 | ||||
570 | pConn->GetData()->AppendConnLine( aSourceFieldName,aDestFieldName ); | |||
571 | ||||
572 | connectionModified(this,pConn,false); | |||
573 | } | |||
574 | } | |||
575 | ||||
576 | void OQueryTableView::ConnDoubleClicked(VclPtr<OTableConnection>& rConnection) | |||
577 | { | |||
578 | if (openJoinDialog(this, rConnection->GetData(), false)) | |||
579 | { | |||
580 | connectionModified(this, rConnection, false); | |||
581 | SelectConn(rConnection); | |||
582 | } | |||
583 | } | |||
584 | ||||
585 | void OQueryTableView::createNewConnection() | |||
586 | { | |||
587 | TTableConnectionData::value_type pData = std::make_shared<OQueryTableConnectionData>(); | |||
588 | if( !openJoinDialog(this,pData,true) ) | |||
589 | return; | |||
590 | ||||
591 | OTableWindowMap& rMap = GetTabWinMap(); | |||
592 | OQueryTableWindow* pSourceWin = static_cast< OQueryTableWindow*>(rMap[pData->getReferencingTable()->GetWinName()].get()); | |||
593 | OQueryTableWindow* pDestWin = static_cast< OQueryTableWindow*>(rMap[pData->getReferencedTable()->GetWinName()].get()); | |||
594 | // first we have to look if the this connection already exists | |||
595 | OTableConnection* pConn = GetTabConn(pSourceWin,pDestWin,true); | |||
596 | bool bNew = true; | |||
597 | if ( pConn ) | |||
598 | { | |||
599 | pConn->GetData()->CopyFrom( *pData ); | |||
600 | bNew = false; | |||
601 | } | |||
602 | else | |||
603 | { | |||
604 | // create a new connection and append it | |||
605 | VclPtrInstance<OQueryTableConnection> pQConn(this, pData); | |||
606 | GetConnection(pQConn); | |||
607 | pConn = pQConn; | |||
608 | } | |||
609 | connectionModified(this,pConn,bNew); | |||
610 | if ( !bNew && pConn == GetSelectedConn() ) // our connection was selected before so we have to reselect it | |||
611 | SelectConn( pConn ); | |||
612 | } | |||
613 | ||||
614 | bool OQueryTableView::RemoveConnection(VclPtr<OTableConnection>& rConnection, bool /*_bDelete*/) | |||
615 | { | |||
616 | VclPtr<OQueryTableConnection> xConnection(static_cast<OQueryTableConnection*>(rConnection.get())); | |||
617 | ||||
618 | // we don't want that our connection will be deleted, we put it in the undo manager | |||
619 | bool bRet = OJoinTableView::RemoveConnection(rConnection, false); | |||
620 | ||||
621 | // add undo action | |||
622 | addUndoAction(this, | |||
623 | std::make_unique<OQueryDelTabConnUndoAction>(this), | |||
624 | xConnection.get(), | |||
625 | true); | |||
626 | ||||
627 | return bRet; | |||
628 | } | |||
629 | ||||
630 | OQueryTableWindow* OQueryTableView::FindTable(const OUString& rAliasName) | |||
631 | { | |||
632 | OSL_ENSURE(!rAliasName.isEmpty(), "OQueryTableView::FindTable : the AliasName should not be empty !")do { if (true && (!(!rAliasName.isEmpty()))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "632" ": "), "%s", "OQueryTableView::FindTable : the AliasName should not be empty !" ); } } while (false); | |||
633 | // (it is harmless but does not make sense and indicates that there is probably an error in the caller) | |||
634 | OTableWindowMap::const_iterator aIter = GetTabWinMap().find(rAliasName); | |||
635 | if(aIter != GetTabWinMap().end()) | |||
636 | return static_cast<OQueryTableWindow*>(aIter->second.get()); | |||
637 | return nullptr; | |||
638 | } | |||
639 | ||||
640 | bool OQueryTableView::FindTableFromField(const OUString& rFieldName, OTableFieldDescRef const & rInfo, sal_uInt16& rCnt) | |||
641 | { | |||
642 | rCnt = 0; | |||
643 | for (auto const& tabWin : GetTabWinMap()) | |||
644 | { | |||
645 | if(static_cast<OQueryTableWindow*>(tabWin.second.get())->ExistsField(rFieldName, rInfo)) | |||
646 | ++rCnt; | |||
647 | } | |||
648 | // TODO JNA : what should we rCnt > 1? | |||
649 | ||||
650 | return rCnt == 1; | |||
651 | } | |||
652 | ||||
653 | bool OQueryTableView::ContainsTabWin(const OTableWindow& rTabWin) | |||
654 | { | |||
655 | ||||
656 | for (auto const& tabWin : GetTabWinMap()) | |||
657 | { | |||
658 | if ( tabWin.second == &rTabWin ) | |||
659 | { | |||
660 | return true; | |||
661 | } | |||
662 | } | |||
663 | ||||
664 | return false; | |||
665 | } | |||
666 | ||||
667 | void OQueryTableView::RemoveTabWin(OTableWindow* pTabWin) | |||
668 | { | |||
669 | OSL_ENSURE(pTabWin != nullptr, "OQueryTableView::RemoveTabWin : Window should not be NULL !")do { if (true && (!(pTabWin != nullptr))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "669" ": "), "%s", "OQueryTableView::RemoveTabWin : Window should not be NULL !" ); } } while (false); | |||
670 | ||||
671 | if(!(pTabWin && ContainsTabWin(*pTabWin))) // #i122589# check if registered before deleting | |||
672 | return; | |||
673 | ||||
674 | // I need my parent so it can be informed about the deletion | |||
675 | OQueryDesignView* pParent = static_cast<OQueryDesignView*>(getDesignView()); | |||
676 | ||||
677 | SfxUndoManager& rUndoMgr = m_pView->getController().GetUndoManager(); | |||
678 | rUndoMgr.EnterListAction(DBA_RES(STR_QUERY_UNDO_TABWINDELETE)::dbaccess::ResourceManager::loadString( reinterpret_cast< char const *>("STR_QUERY_UNDO_TABWINDELETE" "\004" u8"Delete Table Window" ) ) , OUString(), 0, ViewShellId(-1)); | |||
679 | ||||
680 | // add the Undo-Action | |||
681 | std::unique_ptr<OQueryTabWinDelUndoAct> pUndoAction(new OQueryTabWinDelUndoAct(this)); | |||
682 | pUndoAction->SetTabWin(static_cast< OQueryTableWindow*>(pTabWin)); | |||
683 | ||||
684 | // and hide the window | |||
685 | HideTabWin(static_cast< OQueryTableWindow*>(pTabWin), pUndoAction.get()); | |||
686 | ||||
687 | // Undo Actions and delete the fields in SelectionBrowseBox | |||
688 | pParent->TableDeleted( static_cast< OQueryTableWindowData*>(pTabWin->GetData().get())->GetAliasName() ); | |||
689 | ||||
690 | m_pView->getController().addUndoActionAndInvalidate( std::move(pUndoAction) ); | |||
691 | rUndoMgr.LeaveListAction(); | |||
692 | ||||
693 | modified(); | |||
694 | if ( m_pAccessible ) | |||
695 | m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD, | |||
696 | makeAny(pTabWin->GetAccessible()), | |||
697 | Any() | |||
698 | ); | |||
699 | } | |||
700 | ||||
701 | void OQueryTableView::EnsureVisible(const OTableWindow* pWin) | |||
702 | { | |||
703 | ||||
704 | Invalidate(InvalidateFlags::NoChildren); | |||
705 | OJoinTableView::EnsureVisible(pWin); | |||
706 | } | |||
707 | ||||
708 | void OQueryTableView::GetConnection(OQueryTableConnection* pConn) | |||
709 | { | |||
710 | // add to me and the document | |||
711 | ||||
712 | addConnection( pConn ); | |||
713 | } | |||
714 | ||||
715 | void OQueryTableView::DropConnection(VclPtr<OQueryTableConnection> const & rConn) | |||
716 | { | |||
717 | // Pay attention to the selection | |||
718 | // remove from me and the document | |||
719 | VclPtr<OTableConnection> xConn(rConn.get()); | |||
720 | RemoveConnection(xConn, false); | |||
721 | } | |||
722 | ||||
723 | void OQueryTableView::HideTabWin( OQueryTableWindow* pTabWin, OQueryTabWinUndoAct* pUndoAction ) | |||
724 | { | |||
725 | OTableWindowMap& rTabWins = GetTabWinMap(); | |||
726 | ||||
727 | // Window | |||
728 | // save the position in its data | |||
729 | getDesignView()->SaveTabWinUIConfig(pTabWin); | |||
730 | // (I need to go via the parent, as only the parent knows the position of the scrollbars) | |||
731 | // and then out of the TabWins list and hide | |||
732 | OTableWindowMap::const_iterator aIter = std::find_if(rTabWins.begin(), rTabWins.end(), | |||
733 | [&pTabWin](const OTableWindowMap::value_type& rEntry) { return rEntry.second == pTabWin; }); | |||
734 | if (aIter != rTabWins.end()) | |||
735 | rTabWins.erase( aIter ); | |||
736 | ||||
737 | pTabWin->Hide(); // do not destroy it, as it is still in the undo list!! | |||
738 | ||||
739 | // the TabWin data must also be passed out of my responsibility | |||
740 | TTableWindowData& rTabWinDataList = m_pView->getController().getTableWindowData(); | |||
741 | rTabWinDataList.erase( std::remove(rTabWinDataList.begin(), rTabWinDataList.end(), pTabWin->GetData()), rTabWinDataList.end()); | |||
742 | // The data should not be destroyed as TabWin itself - which is still alive - needs them | |||
743 | // Either it goes back into my responsibility, (via ShowTabWin), then I add the data back, | |||
744 | // or the Undo-Action, which currently has full responsibility for the window | |||
745 | // and its data, gets destroyed and destroys both the window and its data | |||
746 | ||||
747 | if (m_pLastFocusTabWin == pTabWin) | |||
748 | m_pLastFocusTabWin = nullptr; | |||
749 | ||||
750 | // collect connections belonging to the window and pass to UndoAction | |||
751 | sal_Int16 nCnt = 0; | |||
752 | const auto& rTabConList = getTableConnections(); | |||
753 | auto aIter2 = rTabConList.begin(); | |||
754 | for(;aIter2 != rTabConList.end();)// the end may change | |||
755 | { | |||
756 | VclPtr<OTableConnection> xTmpEntry = *aIter2; | |||
757 | OQueryTableConnection* pTmpEntry = static_cast<OQueryTableConnection*>(xTmpEntry.get()); | |||
758 | OSL_ENSURE(pTmpEntry,"OQueryTableConnection is null!")do { if (true && (!(pTmpEntry))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "758" ": "), "%s", "OQueryTableConnection is null!"); } } while (false); | |||
759 | if( pTmpEntry->GetAliasName(JTCS_FROM) == pTabWin->GetAliasName() || | |||
760 | pTmpEntry->GetAliasName(JTCS_TO) == pTabWin->GetAliasName() ) | |||
761 | { | |||
762 | // add to undo list | |||
763 | pUndoAction->InsertConnection(xTmpEntry); | |||
764 | ||||
765 | // call base class because we append an undo action | |||
766 | // but this time we are in an undo action list | |||
767 | OJoinTableView::RemoveConnection(xTmpEntry, false); | |||
768 | aIter2 = rTabConList.begin(); | |||
769 | ++nCnt; | |||
770 | } | |||
771 | else | |||
772 | ++aIter2; | |||
773 | } | |||
774 | ||||
775 | if (nCnt) | |||
776 | InvalidateConnections(); | |||
777 | ||||
778 | m_pView->getController().InvalidateFeature(ID_BROWSER_ADDTABLE( 10000 + 722 )); | |||
779 | ||||
780 | // inform the UndoAction that the window and connections belong to it | |||
781 | pUndoAction->SetOwnership(true); | |||
782 | ||||
783 | // by doing so, we have modified the document | |||
784 | m_pView->getController().setModified( true ); | |||
785 | m_pView->getController().InvalidateFeature(SID_BROWSER_CLEAR_QUERY( ((((((((10000 + 1499) + 1) + 499) + 1) + 100) + 1) + 149) + 1) + 44 )); | |||
786 | } | |||
787 | ||||
788 | bool OQueryTableView::ShowTabWin( OQueryTableWindow* pTabWin, OQueryTabWinUndoAct* pUndoAction, bool _bAppend ) | |||
789 | { | |||
790 | ||||
791 | bool bSuccess = false; | |||
792 | ||||
793 | if (pTabWin) | |||
794 | { | |||
795 | if (pTabWin->Init()) | |||
796 | { | |||
797 | TTableWindowData::value_type pData = pTabWin->GetData(); | |||
798 | OSL_ENSURE(pData != nullptr, "OQueryTableView::ShowTabWin : TabWin has no data !")do { if (true && (!(pData != nullptr))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "798" ": "), "%s", "OQueryTableView::ShowTabWin : TabWin has no data !" ); } } while (false); | |||
799 | // If there is a position and size defined, we use them | |||
800 | if (pData->HasPosition() && pData->HasSize()) | |||
801 | { | |||
802 | Size aSize(CalcZoom(pData->GetSize().Width()),CalcZoom(pData->GetSize().Height())); | |||
803 | pTabWin->SetPosSizePixel(pData->GetPosition(), aSize); | |||
804 | } | |||
805 | else | |||
806 | // else set a default position | |||
807 | SetDefaultTabWinPosSize(pTabWin); | |||
808 | ||||
809 | // Show the window and add to the list | |||
810 | OUString sName = static_cast< OQueryTableWindowData*>(pData.get())->GetAliasName(); | |||
811 | OSL_ENSURE(GetTabWinMap().find(sName) == GetTabWinMap().end(),"Alias name already in list!")do { if (true && (!(GetTabWinMap().find(sName) == GetTabWinMap ().end()))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN ), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "811" ": "), "%s", "Alias name already in list!"); } } while (false); | |||
812 | GetTabWinMap().emplace(sName,pTabWin); | |||
813 | ||||
814 | pTabWin->Show(); | |||
815 | ||||
816 | pTabWin->PaintImmediately(); | |||
817 | // We must call Update() in order to show the connections in the window correctly. This sounds strange, | |||
818 | // but the Listbox has an internal Member which is initialized when the Listbox is first shown (after the Listbox | |||
819 | // is filled in Init). This Member will eventually be needed for | |||
820 | // GetEntryPos, and then in turn by the Connection, when its starting point to the window must be determined. | |||
821 | ||||
822 | // the Connections | |||
823 | auto rTableCon = pUndoAction->GetTabConnList(); | |||
824 | for(const auto& conn : rTableCon) | |||
825 | addConnection(conn); // add all connections from the undo action | |||
826 | ||||
827 | rTableCon.clear(); | |||
828 | ||||
829 | // and add the window's data to the list (of the document) | |||
830 | if(_bAppend) | |||
831 | m_pView->getController().getTableWindowData().push_back(pTabWin->GetData()); | |||
832 | ||||
833 | m_pView->getController().InvalidateFeature(ID_BROWSER_ADDTABLE( 10000 + 722 )); | |||
834 | ||||
835 | // and inform the UndoAction that the window belongs to me | |||
836 | pUndoAction->SetOwnership(false); | |||
837 | ||||
838 | bSuccess = true; | |||
839 | } | |||
840 | else | |||
841 | { | |||
842 | // Initialisation failed | |||
843 | // (for example when the Connection to the database is not available at the moment) | |||
844 | pTabWin->clearListBox(); | |||
845 | pTabWin->disposeOnce(); | |||
846 | } | |||
847 | } | |||
848 | ||||
849 | // show that I have changed the document | |||
850 | if(!m_pView->getController().isReadOnly()) | |||
851 | m_pView->getController().setModified( true ); | |||
852 | ||||
853 | m_pView->getController().InvalidateFeature(SID_BROWSER_CLEAR_QUERY( ((((((((10000 + 1499) + 1) + 499) + 1) + 100) + 1) + 149) + 1) + 44 )); | |||
854 | ||||
855 | return bSuccess; | |||
856 | } | |||
857 | ||||
858 | void OQueryTableView::InsertField(const OTableFieldDescRef& rInfo) | |||
859 | { | |||
860 | OSL_ENSURE(getDesignView() != nullptr, "OQueryTableView::InsertField : has no Parent !")do { if (true && (!(getDesignView() != nullptr))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/querydesign/QueryTableView.cxx" ":" "860" ": "), "%s", "OQueryTableView::InsertField : has no Parent !" ); } } while (false); | |||
861 | static_cast<OQueryDesignView*>(getDesignView())->InsertField(rInfo); | |||
862 | } | |||
863 | ||||
864 | bool OQueryTableView::ExistsAVisitedConn(const OQueryTableWindow* pFrom) const | |||
865 | { | |||
866 | for(const auto& conn : getTableConnections()) | |||
867 | { | |||
868 | OQueryTableConnection* pTemp = static_cast<OQueryTableConnection*>(conn.get()); | |||
869 | if (pTemp->IsVisited() && | |||
870 | (pFrom == static_cast< OQueryTableWindow*>(pTemp->GetSourceWin()) || pFrom == static_cast< OQueryTableWindow*>(pTemp->GetDestWin()))) | |||
871 | return true; | |||
872 | } | |||
873 | ||||
874 | return false; | |||
875 | } | |||
876 | ||||
877 | void OQueryTableView::onNoColumns_throw() | |||
878 | { | |||
879 | OUString sError(DBA_RES(STR_STATEMENT_WITHOUT_RESULT_SET)::dbaccess::ResourceManager::loadString( reinterpret_cast< char const *>("STR_STATEMENT_WITHOUT_RESULT_SET" "\004" u8"The query does not create a result set, and thus cannot be part of another query." ) )); | |||
880 | ::dbtools::throwSQLException( sError, ::dbtools::StandardSQLState::GENERAL_ERROR, nullptr ); | |||
881 | } | |||
882 | ||||
883 | bool OQueryTableView::supressCrossNaturalJoin(const TTableConnectionData::value_type& _pData) const | |||
884 | { | |||
885 | OQueryTableConnectionData* pQueryData = static_cast<OQueryTableConnectionData*>(_pData.get()); | |||
886 | return pQueryData && (pQueryData->GetJoinType() == CROSS_JOIN); | |||
887 | } | |||
888 | ||||
889 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | |
20 | #ifndef INCLUDED_VCL_PTR_HXX |
21 | #define INCLUDED_VCL_PTR_HXX |
22 | |
23 | #include <sal/config.h> |
24 | |
25 | #include <rtl/ref.hxx> |
26 | |
27 | #include <utility> |
28 | #include <type_traits> |
29 | |
30 | #ifdef DBG_UTIL |
31 | #ifndef _WIN32 |
32 | #include <vcl/vclmain.hxx> |
33 | #endif |
34 | #endif |
35 | |
36 | class VclReferenceBase; |
37 | |
38 | namespace vcl::detail { |
39 | |
40 | template<typename> |
41 | constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; } |
42 | |
43 | template<typename T> constexpr bool isIncompleteOrDerivedFromVclReferenceBase( |
44 | int (*)[sizeof(T)]) |
45 | { return std::is_base_of<VclReferenceBase, T>::value; } |
46 | |
47 | } // namespace vcl::detail |
48 | |
49 | /** |
50 | * A thin wrapper around rtl::Reference to implement the acquire and dispose semantics we want for references to vcl::Window subclasses. |
51 | * |
52 | * For more details on the design please see vcl/README.lifecycle |
53 | * |
54 | * @param reference_type must be a subclass of vcl::Window |
55 | */ |
56 | template <class reference_type> |
57 | class VclPtr |
58 | { |
59 | static_assert( |
60 | vcl::detail::isIncompleteOrDerivedFromVclReferenceBase<reference_type>( |
61 | nullptr), |
62 | "template argument type must be derived from VclReferenceBase"); |
63 | |
64 | ::rtl::Reference<reference_type> m_rInnerRef; |
65 | |
66 | public: |
67 | /** Constructor... |
68 | */ |
69 | VclPtr() |
70 | : m_rInnerRef() |
71 | {} |
72 | |
73 | /** Constructor... |
74 | */ |
75 | VclPtr (reference_type * pBody) |
76 | : m_rInnerRef(pBody) |
77 | {} |
78 | |
79 | /** Constructor... that doesn't take a ref. |
80 | */ |
81 | VclPtr (reference_type * pBody, __sal_NoAcquire) |
82 | : m_rInnerRef(pBody, SAL_NO_ACQUIRE) |
83 | {} |
84 | |
85 | /** Up-casting conversion constructor: Copies interface reference. |
86 | |
87 | Does not work for up-casts to ambiguous bases. For the special case of |
88 | up-casting to Reference< XInterface >, see the corresponding conversion |
89 | operator. |
90 | |
91 | @param rRef another reference |
92 | */ |
93 | template< class derived_type > |
94 | VclPtr( |
95 | const VclPtr< derived_type > & rRef, |
96 | typename std::enable_if< |
97 | std::is_base_of<reference_type, derived_type>::value, int>::type |
98 | = 0 ) |
99 | : m_rInnerRef( static_cast<reference_type*>(rRef) ) |
100 | { |
101 | } |
102 | |
103 | #if defined(DBG_UTIL) && !defined(_WIN32) |
104 | virtual ~VclPtr() |
105 | { |
106 | assert(m_rInnerRef.get() == nullptr || vclmain::isAlive())(static_cast <bool> (m_rInnerRef.get() == nullptr || vclmain ::isAlive()) ? void (0) : __assert_fail ("m_rInnerRef.get() == nullptr || vclmain::isAlive()" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 106, __extension__ __PRETTY_FUNCTION__)); |
107 | // We can be one of the intermediate counts, but if we are the last |
108 | // VclPtr keeping this object alive, then something forgot to call dispose(). |
109 | assert((!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1)(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)) |
110 | && "someone forgot to call dispose()")(static_cast <bool> ((!m_rInnerRef.get() || m_rInnerRef ->isDisposed() || m_rInnerRef->getRefCount() > 1) && "someone forgot to call dispose()") ? void (0) : __assert_fail ("(!m_rInnerRef.get() || m_rInnerRef->isDisposed() || m_rInnerRef->getRefCount() > 1) && \"someone forgot to call dispose()\"" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 110, __extension__ __PRETTY_FUNCTION__)); |
111 | } |
112 | VclPtr(VclPtr const &) = default; |
113 | VclPtr(VclPtr &&) = default; |
114 | VclPtr & operator =(VclPtr const &) = default; |
115 | VclPtr & operator =(VclPtr &&) = default; |
116 | #endif |
117 | |
118 | /** |
119 | * A construction helper for VclPtr. Since VclPtr types are created |
120 | * with a reference-count of one - to help fit into the existing |
121 | * code-flow; this helps us to construct them easily. |
122 | * |
123 | * For more details on the design please see vcl/README.lifecycle |
124 | * |
125 | * @tparam reference_type must be a subclass of vcl::Window |
126 | */ |
127 | template<typename... Arg> [[nodiscard]] static VclPtr< reference_type > Create(Arg &&... arg) |
128 | { |
129 | return VclPtr< reference_type >( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ); |
130 | } |
131 | |
132 | /** Probably most common used: handle->someBodyOp(). |
133 | */ |
134 | reference_type * operator->() const |
135 | { |
136 | return m_rInnerRef.get(); |
137 | } |
138 | |
139 | /** Get the body. Can be used instead of operator->(). |
140 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() |
141 | are the same. |
142 | */ |
143 | reference_type * get() const |
144 | { |
145 | return m_rInnerRef.get(); |
146 | } |
147 | |
148 | void set(reference_type *pBody) |
149 | { |
150 | m_rInnerRef.set(pBody); |
151 | } |
152 | |
153 | void reset(reference_type *pBody) |
154 | { |
155 | m_rInnerRef.set(pBody); |
156 | } |
157 | |
158 | /** Up-casting copy assignment operator. |
159 | |
160 | Does not work for up-casts to ambiguous bases. |
161 | |
162 | @param rRef another reference |
163 | */ |
164 | template<typename derived_type> |
165 | typename std::enable_if< |
166 | std::is_base_of<reference_type, derived_type>::value, |
167 | VclPtr &>::type |
168 | operator =(VclPtr<derived_type> const & rRef) |
169 | { |
170 | m_rInnerRef.set(rRef.get()); |
171 | return *this; |
172 | } |
173 | |
174 | VclPtr & operator =(reference_type * pBody) |
175 | { |
176 | m_rInnerRef.set(pBody); |
177 | return *this; |
178 | } |
179 | |
180 | operator reference_type * () const |
181 | { |
182 | return m_rInnerRef.get(); |
183 | } |
184 | |
185 | explicit operator bool () const |
186 | { |
187 | return m_rInnerRef.get() != nullptr; |
188 | } |
189 | |
190 | void clear() |
191 | { |
192 | m_rInnerRef.clear(); |
193 | } |
194 | |
195 | void reset() |
196 | { |
197 | m_rInnerRef.clear(); |
198 | } |
199 | |
200 | void disposeAndClear() |
201 | { |
202 | // hold it alive for the lifetime of this method |
203 | ::rtl::Reference<reference_type> aTmp(m_rInnerRef); |
204 | m_rInnerRef.clear(); // we should use some 'swap' method ideally ;-) |
205 | if (aTmp.get()) { |
206 | aTmp->disposeOnce(); |
207 | } |
208 | } |
209 | |
210 | /** Needed to place VclPtr's into STL collection. |
211 | */ |
212 | bool operator< (const VclPtr<reference_type> & handle) const |
213 | { |
214 | return (m_rInnerRef < handle.m_rInnerRef); |
215 | } |
216 | }; // class VclPtr |
217 | |
218 | template<typename T1, typename T2> |
219 | inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
220 | return p1.get() == p2.get(); |
221 | } |
222 | |
223 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2) |
224 | { |
225 | return p1.get() == p2; |
226 | } |
227 | |
228 | template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) { |
229 | return p1.get() == p2; |
230 | } |
231 | |
232 | template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2) |
233 | { |
234 | return p1 == p2.get(); |
235 | } |
236 | |
237 | template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) { |
238 | return p1 == p2.get(); |
239 | } |
240 | |
241 | template<typename T1, typename T2> |
242 | inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) { |
243 | return !(p1 == p2); |
244 | } |
245 | |
246 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2) |
247 | { |
248 | return !(p1 == p2); |
249 | } |
250 | |
251 | template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) { |
252 | return !(p1 == p2); |
253 | } |
254 | |
255 | template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2) |
256 | { |
257 | return !(p1 == p2); |
258 | } |
259 | |
260 | template<typename T> inline bool operator !=(T * p1, VclPtr<T> const & p2) { |
261 | return !(p1 == p2); |
262 | } |
263 | |
264 | /** |
265 | * A construction helper for a temporary VclPtr. Since VclPtr types |
266 | * are created with a reference-count of one - to help fit into |
267 | * the existing code-flow; this helps us to construct them easily. |
268 | * see also VclPtr::Create and ScopedVclPtr |
269 | * |
270 | * For more details on the design please see vcl/README.lifecycle |
271 | * |
272 | * @param reference_type must be a subclass of vcl::Window |
273 | */ |
274 | template <class reference_type> |
275 | class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type> |
276 | { |
277 | public: |
278 | template<typename... Arg> VclPtrInstance(Arg &&... arg) |
279 | : VclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
280 | { |
281 | } |
282 | |
283 | /** |
284 | * Override and disallow this, to prevent people accidentally calling it and actually |
285 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
286 | */ |
287 | template<typename... Arg> static VclPtrInstance< reference_type > Create(Arg &&... ) = delete; |
288 | }; |
289 | |
290 | template <class reference_type> |
291 | class ScopedVclPtr : public VclPtr<reference_type> |
292 | { |
293 | public: |
294 | /** Constructor... |
295 | */ |
296 | ScopedVclPtr() |
297 | : VclPtr<reference_type>() |
298 | {} |
299 | |
300 | /** Constructor |
301 | */ |
302 | ScopedVclPtr (reference_type * pBody) |
303 | : VclPtr<reference_type>(pBody) |
304 | {} |
305 | |
306 | /** Copy constructor... |
307 | */ |
308 | ScopedVclPtr (const VclPtr<reference_type> & handle) |
309 | : VclPtr<reference_type>(handle) |
310 | {} |
311 | |
312 | /** |
313 | Assignment that releases the last reference. |
314 | */ |
315 | void disposeAndReset(reference_type *pBody) |
316 | { |
317 | if (pBody != this->get()) { |
318 | VclPtr<reference_type>::disposeAndClear(); |
319 | VclPtr<reference_type>::set(pBody); |
320 | } |
321 | } |
322 | |
323 | /** |
324 | Assignment that releases the last reference. |
325 | */ |
326 | ScopedVclPtr<reference_type>& operator = (reference_type * pBody) |
327 | { |
328 | disposeAndReset(pBody); |
329 | return *this; |
330 | } |
331 | |
332 | /** Up-casting conversion constructor: Copies interface reference. |
333 | |
334 | Does not work for up-casts to ambiguous bases. For the special case of |
335 | up-casting to Reference< XInterface >, see the corresponding conversion |
336 | operator. |
337 | |
338 | @param rRef another reference |
339 | */ |
340 | template< class derived_type > |
341 | ScopedVclPtr( |
342 | const VclPtr< derived_type > & rRef, |
343 | typename std::enable_if< |
344 | std::is_base_of<reference_type, derived_type>::value, int>::type |
345 | = 0 ) |
346 | : VclPtr<reference_type>( rRef ) |
347 | { |
348 | } |
349 | |
350 | /** Up-casting assignment operator. |
351 | |
352 | Does not work for up-casts to ambiguous bases. |
353 | |
354 | @param rRef another VclPtr |
355 | */ |
356 | template<typename derived_type> |
357 | typename std::enable_if< |
358 | std::is_base_of<reference_type, derived_type>::value, |
359 | ScopedVclPtr &>::type |
360 | operator =(VclPtr<derived_type> const & rRef) |
361 | { |
362 | disposeAndReset(rRef.get()); |
363 | return *this; |
364 | } |
365 | |
366 | /** |
367 | * Override and disallow this, to prevent people accidentally calling it and actually |
368 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
369 | */ |
370 | template<typename... Arg> static ScopedVclPtr< reference_type > Create(Arg &&... ) = delete; |
371 | |
372 | ~ScopedVclPtr() |
373 | { |
374 | VclPtr<reference_type>::disposeAndClear(); |
375 | assert(VclPtr<reference_type>::get() == nullptr)(static_cast <bool> (VclPtr<reference_type>::get( ) == nullptr) ? void (0) : __assert_fail ("VclPtr<reference_type>::get() == nullptr" , "/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx" , 375, __extension__ __PRETTY_FUNCTION__)); // make sure there are no lingering references |
376 | } |
377 | |
378 | private: |
379 | // Most likely we don't want this default copy-constructor. |
380 | ScopedVclPtr (const ScopedVclPtr<reference_type> &) = delete; |
381 | // And certainly we don't want a default assignment operator. |
382 | ScopedVclPtr<reference_type>& operator = (const ScopedVclPtr<reference_type> &) = delete; |
383 | // And disallow reset as that doesn't call disposeAndClear on the original reference |
384 | void reset() = delete; |
385 | void reset(reference_type *pBody) = delete; |
386 | |
387 | protected: |
388 | ScopedVclPtr (reference_type * pBody, __sal_NoAcquire) |
389 | : VclPtr<reference_type>(pBody, SAL_NO_ACQUIRE) |
390 | {} |
391 | }; |
392 | |
393 | /** |
394 | * A construction helper for ScopedVclPtr. Since VclPtr types are created |
395 | * with a reference-count of one - to help fit into the existing |
396 | * code-flow; this helps us to construct them easily. |
397 | * |
398 | * For more details on the design please see vcl/README.lifecycle |
399 | * |
400 | * @param reference_type must be a subclass of vcl::Window |
401 | */ |
402 | #if defined _MSC_VER |
403 | #pragma warning(push) |
404 | #pragma warning(disable: 4521) // " multiple copy constructors specified" |
405 | #endif |
406 | template <class reference_type> |
407 | class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type> |
408 | { |
409 | public: |
410 | template<typename... Arg> ScopedVclPtrInstance(Arg &&... arg) |
411 | : ScopedVclPtr<reference_type>( new reference_type(std::forward<Arg>(arg)...), SAL_NO_ACQUIRE ) |
412 | { |
413 | } |
414 | |
415 | /** |
416 | * Override and disallow this, to prevent people accidentally calling it and actually |
417 | * getting VclPtr::Create and getting a naked VclPtr<> instance |
418 | */ |
419 | template<typename... Arg> static ScopedVclPtrInstance< reference_type > Create(Arg &&...) = delete; |
420 | |
421 | private: |
422 | // Prevent the above perfect forwarding ctor from hijacking (accidental) |
423 | // attempts at ScopedVclPtrInstance copy construction (where the hijacking |
424 | // would typically lead to somewhat obscure error messages); both non-const |
425 | // and const variants are needed here, as the ScopedVclPtr base class has a |
426 | // const--variant copy ctor, so the implicitly declared copy ctor for |
427 | // ScopedVclPtrInstance would also be the const variant, so non-const copy |
428 | // construction attempts would be hijacked by the perfect forwarding ctor; |
429 | // but if we only declared a non-const variant here, the const variant would |
430 | // no longer be implicitly declared (as there would already be an explicitly |
431 | // declared copy ctor), so const copy construction attempts would then be |
432 | // hijacked by the perfect forwarding ctor: |
433 | ScopedVclPtrInstance(ScopedVclPtrInstance &) = delete; |
434 | ScopedVclPtrInstance(ScopedVclPtrInstance const &) = delete; |
435 | }; |
436 | #if defined _MSC_VER |
437 | #pragma warning(pop) |
438 | #endif |
439 | |
440 | #endif // INCLUDED_VCL_PTR_HXX |
441 | |
442 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | |||||||||
20 | #ifndef INCLUDED_RTL_REF_HXX | ||||||||
21 | #define INCLUDED_RTL_REF_HXX | ||||||||
22 | |||||||||
23 | #include "sal/config.h" | ||||||||
24 | |||||||||
25 | #include <cassert> | ||||||||
26 | #include <cstddef> | ||||||||
27 | #include <functional> | ||||||||
28 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
29 | #include <type_traits> | ||||||||
30 | #endif | ||||||||
31 | |||||||||
32 | #include "sal/types.h" | ||||||||
33 | |||||||||
34 | namespace rtl | ||||||||
35 | { | ||||||||
36 | |||||||||
37 | /** Template reference class for reference type. | ||||||||
38 | */ | ||||||||
39 | template <class reference_type> | ||||||||
40 | class Reference | ||||||||
41 | { | ||||||||
42 | /** The <b>reference_type</b> body pointer. | ||||||||
43 | */ | ||||||||
44 | reference_type * m_pBody; | ||||||||
45 | |||||||||
46 | |||||||||
47 | public: | ||||||||
48 | /** Constructor... | ||||||||
49 | */ | ||||||||
50 | Reference() | ||||||||
51 | : m_pBody (NULL__null) | ||||||||
52 | {} | ||||||||
53 | |||||||||
54 | |||||||||
55 | /** Constructor... | ||||||||
56 | */ | ||||||||
57 | Reference (reference_type * pBody, __sal_NoAcquire) | ||||||||
58 | : m_pBody (pBody) | ||||||||
59 | { | ||||||||
60 | } | ||||||||
61 | |||||||||
62 | /** Constructor... | ||||||||
63 | */ | ||||||||
64 | Reference (reference_type * pBody) | ||||||||
65 | : m_pBody (pBody) | ||||||||
66 | { | ||||||||
67 | if (m_pBody) | ||||||||
68 | m_pBody->acquire(); | ||||||||
69 | } | ||||||||
70 | |||||||||
71 | /** Copy constructor... | ||||||||
72 | */ | ||||||||
73 | Reference (const Reference<reference_type> & handle) | ||||||||
74 | : m_pBody (handle.m_pBody) | ||||||||
75 | { | ||||||||
76 | if (m_pBody) | ||||||||
77 | m_pBody->acquire(); | ||||||||
78 | } | ||||||||
79 | |||||||||
80 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
81 | /** Move constructor... | ||||||||
82 | */ | ||||||||
83 | Reference (Reference<reference_type> && handle) noexcept | ||||||||
84 | : m_pBody (handle.m_pBody) | ||||||||
85 | { | ||||||||
86 | handle.m_pBody = nullptr; | ||||||||
87 | } | ||||||||
88 | #endif | ||||||||
89 | |||||||||
90 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
91 | /** Up-casting conversion constructor: Copies interface reference. | ||||||||
92 | |||||||||
93 | Does not work for up-casts to ambiguous bases. | ||||||||
94 | |||||||||
95 | @param rRef another reference | ||||||||
96 | */ | ||||||||
97 | template< class derived_type > | ||||||||
98 | inline Reference( | ||||||||
99 | const Reference< derived_type > & rRef, | ||||||||
100 | std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 ) | ||||||||
101 | : m_pBody (rRef.get()) | ||||||||
102 | { | ||||||||
103 | if (m_pBody) | ||||||||
104 | m_pBody->acquire(); | ||||||||
105 | } | ||||||||
106 | #endif | ||||||||
107 | |||||||||
108 | /** Destructor... | ||||||||
109 | */ | ||||||||
110 | ~Reference() COVERITY_NOEXCEPT_FALSE | ||||||||
111 | { | ||||||||
112 | if (m_pBody
| ||||||||
113 | m_pBody->release(); | ||||||||
114 | } | ||||||||
115 | |||||||||
116 | /** Set... | ||||||||
117 | Similar to assignment. | ||||||||
118 | */ | ||||||||
119 | Reference<reference_type> & | ||||||||
120 | SAL_CALL set (reference_type * pBody) | ||||||||
121 | { | ||||||||
122 | if (pBody) | ||||||||
123 | pBody->acquire(); | ||||||||
124 | reference_type * const pOld = m_pBody; | ||||||||
125 | m_pBody = pBody; | ||||||||
126 | if (pOld) | ||||||||
127 | pOld->release(); | ||||||||
128 | return *this; | ||||||||
129 | } | ||||||||
130 | |||||||||
131 | /** Assignment. | ||||||||
132 | Unbinds this instance from its body (if bound) and | ||||||||
133 | bind it to the body represented by the handle. | ||||||||
134 | */ | ||||||||
135 | Reference<reference_type> & | ||||||||
136 | SAL_CALL operator= (const Reference<reference_type> & handle) | ||||||||
137 | { | ||||||||
138 | return set( handle.m_pBody ); | ||||||||
139 | } | ||||||||
140 | |||||||||
141 | #ifdef LIBO_INTERNAL_ONLY1 | ||||||||
142 | /** Assignment. | ||||||||
143 | * Unbinds this instance from its body (if bound), | ||||||||
144 | * bind it to the body represented by the handle, and | ||||||||
145 | * set the body represented by the handle to nullptr. | ||||||||
146 | */ | ||||||||
147 | Reference<reference_type> & | ||||||||
148 | operator= (Reference<reference_type> && handle) | ||||||||
149 | { | ||||||||
150 | // self-movement guts ourself | ||||||||
151 | if (m_pBody) | ||||||||
152 | m_pBody->release(); | ||||||||
153 | m_pBody = handle.m_pBody; | ||||||||
154 | handle.m_pBody = nullptr; | ||||||||
155 | return *this; | ||||||||
156 | } | ||||||||
157 | #endif | ||||||||
158 | |||||||||
159 | /** Assignment... | ||||||||
160 | */ | ||||||||
161 | Reference<reference_type> & | ||||||||
162 | SAL_CALL operator= (reference_type * pBody) | ||||||||
163 | { | ||||||||
164 | return set( pBody ); | ||||||||
165 | } | ||||||||
166 | |||||||||
167 | /** Unbind the body from this handle. | ||||||||
168 | Note that for a handle representing a large body, | ||||||||
169 | "handle.clear().set(new body());" _might_ | ||||||||
170 | perform a little bit better than "handle.set(new body());", | ||||||||
171 | since in the second case two large objects exist in memory | ||||||||
172 | (the old body and the new body). | ||||||||
173 | */ | ||||||||
174 | Reference<reference_type> & SAL_CALL clear() | ||||||||
175 | { | ||||||||
176 | if (m_pBody) | ||||||||
177 | { | ||||||||
178 | reference_type * const pOld = m_pBody; | ||||||||
179 | m_pBody = NULL__null; | ||||||||
180 | pOld->release(); | ||||||||
181 | } | ||||||||
182 | return *this; | ||||||||
183 | } | ||||||||
184 | |||||||||
185 | |||||||||
186 | /** Get the body. Can be used instead of operator->(). | ||||||||
187 | I.e. handle->someBodyOp() and handle.get()->someBodyOp() | ||||||||
188 | are the same. | ||||||||
189 | */ | ||||||||
190 | reference_type * SAL_CALL get() const | ||||||||
191 | { | ||||||||
192 | return m_pBody; | ||||||||
193 | } | ||||||||
194 | |||||||||
195 | |||||||||
196 | /** Probably most common used: handle->someBodyOp(). | ||||||||
197 | */ | ||||||||
198 | reference_type * SAL_CALL operator->() const | ||||||||
199 | { | ||||||||
200 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 200, __extension__ __PRETTY_FUNCTION__)); | ||||||||
201 | return m_pBody; | ||||||||
202 | } | ||||||||
203 | |||||||||
204 | |||||||||
205 | /** Allows (*handle).someBodyOp(). | ||||||||
206 | */ | ||||||||
207 | reference_type & SAL_CALL operator*() const | ||||||||
208 | { | ||||||||
209 | assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail ("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx" , 209, __extension__ __PRETTY_FUNCTION__)); | ||||||||
210 | return *m_pBody; | ||||||||
211 | } | ||||||||
212 | |||||||||
213 | |||||||||
214 | /** Returns True if the handle does point to a valid body. | ||||||||
215 | */ | ||||||||
216 | bool SAL_CALL is() const | ||||||||
217 | { | ||||||||
218 | return (m_pBody != NULL__null); | ||||||||
219 | } | ||||||||
220 | |||||||||
221 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
222 | /** Returns True if the handle does point to a valid body. | ||||||||
223 | */ | ||||||||
224 | explicit operator bool() const | ||||||||
225 | { | ||||||||
226 | return is(); | ||||||||
227 | } | ||||||||
228 | #endif | ||||||||
229 | |||||||||
230 | /** Returns True if this points to pBody. | ||||||||
231 | */ | ||||||||
232 | bool SAL_CALL operator== (const reference_type * pBody) const | ||||||||
233 | { | ||||||||
234 | return (m_pBody == pBody); | ||||||||
235 | } | ||||||||
236 | |||||||||
237 | |||||||||
238 | /** Returns True if handle points to the same body. | ||||||||
239 | */ | ||||||||
240 | bool | ||||||||
241 | SAL_CALL operator== (const Reference<reference_type> & handle) const | ||||||||
242 | { | ||||||||
243 | return (m_pBody == handle.m_pBody); | ||||||||
244 | } | ||||||||
245 | |||||||||
246 | |||||||||
247 | /** Needed to place References into STL collection. | ||||||||
248 | */ | ||||||||
249 | bool | ||||||||
250 | SAL_CALL operator!= (const Reference<reference_type> & handle) const | ||||||||
251 | { | ||||||||
252 | return (m_pBody != handle.m_pBody); | ||||||||
253 | } | ||||||||
254 | |||||||||
255 | |||||||||
256 | /** Needed to place References into STL collection. | ||||||||
257 | */ | ||||||||
258 | bool | ||||||||
259 | SAL_CALL operator< (const Reference<reference_type> & handle) const | ||||||||
260 | { | ||||||||
261 | return (m_pBody < handle.m_pBody); | ||||||||
262 | } | ||||||||
263 | |||||||||
264 | |||||||||
265 | /** Needed to place References into STL collection. | ||||||||
266 | */ | ||||||||
267 | bool | ||||||||
268 | SAL_CALL operator> (const Reference<reference_type> & handle) const | ||||||||
269 | { | ||||||||
270 | return (m_pBody > handle.m_pBody); | ||||||||
271 | } | ||||||||
272 | }; | ||||||||
273 | |||||||||
274 | } // namespace rtl | ||||||||
275 | |||||||||
276 | #if defined LIBO_INTERNAL_ONLY1 | ||||||||
277 | namespace std | ||||||||
278 | { | ||||||||
279 | |||||||||
280 | /// @cond INTERNAL | ||||||||
281 | /** | ||||||||
282 | Make rtl::Reference hashable by default for use in STL containers. | ||||||||
283 | |||||||||
284 | @since LibreOffice 6.3 | ||||||||
285 | */ | ||||||||
286 | template<typename T> | ||||||||
287 | struct hash<::rtl::Reference<T>> | ||||||||
288 | { | ||||||||
289 | std::size_t operator()(::rtl::Reference<T> const & s) const | ||||||||
290 | { return std::size_t(s.get()); } | ||||||||
291 | }; | ||||||||
292 | /// @endcond | ||||||||
293 | |||||||||
294 | } | ||||||||
295 | |||||||||
296 | #endif | ||||||||
297 | |||||||||
298 | #endif /* ! INCLUDED_RTL_REF_HXX */ | ||||||||
299 | |||||||||
300 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |
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 | #ifndef INCLUDED_VCL_Reference_HXX |
20 | #define INCLUDED_VCL_Reference_HXX |
21 | |
22 | #include <vcl/dllapi.h> |
23 | #include <osl/interlck.h> |
24 | |
25 | class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase |
26 | { |
27 | mutable oslInterlockedCount mnRefCnt; |
28 | |
29 | template<typename T> friend class VclPtr; |
30 | |
31 | public: |
32 | void acquire() const |
33 | { |
34 | osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1); |
35 | } |
36 | |
37 | void release() const |
38 | { |
39 | if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0) |
40 | delete this; |
41 | } |
42 | #ifdef DBG_UTIL |
43 | #ifndef _WIN32 |
44 | sal_Int32 getRefCount() const { return mnRefCnt; } |
45 | #endif |
46 | #endif |
47 | |
48 | |
49 | private: |
50 | VclReferenceBase(const VclReferenceBase&) = delete; |
51 | VclReferenceBase& operator=(const VclReferenceBase&) = delete; |
52 | |
53 | bool mbDisposed : 1; |
54 | |
55 | protected: |
56 | VclReferenceBase(); |
57 | protected: |
58 | virtual ~VclReferenceBase(); |
59 | |
60 | protected: |
61 | virtual void dispose(); |
62 | |
63 | public: |
64 | void disposeOnce(); |
65 | bool isDisposed() const { return mbDisposed; } |
66 | |
67 | }; |
68 | #endif |