Line data Source code
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 : #ifndef INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
21 : #define INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
22 :
23 : /** you may find this file helpful if you implement a component (in it's own library) which can't use
24 : the usual infrastructure.<br/>
25 : More precise, you find helper classes to ease the use of resources and the registration of services.
26 : <p>
27 : You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
28 : which should be used as namespace for the classes defined herein.</p>
29 : */
30 :
31 : #include <osl/mutex.hxx>
32 : #include <tools/resid.hxx>
33 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35 : #include <com/sun/star/uno/Sequence.hxx>
36 : #include <com/sun/star/registry/XRegistryKey.hpp>
37 : #include <cppuhelper/factory.hxx>
38 : #include <rtl/string.hxx>
39 :
40 : class ResMgr;
41 :
42 :
43 : namespace COMPMOD_NAMESPACE
44 : {
45 :
46 :
47 : typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
48 : (
49 : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
50 : const OUString & _rComponentName,
51 : ::cppu::ComponentInstantiation _pCreateFunction,
52 : const ::com::sun::star::uno::Sequence< OUString > & _rServiceNames,
53 : rtl_ModuleCount*
54 : );
55 :
56 :
57 : //= OModule
58 :
59 : class OModuleImpl;
60 : class OModule
61 : {
62 : friend class OModuleResourceClient;
63 :
64 : private:
65 : OModule();
66 : // not implemented. OModule is a static class
67 :
68 : protected:
69 : // resource administration
70 : static ::osl::Mutex s_aMutex; /// access safety
71 : static sal_Int32 s_nClients; /// number of registered clients
72 : static OModuleImpl* s_pImpl; /// impl class. lives as long as at least one client for the module is registered
73 : static OString s_sResPrefix;
74 :
75 : // auto registration administration
76 : static ::com::sun::star::uno::Sequence< OUString >*
77 : s_pImplementationNames;
78 : static ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< OUString > >*
79 : s_pSupportedServices;
80 : static ::com::sun::star::uno::Sequence< sal_Int64 >*
81 : s_pCreationFunctionPointers;
82 : static ::com::sun::star::uno::Sequence< sal_Int64 >*
83 : s_pFactoryFunctionPointers;
84 :
85 : public:
86 : // can be set as long as no resource has been accessed ...
87 : static void setResourceFilePrefix(const OString& _rPrefix);
88 :
89 : /// get the vcl res manager of the module
90 : static ResMgr* getResManager();
91 :
92 : /** register a component implementing a service with the given data.
93 : @param _rImplementationName
94 : the implementation name of the component
95 : @param _rServiceNames
96 : the services the component supports
97 : @param _pCreateFunction
98 : a function for creating an instance of the component
99 : @param _pFactoryFunction
100 : a function for creating a factory for that component
101 : @see revokeComponent
102 : */
103 : static void registerComponent(
104 : const OUString& _rImplementationName,
105 : const ::com::sun::star::uno::Sequence< OUString >& _rServiceNames,
106 : ::cppu::ComponentInstantiation _pCreateFunction,
107 : FactoryInstantiation _pFactoryFunction);
108 :
109 : /** revoke the registration for the specified component
110 : @param _rImplementationName
111 : the implementation name of the component
112 : */
113 : static void revokeComponent(
114 : const OUString& _rImplementationName);
115 :
116 : /** creates a Factory for the component with the given implementation name.
117 : <p>Usually used from within component_getFactory.<p/>
118 : @param _rxServiceManager
119 : a pointer to an XMultiServiceFactory interface as got in component_getFactory
120 : @param _pImplementationName
121 : the implementation name of the component
122 : @return
123 : the XInterface access to a factory for the component
124 : */
125 : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
126 : const OUString& _rImplementationName,
127 : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
128 : );
129 :
130 : protected:
131 : /// register a client for the module
132 : static void registerClient();
133 : /// revoke a client for the module
134 : static void revokeClient();
135 :
136 : private:
137 : /** ensure that the impl class exists
138 : @precond m_aMutex is guarded when this method gets called
139 : */
140 : static void ensureImpl();
141 : };
142 :
143 :
144 : //= OModuleResourceClient
145 :
146 : /** base class for objects which uses any global module-specific resources
147 : */
148 : class OModuleResourceClient
149 : {
150 : public:
151 0 : OModuleResourceClient() { OModule::registerClient(); }
152 0 : ~OModuleResourceClient() { OModule::revokeClient(); }
153 : };
154 :
155 :
156 : //= ModuleRes
157 :
158 : /** specialized ResId, using the resource manager provided by the global module
159 : */
160 : class ModuleRes : public ::ResId
161 : {
162 : public:
163 0 : ModuleRes(sal_uInt16 _nId) : ResId(_nId, *OModule::getResManager()) { }
164 : };
165 :
166 :
167 : //= OMultiInstanceAutoRegistration
168 :
169 : template <class TYPE>
170 : class OMultiInstanceAutoRegistration
171 : {
172 : public:
173 : /** automatically registeres a multi instance component
174 : <p>Assumed that the template argument has the three methods
175 : <ul>
176 : <li><code>static OUString getImplementationName_Static()</code><li/>
177 : <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_Static()</code><li/>
178 : <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
179 : Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
180 : </li>
181 : <ul/>
182 : the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
183 : <p/>
184 : The factory creation function used is <code>::cppu::createSingleFactory</code>.
185 : @see OOneInstanceAutoRegistration
186 : */
187 : OMultiInstanceAutoRegistration();
188 : ~OMultiInstanceAutoRegistration();
189 : };
190 :
191 : template <class TYPE>
192 0 : OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
193 : {
194 0 : OModule::registerComponent(
195 : TYPE::getImplementationName_Static(),
196 : TYPE::getSupportedServiceNames_Static(),
197 : TYPE::Create,
198 : ::cppu::createSingleFactory
199 0 : );
200 0 : }
201 :
202 : template <class TYPE>
203 0 : OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
204 : {
205 0 : OModule::revokeComponent(TYPE::getImplementationName_Static());
206 0 : }
207 :
208 :
209 : //= OOneInstanceAutoRegistration
210 :
211 : template <class TYPE>
212 : class OOneInstanceAutoRegistration
213 : {
214 : public:
215 : /** automatically registeres a single instance component
216 : <p>Assumed that the template argument has the three methods
217 : <ul>
218 : <li><code>static OUString getImplementationName_Static()</code><li/>
219 : <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_Static()</code><li/>
220 : <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
221 : Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
222 : </li>
223 : <ul/>
224 : the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
225 : <p/>
226 : The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
227 : @see OOneInstanceAutoRegistration
228 : */
229 : OOneInstanceAutoRegistration();
230 : ~OOneInstanceAutoRegistration();
231 : };
232 :
233 : template <class TYPE>
234 : OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
235 : {
236 : OModule::registerComponent(
237 : TYPE::getImplementationName_Static(),
238 : TYPE::getSupportedServiceNames_Static(),
239 : TYPE::Create,
240 : ::cppu::createOneInstanceFactory
241 : );
242 : }
243 :
244 : template <class TYPE>
245 : OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
246 : {
247 : OModule::revokeComponent(TYPE::getImplementationName_Static());
248 : }
249 :
250 :
251 : } // namespace COMPMOD_NAMESPACE
252 :
253 :
254 : #endif // INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
255 :
256 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|