1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <comphelper/numberedcollection.hxx>
22 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
27 constexpr OUStringLiteral ERRMSG_INVALID_COMPONENT_PARAM
= u
"NULL as component reference not allowed.";
30 NumberedCollection::NumberedCollection()
35 NumberedCollection::~NumberedCollection()
40 void NumberedCollection::setOwner(const css::uno::Reference
< css::uno::XInterface
>& xOwner
)
43 std::scoped_lock
aLock(m_aMutex
);
51 void NumberedCollection::setUntitledPrefix(const OUString
& sPrefix
)
54 std::scoped_lock
aLock(m_aMutex
);
56 m_sUntitledPrefix
= sPrefix
;
62 ::sal_Int32 SAL_CALL
NumberedCollection::leaseNumber(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
65 std::scoped_lock
aLock(m_aMutex
);
67 if ( ! xComponent
.is ())
68 throw css::lang::IllegalArgumentException(ERRMSG_INVALID_COMPONENT_PARAM
, m_xOwner
.get(), 1);
70 sal_IntPtr pComponent
= reinterpret_cast<sal_IntPtr
>( xComponent
.get() );
71 TNumberedItemHash::const_iterator pIt
= m_lComponents
.find (pComponent
);
73 // a) component already exists - return its number directly
74 if (pIt
!= m_lComponents
.end())
75 return pIt
->second
.nNumber
;
77 // b) component must be added new to this container
79 // b1) collection is full - no further components possible
80 // -> return INVALID_NUMBER
81 ::sal_Int32 nFreeNumber
= impl_searchFreeNumber();
82 if (nFreeNumber
== css::frame::UntitledNumbersConst::INVALID_NUMBER
)
83 return css::frame::UntitledNumbersConst::INVALID_NUMBER
;
85 // b2) add component to collection and return its number
87 aItem
.xItem
= css::uno::WeakReference
< css::uno::XInterface
>(xComponent
);
88 aItem
.nNumber
= nFreeNumber
;
89 m_lComponents
[pComponent
] = aItem
;
97 void SAL_CALL
NumberedCollection::releaseNumber(::sal_Int32 nNumber
)
100 std::scoped_lock
aLock(m_aMutex
);
102 if (nNumber
== css::frame::UntitledNumbersConst::INVALID_NUMBER
)
103 throw css::lang::IllegalArgumentException ("Special value INVALID_NUMBER not allowed as input parameter.", m_xOwner
.get(), 1);
105 TDeadItemList lDeadItems
;
106 TNumberedItemHash::iterator pComponent
;
108 for ( pComponent
= m_lComponents
.begin ();
109 pComponent
!= m_lComponents
.end ();
112 const TNumberedItem
& rItem
= pComponent
->second
;
113 const css::uno::Reference
< css::uno::XInterface
> xItem
= rItem
.xItem
.get();
117 lDeadItems
.push_back(pComponent
->first
);
121 if (rItem
.nNumber
== nNumber
)
123 m_lComponents
.erase (pComponent
);
128 impl_cleanUpDeadItems(m_lComponents
, lDeadItems
);
134 void SAL_CALL
NumberedCollection::releaseNumberForComponent(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
137 std::scoped_lock
aLock(m_aMutex
);
139 if ( ! xComponent
.is ())
140 throw css::lang::IllegalArgumentException(ERRMSG_INVALID_COMPONENT_PARAM
, m_xOwner
.get(), 1);
142 sal_IntPtr pComponent
= reinterpret_cast<sal_IntPtr
>( xComponent
.get() );
143 TNumberedItemHash::iterator pIt
= m_lComponents
.find (pComponent
);
145 // a) component exists and will be removed
146 if (pIt
!= m_lComponents
.end())
147 m_lComponents
.erase(pIt
);
150 // b) component does not exists - nothing todo here (ignore request!)
156 OUString SAL_CALL
NumberedCollection::getUntitledPrefix()
159 std::scoped_lock
aLock(m_aMutex
);
161 return m_sUntitledPrefix
;
167 /** create an ordered list of all possible numbers ...
168 e.g. {1,2,3,...,N} Max size of these list will be
169 current size of component list + 1 .
171 "+1" ... because in case all numbers in range 1..n
172 are in use we need a new number n+1 :-)
174 Every item which is already used as unique number
175 will be removed. At the end a list of e.g. {3,6,...,M}
176 exists where the first item represent the lowest free
177 number (in this example 3).
179 ::sal_Int32
NumberedCollection::impl_searchFreeNumber ()
181 // create bitset, where each position represents one possible number.
182 std::vector
<bool> aUsedNumbers((m_lComponents
.size() * 2) + 1, false);
184 for (const auto& rPair
: m_lComponents
)
186 // numbers start at 1
187 sal_Int32 pos
= rPair
.second
.nNumber
- 1;
188 if (pos
>= static_cast<sal_Int32
>(aUsedNumbers
.size()))
189 aUsedNumbers
.resize(pos
* 2, false); // should be rare
190 aUsedNumbers
[pos
] = true;
193 // a) non free numbers ... return INVALID_NUMBER
194 auto it
= std::find(aUsedNumbers
.begin(), aUsedNumbers
.end(), false);
195 if (it
== aUsedNumbers
.end())
196 return css::frame::UntitledNumbersConst::INVALID_NUMBER
;
198 // b) return first free number
199 return it
- aUsedNumbers
.begin() + 1;
202 void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash
& lItems
,
203 const TDeadItemList
& lDeadItems
)
205 for (const sal_IntPtr
& rDeadItem
: lDeadItems
)
207 lItems
.erase(rDeadItem
);
211 } // namespace comphelper
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */