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

Generated by: LCOV version 1.10