Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 192, column 9
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name bookmrk.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -isystem /usr/include/libxml2 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D SW_DLLIMPLEMENTATION -D SWUI_DLL_NAME="libswuilo.so" -D SYSTEM_LIBXML -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/sw/source/core/inc -I /home/maarten/src/libreoffice/core/sw/source/filter/inc -I /home/maarten/src/libreoffice/core/sw/source/uibase/inc -I /home/maarten/src/libreoffice/core/sw/inc -I /home/maarten/src/libreoffice/core/workdir/SdiTarget/sw/sdi -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/sw/generated -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/oovbaapi/normal -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx

/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx

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 <memory>
21#include <bookmrk.hxx>
22#include <IDocumentUndoRedo.hxx>
23#include <IDocumentLinksAdministration.hxx>
24#include <IDocumentState.hxx>
25#include <doc.hxx>
26#include <ndtxt.hxx>
27#include <pam.hxx>
28#include <swserv.hxx>
29#include <sfx2/linkmgr.hxx>
30#include <UndoBookmark.hxx>
31#include <unobookmark.hxx>
32#include <xmloff/odffields.hxx>
33#include <libxml/xmlwriter.h>
34#include <comphelper/random.hxx>
35#include <comphelper/anytostring.hxx>
36#include <sal/log.hxx>
37#include <svl/zforlist.hxx>
38#include <edtwin.hxx>
39#include <DateFormFieldButton.hxx>
40#include <DropDownFormFieldButton.hxx>
41#include <DocumentContentOperationsManager.hxx>
42#include <comphelper/lok.hxx>
43#include <view.hxx>
44#include <LibreOfficeKit/LibreOfficeKitEnums.h>
45#include <wrtsh.hxx>
46#include <rtl/strbuf.hxx>
47#include <sfx2/lokhelper.hxx>
48#include <strings.hrc>
49
50using namespace ::sw::mark;
51using namespace ::com::sun::star;
52using namespace ::com::sun::star::uno;
53
54namespace sw::mark
55{
56
57 SwPosition FindFieldSep(IFieldmark const& rMark)
58 {
59 SwPosition const& rStartPos(rMark.GetMarkStart());
60 SwPosition const& rEndPos(rMark.GetMarkEnd());
61 SwNodes const& rNodes(rStartPos.nNode.GetNodes());
62 sal_uLong const nStartNode(rStartPos.nNode.GetIndex());
63 sal_uLong const nEndNode(rEndPos.nNode.GetIndex());
64 int nFields(0);
65 std::optional<SwPosition> ret;
66 for (sal_uLong n = nEndNode; nStartNode <= n; --n)
67 {
68 SwNode *const pNode(rNodes[n]);
69 if (pNode->IsTextNode())
70 {
71 SwTextNode & rTextNode(*pNode->GetTextNode());
72 sal_Int32 const nStart(n == nStartNode
73 ? rStartPos.nContent.GetIndex() + 1
74 : 0);
75 sal_Int32 const nEnd(n == nEndNode
76 // subtract 1 to ignore the end char
77 ? rEndPos.nContent.GetIndex() - 1
78 : rTextNode.Len());
79 for (sal_Int32 i = nEnd; nStart < i; --i)
80 {
81 const sal_Unicode c(rTextNode.GetText()[i - 1]);
82 switch (c)
83 {
84 case CH_TXT_ATR_FIELDSTARTu'\x0007':
85 --nFields;
86 assert(0 <= nFields)(static_cast <bool> (0 <= nFields) ? void (0) : __assert_fail
("0 <= nFields", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 86, __extension__ __PRETTY_FUNCTION__))
;
87 break;
88 case CH_TXT_ATR_FIELDENDu'\x0008':
89 ++nFields;
90 // fields in field result could happen by manual
91 // editing, although the field update deletes them
92 break;
93 case CH_TXT_ATR_FIELDSEPu'\x0003':
94 if (nFields == 0)
95 {
96 assert(!ret)(static_cast <bool> (!ret) ? void (0) : __assert_fail (
"!ret", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 96, __extension__ __PRETTY_FUNCTION__))
; // one per field
97 ret = SwPosition(rTextNode, i - 1);
98#ifndef DBG_UTIL
99 return *ret;
100#endif
101 }
102 break;
103 }
104 }
105 }
106 else if (pNode->IsEndNode())
107 {
108 assert(nStartNode <= pNode->StartOfSectionIndex())(static_cast <bool> (nStartNode <= pNode->StartOfSectionIndex
()) ? void (0) : __assert_fail ("nStartNode <= pNode->StartOfSectionIndex()"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 108, __extension__ __PRETTY_FUNCTION__))
;
109 // fieldmark cannot overlap node section
110 n = pNode->StartOfSectionIndex();
111 }
112 else
113 {
114 assert(pNode->IsNoTextNode())(static_cast <bool> (pNode->IsNoTextNode()) ? void (
0) : __assert_fail ("pNode->IsNoTextNode()", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 114, __extension__ __PRETTY_FUNCTION__))
;
115 }
116 }
117 assert(ret)(static_cast <bool> (ret) ? void (0) : __assert_fail ("ret"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 117, __extension__ __PRETTY_FUNCTION__))
; // must have found it
118 return *ret;
119 }
120} // namespace sw::mark
121
122namespace
123{
124 void lcl_FixPosition(SwPosition& rPos)
125 {
126 // make sure the position has 1) the proper node, and 2) a proper index
127 SwTextNode* pTextNode = rPos.nNode.GetNode().GetTextNode();
128 if(pTextNode == nullptr && rPos.nContent.GetIndex() > 0)
129 {
130 SAL_INFO(do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " without proper TextNode"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " without proper TextNode") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
131 "sw.core",do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " without proper TextNode"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " without proper TextNode") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
132 "illegal position: " << rPos.nContent.GetIndex()do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " without proper TextNode"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " without proper TextNode") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
133 << " without proper TextNode")do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " without proper TextNode"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " without proper TextNode") == 1) { ::sal_detail_log
( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " without proper TextNode"), 0); } else {
::std::ostringstream sal_detail_stream; sal_detail_stream <<
"illegal position: " << rPos.nContent.GetIndex() <<
" without proper TextNode"; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "133" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
134 rPos.nContent.Assign(nullptr, 0);
135 }
136 else if(pTextNode != nullptr && rPos.nContent.GetIndex() > pTextNode->Len())
137 {
138 SAL_INFO(do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " is beyond " << pTextNode
->Len()) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len()) == 1)
{ ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
139 "sw.core",do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " is beyond " << pTextNode
->Len()) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len()) == 1)
{ ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
140 "illegal position: " << rPos.nContent.GetIndex()do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " is beyond " << pTextNode
->Len()) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len()) == 1)
{ ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
141 << " is beyond " << pTextNode->Len())do { if (true) { switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_INFO
, "sw.core")) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case
SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail::getResult
( ::sal::detail::StreamStart() << "illegal position: " <<
rPos.nContent.GetIndex() << " is beyond " << pTextNode
->Len()) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO
), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len()) == 1)
{ ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"
), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "illegal position: " << rPos.nContent
.GetIndex() << " is beyond " << pTextNode->Len
()), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "illegal position: " << rPos.nContent.GetIndex
() << " is beyond " << pTextNode->Len(); ::sal
::detail::log( (::SAL_DETAIL_LOG_LEVEL_INFO), ("sw.core"), ("/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
":" "141" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
142 rPos.nContent.Assign(pTextNode, pTextNode->Len());
143 }
144 }
145
146 void lcl_AssertFieldMarksSet(const Fieldmark& rField,
147 const sal_Unicode aStartMark,
148 const sal_Unicode aEndMark)
149 {
150 if (aEndMark != CH_TXT_ATR_FORMELEMENTu'\x0006')
151 {
152 SwPosition const& rStart(rField.GetMarkStart());
153 assert(rStart.nNode.GetNode().GetTextNode()->GetText()[rStart.nContent.GetIndex()] == aStartMark)(static_cast <bool> (rStart.nNode.GetNode().GetTextNode
()->GetText()[rStart.nContent.GetIndex()] == aStartMark) ?
void (0) : __assert_fail ("rStart.nNode.GetNode().GetTextNode()->GetText()[rStart.nContent.GetIndex()] == aStartMark"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 153, __extension__ __PRETTY_FUNCTION__))
; (void) rStart; (void) aStartMark;
154 SwPosition const sepPos(sw::mark::FindFieldSep(rField));
155 assert(sepPos.nNode.GetNode().GetTextNode()->GetText()[sepPos.nContent.GetIndex()] == CH_TXT_ATR_FIELDSEP)(static_cast <bool> (sepPos.nNode.GetNode().GetTextNode
()->GetText()[sepPos.nContent.GetIndex()] == u'\x0003') ? void
(0) : __assert_fail ("sepPos.nNode.GetNode().GetTextNode()->GetText()[sepPos.nContent.GetIndex()] == CH_TXT_ATR_FIELDSEP"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 155, __extension__ __PRETTY_FUNCTION__))
; (void) sepPos;
156 }
157 SwPosition const& rEnd(rField.GetMarkEnd());
158 assert(rEnd.nNode.GetNode().GetTextNode()->GetText()[rEnd.nContent.GetIndex() - 1] == aEndMark)(static_cast <bool> (rEnd.nNode.GetNode().GetTextNode()
->GetText()[rEnd.nContent.GetIndex() - 1] == aEndMark) ? void
(0) : __assert_fail ("rEnd.nNode.GetNode().GetTextNode()->GetText()[rEnd.nContent.GetIndex() - 1] == aEndMark"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 158, __extension__ __PRETTY_FUNCTION__))
; (void) rEnd;
159 }
160
161 void lcl_SetFieldMarks(Fieldmark& rField,
162 SwDoc& io_rDoc,
163 const sal_Unicode aStartMark,
164 const sal_Unicode aEndMark,
165 SwPosition const*const pSepPos)
166 {
167 io_rDoc.GetIDocumentUndoRedo().StartUndo(SwUndoId::UI_REPLACE, nullptr);
168 OUString startChar(aStartMark);
169 if (aEndMark != CH_TXT_ATR_FORMELEMENTu'\x0006'
170 && rField.GetMarkStart() == rField.GetMarkEnd())
171 {
172 // do only 1 InsertString call - to expand existing bookmarks at the
173 // position over the whole field instead of just aStartMark
174 startChar += OUStringChar(CH_TXT_ATR_FIELDSEPu'\x0003') + OUStringChar(aEndMark);
175 }
176
177 SwPosition start = rField.GetMarkStart();
178 if (aEndMark != CH_TXT_ATR_FORMELEMENTu'\x0006')
179 {
180 SwPaM aStartPaM(start);
181 io_rDoc.getIDocumentContentOperations().InsertString(aStartPaM, startChar);
182 start.nContent -= startChar.getLength(); // restore, it was moved by InsertString
183 // do not manipulate via reference directly but call SetMarkStartPos
184 // which works even if start and end pos were the same
185 rField.SetMarkStartPos( start );
186 SwPosition& rEnd = rField.GetMarkEnd(); // note: retrieve after
187 // setting start, because if start==end it can go stale, see SetMarkPos()
188 assert(pSepPos == nullptr || (start < *pSepPos && *pSepPos <= rEnd))(static_cast <bool> (pSepPos == nullptr || (start < *
pSepPos && *pSepPos <= rEnd)) ? void (0) : __assert_fail
("pSepPos == nullptr || (start < *pSepPos && *pSepPos <= rEnd)"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 188, __extension__ __PRETTY_FUNCTION__))
;
189 if (startChar.getLength() == 1)
190 {
191 *aStartPaM.GetPoint() = pSepPos ? *pSepPos : rEnd;
192 io_rDoc.getIDocumentContentOperations().InsertString(aStartPaM, OUString(CH_TXT_ATR_FIELDSEPu'\x0003'));
193 if (!pSepPos || rEnd < *pSepPos)
194 { // rEnd is not moved automatically if it's same as insert pos
195 ++rEnd.nContent;
196 }
197 }
198 assert(pSepPos == nullptr || (start < *pSepPos && *pSepPos <= rEnd))(static_cast <bool> (pSepPos == nullptr || (start < *
pSepPos && *pSepPos <= rEnd)) ? void (0) : __assert_fail
("pSepPos == nullptr || (start < *pSepPos && *pSepPos <= rEnd)"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 198, __extension__ __PRETTY_FUNCTION__))
;
199 }
200 else
201 {
202 assert(pSepPos == nullptr)(static_cast <bool> (pSepPos == nullptr) ? void (0) : __assert_fail
("pSepPos == nullptr", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 202, __extension__ __PRETTY_FUNCTION__))
;
203 }
204
205 SwPosition& rEnd = rField.GetMarkEnd();
206 if (aEndMark && startChar.getLength() == 1)
207 {
208 SwPaM aEndPaM(rEnd);
209 io_rDoc.getIDocumentContentOperations().InsertString(aEndPaM, OUString(aEndMark));
210 ++rEnd.nContent;
211 }
212 lcl_AssertFieldMarksSet(rField, aStartMark, aEndMark);
213
214 io_rDoc.GetIDocumentUndoRedo().EndUndo(SwUndoId::UI_REPLACE, nullptr);
215 }
216
217 void lcl_RemoveFieldMarks(const Fieldmark& rField,
218 SwDoc& io_rDoc,
219 const sal_Unicode aStartMark,
220 const sal_Unicode aEndMark)
221 {
222 io_rDoc.GetIDocumentUndoRedo().StartUndo(SwUndoId::UI_REPLACE, nullptr);
223
224 const SwPosition& rStart = rField.GetMarkStart();
225 SwTextNode const*const pStartTextNode = rStart.nNode.GetNode().GetTextNode();
226 assert(pStartTextNode)(static_cast <bool> (pStartTextNode) ? void (0) : __assert_fail
("pStartTextNode", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 226, __extension__ __PRETTY_FUNCTION__))
;
227 if (aEndMark != CH_TXT_ATR_FORMELEMENTu'\x0006')
228 {
229 (void) pStartTextNode;
230 // check this before start / end because of the +1 / -1 ...
231 SwPosition const sepPos(sw::mark::FindFieldSep(rField));
232 io_rDoc.GetDocumentContentOperationsManager().DeleteDummyChar(rStart, aStartMark);
233 io_rDoc.GetDocumentContentOperationsManager().DeleteDummyChar(sepPos, CH_TXT_ATR_FIELDSEPu'\x0003');
234 }
235
236 const SwPosition& rEnd = rField.GetMarkEnd();
237 SwTextNode *const pEndTextNode = rEnd.nNode.GetNode().GetTextNode();
238 assert(pEndTextNode)(static_cast <bool> (pEndTextNode) ? void (0) : __assert_fail
("pEndTextNode", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 238, __extension__ __PRETTY_FUNCTION__))
;
239 const sal_Int32 nEndPos = (rEnd == rStart)
240 ? rEnd.nContent.GetIndex()
241 : rEnd.nContent.GetIndex() - 1;
242 assert(pEndTextNode->GetText()[nEndPos] == aEndMark)(static_cast <bool> (pEndTextNode->GetText()[nEndPos
] == aEndMark) ? void (0) : __assert_fail ("pEndTextNode->GetText()[nEndPos] == aEndMark"
, "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 242, __extension__ __PRETTY_FUNCTION__))
;
243 SwPosition const aEnd(*pEndTextNode, nEndPos);
244 io_rDoc.GetDocumentContentOperationsManager().DeleteDummyChar(aEnd, aEndMark);
245
246 io_rDoc.GetIDocumentUndoRedo().EndUndo(SwUndoId::UI_REPLACE, nullptr);
247 }
248
249 auto InvalidatePosition(SwPosition const& rPos) -> void
250 {
251 SwUpdateAttr const hint(rPos.nContent.GetIndex(), rPos.nContent.GetIndex(), 0);
252 rPos.nNode.GetNode().GetTextNode()->NotifyClients(nullptr, &hint);
253 }
254}
255
256namespace sw::mark
257{
258 MarkBase::MarkBase(const SwPaM& aPaM,
259 const OUString& rName)
260 : m_pPos1(new SwPosition(*(aPaM.GetPoint())))
261 , m_aName(rName)
262 {
263 m_pPos1->nContent.SetMark(this);
264 lcl_FixPosition(*m_pPos1);
265 if (aPaM.HasMark() && (*aPaM.GetMark() != *aPaM.GetPoint()))
266 {
267 MarkBase::SetOtherMarkPos(*(aPaM.GetMark()));
268 lcl_FixPosition(*m_pPos2);
269 }
270 }
271
272 // For fieldmarks, the CH_TXT_ATR_FIELDSTART and CH_TXT_ATR_FIELDEND
273 // themselves are part of the covered range. This is guaranteed by
274 // TextFieldmark::InitDoc/lcl_AssureFieldMarksSet.
275 bool MarkBase::IsCoveringPosition(const SwPosition& rPos) const
276 {
277 return GetMarkStart() <= rPos && rPos < GetMarkEnd();
278 }
279
280 void MarkBase::SetMarkPos(const SwPosition& rNewPos)
281 {
282 std::make_unique<SwPosition>(rNewPos).swap(m_pPos1);
283 m_pPos1->nContent.SetMark(this);
284 }
285
286 void MarkBase::SetOtherMarkPos(const SwPosition& rNewPos)
287 {
288 std::make_unique<SwPosition>(rNewPos).swap(m_pPos2);
289 m_pPos2->nContent.SetMark(this);
290 }
291
292 OUString MarkBase::ToString( ) const
293 {
294 return "Mark: ( Name, [ Node1, Index1 ] ): ( " + m_aName + ", [ "
295 + OUString::number( GetMarkPos().nNode.GetIndex( ) ) + ", "
296 + OUString::number( GetMarkPos().nContent.GetIndex( ) ) + " ] )";
297 }
298
299 void MarkBase::dumpAsXml(xmlTextWriterPtr pWriter) const
300 {
301 xmlTextWriterStartElement(pWriter, BAD_CAST(xmlChar *)("MarkBase"));
302 xmlTextWriterWriteAttribute(pWriter, BAD_CAST(xmlChar *)("name"), BAD_CAST(xmlChar *)(m_aName.toUtf8().getStr()));
303 xmlTextWriterStartElement(pWriter, BAD_CAST(xmlChar *)("markPos"));
304 GetMarkPos().dumpAsXml(pWriter);
305 xmlTextWriterEndElement(pWriter);
306 if (IsExpanded())
307 {
308 xmlTextWriterStartElement(pWriter, BAD_CAST(xmlChar *)("otherMarkPos"));
309 GetOtherMarkPos().dumpAsXml(pWriter);
310 xmlTextWriterEndElement(pWriter);
311 }
312 xmlTextWriterEndElement(pWriter);
313 }
314
315 MarkBase::~MarkBase()
316 { }
317
318 OUString MarkBase::GenerateNewName(const OUString& rPrefix)
319 {
320 static bool bHack = (getenv("LIBO_ONEWAY_STABLE_ODF_EXPORT") != nullptr);
321
322 if (bHack)
323 {
324 static sal_Int64 nIdCounter = SAL_CONST_INT64(6000000000)6000000000l;
325 return rPrefix + OUString::number(nIdCounter++);
326 }
327 else
328 {
329 static OUString sUniquePostfix;
330 static sal_Int32 nCount = SAL_MAX_INT32((sal_Int32) 0x7FFFFFFF);
331 if(nCount == SAL_MAX_INT32((sal_Int32) 0x7FFFFFFF))
332 {
333 unsigned int const n(comphelper::rng::uniform_uint_distribution(0,
334 std::numeric_limits<unsigned int>::max()));
335 sUniquePostfix = "_" + OUString::number(n);
336 nCount = 0;
337 }
338 // putting the counter in front of the random parts will speed up string comparisons
339 return rPrefix + OUString::number(nCount++) + sUniquePostfix;
340 }
341 }
342
343 void MarkBase::SwClientNotify(const SwModify&, const SfxHint& rHint)
344 {
345 CallSwClientNotify(rHint);
346 auto pLegacy = dynamic_cast<const sw::LegacyModifyHint*>(&rHint);
347 if(pLegacy && RES_REMOVE_UNO_OBJECT == pLegacy->GetWhich())
348 { // invalidate cached uno object
349 SetXBookmark(uno::Reference<text::XTextContent>(nullptr));
350 }
351 }
352
353 auto MarkBase::InvalidateFrames() -> void
354 {
355 }
356
357 NavigatorReminder::NavigatorReminder(const SwPaM& rPaM)
358 : MarkBase(rPaM, MarkBase::GenerateNewName("__NavigatorReminder__"))
359 { }
360
361 UnoMark::UnoMark(const SwPaM& aPaM)
362 : MarkBase(aPaM, MarkBase::GenerateNewName("__UnoMark__"))
363 { }
364
365 DdeBookmark::DdeBookmark(const SwPaM& aPaM)
366 : MarkBase(aPaM, MarkBase::GenerateNewName("__DdeLink__"))
367 { }
368
369 void DdeBookmark::SetRefObject(SwServerObject* pObj)
370 {
371 m_aRefObj = pObj;
372 }
373
374 void DdeBookmark::DeregisterFromDoc(SwDoc& rDoc)
375 {
376 if(m_aRefObj.is())
377 rDoc.getIDocumentLinksAdministration().GetLinkManager().RemoveServer(m_aRefObj.get());
378 }
379
380 DdeBookmark::~DdeBookmark()
381 {
382 if( m_aRefObj.is() )
383 {
384 if(m_aRefObj->HasDataLinks())
385 {
386 ::sfx2::SvLinkSource* p = m_aRefObj.get();
387 p->SendDataChanged();
388 }
389 m_aRefObj->SetNoServer();
390 }
391 }
392
393 Bookmark::Bookmark(const SwPaM& aPaM,
394 const vcl::KeyCode& rCode,
395 const OUString& rName)
396 : DdeBookmark(aPaM)
397 , ::sfx2::Metadatable()
398 , m_aCode(rCode)
399 , m_bHidden(false)
400 {
401 m_aName = rName;
402 }
403
404 void Bookmark::InitDoc(SwDoc& io_rDoc,
405 sw::mark::InsertMode const, SwPosition const*const)
406 {
407 if (io_rDoc.GetIDocumentUndoRedo().DoesUndo())
408 {
409 io_rDoc.GetIDocumentUndoRedo().AppendUndo(
410 std::make_unique<SwUndoInsBookmark>(*this));
411 }
412 io_rDoc.getIDocumentState().SetModified();
413 InvalidateFrames();
414 }
415
416 void Bookmark::DeregisterFromDoc(SwDoc& io_rDoc)
417 {
418 DdeBookmark::DeregisterFromDoc(io_rDoc);
419
420 if (io_rDoc.GetIDocumentUndoRedo().DoesUndo())
421 {
422 io_rDoc.GetIDocumentUndoRedo().AppendUndo(
423 std::make_unique<SwUndoDeleteBookmark>(*this));
424 }
425 io_rDoc.getIDocumentState().SetModified();
426 InvalidateFrames();
427 }
428
429 // invalidate text frames in case it's hidden or Formatting Marks enabled
430 auto Bookmark::InvalidateFrames() -> void
431 {
432 InvalidatePosition(GetMarkPos());
433 if (IsExpanded())
434 {
435 InvalidatePosition(GetOtherMarkPos());
436 }
437 }
438
439 void Bookmark::Hide(bool const isHide)
440 {
441 if (isHide != m_bHidden)
442 {
443 m_bHidden = isHide;
444 InvalidateFrames();
445 }
446 }
447
448 void Bookmark::SetHideCondition(OUString const& rHideCondition)
449 {
450 if (m_sHideCondition != rHideCondition)
451 {
452 m_sHideCondition = rHideCondition;
453 InvalidateFrames();
454 }
455 }
456
457 ::sfx2::IXmlIdRegistry& Bookmark::GetRegistry()
458 {
459 SwDoc& rDoc( GetMarkPos().GetDoc() );
460 return rDoc.GetXmlIdRegistry();
461 }
462
463 bool Bookmark::IsInClipboard() const
464 {
465 SwDoc& rDoc( GetMarkPos().GetDoc() );
466 return rDoc.IsClipBoard();
467 }
468
469 bool Bookmark::IsInUndo() const
470 {
471 return false;
472 }
473
474 bool Bookmark::IsInContent() const
475 {
476 SwDoc& rDoc( GetMarkPos().GetDoc() );
477 return !rDoc.IsInHeaderFooter( GetMarkPos().nNode );
478 }
479
480 uno::Reference< rdf::XMetadatable > Bookmark::MakeUnoObject()
481 {
482 SwDoc& rDoc( GetMarkPos().GetDoc() );
483 const uno::Reference< rdf::XMetadatable> xMeta(
484 SwXBookmark::CreateXBookmark(rDoc, this), uno::UNO_QUERY);
485 return xMeta;
486 }
487
488 Fieldmark::Fieldmark(const SwPaM& rPaM)
489 : MarkBase(rPaM, MarkBase::GenerateNewName("__Fieldmark__"))
490 {
491 if(!IsExpanded())
492 SetOtherMarkPos(GetMarkPos());
493 }
494
495 void Fieldmark::SetMarkStartPos( const SwPosition& rNewStartPos )
496 {
497 if ( GetMarkPos( ) <= GetOtherMarkPos( ) )
498 return SetMarkPos( rNewStartPos );
499 else
500 return SetOtherMarkPos( rNewStartPos );
501 }
502
503 void Fieldmark::SetMarkEndPos( const SwPosition& rNewEndPos )
504 {
505 if ( GetMarkPos( ) <= GetOtherMarkPos( ) )
506 return SetOtherMarkPos( rNewEndPos );
507 else
508 return SetMarkPos( rNewEndPos );
509 }
510
511 OUString Fieldmark::ToString( ) const
512 {
513 return "Fieldmark: ( Name, Type, [ Nd1, Id1 ], [ Nd2, Id2 ] ): ( " + m_aName + ", "
514 + m_aFieldname + ", [ " + OUString::number( GetMarkPos().nNode.GetIndex( ) )
515 + ", " + OUString::number( GetMarkPos( ).nContent.GetIndex( ) ) + " ], ["
516 + OUString::number( GetOtherMarkPos().nNode.GetIndex( ) ) + ", "
517 + OUString::number( GetOtherMarkPos( ).nContent.GetIndex( ) ) + " ] ) ";
518 }
519
520 void Fieldmark::Invalidate( )
521 {
522 // TODO: Does exist a better solution to trigger a format of the
523 // fieldmark portion? If yes, please use it.
524 SwPaM aPaM( GetMarkPos(), GetOtherMarkPos() );
525 aPaM.InvalidatePaM();
526 }
527
528 void Fieldmark::dumpAsXml(xmlTextWriterPtr pWriter) const
529 {
530 xmlTextWriterStartElement(pWriter, BAD_CAST(xmlChar *)("Fieldmark"));
531 xmlTextWriterWriteAttribute(pWriter, BAD_CAST(xmlChar *)("fieldname"), BAD_CAST(xmlChar *)(m_aFieldname.toUtf8().getStr()));
532 xmlTextWriterWriteAttribute(pWriter, BAD_CAST(xmlChar *)("fieldHelptext"), BAD_CAST(xmlChar *)(m_aFieldHelptext.toUtf8().getStr()));
533 MarkBase::dumpAsXml(pWriter);
534 xmlTextWriterStartElement(pWriter, BAD_CAST(xmlChar *)("parameters"));
535 for (auto& rParam : m_vParams)
536 {
537 xmlTextWriterStartElement(pWriter, BAD_CAST(xmlChar *)("parameter"));
538 xmlTextWriterWriteAttribute(pWriter, BAD_CAST(xmlChar *)("name"), BAD_CAST(xmlChar *)(rParam.first.toUtf8().getStr()));
539 xmlTextWriterWriteAttribute(pWriter, BAD_CAST(xmlChar *)("value"), BAD_CAST(xmlChar *)(comphelper::anyToString(rParam.second).toUtf8().getStr()));
540 xmlTextWriterEndElement(pWriter);
541 }
542 xmlTextWriterEndElement(pWriter);
543 xmlTextWriterEndElement(pWriter);
544 }
545
546 TextFieldmark::TextFieldmark(const SwPaM& rPaM, const OUString& rName)
547 : Fieldmark(rPaM)
548 {
549 if ( !rName.isEmpty() )
550 m_aName = rName;
551 }
552
553 void TextFieldmark::InitDoc(SwDoc& io_rDoc,
554 sw::mark::InsertMode const eMode, SwPosition const*const pSepPos)
555 {
556 if (eMode == sw::mark::InsertMode::New)
557 {
558 lcl_SetFieldMarks(*this, io_rDoc, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FIELDENDu'\x0008', pSepPos);
559 // no need to invalidate text frames here, the insertion of the
560 // CH_TXT_ATR already invalidates
561 }
562 else
563 {
564 lcl_AssertFieldMarksSet(*this, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FIELDENDu'\x0008');
565 }
566 }
567
568 void TextFieldmark::ReleaseDoc(SwDoc& rDoc)
569 {
570 IDocumentUndoRedo & rIDUR(rDoc.GetIDocumentUndoRedo());
571 if (rIDUR.DoesUndo())
572 {
573 rIDUR.AppendUndo(std::make_unique<SwUndoDelTextFieldmark>(*this));
574 }
575 ::sw::UndoGuard const ug(rIDUR); // prevent SwUndoDeletes
576 lcl_RemoveFieldMarks(*this, rDoc, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FIELDENDu'\x0008');
577 }
578
579 NonTextFieldmark::NonTextFieldmark(const SwPaM& rPaM)
580 : Fieldmark(rPaM)
581 { }
582
583 void NonTextFieldmark::InitDoc(SwDoc& io_rDoc,
584 sw::mark::InsertMode const eMode, SwPosition const*const pSepPos)
585 {
586 assert(pSepPos == nullptr)(static_cast <bool> (pSepPos == nullptr) ? void (0) : __assert_fail
("pSepPos == nullptr", "/home/maarten/src/libreoffice/core/sw/source/core/crsr/bookmrk.cxx"
, 586, __extension__ __PRETTY_FUNCTION__))
;
587 if (eMode == sw::mark::InsertMode::New)
588 {
589 lcl_SetFieldMarks(*this, io_rDoc, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FORMELEMENTu'\x0006', pSepPos);
590
591 // For some reason the end mark is moved from 1 by the Insert:
592 // we don't want this for checkboxes
593 SwPosition aNewEndPos = GetMarkEnd();
594 aNewEndPos.nContent--;
595 SetMarkEndPos( aNewEndPos );
596 }
597 else
598 {
599 lcl_AssertFieldMarksSet(*this, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FORMELEMENTu'\x0006');
600 }
601 }
602
603 void NonTextFieldmark::ReleaseDoc(SwDoc& rDoc)
604 {
605 IDocumentUndoRedo & rIDUR(rDoc.GetIDocumentUndoRedo());
606 if (rIDUR.DoesUndo())
607 {
608 rIDUR.AppendUndo(std::make_unique<SwUndoDelNoTextFieldmark>(*this));
609 }
610 ::sw::UndoGuard const ug(rIDUR); // prevent SwUndoDeletes
611 lcl_RemoveFieldMarks(*this, rDoc,
612 CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FORMELEMENTu'\x0006');
613 }
614
615
616 CheckboxFieldmark::CheckboxFieldmark(const SwPaM& rPaM)
617 : NonTextFieldmark(rPaM)
618 { }
619
620 void CheckboxFieldmark::SetChecked(bool checked)
621 {
622 if ( IsChecked() != checked )
623 {
624 (*GetParameters())[OUString(ODF_FORMCHECKBOX_RESULT"Checkbox_Checked")] <<= checked;
625 // mark document as modified
626 SwDoc& rDoc( GetMarkPos().GetDoc() );
627 rDoc.getIDocumentState().SetModified();
628 }
629 }
630
631 bool CheckboxFieldmark::IsChecked() const
632 {
633 bool bResult = false;
634 parameter_map_t::const_iterator pResult = GetParameters()->find(OUString(ODF_FORMCHECKBOX_RESULT"Checkbox_Checked"));
635 if(pResult != GetParameters()->end())
636 pResult->second >>= bResult;
637 return bResult;
638 }
639
640 FieldmarkWithDropDownButton::FieldmarkWithDropDownButton(const SwPaM& rPaM)
641 : NonTextFieldmark(rPaM)
642 , m_pButton(nullptr)
643 {
644 }
645
646 FieldmarkWithDropDownButton::~FieldmarkWithDropDownButton()
647 {
648 m_pButton.disposeAndClear();
649 }
650
651 void FieldmarkWithDropDownButton::HideButton()
652 {
653 if(m_pButton)
654 m_pButton->Show(false);
655 }
656
657 void FieldmarkWithDropDownButton::RemoveButton()
658 {
659 if(m_pButton)
2
Taking true branch
660 m_pButton.disposeAndClear();
3
Calling 'VclPtr::disposeAndClear'
661 }
662
663 DropDownFieldmark::DropDownFieldmark(const SwPaM& rPaM)
664 : FieldmarkWithDropDownButton(rPaM)
665 {
666 }
667
668 DropDownFieldmark::~DropDownFieldmark()
669 {
670 SendLOKMessage("hide");
671 }
672
673 void DropDownFieldmark::ShowButton(SwEditWin* pEditWin)
674 {
675 if(pEditWin)
676 {
677 if(!m_pButton)
678 m_pButton = VclPtr<DropDownFormFieldButton>::Create(pEditWin, *this);
679 m_pButton->CalcPosAndSize(m_aPortionPaintArea);
680 m_pButton->Show();
681 SendLOKMessage("show");
682 }
683 }
684
685 void DropDownFieldmark::HideButton()
686 {
687 SendLOKMessage("hide");
688 FieldmarkWithDropDownButton::HideButton();
689 }
690
691 void DropDownFieldmark::RemoveButton()
692 {
693 SendLOKMessage("hide");
694 FieldmarkWithDropDownButton::RemoveButton();
1
Calling 'FieldmarkWithDropDownButton::RemoveButton'
695 }
696
697 void DropDownFieldmark::SetPortionPaintArea(const SwRect& rPortionPaintArea)
698 {
699 m_aPortionPaintArea = rPortionPaintArea;
700 if(m_pButton)
701 {
702 m_pButton->Show();
703 m_pButton->CalcPosAndSize(m_aPortionPaintArea);
704 SendLOKMessage("show");
705 }
706 }
707
708 void DropDownFieldmark::SendLOKMessage(const OString& sAction)
709 {
710 const SfxViewShell* pViewShell = SfxViewShell::Current();
711 if (pViewShell && pViewShell->isLOKMobilePhone())
712 {
713 return;
714 }
715
716 if (!comphelper::LibreOfficeKit::isActive())
717 return;
718
719 if (!m_pButton)
720 return;
721
722 SwEditWin* pEditWin = dynamic_cast<SwEditWin*>(m_pButton->GetParent());
723 if (!pEditWin)
724 return;
725
726 OStringBuffer sPayload;
727 if (sAction == "show")
728 {
729 if(m_aPortionPaintArea.IsEmpty())
730 return;
731
732 sPayload = OStringLiteral("{\"action\": \"show\","
733 " \"type\": \"drop-down\", \"textArea\": \"") +
734 m_aPortionPaintArea.SVRect().toString() + "\",";
735 // Add field params to the message
736 sPayload.append(" \"params\": { \"items\": [");
737
738 // List items
739 auto pParameters = this->GetParameters();
740 auto pListEntriesIter = pParameters->find(ODF_FORMDROPDOWN_LISTENTRY"Dropdown_ListEntry");
741 css::uno::Sequence<OUString> vListEntries;
742 if (pListEntriesIter != pParameters->end())
743 {
744 pListEntriesIter->second >>= vListEntries;
745 for (const OUString& sItem : std::as_const(vListEntries))
746 sPayload.append("\"" + OUStringToOString(sItem, RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76))) + "\", ");
747 sPayload.setLength(sPayload.getLength() - 2);
748 }
749 sPayload.append("], ");
750
751 // Selected item
752 auto pSelectedItemIter = pParameters->find(ODF_FORMDROPDOWN_RESULT"Dropdown_Selected");
753 sal_Int32 nSelection = -1;
754 if (pSelectedItemIter != pParameters->end())
755 {
756 pSelectedItemIter->second >>= nSelection;
757 }
758 sPayload.append("\"selected\": \"" + OString::number(nSelection) + "\", ");
759
760 // Placeholder text
761 sPayload.append("\"placeholderText\": \"" + OUStringToOString(SwResId(STR_DROP_DOWN_EMPTY_LISTreinterpret_cast<char const *>("STR_DROP_DOWN_EMPTY_LIST"
"\004" u8"No Item specified")
), RTL_TEXTENCODING_UTF8(((rtl_TextEncoding) 76))) + "\"}}");
762 }
763 else
764 {
765 sPayload = "{\"action\": \"hide\", \"type\": \"drop-down\"}";
766 }
767 if (sPayload.toString() != m_sLastSentLOKMsg) {
768 m_sLastSentLOKMsg = sPayload.toString();
769 pEditWin->GetView().GetWrtShell().GetSfxViewShell()->libreOfficeKitViewCallback(LOK_CALLBACK_FORM_FIELD_BUTTON, m_sLastSentLOKMsg.getStr());
770 }
771 }
772
773 DateFieldmark::DateFieldmark(const SwPaM& rPaM)
774 : FieldmarkWithDropDownButton(rPaM)
775 , m_pNumberFormatter(nullptr)
776 , m_pDocumentContentOperationsManager(nullptr)
777 {
778 }
779
780 DateFieldmark::~DateFieldmark()
781 {
782 }
783
784 void DateFieldmark::InitDoc(SwDoc& io_rDoc,
785 sw::mark::InsertMode eMode, SwPosition const*const pSepPos)
786 {
787 m_pNumberFormatter = io_rDoc.GetNumberFormatter();
788 m_pDocumentContentOperationsManager = &io_rDoc.GetDocumentContentOperationsManager();
789 if (eMode == sw::mark::InsertMode::New)
790 {
791 lcl_SetFieldMarks(*this, io_rDoc, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FIELDENDu'\x0008', pSepPos);
792 }
793 else
794 {
795 lcl_AssertFieldMarksSet(*this, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FIELDENDu'\x0008');
796 }
797 }
798
799 void DateFieldmark::ReleaseDoc(SwDoc& rDoc)
800 {
801 IDocumentUndoRedo & rIDUR(rDoc.GetIDocumentUndoRedo());
802 if (rIDUR.DoesUndo())
803 {
804 // TODO does this need a 3rd Undo class?
805 rIDUR.AppendUndo(std::make_unique<SwUndoDelTextFieldmark>(*this));
806 }
807 ::sw::UndoGuard const ug(rIDUR); // prevent SwUndoDeletes
808 lcl_RemoveFieldMarks(*this, rDoc, CH_TXT_ATR_FIELDSTARTu'\x0007', CH_TXT_ATR_FIELDENDu'\x0008');
809 }
810
811 void DateFieldmark::ShowButton(SwEditWin* pEditWin)
812 {
813 if(pEditWin)
814 {
815 if(!m_pButton)
816 m_pButton = VclPtr<DateFormFieldButton>::Create(pEditWin, *this, m_pNumberFormatter);
817 SwRect aPaintArea(m_aPaintAreaStart.TopLeft(), m_aPaintAreaEnd.BottomRight());
818 m_pButton->CalcPosAndSize(aPaintArea);
819 m_pButton->Show();
820 }
821 }
822
823 void DateFieldmark::SetPortionPaintAreaStart(const SwRect& rPortionPaintArea)
824 {
825 if (rPortionPaintArea.IsEmpty())
826 return;
827
828 m_aPaintAreaStart = rPortionPaintArea;
829 InvalidateCurrentDateParam();
830 }
831
832 void DateFieldmark::SetPortionPaintAreaEnd(const SwRect& rPortionPaintArea)
833 {
834 if (rPortionPaintArea.IsEmpty())
835 return;
836
837 if(m_aPaintAreaEnd == rPortionPaintArea &&
838 m_pButton && m_pButton->IsVisible())
839 return;
840
841 m_aPaintAreaEnd = rPortionPaintArea;
842 if(m_pButton)
843 {
844 m_pButton->Show();
845 SwRect aPaintArea(m_aPaintAreaStart.TopLeft(), m_aPaintAreaEnd.BottomRight());
846 m_pButton->CalcPosAndSize(aPaintArea);
847 m_pButton->Invalidate();
848 }
849 InvalidateCurrentDateParam();
850 }
851
852 OUString DateFieldmark::GetContent() const
853 {
854 const SwTextNode* const pTextNode = GetMarkEnd().nNode.GetNode().GetTextNode();
855 SwPosition const sepPos(sw::mark::FindFieldSep(*this));
856 const sal_Int32 nStart(sepPos.nContent.GetIndex());
857 const sal_Int32 nEnd (GetMarkEnd().nContent.GetIndex());
858
859 OUString sContent;
860 if(nStart + 1 < pTextNode->GetText().getLength() && nEnd <= pTextNode->GetText().getLength() &&
861 nEnd > nStart + 2)
862 sContent = pTextNode->GetText().copy(nStart + 1, nEnd - nStart - 2);
863 return sContent;
864 }
865
866 void DateFieldmark::ReplaceContent(const OUString& sNewContent)
867 {
868 if(!m_pDocumentContentOperationsManager)
869 return;
870
871 const SwTextNode* const pTextNode = GetMarkEnd().nNode.GetNode().GetTextNode();
872 SwPosition const sepPos(sw::mark::FindFieldSep(*this));
873 const sal_Int32 nStart(sepPos.nContent.GetIndex());
874 const sal_Int32 nEnd (GetMarkEnd().nContent.GetIndex());
875
876 if(nStart + 1 < pTextNode->GetText().getLength() && nEnd <= pTextNode->GetText().getLength() &&
877 nEnd > nStart + 2)
878 {
879 SwPaM aFieldPam(GetMarkStart().nNode, nStart + 1,
880 GetMarkStart().nNode, nEnd - 1);
881 m_pDocumentContentOperationsManager->ReplaceRange(aFieldPam, sNewContent, false);
882 }
883 else
884 {
885 SwPaM aFieldStartPam(GetMarkStart().nNode, nStart + 1);
886 m_pDocumentContentOperationsManager->InsertString(aFieldStartPam, sNewContent);
887 }
888
889 }
890
891 std::pair<bool, double> DateFieldmark::GetCurrentDate() const
892 {
893 // Check current date param first
894 std::pair<bool, double> aResult = ParseCurrentDateParam();
895 if(aResult.first)
896 return aResult;
897
898 const sw::mark::IFieldmark::parameter_map_t* pParameters = GetParameters();
899 bool bFoundValidDate = false;
900 double dCurrentDate = 0;
901 OUString sDateFormat;
902 auto pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT"DateField_DateFormat");
903 if (pResult != pParameters->end())
904 {
905 pResult->second >>= sDateFormat;
906 }
907
908 OUString sLang;
909 pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT_LANGUAGE"DateField_DateFormat_Language");
910 if (pResult != pParameters->end())
911 {
912 pResult->second >>= sLang;
913 }
914
915 // Get current content of the field
916 OUString sContent = GetContent();
917
918 sal_uInt32 nFormat = m_pNumberFormatter->GetEntryKey(sDateFormat, LanguageTag(sLang).getLanguageType());
919 if (nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
920 {
921 sal_Int32 nCheckPos = 0;
922 SvNumFormatType nType;
923 m_pNumberFormatter->PutEntry(sDateFormat,
924 nCheckPos,
925 nType,
926 nFormat,
927 LanguageTag(sLang).getLanguageType());
928 }
929
930 if (nFormat != NUMBERFORMAT_ENTRY_NOT_FOUND)
931 {
932 bFoundValidDate = m_pNumberFormatter->IsNumberFormat(sContent, nFormat, dCurrentDate);
933 }
934 return std::pair<bool, double>(bFoundValidDate, dCurrentDate);
935 }
936
937 void DateFieldmark::SetCurrentDate(double fDate)
938 {
939 // Replace current content with the selected date
940 ReplaceContent(GetDateInCurrentDateFormat(fDate));
941
942 // Also save the current date in a standard format
943 sw::mark::IFieldmark::parameter_map_t* pParameters = GetParameters();
944 (*pParameters)[ODF_FORMDATE_CURRENTDATE"DateField_CurrentDate"] <<= GetDateInStandardDateFormat(fDate);
945 }
946
947 OUString DateFieldmark::GetDateInStandardDateFormat(double fDate) const
948 {
949 OUString sCurrentDate;
950 sal_uInt32 nFormat = m_pNumberFormatter->GetEntryKey(ODF_FORMDATE_CURRENTDATE_FORMAT"YYYY-MM-DD", ODF_FORMDATE_CURRENTDATE_LANGUAGELanguageType(0x0409));
951 if (nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
952 {
953 sal_Int32 nCheckPos = 0;
954 SvNumFormatType nType;
955 OUString sFormat = ODF_FORMDATE_CURRENTDATE_FORMAT"YYYY-MM-DD";
956 m_pNumberFormatter->PutEntry(sFormat,
957 nCheckPos,
958 nType,
959 nFormat,
960 ODF_FORMDATE_CURRENTDATE_LANGUAGELanguageType(0x0409));
961 }
962
963 if (nFormat != NUMBERFORMAT_ENTRY_NOT_FOUND)
964 {
965 const Color* pCol = nullptr;
966 m_pNumberFormatter->GetOutputString(fDate, nFormat, sCurrentDate, &pCol, false);
967 }
968 return sCurrentDate;
969 }
970
971 std::pair<bool, double> DateFieldmark::ParseCurrentDateParam() const
972 {
973 bool bFoundValidDate = false;
974 double dCurrentDate = 0;
975
976 const sw::mark::IFieldmark::parameter_map_t* pParameters = GetParameters();
977 auto pResult = pParameters->find(ODF_FORMDATE_CURRENTDATE"DateField_CurrentDate");
978 OUString sCurrentDate;
979 if (pResult != pParameters->end())
980 {
981 pResult->second >>= sCurrentDate;
982 }
983 if(!sCurrentDate.isEmpty())
984 {
985 sal_uInt32 nFormat = m_pNumberFormatter->GetEntryKey(ODF_FORMDATE_CURRENTDATE_FORMAT"YYYY-MM-DD", ODF_FORMDATE_CURRENTDATE_LANGUAGELanguageType(0x0409));
986 if (nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
987 {
988 sal_Int32 nCheckPos = 0;
989 SvNumFormatType nType;
990 OUString sFormat = ODF_FORMDATE_CURRENTDATE_FORMAT"YYYY-MM-DD";
991 m_pNumberFormatter->PutEntry(sFormat,
992 nCheckPos,
993 nType,
994 nFormat,
995 ODF_FORMDATE_CURRENTDATE_LANGUAGELanguageType(0x0409));
996 }
997
998 if(nFormat != NUMBERFORMAT_ENTRY_NOT_FOUND)
999 {
1000 bFoundValidDate = m_pNumberFormatter->IsNumberFormat(sCurrentDate, nFormat, dCurrentDate);
1001 }
1002 }
1003 return std::pair<bool, double>(bFoundValidDate, dCurrentDate);
1004 }
1005
1006
1007 OUString DateFieldmark::GetDateInCurrentDateFormat(double fDate) const
1008 {
1009 // Get current date format and language
1010 OUString sDateFormat;
1011 const sw::mark::IFieldmark::parameter_map_t* pParameters = GetParameters();
1012 auto pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT"DateField_DateFormat");
1013 if (pResult != pParameters->end())
1014 {
1015 pResult->second >>= sDateFormat;
1016 }
1017
1018 OUString sLang;
1019 pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT_LANGUAGE"DateField_DateFormat_Language");
1020 if (pResult != pParameters->end())
1021 {
1022 pResult->second >>= sLang;
1023 }
1024
1025 // Fill the content with the specified format
1026 OUString sCurrentContent;
1027 sal_uInt32 nFormat = m_pNumberFormatter->GetEntryKey(sDateFormat, LanguageTag(sLang).getLanguageType());
1028 if (nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
1029 {
1030 sal_Int32 nCheckPos = 0;
1031 SvNumFormatType nType;
1032 OUString sFormat = sDateFormat;
1033 m_pNumberFormatter->PutEntry(sFormat,
1034 nCheckPos,
1035 nType,
1036 nFormat,
1037 LanguageTag(sLang).getLanguageType());
1038 }
1039
1040 if (nFormat != NUMBERFORMAT_ENTRY_NOT_FOUND)
1041 {
1042 const Color* pCol = nullptr;
1043 m_pNumberFormatter->GetOutputString(fDate, nFormat, sCurrentContent, &pCol, false);
1044 }
1045 return sCurrentContent;
1046 }
1047
1048 void DateFieldmark::InvalidateCurrentDateParam()
1049 {
1050 std::pair<bool, double> aResult = ParseCurrentDateParam();
1051 if(!aResult.first)
1052 return;
1053
1054 // Current date became invalid
1055 if(GetDateInCurrentDateFormat(aResult.second) != GetContent())
1056 {
1057 sw::mark::IFieldmark::parameter_map_t* pParameters = GetParameters();
1058 (*pParameters)[ODF_FORMDATE_CURRENTDATE"DateField_CurrentDate"] <<= OUString();
1059 }
1060 }
1061}
1062
1063/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclptr.hxx

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
36class VclReferenceBase;
37
38namespace vcl::detail {
39
40template<typename>
41constexpr bool isIncompleteOrDerivedFromVclReferenceBase(...) { return true; }
42
43template<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 */
56template <class reference_type>
57class 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
66public:
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 ;-)
4
Calling 'Reference::clear'
11
Returning; memory was released
205 if (aTmp.get()) {
12
Calling 'Reference::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
218template<typename T1, typename T2>
219inline bool operator ==(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
220 return p1.get() == p2.get();
221}
222
223template<typename T> inline bool operator ==(VclPtr<T> const & p1, T const * p2)
224{
225 return p1.get() == p2;
226}
227
228template<typename T> inline bool operator ==(VclPtr<T> const & p1, T * p2) {
229 return p1.get() == p2;
230}
231
232template<typename T> inline bool operator ==(T const * p1, VclPtr<T> const & p2)
233{
234 return p1 == p2.get();
235}
236
237template<typename T> inline bool operator ==(T * p1, VclPtr<T> const & p2) {
238 return p1 == p2.get();
239}
240
241template<typename T1, typename T2>
242inline bool operator !=(VclPtr<T1> const & p1, VclPtr<T2> const & p2) {
243 return !(p1 == p2);
244}
245
246template<typename T> inline bool operator !=(VclPtr<T> const & p1, T const * p2)
247{
248 return !(p1 == p2);
249}
250
251template<typename T> inline bool operator !=(VclPtr<T> const & p1, T * p2) {
252 return !(p1 == p2);
253}
254
255template<typename T> inline bool operator !=(T const * p1, VclPtr<T> const & p2)
256{
257 return !(p1 == p2);
258}
259
260template<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 */
274template <class reference_type>
275class SAL_WARN_UNUSED__attribute__((warn_unused)) VclPtrInstance final : public VclPtr<reference_type>
276{
277public:
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
290template <class reference_type>
291class ScopedVclPtr : public VclPtr<reference_type>
292{
293public:
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
378private:
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
387protected:
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
406template <class reference_type>
407class SAL_WARN_UNUSED__attribute__((warn_unused)) ScopedVclPtrInstance final : public ScopedVclPtr<reference_type>
408{
409public:
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
421private:
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: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

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
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
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
4.1
Field 'm_pBody' is non-null
4.1
Field 'm_pBody' is non-null
4.1
Field 'm_pBody' is non-null
4.1
Field 'm_pBody' is non-null
)
5
Taking true branch
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
6
Calling 'VclReferenceBase::release'
10
Returning; memory was released
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;
13
Use of memory after it is freed
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
277namespace 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*/
286template<typename T>
287struct 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: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

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
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
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)
7
Assuming the condition is true
8
Taking true branch
40 delete this;
9
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif