bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / inc / componentmodule.hxx
blobecd6d73a445072a7036a793d3c225c7743279561
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
21 #define INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
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>
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>
40 class ResMgr;
43 namespace COMPMOD_NAMESPACE
47 typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
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*
56 class OModuleImpl;
57 class OModule
59 friend class OModuleResourceClient;
61 private:
62 OModule() SAL_DELETED_FUNCTION; //TODO: get rid of this class
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;
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;
81 public:
82 // can be set as long as no resource has been accessed ...
83 static void setResourceFilePrefix(const OString& _rPrefix);
85 /// get the vcl res manager of the module
86 static ResMgr* getResManager();
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
99 static void registerComponent(
100 const OUString& _rImplementationName,
101 const ::com::sun::star::uno::Sequence< OUString >& _rServiceNames,
102 ::cppu::ComponentInstantiation _pCreateFunction,
103 FactoryInstantiation _pFactoryFunction);
105 /** revoke the registration for the specified component
106 @param _rImplementationName
107 the implementation name of the component
109 static void revokeComponent(
110 const OUString& _rImplementationName);
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
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
126 protected:
127 /// register a client for the module
128 static void registerClient();
129 /// revoke a client for the module
130 static void revokeClient();
132 private:
133 /** ensure that the impl class exists
134 @precond m_aMutex is guarded when this method gets called
136 static void ensureImpl();
140 // base class for objects which uses any global module-specific resources
141 class OModuleResourceClient
143 public:
144 OModuleResourceClient() { OModule::registerClient(); }
145 ~OModuleResourceClient() { OModule::revokeClient(); }
149 // specialized ResId, using the resource manager provided by the global module
150 class ModuleRes : public ::ResId
152 public:
153 ModuleRes(sal_uInt16 _nId) : ResId(_nId, *OModule::getResManager()) { }
157 template <class TYPE>
158 class OMultiInstanceAutoRegistration
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
175 OMultiInstanceAutoRegistration();
176 ~OMultiInstanceAutoRegistration();
179 template <class TYPE>
180 OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
182 OModule::registerComponent(
183 TYPE::getImplementationName_Static(),
184 TYPE::getSupportedServiceNames_Static(),
185 TYPE::Create,
186 ::cppu::createSingleFactory
190 template <class TYPE>
191 OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
193 OModule::revokeComponent(TYPE::getImplementationName_Static());
196 template <class TYPE>
197 class OOneInstanceAutoRegistration
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
214 OOneInstanceAutoRegistration();
215 ~OOneInstanceAutoRegistration();
218 template <class TYPE>
219 OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
221 OModule::registerComponent(
222 TYPE::getImplementationName_Static(),
223 TYPE::getSupportedServiceNames_Static(),
224 TYPE::Create,
225 ::cppu::createOneInstanceFactory
229 template <class TYPE>
230 OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
232 OModule::revokeComponent(TYPE::getImplementationName_Static());
236 } // namespace COMPMOD_NAMESPACE
239 #endif // INCLUDED_EXTENSIONS_SOURCE_INC_COMPONENTMODULE_HXX
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */