LCOV - code coverage report
Current view: top level - sd/source/ui/tools - SdGlobalResourceContainer.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 45 58 77.6 %
Date: 2015-06-13 12:38:46 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          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             : #include "tools/SdGlobalResourceContainer.hxx"
      21             : 
      22             : #include <algorithm>
      23             : #include <vector>
      24             : 
      25             : using namespace ::com::sun::star;
      26             : using namespace ::com::sun::star::uno;
      27             : 
      28             : namespace sd {
      29             : 
      30             : //===== SdGlobalResourceContainer::Implementation =============================
      31             : 
      32          32 : class SdGlobalResourceContainer::Implementation
      33             : {
      34             : private:
      35             :     friend class SdGlobalResourceContainer;
      36             :     static SdGlobalResourceContainer* mpInstance;
      37             : 
      38             :     ::osl::Mutex maMutex;
      39             : 
      40             :     /** All instances of SdGlobalResource in this vector are owned by the
      41             :         container and will be destroyed when the container is destroyed.
      42             :     */
      43             :     typedef ::std::vector<SdGlobalResource*> ResourceList;
      44             :     ResourceList maResources;
      45             : 
      46             :     typedef ::std::vector<boost::shared_ptr<SdGlobalResource> > SharedResourceList;
      47             :     SharedResourceList maSharedResources;
      48             : 
      49             :     typedef ::std::vector<Reference<XInterface> > XInterfaceResourceList;
      50             :     XInterfaceResourceList maXInterfaceResources;
      51             : };
      52             : 
      53             : // static
      54          32 : SdGlobalResourceContainer& SdGlobalResourceContainer::Instance()
      55             : {
      56             :     DBG_ASSERT(Implementation::mpInstance!=NULL,
      57             :         "SdGlobalResourceContainer::Instance(): instance has been deleted");
      58             :     // Maybe we should throw an exception when the instance has been deleted.
      59          32 :     return *Implementation::mpInstance;
      60             : }
      61             : 
      62             : SdGlobalResourceContainer*
      63             :     SdGlobalResourceContainer::Implementation::mpInstance = NULL;
      64             : 
      65             : //===== SdGlobalResourceContainer =============================================
      66             : 
      67          17 : void SdGlobalResourceContainer::AddResource (
      68             :     ::std::unique_ptr<SdGlobalResource> && pResource)
      69             : {
      70          17 :     ::osl::MutexGuard aGuard (mpImpl->maMutex);
      71             : 
      72          17 :     Implementation::ResourceList::iterator iResource;
      73             :     iResource = ::std::find (
      74          17 :         mpImpl->maResources.begin(),
      75          17 :         mpImpl->maResources.end(),
      76          51 :         pResource.get());
      77          17 :     if (iResource == mpImpl->maResources.end())
      78          17 :         mpImpl->maResources.push_back(pResource.get());
      79             :     else
      80             :     {
      81             :         // Because the given resource is a unique_ptr it is highly unlikely
      82             :         // that we come here.  But who knows?
      83             :         DBG_ASSERT (false,
      84             :             "SdGlobalResourceContainer:AddResource(): Resource added twice.");
      85             :     }
      86             :     // We can not put the unique_ptr into the vector so we release the
      87             :     // unique_ptr and document that we take ownership explicitly.
      88          17 :     pResource.release();
      89          17 : }
      90             : 
      91           0 : void SdGlobalResourceContainer::AddResource (
      92             :     ::boost::shared_ptr<SdGlobalResource> pResource)
      93             : {
      94           0 :     ::osl::MutexGuard aGuard (mpImpl->maMutex);
      95             : 
      96           0 :     Implementation::SharedResourceList::iterator iResource;
      97             :     iResource = ::std::find (
      98           0 :         mpImpl->maSharedResources.begin(),
      99           0 :         mpImpl->maSharedResources.end(),
     100           0 :         pResource);
     101           0 :     if (iResource == mpImpl->maSharedResources.end())
     102           0 :         mpImpl->maSharedResources.push_back(pResource);
     103             :     else
     104             :     {
     105             :         DBG_ASSERT (false,
     106             :             "SdGlobalResourceContainer:AddResource(): Resource added twice.");
     107           0 :     }
     108           0 : }
     109             : 
     110          15 : void SdGlobalResourceContainer::AddResource (const Reference<XInterface>& rxResource)
     111             : {
     112          15 :     ::osl::MutexGuard aGuard (mpImpl->maMutex);
     113             : 
     114          15 :     Implementation::XInterfaceResourceList::iterator iResource;
     115             :     iResource = ::std::find (
     116          15 :         mpImpl->maXInterfaceResources.begin(),
     117          15 :         mpImpl->maXInterfaceResources.end(),
     118          30 :         rxResource);
     119          15 :     if (iResource == mpImpl->maXInterfaceResources.end())
     120          15 :         mpImpl->maXInterfaceResources.push_back(rxResource);
     121             :     else
     122             :     {
     123             :         DBG_ASSERT (false,
     124             :             "SdGlobalResourceContainer:AddResource(): Resource added twice.");
     125          15 :     }
     126          15 : }
     127             : 
     128          21 : SdGlobalResourceContainer::SdGlobalResourceContainer()
     129          21 :     : mpImpl (new SdGlobalResourceContainer::Implementation())
     130             : {
     131          21 :     Implementation::mpInstance = this;
     132          21 : }
     133             : 
     134          22 : SdGlobalResourceContainer::~SdGlobalResourceContainer()
     135             : {
     136          11 :     ::osl::MutexGuard aGuard (mpImpl->maMutex);
     137             : 
     138             :     // Release the resources in reversed order of their addition to the
     139             :     // container.  This is because a resource A added before resource B
     140             :     // may have been created due to a request of B.  Thus B depends on A and
     141             :     // should be destroyed first.
     142          11 :     Implementation::ResourceList::reverse_iterator iResource;
     143          72 :     for (iResource = mpImpl->maResources.rbegin();
     144          48 :          iResource != mpImpl->maResources.rend();
     145             :          ++iResource)
     146             :     {
     147          13 :         delete *iResource;
     148             :     }
     149             : 
     150             :     // The SharedResourceList has not to be released manually.  We just
     151             :     // assert resources that are still held by someone other than us.
     152          11 :     Implementation::SharedResourceList::reverse_iterator iSharedResource;
     153          33 :     for (iSharedResource = mpImpl->maSharedResources.rbegin();
     154          22 :          iSharedResource != mpImpl->maSharedResources.rend();
     155             :          ++iSharedResource)
     156             :     {
     157           0 :         if ( ! iSharedResource->unique())
     158             :         {
     159           0 :             SdGlobalResource* pResource = iSharedResource->get();
     160             :             SAL_INFO(
     161             :                 "sd.tools", pResource << " " << iSharedResource->use_count());
     162             :             DBG_ASSERT(iSharedResource->unique(),
     163             :                 "SdGlobalResource still held in ~SdGlobalResourceContainer");
     164             :         }
     165             :     }
     166             : 
     167          11 :     Implementation::XInterfaceResourceList::reverse_iterator iXInterfaceResource;
     168          63 :     for (iXInterfaceResource = mpImpl->maXInterfaceResources.rbegin();
     169          42 :          iXInterfaceResource != mpImpl->maXInterfaceResources.rend();
     170             :          ++iXInterfaceResource)
     171             :     {
     172          10 :         Reference<lang::XComponent> xComponent (*iXInterfaceResource, UNO_QUERY);
     173          10 :         *iXInterfaceResource = NULL;
     174          10 :         if (xComponent.is())
     175           0 :             xComponent->dispose();
     176          10 :     }
     177             : 
     178             :     DBG_ASSERT(Implementation::mpInstance == this,
     179             :         "~SdGlobalResourceContainer(): more than one instance of singleton");
     180          11 :     Implementation::mpInstance = NULL;
     181          11 : }
     182             : 
     183             : } // end of namespace sd
     184             : 
     185             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11