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 0 : NumberedCollection::NumberedCollection()
32 : : ::cppu::BaseMutex ()
33 : , m_sUntitledPrefix ()
34 : , m_lComponents ()
35 0 : , m_xOwner ()
36 : {
37 0 : }
38 :
39 :
40 0 : NumberedCollection::~NumberedCollection()
41 : {
42 0 : }
43 :
44 :
45 0 : void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner)
46 : {
47 : // SYNCHRONIZED ->
48 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
49 :
50 0 : m_xOwner = xOwner;
51 :
52 : // <- SYNCHRONIZED
53 0 : }
54 :
55 :
56 0 : void NumberedCollection::setUntitledPrefix(const OUString& sPrefix)
57 : {
58 : // SYNCHRONIZED ->
59 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
60 :
61 0 : m_sUntitledPrefix = sPrefix;
62 :
63 : // <- SYNCHRONIZED
64 0 : }
65 :
66 :
67 0 : ::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 )
70 : {
71 : // SYNCHRONIZED ->
72 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
73 :
74 0 : if ( ! xComponent.is ())
75 0 : throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
76 :
77 0 : sal_IntPtr pComponent = (sal_IntPtr) xComponent.get ();
78 0 : TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent);
79 :
80 : // a) component already exists - return it's number directly
81 0 : 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 0 : ::sal_Int32 nFreeNumber = impl_searchFreeNumber();
89 0 : 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 0 : TNumberedItem aItem;
94 0 : aItem.xItem = css::uno::WeakReference< css::uno::XInterface >(xComponent);
95 0 : aItem.nNumber = nFreeNumber;
96 0 : m_lComponents[pComponent] = aItem;
97 :
98 0 : return nFreeNumber;
99 :
100 : // <- SYNCHRONIZED
101 : }
102 :
103 :
104 0 : void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber)
105 : throw (css::lang::IllegalArgumentException,
106 : css::uno::RuntimeException, std::exception )
107 : {
108 : // SYNCHRONIZED ->
109 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
110 :
111 0 : if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER)
112 0 : throw css::lang::IllegalArgumentException ("Special valkud INVALID_NUMBER not allowed as input parameter.", m_xOwner.get(), 1);
113 :
114 0 : TDeadItemList lDeadItems;
115 0 : TNumberedItemHash::iterator pComponent;
116 :
117 0 : for ( pComponent = m_lComponents.begin ();
118 0 : pComponent != m_lComponents.end ();
119 : ++pComponent )
120 : {
121 0 : const TNumberedItem& rItem = pComponent->second;
122 0 : const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
123 :
124 0 : if ( ! xItem.is ())
125 : {
126 0 : lDeadItems.push_back(pComponent->first);
127 0 : continue;
128 : }
129 :
130 0 : if (rItem.nNumber == nNumber)
131 : {
132 0 : m_lComponents.erase (pComponent);
133 0 : break;
134 : }
135 0 : }
136 :
137 0 : impl_cleanUpDeadItems(m_lComponents, lDeadItems);
138 :
139 : // <- SYNCHRONIZED
140 0 : }
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, std::exception )
146 : {
147 : // SYNCHRONIZED ->
148 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
149 :
150 0 : if ( ! xComponent.is ())
151 0 : throw css::lang::IllegalArgumentException (OUString(ERRMSG_INVALID_COMPONENT_PARAM), m_xOwner.get(), 1);
152 :
153 0 : sal_IntPtr pComponent = (sal_IntPtr) 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 0 : OUString SAL_CALL NumberedCollection::getUntitledPrefix()
168 : throw (css::uno::RuntimeException, std::exception)
169 : {
170 : // SYNCHRONIZED ->
171 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
172 :
173 0 : 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 0 : ::sal_Int32 NumberedCollection::impl_searchFreeNumber ()
192 : {
193 : // create ordered list of all possible numbers.
194 0 : ::std::vector< ::sal_Int32 > lPossibleNumbers;
195 0 : ::sal_Int32 c = (::sal_Int32)m_lComponents.size ();
196 0 : ::sal_Int32 i = 1;
197 :
198 : // c cant be less then 0 ... otherwise hash.size() has an error :-)
199 : // But we need at least n+1 numbers here.
200 0 : c += 1;
201 :
202 0 : for (i=1; i<=c; ++i)
203 0 : lPossibleNumbers.push_back (i);
204 :
205 : // SYNCHRONIZED ->
206 0 : ::osl::ResettableMutexGuard aLock(m_aMutex);
207 :
208 0 : TDeadItemList lDeadItems;
209 0 : TNumberedItemHash::const_iterator pComponent;
210 :
211 0 : for ( pComponent = m_lComponents.begin ();
212 0 : pComponent != m_lComponents.end ();
213 : ++pComponent )
214 : {
215 0 : const TNumberedItem& rItem = pComponent->second;
216 0 : const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get();
217 :
218 0 : if ( ! xItem.is ())
219 : {
220 0 : lDeadItems.push_back(pComponent->first);
221 0 : continue;
222 : }
223 :
224 0 : ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber);
225 0 : if (pPossible != lPossibleNumbers.end ())
226 0 : lPossibleNumbers.erase (pPossible);
227 0 : }
228 :
229 0 : impl_cleanUpDeadItems(m_lComponents, lDeadItems);
230 :
231 : // a) non free numbers ... return INVALID_NUMBER
232 0 : if (lPossibleNumbers.size () < 1)
233 0 : return css::frame::UntitledNumbersConst::INVALID_NUMBER;
234 :
235 : // b) return first free number
236 0 : return *(lPossibleNumbers.begin ());
237 :
238 : // <- SYNCHRONIZED
239 : }
240 :
241 0 : void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash& lItems ,
242 : const TDeadItemList& lDeadItems)
243 : {
244 0 : TDeadItemList::const_iterator pIt;
245 :
246 0 : for ( pIt = lDeadItems.begin ();
247 0 : pIt != lDeadItems.end ();
248 : ++pIt )
249 : {
250 0 : const long& rDeadItem = *pIt;
251 0 : lItems.erase(rDeadItem);
252 : }
253 0 : }
254 :
255 : } // namespace comphelper
256 :
257 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|