Bump version to 4.3-4
[LibreOffice.git] / comphelper / source / misc / numberedcollection.cxx
blob4283360bf06a22d1edd25567077272c59304d2d2
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 .
21 #include <comphelper/numberedcollection.hxx>
23 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
26 namespace comphelper{
28 static const char ERRMSG_INVALID_COMPONENT_PARAM[] = "NULL as component reference not allowed.";
31 NumberedCollection::NumberedCollection()
32 : ::cppu::BaseMutex ()
33 , m_sUntitledPrefix ()
34 , m_lComponents ()
35 , m_xOwner ()
40 NumberedCollection::~NumberedCollection()
45 void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
47 // SYNCHRONIZED ->
48 ::osl::ResettableMutexGuard aLock(m_aMutex);
50 m_xOwner = xOwner;
52 // <- SYNCHRONIZED
56 void NumberedCollection::setUntitledPrefix(const OUString& sPrefix)
58 // SYNCHRONIZED ->
59 ::osl::ResettableMutexGuard aLock(m_aMutex);
61 m_sUntitledPrefix = sPrefix;
63 // <- SYNCHRONIZED
67 ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
68 throw (css::lang::IllegalArgumentException,
69 css::uno::RuntimeException, std::exception )
71 // SYNCHRONIZED ->
72 ::osl::ResettableMutexGuard aLock(m_aMutex);
74 if ( ! xComponent.is ())
75 throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
77 sal_IntPtr pComponent = (sal_IntPtr) xComponent.get ();
78 TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent);
80 // a) component already exists - return it's number directly
81 if (pIt != m_lComponents.end())
82 return pIt->second.nNumber;
84 // b) component must be added new to this container
86 // b1) collection is full - no further components possible
87 // -> return INVALID_NUMBER
88 ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
89 if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
90 return css::frame::UntitledNumbersConst::INVALID_NUMBER;
92 // b2) add component to collection and return its number
93 TNumberedItem aItem;
94 aItem.xItem = css::uno::WeakReference< css::uno::XInterface >(xComponent);
95 aItem.nNumber = nFreeNumber;
96 m_lComponents[pComponent] = aItem;
98 return nFreeNumber;
100 // <- SYNCHRONIZED
104 void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
105 throw (css::lang::IllegalArgumentException,
106 css::uno::RuntimeException, std::exception )
108 // SYNCHRONIZED ->
109 ::osl::ResettableMutexGuard aLock(m_aMutex);
111 if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
112 throw css::lang::IllegalArgumentException ("Special valkud INVALID_NUMBER not allowed as input parameter.", m_xOwner.get(), 1);
114 TDeadItemList lDeadItems;
115 TNumberedItemHash::iterator pComponent;
117 for ( pComponent = m_lComponents.begin ();
118 pComponent != m_lComponents.end ();
119 ++pComponent )
121 const TNumberedItem& rItem = pComponent->second;
122 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
124 if ( ! xItem.is ())
126 lDeadItems.push_back(pComponent->first);
127 continue;
130 if (rItem.nNumber == nNumber)
132 m_lComponents.erase (pComponent);
133 break;
137 impl_cleanUpDeadItems(m_lComponents, lDeadItems);
139 // <- SYNCHRONIZED
143 void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
144 throw (css::lang::IllegalArgumentException,
145 css::uno::RuntimeException, std::exception )
147 // SYNCHRONIZED ->
148 ::osl::ResettableMutexGuard aLock(m_aMutex);
150 if ( ! xComponent.is ())
151 throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
153 sal_IntPtr pComponent = (sal_IntPtr) xComponent.get ();
154 TNumberedItemHash::iterator pIt = m_lComponents.find (pComponent);
156 // a) component exists and will be removed
157 if (pIt != m_lComponents.end())
158 m_lComponents.erase(pIt);
160 // else
161 // b) component does not exists - nothing todo here (ignore request!)
163 // <- SYNCHRONIZED
167 OUString SAL_CALL NumberedCollection::getUntitledPrefix()
168 throw (css::uno::RuntimeException, std::exception)
170 // SYNCHRONIZED ->
171 ::osl::ResettableMutexGuard aLock(m_aMutex);
173 return m_sUntitledPrefix;
175 // <- SYNCHRONIZED
179 /** create an ordered list of all possible numbers ...
180 e.g. {1,2,3,...,N} Max size of these list will be
181 current size of component list + 1 .
183 "+1" ... because in case all numbers in range 1..n
184 are in use we need a new number n+1 :-)
186 Every item which is already used as unique number
187 will be removed. At the end a list of e.g. {3,6,...,M}
188 exists where the first item represent the lowest free
189 number (in this example 3).
191 ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
193 // create ordered list of all possible numbers.
194 ::std::vector< ::sal_Int32 > lPossibleNumbers;
195 ::sal_Int32 c = (::sal_Int32)m_lComponents.size ();
196 ::sal_Int32 i = 1;
198 // c can't be less then 0 ... otherwise hash.size() has an error :-)
199 // But we need at least n+1 numbers here.
200 c += 1;
202 for (i=1; i<=c; ++i)
203 lPossibleNumbers.push_back (i);
205 // SYNCHRONIZED ->
206 ::osl::ResettableMutexGuard aLock(m_aMutex);
208 TDeadItemList lDeadItems;
209 TNumberedItemHash::const_iterator pComponent;
211 for ( pComponent = m_lComponents.begin ();
212 pComponent != m_lComponents.end ();
213 ++pComponent )
215 const TNumberedItem& rItem = pComponent->second;
216 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
218 if ( ! xItem.is ())
220 lDeadItems.push_back(pComponent->first);
221 continue;
224 ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
225 if (pPossible != lPossibleNumbers.end ())
226 lPossibleNumbers.erase (pPossible);
229 impl_cleanUpDeadItems(m_lComponents, lDeadItems);
231 // a) non free numbers ... return INVALID_NUMBER
232 if (lPossibleNumbers.size () < 1)
233 return css::frame::UntitledNumbersConst::INVALID_NUMBER;
235 // b) return first free number
236 return *(lPossibleNumbers.begin ());
238 // <- SYNCHRONIZED
241 void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
242 const TDeadItemList& lDeadItems)
244 TDeadItemList::const_iterator pIt;
246 for ( pIt = lDeadItems.begin ();
247 pIt != lDeadItems.end ();
248 ++pIt )
250 const long& rDeadItem = *pIt;
251 lItems.erase(rDeadItem);
255 } // namespace comphelper
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */