bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / numberedcollection.cxx
blob1056a5e566ab67bbd8571f629078768acb67dee2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <algorithm>
21 #include <comphelper/numberedcollection.hxx>
22 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
24 namespace comphelper{
26 static const char ERRMSG_INVALID_COMPONENT_PARAM[] = "NULL as component reference not allowed.";
29 NumberedCollection::NumberedCollection()
30 : ::cppu::BaseMutex ()
31 , m_sUntitledPrefix ()
32 , m_lComponents ()
33 , m_xOwner ()
38 NumberedCollection::~NumberedCollection()
43 void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
45 // SYNCHRONIZED ->
46 ::osl::ResettableMutexGuard aLock(m_aMutex);
48 m_xOwner = xOwner;
50 // <- SYNCHRONIZED
54 void NumberedCollection::setUntitledPrefix(const OUString& sPrefix)
56 // SYNCHRONIZED ->
57 ::osl::ResettableMutexGuard aLock(m_aMutex);
59 m_sUntitledPrefix = sPrefix;
61 // <- SYNCHRONIZED
65 ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
66 throw (css::lang::IllegalArgumentException,
67 css::uno::RuntimeException, std::exception )
69 // SYNCHRONIZED ->
70 ::osl::ResettableMutexGuard aLock(m_aMutex);
72 if ( ! xComponent.is ())
73 throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
75 sal_IntPtr pComponent = reinterpret_cast<sal_IntPtr>( xComponent.get() );
76 TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent);
78 // a) component already exists - return it's number directly
79 if (pIt != m_lComponents.end())
80 return pIt->second.nNumber;
82 // b) component must be added new to this container
84 // b1) collection is full - no further components possible
85 // -> return INVALID_NUMBER
86 ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
87 if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
88 return css::frame::UntitledNumbersConst::INVALID_NUMBER;
90 // b2) add component to collection and return its number
91 TNumberedItem aItem;
92 aItem.xItem = css::uno::WeakReference< css::uno::XInterface >(xComponent);
93 aItem.nNumber = nFreeNumber;
94 m_lComponents[pComponent] = aItem;
96 return nFreeNumber;
98 // <- SYNCHRONIZED
102 void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
103 throw (css::lang::IllegalArgumentException,
104 css::uno::RuntimeException, std::exception )
106 // SYNCHRONIZED ->
107 ::osl::ResettableMutexGuard aLock(m_aMutex);
109 if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
110 throw css::lang::IllegalArgumentException ("Special valkud INVALID_NUMBER not allowed as input parameter.", m_xOwner.get(), 1);
112 TDeadItemList lDeadItems;
113 TNumberedItemHash::iterator pComponent;
115 for ( pComponent = m_lComponents.begin ();
116 pComponent != m_lComponents.end ();
117 ++pComponent )
119 const TNumberedItem& rItem = pComponent->second;
120 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
122 if ( ! xItem.is ())
124 lDeadItems.push_back(pComponent->first);
125 continue;
128 if (rItem.nNumber == nNumber)
130 m_lComponents.erase (pComponent);
131 break;
135 impl_cleanUpDeadItems(m_lComponents, lDeadItems);
137 // <- SYNCHRONIZED
141 void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
142 throw (css::lang::IllegalArgumentException,
143 css::uno::RuntimeException, std::exception )
145 // SYNCHRONIZED ->
146 ::osl::ResettableMutexGuard aLock(m_aMutex);
148 if ( ! xComponent.is ())
149 throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
151 sal_IntPtr pComponent = reinterpret_cast<sal_IntPtr>( xComponent.get() );
152 TNumberedItemHash::iterator pIt = m_lComponents.find (pComponent);
154 // a) component exists and will be removed
155 if (pIt != m_lComponents.end())
156 m_lComponents.erase(pIt);
158 // else
159 // b) component does not exists - nothing todo here (ignore request!)
161 // <- SYNCHRONIZED
165 OUString SAL_CALL NumberedCollection::getUntitledPrefix()
166 throw (css::uno::RuntimeException, std::exception)
168 // SYNCHRONIZED ->
169 ::osl::ResettableMutexGuard aLock(m_aMutex);
171 return m_sUntitledPrefix;
173 // <- SYNCHRONIZED
177 /** create an ordered list of all possible numbers ...
178 e.g. {1,2,3,...,N} Max size of these list will be
179 current size of component list + 1 .
181 "+1" ... because in case all numbers in range 1..n
182 are in use we need a new number n+1 :-)
184 Every item which is already used as unique number
185 will be removed. At the end a list of e.g. {3,6,...,M}
186 exists where the first item represent the lowest free
187 number (in this example 3).
189 ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
191 // create ordered list of all possible numbers.
192 ::std::vector< ::sal_Int32 > lPossibleNumbers;
193 ::sal_Int32 c = (::sal_Int32)m_lComponents.size ();
194 ::sal_Int32 i = 1;
196 // c can't be less then 0 ... otherwise hash.size() has an error :-)
197 // But we need at least n+1 numbers here.
198 c += 1;
200 for (i=1; i<=c; ++i)
201 lPossibleNumbers.push_back (i);
203 // SYNCHRONIZED ->
204 ::osl::ResettableMutexGuard aLock(m_aMutex);
206 TDeadItemList lDeadItems;
207 TNumberedItemHash::const_iterator pComponent;
209 for ( pComponent = m_lComponents.begin ();
210 pComponent != m_lComponents.end ();
211 ++pComponent )
213 const TNumberedItem& rItem = pComponent->second;
214 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
216 if ( ! xItem.is ())
218 lDeadItems.push_back(pComponent->first);
219 continue;
222 ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
223 if (pPossible != lPossibleNumbers.end ())
224 lPossibleNumbers.erase (pPossible);
227 impl_cleanUpDeadItems(m_lComponents, lDeadItems);
229 // a) non free numbers ... return INVALID_NUMBER
230 if (lPossibleNumbers.size () < 1)
231 return css::frame::UntitledNumbersConst::INVALID_NUMBER;
233 // b) return first free number
234 return *(lPossibleNumbers.begin ());
236 // <- SYNCHRONIZED
239 void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
240 const TDeadItemList& lDeadItems)
242 TDeadItemList::const_iterator pIt;
244 for ( pIt = lDeadItems.begin ();
245 pIt != lDeadItems.end ();
246 ++pIt )
248 const long& rDeadItem = *pIt;
249 lItems.erase(rDeadItem);
253 } // namespace comphelper
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */