1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski #ifndef SD_GLOBAL_RESOURCE_CONTAINER_HXX
25*b1cdbd2cSJim Jagielski #define SD_GLOBAL_RESOURCE_CONTAINER_HXX
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski #include "sdmod.hxx"
28*b1cdbd2cSJim Jagielski #include <memory>
29*b1cdbd2cSJim Jagielski #include <boost/shared_ptr.hpp>
30*b1cdbd2cSJim Jagielski #include <com/sun/star/uno/XInterface.hpp>
31*b1cdbd2cSJim Jagielski 
32*b1cdbd2cSJim Jagielski namespace css = ::com::sun::star;
33*b1cdbd2cSJim Jagielski 
34*b1cdbd2cSJim Jagielski namespace sd {
35*b1cdbd2cSJim Jagielski 
36*b1cdbd2cSJim Jagielski class SdGlobalResource
37*b1cdbd2cSJim Jagielski {
38*b1cdbd2cSJim Jagielski public:
~SdGlobalResource(void)39*b1cdbd2cSJim Jagielski     virtual ~SdGlobalResource (void) {};
40*b1cdbd2cSJim Jagielski };
41*b1cdbd2cSJim Jagielski 
42*b1cdbd2cSJim Jagielski /** The purpose of this container is to hold references to resources that
43*b1cdbd2cSJim Jagielski     are globally available to all interested objects and to destroy them
44*b1cdbd2cSJim Jagielski     when the sd module is destroyed.  Examples for resources can be
45*b1cdbd2cSJim Jagielski     containers of bitmaps or the container of master pages used by the
46*b1cdbd2cSJim Jagielski     MasterPagesSelector objects in the task panel.
47*b1cdbd2cSJim Jagielski 
48*b1cdbd2cSJim Jagielski     It works like a singleton in that there is one instance per sd module.
49*b1cdbd2cSJim Jagielski     Resources can be added (by themselves or their owners) to the
50*b1cdbd2cSJim Jagielski     container.  The main task of the container is the destruction of all
51*b1cdbd2cSJim Jagielski     resources that have been added to it.
52*b1cdbd2cSJim Jagielski 
53*b1cdbd2cSJim Jagielski     As you may note, there is no method to get a resource from the
54*b1cdbd2cSJim Jagielski     container.  It is the task of the resource to provide other means of
55*b1cdbd2cSJim Jagielski     access to it.
56*b1cdbd2cSJim Jagielski 
57*b1cdbd2cSJim Jagielski     The reason for this design is not to have to change the SdModule
58*b1cdbd2cSJim Jagielski     destructor every time when there is a new resource.  This is done by
59*b1cdbd2cSJim Jagielski     reversing the dependency between module and resource: the resource knows
60*b1cdbd2cSJim Jagielski     about the module--this container class to be more precisely--and tells
61*b1cdbd2cSJim Jagielski     it to destroy the resource when the sd module is at the end of its
62*b1cdbd2cSJim Jagielski     lifetime.
63*b1cdbd2cSJim Jagielski */
64*b1cdbd2cSJim Jagielski class SdGlobalResourceContainer
65*b1cdbd2cSJim Jagielski {
66*b1cdbd2cSJim Jagielski public:
67*b1cdbd2cSJim Jagielski     static SdGlobalResourceContainer& Instance (void);
68*b1cdbd2cSJim Jagielski 
69*b1cdbd2cSJim Jagielski     /** Add a resource to the container.  The ownership of the resource is
70*b1cdbd2cSJim Jagielski         transferred to the container.  The resource is destroyed when the
71*b1cdbd2cSJim Jagielski         container is destroyed, i.e. when the sd module is destroyed.
72*b1cdbd2cSJim Jagielski 
73*b1cdbd2cSJim Jagielski         When in doubt, use the shared_ptr variant of this method.
74*b1cdbd2cSJim Jagielski     */
75*b1cdbd2cSJim Jagielski     void AddResource (::std::auto_ptr<SdGlobalResource> pResource);
76*b1cdbd2cSJim Jagielski 
77*b1cdbd2cSJim Jagielski     /** Add a resource to the container.  By using a shared_ptr and
78*b1cdbd2cSJim Jagielski         releasing it only when the SgGlobalResourceContainer is destroyed
79*b1cdbd2cSJim Jagielski         the given resource is kept alive at least that long.  When at the
80*b1cdbd2cSJim Jagielski         time of the destruction of SgGlobalResourceContainer no other
81*b1cdbd2cSJim Jagielski         references exist the resource is destroyed as well.
82*b1cdbd2cSJim Jagielski     */
83*b1cdbd2cSJim Jagielski     void AddResource (::boost::shared_ptr<SdGlobalResource> pResource);
84*b1cdbd2cSJim Jagielski 
85*b1cdbd2cSJim Jagielski     /** Add a resource that is implemented as UNO object.  Destruction
86*b1cdbd2cSJim Jagielski         (when the sd modules is unloaded) is done by a) calling dispose()
87*b1cdbd2cSJim Jagielski         when the XComponent is supported and by b) releasing the reference.
88*b1cdbd2cSJim Jagielski     */
89*b1cdbd2cSJim Jagielski     void AddResource (const ::css::uno::Reference<css::uno::XInterface>& rxResource);
90*b1cdbd2cSJim Jagielski 
91*b1cdbd2cSJim Jagielski protected:
92*b1cdbd2cSJim Jagielski     friend class ::SdModule;
93*b1cdbd2cSJim Jagielski     friend class ::std::auto_ptr<SdGlobalResourceContainer>;
94*b1cdbd2cSJim Jagielski 
95*b1cdbd2cSJim Jagielski     class Implementation;
96*b1cdbd2cSJim Jagielski     ::std::auto_ptr<Implementation> mpImpl;
97*b1cdbd2cSJim Jagielski 
98*b1cdbd2cSJim Jagielski     SdGlobalResourceContainer (void);
99*b1cdbd2cSJim Jagielski     ~SdGlobalResourceContainer (void);
100*b1cdbd2cSJim Jagielski };
101*b1cdbd2cSJim Jagielski 
102*b1cdbd2cSJim Jagielski } // end of namespace sd
103*b1cdbd2cSJim Jagielski 
104*b1cdbd2cSJim Jagielski #endif
105