1*5b190011SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*5b190011SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*5b190011SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*5b190011SAndrew Rist * distributed with this work for additional information 6*5b190011SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*5b190011SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*5b190011SAndrew Rist * "License"); you may not use this file except in compliance 9*5b190011SAndrew Rist * with the License. You may obtain a copy of the License at 10*5b190011SAndrew Rist * 11*5b190011SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*5b190011SAndrew Rist * 13*5b190011SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*5b190011SAndrew Rist * software distributed under the License is distributed on an 15*5b190011SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*5b190011SAndrew Rist * KIND, either express or implied. See the License for the 17*5b190011SAndrew Rist * specific language governing permissions and limitations 18*5b190011SAndrew Rist * under the License. 19*5b190011SAndrew Rist * 20*5b190011SAndrew Rist *************************************************************/ 21*5b190011SAndrew Rist 22*5b190011SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_sd.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include "tools/SdGlobalResourceContainer.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <algorithm> 30cdf0e10cSrcweir #include <vector> 31cdf0e10cSrcweir 32cdf0e10cSrcweir using namespace ::com::sun::star; 33cdf0e10cSrcweir using namespace ::com::sun::star::uno; 34cdf0e10cSrcweir 35cdf0e10cSrcweir 36cdf0e10cSrcweir namespace sd { 37cdf0e10cSrcweir 38cdf0e10cSrcweir 39cdf0e10cSrcweir //===== SdGlobalResourceContainer::Implementation ============================= 40cdf0e10cSrcweir 41cdf0e10cSrcweir class SdGlobalResourceContainer::Implementation 42cdf0e10cSrcweir { 43cdf0e10cSrcweir private: 44cdf0e10cSrcweir friend class SdGlobalResourceContainer; 45cdf0e10cSrcweir static SdGlobalResourceContainer* mpInstance; 46cdf0e10cSrcweir 47cdf0e10cSrcweir ::osl::Mutex maMutex; 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** All instances of SdGlobalResource in this vector are owned by the 50cdf0e10cSrcweir container and will be destroyed when the container is destroyed. 51cdf0e10cSrcweir */ 52cdf0e10cSrcweir typedef ::std::vector<SdGlobalResource*> ResourceList; 53cdf0e10cSrcweir ResourceList maResources; 54cdf0e10cSrcweir 55cdf0e10cSrcweir typedef ::std::vector<boost::shared_ptr<SdGlobalResource> > SharedResourceList; 56cdf0e10cSrcweir SharedResourceList maSharedResources; 57cdf0e10cSrcweir 58cdf0e10cSrcweir typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList; 59cdf0e10cSrcweir XInterfaceResourceList maXInterfaceResources; 60cdf0e10cSrcweir }; 61cdf0e10cSrcweir 62cdf0e10cSrcweir 63cdf0e10cSrcweir 64cdf0e10cSrcweir 65cdf0e10cSrcweir // static 66cdf0e10cSrcweir SdGlobalResourceContainer& SdGlobalResourceContainer::Instance (void) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir DBG_ASSERT(Implementation::mpInstance!=NULL, 69cdf0e10cSrcweir "SdGlobalResourceContainer::Instance(): instance has been deleted"); 70cdf0e10cSrcweir // Maybe we should throw an exception when the instance has been deleted. 71cdf0e10cSrcweir return *Implementation::mpInstance; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir 74cdf0e10cSrcweir SdGlobalResourceContainer* 75cdf0e10cSrcweir SdGlobalResourceContainer::Implementation::mpInstance = NULL; 76cdf0e10cSrcweir 77cdf0e10cSrcweir 78cdf0e10cSrcweir 79cdf0e10cSrcweir 80cdf0e10cSrcweir //===== SdGlobalResourceContainer ============================================= 81cdf0e10cSrcweir 82cdf0e10cSrcweir void SdGlobalResourceContainer::AddResource ( 83cdf0e10cSrcweir ::std::auto_ptr<SdGlobalResource> pResource) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir ::osl::MutexGuard aGuard (mpImpl->maMutex); 86cdf0e10cSrcweir 87cdf0e10cSrcweir Implementation::ResourceList::iterator iResource; 88cdf0e10cSrcweir iResource = ::std::find ( 89cdf0e10cSrcweir mpImpl->maResources.begin(), 90cdf0e10cSrcweir mpImpl->maResources.end(), 91cdf0e10cSrcweir pResource.get()); 92cdf0e10cSrcweir if (iResource == mpImpl->maResources.end()) 93cdf0e10cSrcweir mpImpl->maResources.push_back(pResource.get()); 94cdf0e10cSrcweir else 95cdf0e10cSrcweir { 96cdf0e10cSrcweir // Because the given resource is an auto_ptr it is highly unlikely 97cdf0e10cSrcweir // that we come here. But who knows? 98cdf0e10cSrcweir DBG_ASSERT (false, 99cdf0e10cSrcweir "SdGlobalResourceContainer:AddResource(): Resource added twice."); 100cdf0e10cSrcweir } 101cdf0e10cSrcweir // We can not put the auto_ptr into the vector so we release the 102cdf0e10cSrcweir // auto_ptr and document that we take ownership explicitly. 103cdf0e10cSrcweir pResource.release(); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir 107cdf0e10cSrcweir 108cdf0e10cSrcweir 109cdf0e10cSrcweir void SdGlobalResourceContainer::AddResource ( 110cdf0e10cSrcweir ::boost::shared_ptr<SdGlobalResource> pResource) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir ::osl::MutexGuard aGuard (mpImpl->maMutex); 113cdf0e10cSrcweir 114cdf0e10cSrcweir Implementation::SharedResourceList::iterator iResource; 115cdf0e10cSrcweir iResource = ::std::find ( 116cdf0e10cSrcweir mpImpl->maSharedResources.begin(), 117cdf0e10cSrcweir mpImpl->maSharedResources.end(), 118cdf0e10cSrcweir pResource); 119cdf0e10cSrcweir if (iResource == mpImpl->maSharedResources.end()) 120cdf0e10cSrcweir mpImpl->maSharedResources.push_back(pResource); 121cdf0e10cSrcweir else 122cdf0e10cSrcweir { 123cdf0e10cSrcweir DBG_ASSERT (false, 124cdf0e10cSrcweir "SdGlobalResourceContainer:AddResource(): Resource added twice."); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir 129cdf0e10cSrcweir 130cdf0e10cSrcweir 131cdf0e10cSrcweir void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir ::osl::MutexGuard aGuard (mpImpl->maMutex); 134cdf0e10cSrcweir 135cdf0e10cSrcweir Implementation::XInterfaceResourceList::iterator iResource; 136cdf0e10cSrcweir iResource = ::std::find ( 137cdf0e10cSrcweir mpImpl->maXInterfaceResources.begin(), 138cdf0e10cSrcweir mpImpl->maXInterfaceResources.end(), 139cdf0e10cSrcweir rxResource); 140cdf0e10cSrcweir if (iResource == mpImpl->maXInterfaceResources.end()) 141cdf0e10cSrcweir mpImpl->maXInterfaceResources.push_back(rxResource); 142cdf0e10cSrcweir else 143cdf0e10cSrcweir { 144cdf0e10cSrcweir DBG_ASSERT (false, 145cdf0e10cSrcweir "SdGlobalResourceContainer:AddResource(): Resource added twice."); 146cdf0e10cSrcweir } 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir 150cdf0e10cSrcweir 151cdf0e10cSrcweir SdGlobalResourceContainer::SdGlobalResourceContainer (void) 152cdf0e10cSrcweir : mpImpl (new SdGlobalResourceContainer::Implementation()) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir Implementation::mpInstance = this; 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir 158cdf0e10cSrcweir 159cdf0e10cSrcweir 160cdf0e10cSrcweir SdGlobalResourceContainer::~SdGlobalResourceContainer (void) 161cdf0e10cSrcweir { 162cdf0e10cSrcweir ::osl::MutexGuard aGuard (mpImpl->maMutex); 163cdf0e10cSrcweir 164cdf0e10cSrcweir // Release the resources in reversed order of their addition to the 165cdf0e10cSrcweir // container. This is because a resource A added before resource B 166cdf0e10cSrcweir // may have been created due to a request of B. Thus B depends on A and 167cdf0e10cSrcweir // should be destroyed first. 168cdf0e10cSrcweir Implementation::ResourceList::reverse_iterator iResource; 169cdf0e10cSrcweir for (iResource = mpImpl->maResources.rbegin(); 170cdf0e10cSrcweir iResource != mpImpl->maResources.rend(); 171cdf0e10cSrcweir ++iResource) 172cdf0e10cSrcweir { 173cdf0e10cSrcweir delete *iResource; 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir // The SharedResourceList has not to be released manually. We just 177cdf0e10cSrcweir // assert resources that are still held by someone other than us. 178cdf0e10cSrcweir Implementation::SharedResourceList::reverse_iterator iSharedResource; 179cdf0e10cSrcweir for (iSharedResource = mpImpl->maSharedResources.rbegin(); 180cdf0e10cSrcweir iSharedResource != mpImpl->maSharedResources.rend(); 181cdf0e10cSrcweir ++iSharedResource) 182cdf0e10cSrcweir { 183cdf0e10cSrcweir if ( ! iSharedResource->unique()) 184cdf0e10cSrcweir { 185cdf0e10cSrcweir SdGlobalResource* pResource = iSharedResource->get(); 186cdf0e10cSrcweir OSL_TRACE(" %p %d", pResource, iSharedResource->use_count()); 187cdf0e10cSrcweir DBG_ASSERT(iSharedResource->unique(), 188cdf0e10cSrcweir "SdGlobalResource still held in ~SdGlobalResourceContainer"); 189cdf0e10cSrcweir } 190cdf0e10cSrcweir } 191cdf0e10cSrcweir 192cdf0e10cSrcweir Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource; 193cdf0e10cSrcweir for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin(); 194cdf0e10cSrcweir iXInterfaceResource != mpImpl->maXInterfaceResources.rend(); 195cdf0e10cSrcweir ++iXInterfaceResource) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY); 198cdf0e10cSrcweir *iXInterfaceResource = NULL; 199cdf0e10cSrcweir if (xComponent.is()) 200cdf0e10cSrcweir xComponent->dispose(); 201cdf0e10cSrcweir } 202cdf0e10cSrcweir 203cdf0e10cSrcweir DBG_ASSERT(Implementation::mpInstance == this, 204cdf0e10cSrcweir "~SdGlobalResourceContainer(): more than one instance of singleton"); 205cdf0e10cSrcweir Implementation::mpInstance = NULL; 206cdf0e10cSrcweir } 207cdf0e10cSrcweir 208cdf0e10cSrcweir 209cdf0e10cSrcweir 210cdf0e10cSrcweir 211cdf0e10cSrcweir } // end of namespace sd 212