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