/src/libreoffice/sd/source/ui/inc/tools/SdGlobalResourceContainer.hxx
Line | Count | Source |
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 | | #pragma once |
21 | | |
22 | | #include <memory> |
23 | | #include <sal/types.h> |
24 | | #include <o3tl/deleter.hxx> |
25 | | |
26 | | namespace com::sun::star::uno |
27 | | { |
28 | | template <class interface_type> class Reference; |
29 | | } |
30 | | namespace com::sun::star::uno |
31 | | { |
32 | | class XInterface; |
33 | | } |
34 | | |
35 | | namespace sd |
36 | | { |
37 | | class SdGlobalResource |
38 | | { |
39 | | public: |
40 | 0 | virtual ~SdGlobalResource() {} |
41 | | }; |
42 | | |
43 | | /** The purpose of this container is to hold references to resources that |
44 | | are globally available to all interested objects and to destroy them |
45 | | when the sd module is destroyed. Examples for resources can be |
46 | | containers of bitmaps or the container of master pages used by the |
47 | | MasterPagesSelector objects in the task panel. |
48 | | |
49 | | It works like a singleton in that there is one instance per sd module. |
50 | | Resources can be added (by themselves or their owners) to the |
51 | | container. The main task of the container is the destruction of all |
52 | | resources that have been added to it. |
53 | | |
54 | | As you may note, there is no method to get a resource from the |
55 | | container. It is the task of the resource to provide other means of |
56 | | access to it. |
57 | | |
58 | | The reason for this design is not to have to change the SdModule |
59 | | destructor every time when there is a new resource. This is done by |
60 | | reversing the dependency between module and resource: the resource knows |
61 | | about the module--this container class to be more precisely--and tells |
62 | | it to destroy the resource when the sd module is at the end of its |
63 | | lifetime. |
64 | | */ |
65 | | class SdGlobalResourceContainer final |
66 | | { |
67 | | public: |
68 | | static SdGlobalResourceContainer& Instance(); |
69 | | |
70 | | /** Add a resource to the container. The ownership of the resource is |
71 | | transferred to the container. The resource is destroyed when the |
72 | | container is destroyed, i.e. when the sd module is destroyed. |
73 | | |
74 | | When in doubt, use the shared_ptr variant of this method. |
75 | | */ |
76 | | void AddResource(::std::unique_ptr<SdGlobalResource> pResource); |
77 | | |
78 | | /** Add a resource to the container. By using a shared_ptr and |
79 | | releasing it only when the SgGlobalResourceContainer is destroyed |
80 | | the given resource is kept alive at least that long. When at the |
81 | | time of the destruction of SgGlobalResourceContainer no other |
82 | | references exist the resource is destroyed as well. |
83 | | */ |
84 | | void AddResource(const std::shared_ptr<SdGlobalResource>& pResource); |
85 | | |
86 | | /** Add a resource that is implemented as UNO object. Destruction |
87 | | (when the sd modules is unloaded) is done by a) calling dispose() |
88 | | when the XComponent is supported and by b) releasing the reference. |
89 | | */ |
90 | | void AddResource(const css::uno::Reference<css::uno::XInterface>& rxResource); |
91 | | |
92 | | private: |
93 | | friend class SdGlobalResourceContainerInstance; |
94 | | friend struct o3tl::default_delete<SdGlobalResourceContainer>; |
95 | | |
96 | | class Implementation; |
97 | | ::std::unique_ptr<Implementation> mpImpl; |
98 | | |
99 | | SdGlobalResourceContainer(); |
100 | | ~SdGlobalResourceContainer(); |
101 | | }; |
102 | | |
103 | | } // end of namespace sd |
104 | | |
105 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |