Update ooo320-m1
[ooovba.git] / comphelper / source / misc / numberedcollection.cxx
blobc87143186478e247d18e1b7d104eb95916350458
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: numberedcollection.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_comphelper.hxx"
35 #include <comphelper/numberedcollection.hxx>
37 //_______________________________________________
38 // includes
40 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
42 //_______________________________________________
43 // namespace
45 namespace comphelper{
47 namespace css = ::com::sun::star;
49 //_______________________________________________
50 // definitions
52 static const ::rtl::OUString ERRMSG_INVALID_COMPONENT_PARAM = ::rtl::OUString::createFromAscii("NULL as component reference not allowed.");
53 static const ::rtl::OUString ERRMSG_INVALID_NUMBER_PARAM = ::rtl::OUString::createFromAscii("Special valkud INVALID_NUMBER not allowed as input parameter.");
55 //-----------------------------------------------
56 NumberedCollection::NumberedCollection()
57 : ::cppu::BaseMutex ()
58 , m_sUntitledPrefix ()
59 , m_lComponents ()
60 , m_xOwner ()
64 //-----------------------------------------------
65 NumberedCollection::~NumberedCollection()
69 //-----------------------------------------------
70 void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
72 // SYNCHRONIZED ->
73 ::osl::ResettableMutexGuard aLock(m_aMutex);
75 m_xOwner = xOwner;
77 // <- SYNCHRONIZED
80 //-----------------------------------------------
81 void NumberedCollection::setUntitledPrefix(const ::rtl::OUString& sPrefix)
83 // SYNCHRONIZED ->
84 ::osl::ResettableMutexGuard aLock(m_aMutex);
86 m_sUntitledPrefix = sPrefix;
88 // <- SYNCHRONIZED
91 //-----------------------------------------------
92 ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
93 throw (css::lang::IllegalArgumentException,
94 css::uno::RuntimeException )
96 // SYNCHRONIZED ->
97 ::osl::ResettableMutexGuard aLock(m_aMutex);
99 if ( ! xComponent.is ())
100 throw css::lang::IllegalArgumentException (ERRMSG_INVALID_COMPONENT_PARAM, m_xOwner.get(), 1);
102 long pComponent = (long) xComponent.get ();
103 TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent);
105 // a) component already exists - return it's number directly
106 if (pIt != m_lComponents.end())
107 return pIt->second.nNumber;
109 // b) component must be added new to this container
111 // b1) collection is full - no further components possible
112 // -> return INVALID_NUMBER
113 ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
114 if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
115 return css::frame::UntitledNumbersConst::INVALID_NUMBER;
117 // b2) add component to collection and return its number
118 TNumberedItem aItem;
119 aItem.xItem = css::uno::WeakReference< css::uno::XInterface >(xComponent);
120 aItem.nNumber = nFreeNumber;
121 m_lComponents[pComponent] = aItem;
123 return nFreeNumber;
125 // <- SYNCHRONIZED
128 //-----------------------------------------------
129 void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
130 throw (css::lang::IllegalArgumentException,
131 css::uno::RuntimeException )
133 // SYNCHRONIZED ->
134 ::osl::ResettableMutexGuard aLock(m_aMutex);
136 if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
137 throw css::lang::IllegalArgumentException (ERRMSG_INVALID_NUMBER_PARAM, m_xOwner.get(), 1);
139 TDeadItemList lDeadItems;
140 TNumberedItemHash::iterator pComponent;
142 for ( pComponent = m_lComponents.begin ();
143 pComponent != m_lComponents.end ();
144 ++pComponent )
146 const TNumberedItem& rItem = pComponent->second;
147 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
149 if ( ! xItem.is ())
151 lDeadItems.push_back(pComponent->first);
152 continue;
155 if (rItem.nNumber == nNumber)
157 m_lComponents.erase (pComponent);
158 break;
162 impl_cleanUpDeadItems(m_lComponents, lDeadItems);
164 // <- SYNCHRONIZED
167 //-----------------------------------------------
168 void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
169 throw (css::lang::IllegalArgumentException,
170 css::uno::RuntimeException )
172 // SYNCHRONIZED ->
173 ::osl::ResettableMutexGuard aLock(m_aMutex);
175 if ( ! xComponent.is ())
176 throw css::lang::IllegalArgumentException (ERRMSG_INVALID_COMPONENT_PARAM, m_xOwner.get(), 1);
178 long pComponent = (long) xComponent.get ();
179 TNumberedItemHash::iterator pIt = m_lComponents.find (pComponent);
181 // a) component exists and will be removed
182 if (pIt != m_lComponents.end())
183 m_lComponents.erase(pIt);
185 // else
186 // b) component does not exists - nothing todo here (ignore request!)
188 // <- SYNCHRONIZED
191 //-----------------------------------------------
192 ::rtl::OUString SAL_CALL NumberedCollection::getUntitledPrefix()
193 throw (css::uno::RuntimeException)
195 // SYNCHRONIZED ->
196 ::osl::ResettableMutexGuard aLock(m_aMutex);
198 return m_sUntitledPrefix;
200 // <- SYNCHRONIZED
203 //-----------------------------------------------
204 /** create an ordered list of all possible numbers ...
205 e.g. {1,2,3,...,N} Max size of these list will be
206 current size of component list + 1 .
208 "+1" ... because in case all numbers in range 1..n
209 are in use we need a new number n+1 :-)
211 Every item which is already used as unique number
212 will be removed. At the end a list of e.g. {3,6,...,M}
213 exists where the first item represent the lowest free
214 number (in this example 3).
216 ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
218 // create ordered list of all possible numbers.
219 ::std::vector< ::sal_Int32 > lPossibleNumbers;
220 ::sal_Int32 c = (::sal_Int32)m_lComponents.size ();
221 ::sal_Int32 i = 1;
223 // c cant be less then 0 ... otherwhise hash.size() has an error :-)
224 // But we need at least n+1 numbers here.
225 c += 1;
227 for (i=1; i<=c; ++i)
228 lPossibleNumbers.push_back (i);
230 // SYNCHRONIZED ->
231 ::osl::ResettableMutexGuard aLock(m_aMutex);
233 TDeadItemList lDeadItems;
234 TNumberedItemHash::const_iterator pComponent;
236 for ( pComponent = m_lComponents.begin ();
237 pComponent != m_lComponents.end ();
238 ++pComponent )
240 const TNumberedItem& rItem = pComponent->second;
241 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
243 if ( ! xItem.is ())
245 lDeadItems.push_back(pComponent->first);
246 continue;
249 ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
250 if (pPossible != lPossibleNumbers.end ())
251 lPossibleNumbers.erase (pPossible);
254 impl_cleanUpDeadItems(m_lComponents, lDeadItems);
256 // a) non free numbers ... return INVALID_NUMBER
257 if (lPossibleNumbers.size () < 1)
258 return css::frame::UntitledNumbersConst::INVALID_NUMBER;
260 // b) return first free number
261 return *(lPossibleNumbers.begin ());
263 // <- SYNCHRONIZED
266 void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
267 const TDeadItemList& lDeadItems)
269 TDeadItemList::const_iterator pIt;
271 for ( pIt = lDeadItems.begin ();
272 pIt != lDeadItems.end ();
273 ++pIt )
275 const long& rDeadItem = *pIt;
276 lItems.erase(rDeadItem);
280 } // namespace comphelper