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 : class OModuleImpl;
57 : class OModule
58 : {
59 : friend class OModuleResourceClient;
60 :
61 : private:
62 : OModule() SAL_DELETED_FUNCTION; //TODO: get rid of this class
63 :
64 : protected:
65 : // resource administration
66 : static ::osl::Mutex s_aMutex; /// access safety
67 : static sal_Int32 s_nClients; /// number of registered clients
68 : static OModuleImpl* s_pImpl; /// impl class. lives as long as at least one client for the module is registered
69 : static OString s_sResPrefix;
70 :
71 : // auto registration administration
72 : static ::com::sun::star::uno::Sequence< OUString >*
73 : s_pImplementationNames;
74 : static ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< OUString > >*
75 : s_pSupportedServices;
76 : static ::com::sun::star::uno::Sequence< sal_Int64 >*
77 : s_pCreationFunctionPointers;
78 : static ::com::sun::star::uno::Sequence< sal_Int64 >*
79 : s_pFactoryFunctionPointers;
80 :
81 : public:
82 : // can be set as long as no resource has been accessed ...
83 : static void setResourceFilePrefix(const OString& _rPrefix);
84 :
85 : /// get the vcl res manager of the module
86 : static ResMgr* getResManager();
87 :
88 : /** register a component implementing a service with the given data.
89 : @param _rImplementationName
90 : the implementation name of the component
91 : @param _rServiceNames
92 : the services the component supports
93 : @param _pCreateFunction
94 : a function for creating an instance of the component
95 : @param _pFactoryFunction
96 : a function for creating a factory for that component
97 : @see revokeComponent
98 : */
99 : static void registerComponent(
100 : const OUString& _rImplementationName,
101 : const ::com::sun::star::uno::Sequence< OUString >& _rServiceNames,
102 : ::cppu::ComponentInstantiation _pCreateFunction,
103 : FactoryInstantiation _pFactoryFunction);
104 :
105 : /** revoke the registration for the specified component
106 : @param _rImplementationName
107 : the implementation name of the component
108 : */
109 : static void revokeComponent(
110 : const OUString& _rImplementationName);
111 :
112 : /** creates a Factory for the component with the given implementation name.
113 : <p>Usually used from within component_getFactory.<p/>
114 : @param _rxServiceManager
115 : a pointer to an XMultiServiceFactory interface as got in component_getFactory
116 : @param _pImplementationName
117 : the implementation name of the component
118 : @return
119 : the XInterface access to a factory for the component
120 : */
121 : static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
122 : const OUString& _rImplementationName,
123 : const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
124 : );
125 :
126 : protected:
127 : /// register a client for the module
128 : static void registerClient();
129 : /// revoke a client for the module
130 : static void revokeClient();
131 :
132 : private:
133 : /** ensure that the impl class exists
134 : @precond m_aMutex is guarded when this method gets called
135 : */
136 : static void ensureImpl();
137 : };
138 :
139 :
140 : // base class for objects which uses any global module-specific resources
141 : class OModuleResourceClient
142 : {
143 : public:
144 0 : OModuleResourceClient() { OModule::registerClient(); }
145 0 : ~OModuleResourceClient() { OModule::revokeClient(); }
146 : };
147 :
148 :
149 : // specialized ResId, using the resource manager provided by the global module
150 : class ModuleRes : public ::ResId
151 : {
152 : public:
153 0 : ModuleRes(sal_uInt16 _nId) : ResId(_nId, *OModule::getResManager()) { }
154 : };
155 :
156 :
157 : template <class TYPE>
158 : class OMultiInstanceAutoRegistration
159 : {
160 : public:
161 : /** automatically registeres a multi instance component
162 : <p>Assumed that the template argument has the three methods
163 : <ul>
164 : <li><code>static OUString getImplementationName_Static()</code><li/>
165 : <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_Static()</code><li/>
166 : <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
167 : Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
168 : </li>
169 : <ul/>
170 : the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
171 : <p/>
172 : The factory creation function used is <code>::cppu::createSingleFactory</code>.
173 : @see OOneInstanceAutoRegistration
174 : */
175 : OMultiInstanceAutoRegistration();
176 : ~OMultiInstanceAutoRegistration();
177 : };
178 :
179 : template <class TYPE>
180 0 : OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
181 : {
182 0 : OModule::registerComponent(
183 : TYPE::getImplementationName_Static(),
184 : TYPE::getSupportedServiceNames_Static(),
185 : TYPE::Create,
186 : ::cppu::createSingleFactory
187 0 : );
188 0 : }
189 :
190 : template <class TYPE>
191 0 : OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
192 : {
193 0 : OModule::revokeComponent(TYPE::getImplementationName_Static());
194 0 : }
195 :
196 : template <class TYPE>
197 : class OOneInstanceAutoRegistration
198 : {
199 : public:
200 : /** automatically registeres a single instance component
201 : <p>Assumed that the template argument has the three methods
202 : <ul>
203 : <li><code>static OUString getImplementationName_Static()</code><li/>
204 : <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_Static()</code><li/>
205 : <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
206 : Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
207 : </li>
208 : <ul/>
209 : the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
210 : <p/>
211 : The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
212 : @see OOneInstanceAutoRegistration
213 : */
214 : OOneInstanceAutoRegistration();
215 : ~OOneInstanceAutoRegistration();
216 : };
217 :
218 : template <class TYPE>
219 : OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
220 : {
221 : OModule::registerComponent(
222 : TYPE::getImplementationName_Static(),
223 : TYPE::getSupportedServiceNames_Static(),
224 : TYPE::Create,
225 : ::cppu::createOneInstanceFactory
226 : );
227 : }
228 :
229 : template <class TYPE>
230 : OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
231 : {
232 : OModule::revokeComponent(TYPE::getImplementationName_Static());
233 : }
234 :
235 :
236 : } // namespace COMPMOD_NAMESPACE
237 :
238 :
239 : #endif // INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
240 :
241 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|