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>
26 static const char ERRMSG_INVALID_COMPONENT_PARAM
[] = "NULL as component reference not allowed.";
29 NumberedCollection::NumberedCollection()
30 : ::cppu::BaseMutex ()
31 , m_sUntitledPrefix ()
38 NumberedCollection::~NumberedCollection()
43 void NumberedCollection::setOwner(const css::uno::Reference
< css::uno::XInterface
>& xOwner
)
46 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
54 void NumberedCollection::setUntitledPrefix(const OUString
& sPrefix
)
57 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
59 m_sUntitledPrefix
= sPrefix
;
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
)
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
92 aItem
.xItem
= css::uno::WeakReference
< css::uno::XInterface
>(xComponent
);
93 aItem
.nNumber
= nFreeNumber
;
94 m_lComponents
[pComponent
] = aItem
;
102 void SAL_CALL
NumberedCollection::releaseNumber(::sal_Int32 nNumber
)
103 throw (css::lang::IllegalArgumentException
,
104 css::uno::RuntimeException
, std::exception
)
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 ();
119 const TNumberedItem
& rItem
= pComponent
->second
;
120 const css::uno::Reference
< css::uno::XInterface
> xItem
= rItem
.xItem
.get();
124 lDeadItems
.push_back(pComponent
->first
);
128 if (rItem
.nNumber
== nNumber
)
130 m_lComponents
.erase (pComponent
);
135 impl_cleanUpDeadItems(m_lComponents
, lDeadItems
);
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
)
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
);
159 // b) component does not exists - nothing todo here (ignore request!)
165 OUString SAL_CALL
NumberedCollection::getUntitledPrefix()
166 throw (css::uno::RuntimeException
, std::exception
)
169 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
171 return m_sUntitledPrefix
;
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 ();
196 // c can't be less then 0 ... otherwise hash.size() has an error :-)
197 // But we need at least n+1 numbers here.
201 lPossibleNumbers
.push_back (i
);
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 ();
213 const TNumberedItem
& rItem
= pComponent
->second
;
214 const css::uno::Reference
< css::uno::XInterface
> xItem
= rItem
.xItem
.get();
218 lDeadItems
.push_back(pComponent
->first
);
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 ());
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 ();
248 const long& rDeadItem
= *pIt
;
249 lItems
.erase(rDeadItem
);
253 } // namespace comphelper
255 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */