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