1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_comphelper.hxx" 26 27 #include <comphelper/numberedcollection.hxx> 28 29 //_______________________________________________ 30 // includes 31 32 #include <com/sun/star/frame/UntitledNumbersConst.hpp> 33 34 //_______________________________________________ 35 // namespace 36 37 namespace comphelper{ 38 39 namespace css = ::com::sun::star; 40 41 //_______________________________________________ 42 // definitions 43 44 static const ::rtl::OUString ERRMSG_INVALID_COMPONENT_PARAM = ::rtl::OUString::createFromAscii("NULL as component reference not allowed."); 45 static const ::rtl::OUString ERRMSG_INVALID_NUMBER_PARAM = ::rtl::OUString::createFromAscii("Special valkud INVALID_NUMBER not allowed as input parameter."); 46 47 //----------------------------------------------- 48 NumberedCollection::NumberedCollection() 49 : ::cppu::BaseMutex () 50 , m_sUntitledPrefix () 51 , m_lComponents () 52 , m_xOwner () 53 { 54 } 55 56 //----------------------------------------------- 57 NumberedCollection::~NumberedCollection() 58 { 59 } 60 61 //----------------------------------------------- 62 void NumberedCollection::setOwner(const css::uno::Reference< css::uno::XInterface >& xOwner) 63 { 64 // SYNCHRONIZED -> 65 ::osl::ResettableMutexGuard aLock(m_aMutex); 66 67 m_xOwner = xOwner; 68 69 // <- SYNCHRONIZED 70 } 71 72 //----------------------------------------------- 73 void NumberedCollection::setUntitledPrefix(const ::rtl::OUString& sPrefix) 74 { 75 // SYNCHRONIZED -> 76 ::osl::ResettableMutexGuard aLock(m_aMutex); 77 78 m_sUntitledPrefix = sPrefix; 79 80 // <- SYNCHRONIZED 81 } 82 83 //----------------------------------------------- 84 ::sal_Int32 SAL_CALL NumberedCollection::leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent) 85 throw (css::lang::IllegalArgumentException, 86 css::uno::RuntimeException ) 87 { 88 // SYNCHRONIZED -> 89 ::osl::ResettableMutexGuard aLock(m_aMutex); 90 91 if ( ! xComponent.is ()) 92 throw css::lang::IllegalArgumentException (ERRMSG_INVALID_COMPONENT_PARAM, m_xOwner.get(), 1); 93 94 long pComponent = (long) xComponent.get (); 95 TNumberedItemHash::const_iterator pIt = m_lComponents.find (pComponent); 96 97 // a) component already exists - return it's number directly 98 if (pIt != m_lComponents.end()) 99 return pIt->second.nNumber; 100 101 // b) component must be added new to this container 102 103 // b1) collection is full - no further components possible 104 // -> return INVALID_NUMBER 105 ::sal_Int32 nFreeNumber = impl_searchFreeNumber(); 106 if (nFreeNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER) 107 return css::frame::UntitledNumbersConst::INVALID_NUMBER; 108 109 // b2) add component to collection and return its number 110 TNumberedItem aItem; 111 aItem.xItem = css::uno::WeakReference< css::uno::XInterface >(xComponent); 112 aItem.nNumber = nFreeNumber; 113 m_lComponents[pComponent] = aItem; 114 115 return nFreeNumber; 116 117 // <- SYNCHRONIZED 118 } 119 120 //----------------------------------------------- 121 void SAL_CALL NumberedCollection::releaseNumber(::sal_Int32 nNumber) 122 throw (css::lang::IllegalArgumentException, 123 css::uno::RuntimeException ) 124 { 125 // SYNCHRONIZED -> 126 ::osl::ResettableMutexGuard aLock(m_aMutex); 127 128 if (nNumber == css::frame::UntitledNumbersConst::INVALID_NUMBER) 129 throw css::lang::IllegalArgumentException (ERRMSG_INVALID_NUMBER_PARAM, m_xOwner.get(), 1); 130 131 TDeadItemList lDeadItems; 132 TNumberedItemHash::iterator pComponent; 133 134 for ( pComponent = m_lComponents.begin (); 135 pComponent != m_lComponents.end (); 136 ++pComponent ) 137 { 138 const TNumberedItem& rItem = pComponent->second; 139 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get(); 140 141 if ( ! xItem.is ()) 142 { 143 lDeadItems.push_back(pComponent->first); 144 continue; 145 } 146 147 if (rItem.nNumber == nNumber) 148 { 149 m_lComponents.erase (pComponent); 150 break; 151 } 152 } 153 154 impl_cleanUpDeadItems(m_lComponents, lDeadItems); 155 156 // <- SYNCHRONIZED 157 } 158 159 //----------------------------------------------- 160 void SAL_CALL NumberedCollection::releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent) 161 throw (css::lang::IllegalArgumentException, 162 css::uno::RuntimeException ) 163 { 164 // SYNCHRONIZED -> 165 ::osl::ResettableMutexGuard aLock(m_aMutex); 166 167 if ( ! xComponent.is ()) 168 throw css::lang::IllegalArgumentException (ERRMSG_INVALID_COMPONENT_PARAM, m_xOwner.get(), 1); 169 170 long pComponent = (long) xComponent.get (); 171 TNumberedItemHash::iterator pIt = m_lComponents.find (pComponent); 172 173 // a) component exists and will be removed 174 if (pIt != m_lComponents.end()) 175 m_lComponents.erase(pIt); 176 177 // else 178 // b) component does not exists - nothing todo here (ignore request!) 179 180 // <- SYNCHRONIZED 181 } 182 183 //----------------------------------------------- 184 ::rtl::OUString SAL_CALL NumberedCollection::getUntitledPrefix() 185 throw (css::uno::RuntimeException) 186 { 187 // SYNCHRONIZED -> 188 ::osl::ResettableMutexGuard aLock(m_aMutex); 189 190 return m_sUntitledPrefix; 191 192 // <- SYNCHRONIZED 193 } 194 195 //----------------------------------------------- 196 /** create an ordered list of all possible numbers ... 197 e.g. {1,2,3,...,N} Max size of these list will be 198 current size of component list + 1 . 199 200 "+1" ... because in case all numbers in range 1..n 201 are in use we need a new number n+1 :-) 202 203 Every item which is already used as unique number 204 will be removed. At the end a list of e.g. {3,6,...,M} 205 exists where the first item represent the lowest free 206 number (in this example 3). 207 */ 208 ::sal_Int32 NumberedCollection::impl_searchFreeNumber () 209 { 210 // create ordered list of all possible numbers. 211 ::std::vector< ::sal_Int32 > lPossibleNumbers; 212 ::sal_Int32 c = (::sal_Int32)m_lComponents.size (); 213 ::sal_Int32 i = 1; 214 215 // c cant be less then 0 ... otherwhise hash.size() has an error :-) 216 // But we need at least n+1 numbers here. 217 c += 1; 218 219 for (i=1; i<=c; ++i) 220 lPossibleNumbers.push_back (i); 221 222 // SYNCHRONIZED -> 223 ::osl::ResettableMutexGuard aLock(m_aMutex); 224 225 TDeadItemList lDeadItems; 226 TNumberedItemHash::const_iterator pComponent; 227 228 for ( pComponent = m_lComponents.begin (); 229 pComponent != m_lComponents.end (); 230 ++pComponent ) 231 { 232 const TNumberedItem& rItem = pComponent->second; 233 const css::uno::Reference< css::uno::XInterface > xItem = rItem.xItem.get(); 234 235 if ( ! xItem.is ()) 236 { 237 lDeadItems.push_back(pComponent->first); 238 continue; 239 } 240 241 ::std::vector< ::sal_Int32 >::iterator pPossible = ::std::find(lPossibleNumbers.begin (), lPossibleNumbers.end (), rItem.nNumber); 242 if (pPossible != lPossibleNumbers.end ()) 243 lPossibleNumbers.erase (pPossible); 244 } 245 246 impl_cleanUpDeadItems(m_lComponents, lDeadItems); 247 248 // a) non free numbers ... return INVALID_NUMBER 249 if (lPossibleNumbers.size () < 1) 250 return css::frame::UntitledNumbersConst::INVALID_NUMBER; 251 252 // b) return first free number 253 return *(lPossibleNumbers.begin ()); 254 255 // <- SYNCHRONIZED 256 } 257 258 void NumberedCollection::impl_cleanUpDeadItems ( TNumberedItemHash& lItems , 259 const TDeadItemList& lDeadItems) 260 { 261 TDeadItemList::const_iterator pIt; 262 263 for ( pIt = lDeadItems.begin (); 264 pIt != lDeadItems.end (); 265 ++pIt ) 266 { 267 const long& rDeadItem = *pIt; 268 lItems.erase(rDeadItem); 269 } 270 } 271 272 } // namespace comphelper 273