1*9877b273SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*9877b273SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*9877b273SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*9877b273SAndrew Rist * distributed with this work for additional information 6*9877b273SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*9877b273SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*9877b273SAndrew Rist * "License"); you may not use this file except in compliance 9*9877b273SAndrew Rist * with the License. You may obtain a copy of the License at 10*9877b273SAndrew Rist * 11*9877b273SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*9877b273SAndrew Rist * 13*9877b273SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*9877b273SAndrew Rist * software distributed under the License is distributed on an 15*9877b273SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*9877b273SAndrew Rist * KIND, either express or implied. See the License for the 17*9877b273SAndrew Rist * specific language governing permissions and limitations 18*9877b273SAndrew Rist * under the License. 19*9877b273SAndrew Rist * 20*9877b273SAndrew Rist *************************************************************/ 21*9877b273SAndrew Rist 22*9877b273SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef _COMPHELPER_NUMBEREDCOLLECTION_HXX_ 25cdf0e10cSrcweir #define _COMPHELPER_NUMBEREDCOLLECTION_HXX_ 26cdf0e10cSrcweir 27cdf0e10cSrcweir //_______________________________________________ 28cdf0e10cSrcweir // includes 29cdf0e10cSrcweir 30cdf0e10cSrcweir #include "comphelper/comphelperdllapi.h" 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx> 33cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 34cdf0e10cSrcweir #include <com/sun/star/uno/XInterface.hpp> 35cdf0e10cSrcweir #include <com/sun/star/frame/XUntitledNumbers.hpp> 36cdf0e10cSrcweir 37cdf0e10cSrcweir #include <cppuhelper/basemutex.hxx> 38cdf0e10cSrcweir #include <cppuhelper/weakref.hxx> 39cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 40cdf0e10cSrcweir 41cdf0e10cSrcweir #include <vector> 42cdf0e10cSrcweir #include <hash_map> 43cdf0e10cSrcweir 44cdf0e10cSrcweir //_______________________________________________ 45cdf0e10cSrcweir // namespace 46cdf0e10cSrcweir 47cdf0e10cSrcweir namespace comphelper{ 48cdf0e10cSrcweir 49cdf0e10cSrcweir #ifdef css 50cdf0e10cSrcweir #error "Ambigious namespace definition of css." 51cdf0e10cSrcweir #else 52cdf0e10cSrcweir #define css ::com::sun::star 53cdf0e10cSrcweir #endif 54cdf0e10cSrcweir 55cdf0e10cSrcweir //_______________________________________________ 56cdf0e10cSrcweir // definitions 57cdf0e10cSrcweir 58cdf0e10cSrcweir /** @short defines a collection of UNO components, where every component will get it's own unique number. 59cdf0e10cSrcweir 60cdf0e10cSrcweir @descr Such number will be unique at runtime only ... but it supports fragmentation. 61cdf0e10cSrcweir Note: This collection uses weak refrences only to know her components. 62cdf0e10cSrcweir So lifetime of thise components must be controlled outside. 63cdf0e10cSrcweir 64cdf0e10cSrcweir @threadsafe 65cdf0e10cSrcweir */ 66cdf0e10cSrcweir class COMPHELPER_DLLPUBLIC NumberedCollection : private ::cppu::BaseMutex 67cdf0e10cSrcweir , public ::cppu::WeakImplHelper1< css::frame::XUntitledNumbers > 68cdf0e10cSrcweir { 69cdf0e10cSrcweir //------------------------------------------- 70cdf0e10cSrcweir // types, const 71cdf0e10cSrcweir private: 72cdf0e10cSrcweir 73cdf0e10cSrcweir struct TNumberedItem 74cdf0e10cSrcweir { 75cdf0e10cSrcweir css::uno::WeakReference< css::uno::XInterface > xItem; 76cdf0e10cSrcweir ::sal_Int32 nNumber; 77cdf0e10cSrcweir }; 78cdf0e10cSrcweir 79cdf0e10cSrcweir typedef ::std::hash_map< 80cdf0e10cSrcweir long , 81cdf0e10cSrcweir TNumberedItem , 82cdf0e10cSrcweir ::std::hash< long > , 83cdf0e10cSrcweir ::std::equal_to< long > > TNumberedItemHash; 84cdf0e10cSrcweir 85cdf0e10cSrcweir typedef ::std::vector< long > TDeadItemList; 86cdf0e10cSrcweir 87cdf0e10cSrcweir //------------------------------------------- 88cdf0e10cSrcweir // interface 89cdf0e10cSrcweir public: 90cdf0e10cSrcweir 91cdf0e10cSrcweir //--------------------------------------- 92cdf0e10cSrcweir /** @short lightweight constructor. 93cdf0e10cSrcweir */ 94cdf0e10cSrcweir NumberedCollection(); 95cdf0e10cSrcweir 96cdf0e10cSrcweir //--------------------------------------- 97cdf0e10cSrcweir /** @short free all internaly used resources. 98cdf0e10cSrcweir */ 99cdf0e10cSrcweir virtual ~NumberedCollection(); 100cdf0e10cSrcweir 101cdf0e10cSrcweir //--------------------------------------- 102cdf0e10cSrcweir /** set an outside component which uses this container and must be set 103cdf0e10cSrcweir as source of all broadcasted messages, exceptions. 104cdf0e10cSrcweir 105cdf0e10cSrcweir It's holded weak only so we do not need any complex dispose sessions. 106cdf0e10cSrcweir 107cdf0e10cSrcweir Note: Passing NULL as parameter will be allowed. It will reset the internal 108cdf0e10cSrcweir member reference only. 109cdf0e10cSrcweir 110cdf0e10cSrcweir @param xOwner 111cdf0e10cSrcweir the new owner of this collection. 112cdf0e10cSrcweir */ 113cdf0e10cSrcweir void setOwner (const css::uno::Reference< css::uno::XInterface >& xOwner); 114cdf0e10cSrcweir 115cdf0e10cSrcweir //--------------------------------------- 116cdf0e10cSrcweir /** set the localized prefix to be used for untitled components. 117cdf0e10cSrcweir 118cdf0e10cSrcweir Localization has to be done outside. This container will return 119cdf0e10cSrcweir those value then. There are no further checks. Its up to you to define 120cdf0e10cSrcweir a suitable string here :-) 121cdf0e10cSrcweir 122cdf0e10cSrcweir @param sPrefix 123cdf0e10cSrcweir the new prefix for untitled components. 124cdf0e10cSrcweir */ 125cdf0e10cSrcweir void setUntitledPrefix(const ::rtl::OUString& sPrefix); 126cdf0e10cSrcweir 127cdf0e10cSrcweir //--------------------------------------- 128cdf0e10cSrcweir /** @see css.frame.XUntitledNumbers */ 129cdf0e10cSrcweir virtual ::sal_Int32 SAL_CALL leaseNumber(const css::uno::Reference< css::uno::XInterface >& xComponent) 130cdf0e10cSrcweir throw (css::lang::IllegalArgumentException, 131cdf0e10cSrcweir css::uno::RuntimeException ); 132cdf0e10cSrcweir 133cdf0e10cSrcweir //--------------------------------------- 134cdf0e10cSrcweir /** @see css.frame.XUntitledNumbers */ 135cdf0e10cSrcweir virtual void SAL_CALL releaseNumber(::sal_Int32 nNumber) 136cdf0e10cSrcweir throw (css::lang::IllegalArgumentException, 137cdf0e10cSrcweir css::uno::RuntimeException ); 138cdf0e10cSrcweir 139cdf0e10cSrcweir //--------------------------------------- 140cdf0e10cSrcweir /** @see css.frame.XUntitledNumbers */ 141cdf0e10cSrcweir virtual void SAL_CALL releaseNumberForComponent(const css::uno::Reference< css::uno::XInterface >& xComponent) 142cdf0e10cSrcweir throw (css::lang::IllegalArgumentException, 143cdf0e10cSrcweir css::uno::RuntimeException ); 144cdf0e10cSrcweir 145cdf0e10cSrcweir //--------------------------------------- 146cdf0e10cSrcweir /** @see css.frame.XUntitledNumbers */ 147cdf0e10cSrcweir virtual ::rtl::OUString SAL_CALL getUntitledPrefix() 148cdf0e10cSrcweir throw (css::uno::RuntimeException); 149cdf0e10cSrcweir 150cdf0e10cSrcweir //------------------------------------------- 151cdf0e10cSrcweir // internal 152cdf0e10cSrcweir private: 153cdf0e10cSrcweir 154cdf0e10cSrcweir //--------------------------------------- 155cdf0e10cSrcweir /** @short trys to find an unique number not already used within this collection. 156cdf0e10cSrcweir 157cdf0e10cSrcweir @descr It reuses the smalles number which isnt used by any component 158cdf0e10cSrcweir of this collection. (fragmentation!) If collection is full (means there 159cdf0e10cSrcweir is no free number) the special value INVALID_NUMBER will be returned. 160cdf0e10cSrcweir 161cdf0e10cSrcweir @note Those method cant be called within a multithreaded environment .. 162cdf0e10cSrcweir Because such number wont be "reserved" for the calli of these method 163cdf0e10cSrcweir it can happen that two calls returns the same number (reasoned by the fact that first calli 164cdf0e10cSrcweir doesnt used the returned number already. 165cdf0e10cSrcweir 166cdf0e10cSrcweir So the outside code has to make sure that retrieving and using of those number 167cdf0e10cSrcweir will be an atomic operation. 168cdf0e10cSrcweir 169cdf0e10cSrcweir @return an unique number or special value INVALID_NUMBER if collection is full. 170cdf0e10cSrcweir */ 171cdf0e10cSrcweir ::sal_Int32 impl_searchFreeNumber (); 172cdf0e10cSrcweir 173cdf0e10cSrcweir void impl_cleanUpDeadItems ( TNumberedItemHash& lItems , 174cdf0e10cSrcweir const TDeadItemList& lDeadItems); 175cdf0e10cSrcweir 176cdf0e10cSrcweir //------------------------------------------- 177cdf0e10cSrcweir // member 178cdf0e10cSrcweir private: 179cdf0e10cSrcweir 180cdf0e10cSrcweir /// localized string to be used for untitled components 181cdf0e10cSrcweir ::rtl::OUString m_sUntitledPrefix; 182cdf0e10cSrcweir 183cdf0e10cSrcweir /// cache of all "leased numbers" and its bound components 184cdf0e10cSrcweir TNumberedItemHash m_lComponents; 185cdf0e10cSrcweir 186cdf0e10cSrcweir /// used as source of broadcasted messages or exceptions (can be null !) 187cdf0e10cSrcweir css::uno::WeakReference< css::uno::XInterface > m_xOwner; 188cdf0e10cSrcweir }; 189cdf0e10cSrcweir 190cdf0e10cSrcweir #undef css 191cdf0e10cSrcweir 192cdf0e10cSrcweir } // namespace comphelper 193cdf0e10cSrcweir 194cdf0e10cSrcweir #endif // _COMPHELPER_NUMBEREDCOLLECTION_HXX_ 195