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