File: | home/maarten/src/libreoffice/core/include/rtl/ref.hxx |
Warning: | line 192, column 9 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 <sidebar/DeckDescriptor.hxx> | |||
21 | #include <sidebar/PanelDescriptor.hxx> | |||
22 | #include <sfx2/sidebar/ResourceManager.hxx> | |||
23 | #include <sidebar/Tools.hxx> | |||
24 | ||||
25 | #include <officecfg/Office/UI/Sidebar.hxx> | |||
26 | #include <unotools/confignode.hxx> | |||
27 | #include <comphelper/lok.hxx> | |||
28 | #include <comphelper/processfactory.hxx> | |||
29 | #include <comphelper/namedvaluecollection.hxx> | |||
30 | #include <comphelper/sequence.hxx> | |||
31 | #include <comphelper/types.hxx> | |||
32 | ||||
33 | #include <tools/diagnose_ex.h> | |||
34 | #include <sal/log.hxx> | |||
35 | #include <vcl/EnumContext.hxx> | |||
36 | ||||
37 | #include <com/sun/star/frame/ModuleManager.hpp> | |||
38 | #include <com/sun/star/ui/XSidebarPanel.hpp> | |||
39 | #include <com/sun/star/ui/XUpdateModel.hpp> | |||
40 | ||||
41 | #include <map> | |||
42 | ||||
43 | using namespace css; | |||
44 | using namespace css::uno; | |||
45 | ||||
46 | namespace sfx2::sidebar { | |||
47 | ||||
48 | namespace | |||
49 | { | |||
50 | ||||
51 | OUString getString(utl::OConfigurationNode const & aNode, const char* pNodeName) | |||
52 | { | |||
53 | return comphelper::getString(aNode.getNodeValue(pNodeName)); | |||
54 | } | |||
55 | sal_Int32 getInt32(utl::OConfigurationNode const & aNode, const char* pNodeName) | |||
56 | { | |||
57 | return comphelper::getINT32(aNode.getNodeValue(pNodeName)); | |||
58 | } | |||
59 | bool getBool(utl::OConfigurationNode const & aNode, const char* pNodeName) | |||
60 | { | |||
61 | return comphelper::getBOOL(aNode.getNodeValue(pNodeName)); | |||
62 | } | |||
63 | ||||
64 | css::uno::Sequence<OUString> BuildContextList (const ContextList& rContextList) | |||
65 | { | |||
66 | const ::std::vector<ContextList::Entry>& entries = rContextList.GetEntries(); | |||
67 | ||||
68 | css::uno::Sequence<OUString> result(entries.size()); | |||
69 | long i = 0; | |||
70 | ||||
71 | for (auto const& entry : entries) | |||
72 | { | |||
73 | OUString appName = entry.maContext.msApplication; | |||
74 | OUString contextName = entry.maContext.msContext; | |||
75 | OUString menuCommand = entry.msMenuCommand; | |||
76 | ||||
77 | OUString visibility; | |||
78 | if (entry.mbIsInitiallyVisible) | |||
79 | visibility = "visible"; | |||
80 | else | |||
81 | visibility = "hidden"; | |||
82 | ||||
83 | OUString element = appName + ", " + contextName +", " + visibility; | |||
84 | ||||
85 | if (!menuCommand.isEmpty()) | |||
86 | element += ", "+menuCommand; | |||
87 | ||||
88 | result[i] = element; | |||
89 | ||||
90 | ++i; | |||
91 | } | |||
92 | ||||
93 | return result; | |||
94 | ||||
95 | } | |||
96 | ||||
97 | } //end anonymous namespace | |||
98 | ||||
99 | ResourceManager::ResourceManager() | |||
100 | : maDecks(), | |||
101 | maPanels(), | |||
102 | maProcessedApplications(), | |||
103 | maMiscOptions() | |||
104 | { | |||
105 | ReadDeckList(); | |||
106 | ReadPanelList(); | |||
107 | ReadLastActive(); | |||
108 | } | |||
109 | ||||
110 | ResourceManager::~ResourceManager() | |||
111 | { | |||
112 | } | |||
113 | ||||
114 | void ResourceManager::InitDeckContext(const Context& rContext) | |||
115 | { | |||
116 | for (auto const& deck : maDecks) | |||
117 | { | |||
118 | const ContextList::Entry* pMatchingEntry = deck->maContextList.GetMatch(rContext); | |||
119 | ||||
120 | bool bIsEnabled; | |||
121 | if (pMatchingEntry) | |||
122 | bIsEnabled = pMatchingEntry->mbIsInitiallyVisible; | |||
123 | else | |||
124 | bIsEnabled = false; | |||
125 | ||||
126 | deck->mbIsEnabled = bIsEnabled; | |||
127 | } | |||
128 | } | |||
129 | ||||
130 | std::shared_ptr<DeckDescriptor> ResourceManager::ImplGetDeckDescriptor(const OUString& rsDeckId) const | |||
131 | { | |||
132 | for (auto const& deck : maDecks) | |||
133 | { | |||
134 | if (deck->mbExperimental && !maMiscOptions.IsExperimentalMode()) | |||
135 | continue; | |||
136 | if (deck->msId == rsDeckId) | |||
137 | return deck; | |||
138 | } | |||
139 | return nullptr; | |||
140 | } | |||
141 | ||||
142 | std::shared_ptr<DeckDescriptor> ResourceManager::GetDeckDescriptor(const OUString& rsDeckId) const | |||
143 | { | |||
144 | return ImplGetDeckDescriptor( rsDeckId ); | |||
145 | } | |||
146 | ||||
147 | std::shared_ptr<PanelDescriptor> ResourceManager::ImplGetPanelDescriptor(const OUString& rsPanelId) const | |||
148 | { | |||
149 | for (auto const& panel : maPanels) | |||
150 | { | |||
151 | if (panel->msId == rsPanelId) | |||
152 | return panel; | |||
153 | } | |||
154 | return nullptr; | |||
155 | } | |||
156 | ||||
157 | std::shared_ptr<PanelDescriptor> ResourceManager::GetPanelDescriptor(const OUString& rsPanelId) const | |||
158 | { | |||
159 | return ImplGetPanelDescriptor( rsPanelId ); | |||
160 | } | |||
161 | ||||
162 | const ResourceManager::DeckContextDescriptorContainer& ResourceManager::GetMatchingDecks ( | |||
163 | DeckContextDescriptorContainer& rDecks, | |||
164 | const Context& rContext, | |||
165 | const bool bIsDocumentReadOnly, | |||
166 | const Reference<frame::XController>& rxController) | |||
167 | { | |||
168 | ReadLegacyAddons(rxController); | |||
169 | ||||
170 | std::multimap<sal_Int32,DeckContextDescriptor> aOrderedIds; | |||
171 | for (auto const& deck : maDecks) | |||
172 | { | |||
173 | if (deck->mbExperimental && !maMiscOptions.IsExperimentalMode()) | |||
174 | continue; | |||
175 | ||||
176 | const DeckDescriptor& rDeckDescriptor (*deck); | |||
177 | if (rDeckDescriptor.maContextList.GetMatch(rContext) == nullptr) | |||
178 | continue; | |||
179 | ||||
180 | DeckContextDescriptor aDeckContextDescriptor; | |||
181 | aDeckContextDescriptor.msId = rDeckDescriptor.msId; | |||
182 | ||||
183 | aDeckContextDescriptor.mbIsEnabled = (! bIsDocumentReadOnly || IsDeckEnabled(rDeckDescriptor.msId, rContext, rxController) ) | |||
184 | && rDeckDescriptor.mbIsEnabled; | |||
185 | ||||
186 | ||||
187 | aOrderedIds.emplace(rDeckDescriptor.mnOrderIndex, aDeckContextDescriptor); | |||
188 | } | |||
189 | ||||
190 | for (auto const& orderId : aOrderedIds) | |||
191 | { | |||
192 | rDecks.push_back(orderId.second); | |||
193 | } | |||
194 | ||||
195 | return rDecks; | |||
196 | } | |||
197 | ||||
198 | const ResourceManager::PanelContextDescriptorContainer& ResourceManager::GetMatchingPanels ( | |||
199 | PanelContextDescriptorContainer& rPanelIds, | |||
200 | const Context& rContext, | |||
201 | const OUString& sDeckId, | |||
202 | const Reference<frame::XController>& rxController) | |||
203 | { | |||
204 | ReadLegacyAddons(rxController); | |||
205 | ||||
206 | std::multimap<sal_Int32, PanelContextDescriptor> aOrderedIds; | |||
207 | for (auto const& panel : maPanels) | |||
208 | { | |||
209 | const PanelDescriptor& rPanelDescriptor (*panel); | |||
210 | if (rPanelDescriptor.mbExperimental && !maMiscOptions.IsExperimentalMode()) | |||
211 | continue; | |||
212 | if ( rPanelDescriptor.msDeckId != sDeckId ) | |||
213 | continue; | |||
214 | ||||
215 | const ContextList::Entry* pEntry = rPanelDescriptor.maContextList.GetMatch(rContext); | |||
216 | if (pEntry == nullptr) | |||
217 | continue; | |||
218 | ||||
219 | PanelContextDescriptor aPanelContextDescriptor; | |||
220 | aPanelContextDescriptor.msId = rPanelDescriptor.msId; | |||
221 | aPanelContextDescriptor.msMenuCommand = pEntry->msMenuCommand; | |||
222 | aPanelContextDescriptor.mbIsInitiallyVisible = pEntry->mbIsInitiallyVisible; | |||
223 | aPanelContextDescriptor.mbShowForReadOnlyDocuments = rPanelDescriptor.mbShowForReadOnlyDocuments; | |||
224 | aOrderedIds.emplace(rPanelDescriptor.mnOrderIndex, aPanelContextDescriptor); | |||
225 | } | |||
226 | ||||
227 | for (auto const& orderId : aOrderedIds) | |||
228 | { | |||
229 | rPanelIds.push_back(orderId.second); | |||
230 | } | |||
231 | ||||
232 | return rPanelIds; | |||
233 | } | |||
234 | ||||
235 | const OUString& ResourceManager::GetLastActiveDeck( const Context& rContext ) | |||
236 | { | |||
237 | if( maLastActiveDecks.find( rContext.msApplication ) == maLastActiveDecks.end()) | |||
238 | return maLastActiveDecks["any"]; | |||
239 | else | |||
240 | return maLastActiveDecks[rContext.msApplication]; | |||
241 | } | |||
242 | ||||
243 | void ResourceManager::SetLastActiveDeck( const Context& rContext, const OUString &rsDeckId ) | |||
244 | { | |||
245 | maLastActiveDecks[rContext.msApplication] = rsDeckId; | |||
246 | } | |||
247 | ||||
248 | void ResourceManager::ReadDeckList() | |||
249 | { | |||
250 | const utl::OConfigurationTreeRoot aDeckRootNode( | |||
251 | comphelper::getProcessComponentContext(), | |||
252 | "org.openoffice.Office.UI.Sidebar/Content/DeckList", | |||
253 | false); | |||
254 | if (!aDeckRootNode.isValid()) | |||
255 | return; | |||
256 | ||||
257 | const Sequence<OUString> aDeckNodeNames (aDeckRootNode.getNodeNames()); | |||
258 | maDecks.clear(); | |||
259 | for (const OUString& aDeckName : aDeckNodeNames) | |||
260 | { | |||
261 | if (comphelper::LibreOfficeKit::isActive()) | |||
262 | { | |||
263 | // Hide these decks in LOK as they aren't fully functional. | |||
264 | if (aDeckName == "GalleryDeck" || aDeckName == "NavigatorDeck" | |||
265 | || aDeckName == "StyleListDeck") | |||
266 | continue; | |||
267 | } | |||
268 | ||||
269 | const utl::OConfigurationNode aDeckNode(aDeckRootNode.openNode(aDeckName)); | |||
270 | if (!aDeckNode.isValid()) | |||
271 | continue; | |||
272 | ||||
273 | maDecks.push_back(std::make_shared<DeckDescriptor>()); | |||
274 | DeckDescriptor& rDeckDescriptor (*maDecks.back()); | |||
275 | ||||
276 | rDeckDescriptor.msTitle = getString(aDeckNode, "Title"); | |||
277 | rDeckDescriptor.msId = getString(aDeckNode, "Id"); | |||
278 | rDeckDescriptor.msIconURL = getString(aDeckNode, "IconURL"); | |||
279 | rDeckDescriptor.msHighContrastIconURL = getString(aDeckNode, "HighContrastIconURL"); | |||
280 | rDeckDescriptor.msTitleBarIconURL = getString(aDeckNode, "TitleBarIconURL"); | |||
281 | rDeckDescriptor.msHighContrastTitleBarIconURL = getString(aDeckNode, "HighContrastTitleBarIconURL"); | |||
282 | rDeckDescriptor.msHelpText = rDeckDescriptor.msTitle; | |||
283 | rDeckDescriptor.mnOrderIndex = getInt32(aDeckNode, "OrderIndex"); | |||
284 | rDeckDescriptor.mbExperimental = getBool(aDeckNode, "IsExperimental"); | |||
285 | ||||
286 | rDeckDescriptor.msNodeName = aDeckName; | |||
287 | ||||
288 | ReadContextList( | |||
289 | aDeckNode, | |||
290 | rDeckDescriptor.maContextList, | |||
291 | OUString()); | |||
292 | ||||
293 | } | |||
294 | } | |||
295 | ||||
296 | void ResourceManager::SaveDecksSettings(const Context& rContext) | |||
297 | { | |||
298 | for (auto const& deck : maDecks) | |||
299 | { | |||
300 | const ContextList::Entry* pMatchingEntry = deck->maContextList.GetMatch(rContext); | |||
301 | if (pMatchingEntry) | |||
302 | { | |||
303 | std::shared_ptr<DeckDescriptor> xDeckDesc = GetDeckDescriptor(deck->msId); | |||
304 | if (xDeckDesc) | |||
305 | SaveDeckSettings(xDeckDesc.get()); | |||
306 | } | |||
307 | ||||
308 | } | |||
309 | } | |||
310 | ||||
311 | void ResourceManager::SaveDeckSettings(const DeckDescriptor* pDeckDesc) | |||
312 | { | |||
313 | const utl::OConfigurationTreeRoot aDeckRootNode( | |||
314 | comphelper::getProcessComponentContext(), | |||
315 | "org.openoffice.Office.UI.Sidebar/Content/DeckList", | |||
316 | true); | |||
317 | if (!aDeckRootNode.isValid()) | |||
318 | return; | |||
319 | ||||
320 | // save deck settings | |||
321 | ||||
322 | ::uno::Sequence< OUString > sContextList = BuildContextList(pDeckDesc->maContextList); | |||
323 | ||||
324 | utl::OConfigurationNode aDeckNode (aDeckRootNode.openNode(pDeckDesc->msNodeName)); | |||
325 | ||||
326 | css::uno::Any aTitle(makeAny(pDeckDesc->msTitle)); | |||
327 | css::uno::Any aOrder(makeAny(pDeckDesc->mnOrderIndex)); | |||
328 | css::uno::Any aContextList(makeAny(sContextList)); | |||
329 | ||||
330 | bool bChanged = false; | |||
331 | if (aTitle != aDeckNode.getNodeValue("Title")) | |||
332 | { | |||
333 | aDeckNode.setNodeValue("Title", aTitle); | |||
334 | bChanged = true; | |||
335 | } | |||
336 | if (aOrder != aDeckNode.getNodeValue("OrderIndex")) | |||
337 | { | |||
338 | aDeckNode.setNodeValue("OrderIndex", aOrder); | |||
339 | bChanged = true; | |||
340 | } | |||
341 | if (aContextList != aDeckNode.getNodeValue("ContextList")) | |||
342 | { | |||
343 | aDeckNode.setNodeValue("ContextList", aContextList); | |||
344 | bChanged = true; | |||
345 | } | |||
346 | ||||
347 | if (bChanged) | |||
348 | aDeckRootNode.commit(); | |||
349 | ||||
350 | // save panel settings | |||
351 | ||||
352 | const utl::OConfigurationTreeRoot aPanelRootNode( | |||
353 | comphelper::getProcessComponentContext(), | |||
354 | "org.openoffice.Office.UI.Sidebar/Content/PanelList", | |||
355 | true); | |||
356 | ||||
357 | if (!aPanelRootNode.isValid()) | |||
358 | return; | |||
359 | ||||
360 | if (!pDeckDesc->mpDeck) // the deck has not been edited | |||
361 | return; | |||
362 | ||||
363 | SharedPanelContainer rPanels = pDeckDesc->mpDeck->GetPanels(); | |||
364 | ||||
365 | bChanged = false; | |||
366 | for (auto const& panel : rPanels) | |||
367 | { | |||
368 | OUString panelId = panel->GetId(); | |||
369 | std::shared_ptr<PanelDescriptor> xPanelDesc = GetPanelDescriptor(panelId); | |||
370 | ||||
371 | ::uno::Sequence< OUString > sPanelContextList = BuildContextList(xPanelDesc->maContextList); | |||
372 | ||||
373 | utl::OConfigurationNode aPanelNode (aPanelRootNode.openNode(xPanelDesc->msNodeName)); | |||
374 | ||||
375 | aTitle <<= xPanelDesc->msTitle; | |||
376 | aOrder <<= xPanelDesc->mnOrderIndex; | |||
377 | aContextList <<= sPanelContextList; | |||
378 | ||||
379 | if (aTitle != aPanelNode.getNodeValue("Title")) | |||
380 | { | |||
381 | aPanelNode.setNodeValue("Title", aTitle); | |||
382 | bChanged = true; | |||
383 | } | |||
384 | if (aOrder != aPanelNode.getNodeValue("OrderIndex")) | |||
385 | { | |||
386 | aPanelNode.setNodeValue("OrderIndex", aOrder); | |||
387 | bChanged = true; | |||
388 | } | |||
389 | if (aContextList != aPanelNode.getNodeValue("ContextList")) | |||
390 | { | |||
391 | aPanelNode.setNodeValue("ContextList", aContextList); | |||
392 | bChanged = true; | |||
393 | } | |||
394 | } | |||
395 | ||||
396 | if (bChanged) | |||
397 | aPanelRootNode.commit(); | |||
398 | } | |||
399 | ||||
400 | void ResourceManager::SaveLastActiveDeck(const Context& rContext, const OUString& rActiveDeck) | |||
401 | { | |||
402 | maLastActiveDecks[rContext.msApplication] = rActiveDeck; | |||
403 | ||||
404 | std::set<OUString> aLastActiveDecks; | |||
405 | for ( auto const & rEntry : maLastActiveDecks ) | |||
406 | aLastActiveDecks.insert( rEntry.first + "," + rEntry.second); | |||
407 | ||||
408 | std::shared_ptr<comphelper::ConfigurationChanges> cfgWriter( comphelper::ConfigurationChanges::create() ); | |||
409 | ||||
410 | officecfg::Office::UI::Sidebar::Content::LastActiveDeck::set(comphelper::containerToSequence(aLastActiveDecks), cfgWriter); | |||
411 | cfgWriter->commit(); | |||
412 | ||||
413 | } | |||
414 | ||||
415 | void ResourceManager::ReadPanelList() | |||
416 | { | |||
417 | const utl::OConfigurationTreeRoot aPanelRootNode( | |||
418 | comphelper::getProcessComponentContext(), | |||
419 | "org.openoffice.Office.UI.Sidebar/Content/PanelList", | |||
420 | false); | |||
421 | if (!aPanelRootNode.isValid()) | |||
422 | return; | |||
423 | ||||
424 | const Sequence<OUString> aPanelNodeNames (aPanelRootNode.getNodeNames()); | |||
425 | maPanels.clear(); | |||
426 | for (const auto& rPanelNodeName : aPanelNodeNames) | |||
427 | { | |||
428 | const utl::OConfigurationNode aPanelNode (aPanelRootNode.openNode(rPanelNodeName)); | |||
429 | if (!aPanelNode.isValid()) | |||
430 | continue; | |||
431 | ||||
432 | if (comphelper::LibreOfficeKit::isActive()) | |||
433 | { | |||
434 | // Hide these panels in LOK as they aren't fully functional. | |||
435 | OUString aPanelId = getString(aPanelNode, "Id"); | |||
436 | if (aPanelId == "PageStylesPanel" || aPanelId == "PageHeaderPanel" | |||
437 | || aPanelId == "PageFooterPanel") | |||
438 | continue; | |||
439 | } | |||
440 | ||||
441 | maPanels.push_back(std::make_shared<PanelDescriptor>()); | |||
442 | PanelDescriptor& rPanelDescriptor(*maPanels.back()); | |||
443 | ||||
444 | rPanelDescriptor.msTitle = getString(aPanelNode, "Title"); | |||
445 | rPanelDescriptor.mbIsTitleBarOptional = getBool(aPanelNode, "TitleBarIsOptional"); | |||
446 | rPanelDescriptor.msId = getString(aPanelNode, "Id"); | |||
447 | rPanelDescriptor.msDeckId = getString(aPanelNode, "DeckId"); | |||
448 | rPanelDescriptor.msTitleBarIconURL = getString(aPanelNode, "TitleBarIconURL"); | |||
449 | rPanelDescriptor.msHighContrastTitleBarIconURL = getString(aPanelNode, "HighContrastTitleBarIconURL"); | |||
450 | rPanelDescriptor.msImplementationURL = getString(aPanelNode, "ImplementationURL"); | |||
451 | rPanelDescriptor.mnOrderIndex = getInt32(aPanelNode, "OrderIndex"); | |||
452 | rPanelDescriptor.mbShowForReadOnlyDocuments = getBool(aPanelNode, "ShowForReadOnlyDocument"); | |||
453 | rPanelDescriptor.mbWantsCanvas = getBool(aPanelNode, "WantsCanvas"); | |||
454 | rPanelDescriptor.mbExperimental = getBool(aPanelNode, "IsExperimental"); | |||
455 | const OUString sDefaultMenuCommand(getString(aPanelNode, "DefaultMenuCommand")); | |||
456 | ||||
457 | rPanelDescriptor.msNodeName = rPanelNodeName; | |||
458 | ||||
459 | ReadContextList(aPanelNode, rPanelDescriptor.maContextList, sDefaultMenuCommand); | |||
460 | } | |||
461 | } | |||
462 | ||||
463 | void ResourceManager::ReadLastActive() | |||
464 | { | |||
465 | const Sequence <OUString> aLastActive (officecfg::Office::UI::Sidebar::Content::LastActiveDeck::get()); | |||
466 | ||||
467 | for (const auto& rDeckInfo : aLastActive) | |||
468 | { | |||
469 | sal_Int32 nCharIdx = rDeckInfo.lastIndexOf(','); | |||
470 | if ( nCharIdx <= 0 || (nCharIdx == rDeckInfo.getLength() - 1) ) | |||
471 | { | |||
472 | SAL_WARN("sfx.sidebar", "Expecting 2 values separated by comma")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN , "sfx.sidebar")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult ( ::sal::detail::StreamStart() << "Expecting 2 values separated by comma" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar" ), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "472" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "Expecting 2 values separated by comma" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "Expecting 2 values separated by comma"; ::sal::detail ::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "472" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "Expecting 2 values separated by comma") == 1) { :: sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar" ), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "472" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "Expecting 2 values separated by comma" ), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "Expecting 2 values separated by comma"; ::sal::detail ::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "472" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | |||
473 | continue; | |||
474 | } | |||
475 | ||||
476 | const OUString sApplicationName = rDeckInfo.copy( 0, nCharIdx ); | |||
477 | vcl::EnumContext::Application eApplication (vcl::EnumContext::GetApplicationEnum(sApplicationName)); | |||
478 | const OUString sLastUsed = rDeckInfo.copy( nCharIdx + 1 ); | |||
479 | ||||
480 | // guard against garbage in place of application | |||
481 | if (eApplication != vcl::EnumContext::Application::NONE) | |||
482 | maLastActiveDecks.insert( std::make_pair(sApplicationName, sLastUsed ) ); | |||
483 | } | |||
484 | } | |||
485 | ||||
486 | void ResourceManager::ReadContextList ( | |||
487 | const utl::OConfigurationNode& rParentNode, | |||
488 | ContextList& rContextList, | |||
489 | const OUString& rsDefaultMenuCommand) | |||
490 | { | |||
491 | const Any aValue = rParentNode.getNodeValue("ContextList"); | |||
492 | Sequence<OUString> aValues; | |||
493 | if (!(aValue >>= aValues)) | |||
494 | return; | |||
495 | ||||
496 | for (const OUString& sValue : std::as_const(aValues)) | |||
497 | { | |||
498 | sal_Int32 nCharacterIndex (0); | |||
499 | const OUString sApplicationName (sValue.getToken(0, ',', nCharacterIndex).trim()); | |||
500 | if (nCharacterIndex < 0) | |||
501 | { | |||
502 | if (sApplicationName.getLength() == 0) | |||
503 | { | |||
504 | // This is a valid case: in the XML file the separator | |||
505 | // was used as terminator. Using it in the last line | |||
506 | // creates an additional but empty entry. | |||
507 | break; | |||
508 | } | |||
509 | else | |||
510 | { | |||
511 | OSL_FAIL("expecting three or four values per ContextList entry, separated by comma")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "511" ": "), "%s", "expecting three or four values per ContextList entry, separated by comma" ); } } while (false); | |||
512 | continue; | |||
513 | } | |||
514 | } | |||
515 | ||||
516 | const OUString sContextName(sValue.getToken(0, ',', nCharacterIndex).trim()); | |||
517 | if (nCharacterIndex < 0) | |||
518 | { | |||
519 | OSL_FAIL("expecting three or four values per ContextList entry, separated by comma")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "519" ": "), "%s", "expecting three or four values per ContextList entry, separated by comma" ); } } while (false); | |||
520 | continue; | |||
521 | } | |||
522 | ||||
523 | const OUString sInitialState(sValue.getToken(0, ',', nCharacterIndex).trim()); | |||
524 | ||||
525 | // The fourth argument is optional. | |||
526 | const OUString sMenuCommandOverride( | |||
527 | nCharacterIndex < 0 | |||
528 | ? OUString() | |||
529 | : sValue.getToken(0, ',', nCharacterIndex).trim()); | |||
530 | ||||
531 | const OUString sMenuCommand( | |||
532 | sMenuCommandOverride.getLength() > 0 | |||
533 | ? (sMenuCommandOverride == "none" | |||
534 | ? OUString() | |||
535 | : sMenuCommandOverride) | |||
536 | : rsDefaultMenuCommand); | |||
537 | ||||
538 | // Setup a list of application enums. Note that the | |||
539 | // application name may result in more than one value (eg | |||
540 | // DrawImpress will result in two enums, one for Draw and one | |||
541 | // for Impress). | |||
542 | std::vector<vcl::EnumContext::Application> aApplications; | |||
543 | vcl::EnumContext::Application eApplication (vcl::EnumContext::GetApplicationEnum(sApplicationName)); | |||
544 | ||||
545 | if (eApplication == vcl::EnumContext::Application::NONE | |||
546 | && sApplicationName != vcl::EnumContext::GetApplicationName(vcl::EnumContext::Application::NONE)) | |||
547 | { | |||
548 | // Handle some special names: abbreviations that make | |||
549 | // context descriptions more readable. | |||
550 | if (sApplicationName == "Writer") | |||
551 | aApplications.push_back(vcl::EnumContext::Application::Writer); | |||
552 | else if (sApplicationName == "Calc") | |||
553 | aApplications.push_back(vcl::EnumContext::Application::Calc); | |||
554 | else if (sApplicationName == "Draw") | |||
555 | aApplications.push_back(vcl::EnumContext::Application::Draw); | |||
556 | else if (sApplicationName == "Impress") | |||
557 | aApplications.push_back(vcl::EnumContext::Application::Impress); | |||
558 | else if (sApplicationName == "Chart") | |||
559 | aApplications.push_back(vcl::EnumContext::Application::Chart); | |||
560 | else if (sApplicationName == "DrawImpress") | |||
561 | { | |||
562 | // A special case among the special names: it is | |||
563 | // common to use the same context descriptions for | |||
564 | // both Draw and Impress. This special case helps to | |||
565 | // avoid duplication in the .xcu file. | |||
566 | aApplications.push_back(vcl::EnumContext::Application::Draw); | |||
567 | aApplications.push_back(vcl::EnumContext::Application::Impress); | |||
568 | } | |||
569 | else if (sApplicationName == "WriterVariants") | |||
570 | { | |||
571 | // Another special case for all Writer variants. | |||
572 | aApplications.push_back(vcl::EnumContext::Application::Writer); | |||
573 | aApplications.push_back(vcl::EnumContext::Application::WriterGlobal); | |||
574 | aApplications.push_back(vcl::EnumContext::Application::WriterWeb); | |||
575 | aApplications.push_back(vcl::EnumContext::Application::WriterXML); | |||
576 | aApplications.push_back(vcl::EnumContext::Application::WriterForm); | |||
577 | aApplications.push_back(vcl::EnumContext::Application::WriterReport); | |||
578 | } | |||
579 | else | |||
580 | { | |||
581 | SAL_WARN("sfx.sidebar", "application name " << sApplicationName << " not recognized")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN , "sfx.sidebar")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult ( ::sal::detail::StreamStart() << "application name " << sApplicationName << " not recognized") == 1) { ::sal_detail_log ( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "581" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "application name " << sApplicationName << " not recognized"), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "application name " << sApplicationName << " not recognized"; ::sal:: detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar"), ( "/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "581" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "application name " << sApplicationName << " not recognized") == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN ), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "581" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "application name " << sApplicationName << " not recognized"), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream << "application name " << sApplicationName << " not recognized"; ::sal:: detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar"), ( "/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "581" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | |||
582 | continue; | |||
583 | } | |||
584 | } | |||
585 | else | |||
586 | { | |||
587 | // No conversion of the application name necessary. | |||
588 | aApplications.push_back(eApplication); | |||
589 | } | |||
590 | ||||
591 | // Setup the actual context enum. | |||
592 | const vcl::EnumContext::Context eContext (vcl::EnumContext::GetContextEnum(sContextName)); | |||
593 | if (eContext == vcl::EnumContext::Context::Unknown) | |||
594 | { | |||
595 | SAL_WARN("sfx.sidebar", "context name " << sContextName << " not recognized")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN , "sfx.sidebar")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult ( ::sal::detail::StreamStart() << "context name " << sContextName << " not recognized") == 1) { ::sal_detail_log ( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "595" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "context name " << sContextName << " not recognized"), 0); } else { ::std::ostringstream sal_detail_stream ; sal_detail_stream << "context name " << sContextName << " not recognized"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN ), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "595" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL : if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart () << "context name " << sContextName << " not recognized" ) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sfx.sidebar" ), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "595" ": "), ::sal::detail::unwrapStream( ::sal::detail:: StreamStart() << "context name " << sContextName << " not recognized"), 0); } else { ::std::ostringstream sal_detail_stream ; sal_detail_stream << "context name " << sContextName << " not recognized"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN ), ("sfx.sidebar"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "595" ": "), sal_detail_stream, 0); }; std::abort(); break ; } } } while (false); | |||
596 | continue; | |||
597 | } | |||
598 | ||||
599 | // Setup the flag that controls whether a deck/pane is | |||
600 | // initially visible/expanded. | |||
601 | bool bIsInitiallyVisible; | |||
602 | if (sInitialState == "visible") | |||
603 | bIsInitiallyVisible = true; | |||
604 | else if (sInitialState == "hidden") | |||
605 | bIsInitiallyVisible = false; | |||
606 | else | |||
607 | { | |||
608 | OSL_FAIL("unrecognized state")do { if (true && (((sal_Bool)1))) { sal_detail_logFormat ((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "608" ": "), "%s", "unrecognized state"); } } while (false ); | |||
609 | continue; | |||
610 | } | |||
611 | ||||
612 | ||||
613 | // Add context descriptors. | |||
614 | for (auto const& application : aApplications) | |||
615 | { | |||
616 | if (application != vcl::EnumContext::Application::NONE) | |||
617 | { | |||
618 | rContextList.AddContextDescription( | |||
619 | Context( | |||
620 | vcl::EnumContext::GetApplicationName(application), | |||
621 | vcl::EnumContext::GetContextName(eContext)), | |||
622 | bIsInitiallyVisible, | |||
623 | sMenuCommand); | |||
624 | } | |||
625 | } | |||
626 | } | |||
627 | } | |||
628 | ||||
629 | void ResourceManager::ReadLegacyAddons (const Reference<frame::XController>& rxController) | |||
630 | { | |||
631 | // Get module name for given frame. | |||
632 | OUString sModuleName (Tools::GetModuleName(rxController)); | |||
633 | if (sModuleName.getLength() == 0) | |||
634 | return; | |||
635 | if (maProcessedApplications.find(sModuleName) != maProcessedApplications.end()) | |||
636 | { | |||
637 | // Addons for this application have already been read. | |||
638 | // There is nothing more to do. | |||
639 | return; | |||
640 | } | |||
641 | ||||
642 | // Mark module as processed. Even when there is an error that | |||
643 | // prevents the configuration data from being read, this error | |||
644 | // will not be triggered a second time. | |||
645 | maProcessedApplications.insert(sModuleName); | |||
646 | ||||
647 | // Get access to the configuration root node for the application. | |||
648 | utl::OConfigurationTreeRoot aLegacyRootNode (GetLegacyAddonRootNode(sModuleName)); | |||
649 | if (!aLegacyRootNode.isValid()) | |||
650 | return; | |||
651 | ||||
652 | // Process child nodes. | |||
653 | std::vector<OUString> aMatchingNodeNames; | |||
654 | GetToolPanelNodeNames(aMatchingNodeNames, aLegacyRootNode); | |||
655 | const sal_Int32 nCount (aMatchingNodeNames.size()); | |||
656 | for (sal_Int32 nReadIndex(0); nReadIndex<nCount; ++nReadIndex) | |||
657 | { | |||
658 | const OUString& rsNodeName (aMatchingNodeNames[nReadIndex]); | |||
659 | const utl::OConfigurationNode aChildNode (aLegacyRootNode.openNode(rsNodeName)); | |||
660 | if (!aChildNode.isValid()) | |||
661 | continue; | |||
662 | ||||
663 | if ( rsNodeName == "private:resource/toolpanel/DrawingFramework/CustomAnimations" || | |||
664 | rsNodeName == "private:resource/toolpanel/DrawingFramework/Layouts" || | |||
665 | rsNodeName == "private:resource/toolpanel/DrawingFramework/MasterPages" || | |||
666 | rsNodeName == "private:resource/toolpanel/DrawingFramework/SlideTransitions" || | |||
667 | rsNodeName == "private:resource/toolpanel/DrawingFramework/TableDesign" ) | |||
668 | continue; | |||
669 | ||||
670 | maDecks.push_back(std::make_shared<DeckDescriptor>()); | |||
671 | DeckDescriptor& rDeckDescriptor(*maDecks.back()); | |||
672 | rDeckDescriptor.msTitle = getString(aChildNode, "UIName"); | |||
673 | rDeckDescriptor.msId = rsNodeName; | |||
674 | rDeckDescriptor.msIconURL = getString(aChildNode, "ImageURL"); | |||
675 | rDeckDescriptor.msHighContrastIconURL = rDeckDescriptor.msIconURL; | |||
676 | rDeckDescriptor.msTitleBarIconURL.clear(); | |||
677 | rDeckDescriptor.msHighContrastTitleBarIconURL.clear(); | |||
678 | rDeckDescriptor.msHelpText = rDeckDescriptor.msTitle; | |||
679 | rDeckDescriptor.mbIsEnabled = true; | |||
680 | rDeckDescriptor.mnOrderIndex = 100000 + nReadIndex; | |||
681 | rDeckDescriptor.maContextList.AddContextDescription(Context(sModuleName, "any"), true, OUString()); | |||
682 | ||||
683 | maPanels.push_back(std::make_shared<PanelDescriptor>()); | |||
684 | PanelDescriptor& rPanelDescriptor(*maPanels.back()); | |||
685 | rPanelDescriptor.msTitle = getString(aChildNode, "UIName"); | |||
686 | rPanelDescriptor.mbIsTitleBarOptional = true; | |||
687 | rPanelDescriptor.msId = rsNodeName; | |||
688 | rPanelDescriptor.msDeckId = rsNodeName; | |||
689 | rPanelDescriptor.msTitleBarIconURL.clear(); | |||
690 | rPanelDescriptor.msHighContrastTitleBarIconURL.clear(); | |||
691 | rPanelDescriptor.msImplementationURL = rsNodeName; | |||
692 | rPanelDescriptor.mnOrderIndex = 100000 + nReadIndex; | |||
693 | rPanelDescriptor.mbShowForReadOnlyDocuments = false; | |||
694 | rPanelDescriptor.mbWantsCanvas = false; | |||
695 | rPanelDescriptor.maContextList.AddContextDescription(Context(sModuleName, "any"), true, OUString()); | |||
696 | } | |||
697 | } | |||
698 | ||||
699 | void ResourceManager::StorePanelExpansionState ( | |||
700 | const OUString& rsPanelId, | |||
701 | const bool bExpansionState, | |||
702 | const Context& rContext) | |||
703 | { | |||
704 | for (auto const& panel : maPanels) | |||
705 | { | |||
706 | if (panel->msId == rsPanelId) | |||
707 | { | |||
708 | ContextList::Entry* pEntry(panel->maContextList.GetMatch(rContext)); | |||
709 | if (pEntry != nullptr) | |||
710 | pEntry->mbIsInitiallyVisible = bExpansionState; | |||
711 | } | |||
712 | } | |||
713 | } | |||
714 | ||||
715 | utl::OConfigurationTreeRoot ResourceManager::GetLegacyAddonRootNode (const OUString& rsModuleName) | |||
716 | { | |||
717 | try | |||
718 | { | |||
719 | const Reference<XComponentContext> xContext(comphelper::getProcessComponentContext()); | |||
720 | const Reference<frame::XModuleManager2> xModuleAccess = frame::ModuleManager::create(xContext); | |||
721 | const comphelper::NamedValueCollection aModuleProperties(xModuleAccess->getByName(rsModuleName)); | |||
722 | const OUString sWindowStateRef(aModuleProperties.getOrDefault( | |||
723 | "ooSetupFactoryWindowStateConfigRef", | |||
724 | OUString())); | |||
725 | ||||
726 | OUString aPathComposer = "org.openoffice.Office.UI." + sWindowStateRef + | |||
727 | "/UIElements/States"; | |||
728 | ||||
729 | return utl::OConfigurationTreeRoot(xContext, aPathComposer, false); | |||
730 | } | |||
731 | catch (const Exception&) | |||
732 | { | |||
733 | DBG_UNHANDLED_EXCEPTION("sfx.sidebar")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/sfx2/source/sidebar/ResourceManager.cxx" ":" "733" ": ", "sfx.sidebar" );; | |||
734 | } | |||
735 | ||||
736 | return utl::OConfigurationTreeRoot(); | |||
737 | } | |||
738 | ||||
739 | void ResourceManager::GetToolPanelNodeNames ( | |||
740 | std::vector<OUString>& rMatchingNames, | |||
741 | const utl::OConfigurationTreeRoot& aRoot) | |||
742 | { | |||
743 | Sequence<OUString> aChildNodeNames (aRoot.getNodeNames()); | |||
744 | std::copy_if(aChildNodeNames.begin(), aChildNodeNames.end(), std::back_inserter(rMatchingNames), | |||
745 | [](const OUString& rChildNodeName) { return rChildNodeName.startsWith( "private:resource/toolpanel/" ); }); | |||
746 | } | |||
747 | ||||
748 | bool ResourceManager::IsDeckEnabled ( | |||
749 | const OUString& rsDeckId, | |||
750 | const Context& rContext, | |||
751 | const Reference<frame::XController>& rxController) | |||
752 | { | |||
753 | ||||
754 | // Check if any panel that matches the current context can be | |||
755 | // displayed. | |||
756 | PanelContextDescriptorContainer aPanelContextDescriptors; | |||
757 | ||||
758 | GetMatchingPanels(aPanelContextDescriptors, rContext, rsDeckId, rxController); | |||
759 | ||||
760 | for (auto const& panelContextDescriptor : aPanelContextDescriptors) | |||
761 | { | |||
762 | if (panelContextDescriptor.mbShowForReadOnlyDocuments) | |||
763 | return true; | |||
764 | } | |||
765 | return false; | |||
766 | } | |||
767 | ||||
768 | void ResourceManager::UpdateModel(const css::uno::Reference<css::frame::XModel>& xModel) | |||
769 | { | |||
770 | for (auto const& deck : maDecks) | |||
771 | { | |||
772 | if (!deck->mpDeck) | |||
773 | continue; | |||
774 | ||||
775 | const SharedPanelContainer& rContainer = deck->mpDeck->GetPanels(); | |||
776 | ||||
777 | for (auto const& elem : rContainer) | |||
778 | { | |||
779 | css::uno::Reference<css::ui::XUpdateModel> xPanel(elem->GetPanelComponent(), css::uno::UNO_QUERY); | |||
780 | if (xPanel.is()) // tdf#108814 interface is optional | |||
781 | { | |||
782 | xPanel->updateModel(xModel); | |||
783 | } | |||
784 | } | |||
785 | } | |||
786 | } | |||
787 | ||||
788 | void ResourceManager::disposeDecks() | |||
789 | { | |||
790 | for (auto const& deck : maDecks) | |||
791 | { | |||
792 | deck->mpDeck.disposeAndClear(); | |||
| ||||
793 | } | |||
794 | } | |||
795 | ||||
796 | } // end of namespace sfx2::sidebar | |||
797 | ||||
798 | /* 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 |