Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <comphelper/numberedcollection.hxx>
22 :
23 : #include <com/sun/star/frame/UntitledNumbersConst.hpp>
24 :
25 :
26 : namespace comphelper{
27 :
28 : static const char ERRMSG_INVALID_COMPONENT_PARAM[] = "NULL as component reference not allowed.";
29 :
30 : //-----------------------------------------------
31 201 : NumberedCollection::NumberedCollection()
32 : : ::cppu::BaseMutex ()
33 : , m_sUntitledPrefix ()
34 : , m_lComponents ()
35 201 : , m_xOwner ()
36 : {
37 201 : }
38 :
39 : //-----------------------------------------------
40 82 : NumberedCollection::~NumberedCollection()
41 : {
42 82 : }
43 :
44 : //-----------------------------------------------
45 201 : void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
46 : {
47 : // SYNCHRONIZED ->
48 201 : ::osl::ResettableMutexGuard aLock(m_aMutex);
49 :
50 201 : m_xOwner = xOwner;
51 :
52 : // <- SYNCHRONIZED
53 201 : }
54 :
55 : //-----------------------------------------------
56 201 : void NumberedCollection::setUntitledPrefix(const ::rtl::OUString& sPrefix)
57 : {
58 : // SYNCHRONIZED ->
59 201 : ::osl::ResettableMutexGuard aLock(m_aMutex);
60 :
61 201 : m_sUntitledPrefix = sPrefix;
62 :
63 : // <- SYNCHRONIZED
64 201 : }
65 :
66 : //-----------------------------------------------
67 274 : ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent)
68 : throw (css::lang::IllegalArgumentException,
69 : css::uno::RuntimeException )
70 : {
71 : // SYNCHRONIZED ->
72 274 : ::osl::ResettableMutexGuard aLock(m_aMutex);
73 :
74 274 : if ( ! xComponent.is ())
75 0 : throw css::lang::IllegalArgumentException (rtl::OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
76 :
77 274 : long pComponent = (long) xComponent.get ();
78 274 : TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent);
79 :
80 : // a) component already exists - return it's number directly
81 274 : if (pIt != m_lComponents.end())
82 0 : return pIt->second.nNumber;
83 :
84 : // b) component must be added new to this container
85 :
86 : // b1) collection is full - no further components possible
87 : // -> return INVALID_NUMBER
88 274 : ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
89 274 : if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
90 0 : return css::frame::UntitledNumbersConst::INVALID_NUMBER;
91 :
92 : // b2) add component to collection and return its number
93 274 : TNumberedItem aItem;
94 274 : aItem.xItem = css::uno::WeakReference< css::uno::XInterface >(xComponent);
95 274 : aItem.nNumber = nFreeNumber;
96 274 : m_lComponents[pComponent] = aItem;
97 :
98 274 : return nFreeNumber;
99 :
100 : // <- SYNCHRONIZED
101 : }
102 :
103 : //-----------------------------------------------
104 66 : void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
105 : throw (css::lang::IllegalArgumentException,
106 : css::uno::RuntimeException )
107 : {
108 : // SYNCHRONIZED ->
109 66 : ::osl::ResettableMutexGuard aLock(m_aMutex);
110 :
111 66 : if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
112 0 : throw css::lang::IllegalArgumentException (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Special valkud INVALID_NUMBER not allowed as input parameter.")), m_xOwner.get(), 1);
113 :
114 66 : TDeadItemList lDeadItems;
115 66 : TNumberedItemHash::iterator pComponent;
116 :
117 1770 : for ( pComponent = m_lComponents.begin ();
118 1180 : pComponent != m_lComponents.end ();
119 : ++pComponent )
120 : {
121 590 : const TNumberedItem& rItem = pComponent->second;
122 590 : const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
123 :
124 590 : if ( ! xItem.is ())
125 : {
126 0 : lDeadItems.push_back(pComponent->first);
127 0 : continue;
128 : }
129 :
130 590 : if (rItem.nNumber == nNumber)
131 : {
132 66 : m_lComponents.erase (pComponent);
133 : break;
134 : }
135 590 : }
136 :
137 66 : impl_cleanUpDeadItems(m_lComponents, lDeadItems);
138 :
139 : // <- SYNCHRONIZED
140 66 : }
141 :
142 : //-----------------------------------------------
143 0 : void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent)
144 : throw (css::lang::IllegalArgumentException,
145 : css::uno::RuntimeException )
146 : {
147 : // SYNCHRONIZED ->
148 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
149 :
150 0 : if ( ! xComponent.is ())
151 0 : throw css::lang::IllegalArgumentException (rtl::OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
152 :
153 0 : long pComponent = (long) xComponent.get ();
154 0 : TNumberedItemHash::iterator pIt = m_lComponents.find (pComponent);
155 :
156 : // a) component exists and will be removed
157 0 : if (pIt != m_lComponents.end())
158 0 : m_lComponents.erase(pIt);
159 :
160 : // else
161 : // b) component does not exists - nothing todo here (ignore request!)
162 :
163 : // <- SYNCHRONIZED
164 0 : }
165 :
166 : //-----------------------------------------------
167 693 : ::rtl::OUString SAL_CALL NumberedCollection::getUntitledPrefix()
168 : throw (css::uno::RuntimeException)
169 : {
170 : // SYNCHRONIZED ->
171 693 : ::osl::ResettableMutexGuard aLock(m_aMutex);
172 :
173 693 : return m_sUntitledPrefix;
174 :
175 : // <- SYNCHRONIZED
176 : }
177 :
178 : //-----------------------------------------------
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 .
182 :
183 : "+1" ... because in case all numbers in range 1..n
184 : are in use we need a new number n+1 :-)
185 :
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).
190 : */
191 274 : ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
192 : {
193 : // create ordered list of all possible numbers.
194 274 : ::std::vector< ::sal_Int32 > lPossibleNumbers;
195 274 : ::sal_Int32 c = (::sal_Int32)m_lComponents.size ();
196 274 : ::sal_Int32 i = 1;
197 :
198 : // c cant be less then 0 ... otherwhise hash.size() has an error :-)
199 : // But we need at least n+1 numbers here.
200 274 : c += 1;
201 :
202 1745 : for (i=1; i<=c; ++i)
203 1471 : lPossibleNumbers.push_back (i);
204 :
205 : // SYNCHRONIZED ->
206 274 : ::osl::ResettableMutexGuard aLock(m_aMutex);
207 :
208 274 : TDeadItemList lDeadItems;
209 274 : TNumberedItemHash::const_iterator pComponent;
210 :
211 4413 : for ( pComponent = m_lComponents.begin ();
212 2942 : pComponent != m_lComponents.end ();
213 : ++pComponent )
214 : {
215 1197 : const TNumberedItem& rItem = pComponent->second;
216 1197 : const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
217 :
218 1197 : if ( ! xItem.is ())
219 : {
220 0 : lDeadItems.push_back(pComponent->first);
221 0 : continue;
222 : }
223 :
224 1197 : ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
225 1197 : if (pPossible != lPossibleNumbers.end ())
226 1171 : lPossibleNumbers.erase (pPossible);
227 1197 : }
228 :
229 274 : impl_cleanUpDeadItems(m_lComponents, lDeadItems);
230 :
231 : // a) non free numbers ... return INVALID_NUMBER
232 274 : if (lPossibleNumbers.size () < 1)
233 0 : return css::frame::UntitledNumbersConst::INVALID_NUMBER;
234 :
235 : // b) return first free number
236 274 : return *(lPossibleNumbers.begin ());
237 :
238 : // <- SYNCHRONIZED
239 : }
240 :
241 340 : void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
242 : const TDeadItemList& lDeadItems)
243 : {
244 340 : TDeadItemList::const_iterator pIt;
245 :
246 1020 : for ( pIt = lDeadItems.begin ();
247 680 : pIt != lDeadItems.end ();
248 : ++pIt )
249 : {
250 0 : const long& rDeadItem = *pIt;
251 0 : lItems.erase(rDeadItem);
252 : }
253 340 : }
254 :
255 : } // namespace comphelper
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|