1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: numberedcollection.cxx,v $
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 //_______________________________________________
40 #include <com/sun/star/frame/UntitledNumbersConst.hpp>
42 //_______________________________________________
47 namespace css
= ::com::sun::star
;
49 //_______________________________________________
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 ()
64 //-----------------------------------------------
65 NumberedCollection::~NumberedCollection()
69 //-----------------------------------------------
70 void NumberedCollection::setOwner(const css::uno::Reference
< css::uno::XInterface
>& xOwner
)
73 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
80 //-----------------------------------------------
81 void NumberedCollection::setUntitledPrefix(const ::rtl::OUString
& sPrefix
)
84 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
86 m_sUntitledPrefix
= sPrefix
;
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
)
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
119 aItem
.xItem
= css::uno::WeakReference
< css::uno::XInterface
>(xComponent
);
120 aItem
.nNumber
= nFreeNumber
;
121 m_lComponents
[pComponent
] = aItem
;
128 //-----------------------------------------------
129 void SAL_CALL
NumberedCollection::releaseNumber(::sal_Int32 nNumber
)
130 throw (css::lang::IllegalArgumentException
,
131 css::uno::RuntimeException
)
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 ();
146 const TNumberedItem
& rItem
= pComponent
->second
;
147 const css::uno::Reference
< css::uno::XInterface
> xItem
= rItem
.xItem
.get();
151 lDeadItems
.push_back(pComponent
->first
);
155 if (rItem
.nNumber
== nNumber
)
157 m_lComponents
.erase (pComponent
);
162 impl_cleanUpDeadItems(m_lComponents
, lDeadItems
);
167 //-----------------------------------------------
168 void SAL_CALL
NumberedCollection::releaseNumberForComponent(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
169 throw (css::lang::IllegalArgumentException
,
170 css::uno::RuntimeException
)
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
);
186 // b) component does not exists - nothing todo here (ignore request!)
191 //-----------------------------------------------
192 ::rtl::OUString SAL_CALL
NumberedCollection::getUntitledPrefix()
193 throw (css::uno::RuntimeException
)
196 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
198 return m_sUntitledPrefix
;
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 ();
223 // c cant be less then 0 ... otherwhise hash.size() has an error :-)
224 // But we need at least n+1 numbers here.
228 lPossibleNumbers
.push_back (i
);
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 ();
240 const TNumberedItem
& rItem
= pComponent
->second
;
241 const css::uno::Reference
< css::uno::XInterface
> xItem
= rItem
.xItem
.get();
245 lDeadItems
.push_back(pComponent
->first
);
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 ());
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 ();
275 const long& rDeadItem
= *pIt
;
276 lItems
.erase(rDeadItem
);
280 } // namespace comphelper