Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / componentmodule.hxx
blob05626ebaeceeba2ad023a3fcc9e3c2077e072aeb
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 .
19 #ifndef INCLUDED_COMPHELPER_COMPONENTMODULE_HXX
20 #define INCLUDED_COMPHELPER_COMPONENTMODULE_HXX
22 #include <comphelper/comphelperdllapi.h>
24 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
27 #include <cppuhelper/factory.hxx>
29 #include <osl/mutex.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/instance.hxx>
33 #include <memory>
36 namespace comphelper
40 /** factory factory declaration
42 typedef css::uno::Reference< css::lang::XSingleComponentFactory > (SAL_CALL *FactoryInstantiation)
44 ::cppu::ComponentFactoryFunc _pFactoryFunc,
45 OUString const& _rComponentName,
46 css::uno::Sequence< OUString > const & _rServiceNames,
47 rtl_ModuleCount*
51 //= ComponentDescription
53 struct COMPHELPER_DLLPUBLIC ComponentDescription
55 /// the implementation name of the component
56 OUString sImplementationName;
57 /// the services supported by the component implementation
58 css::uno::Sequence< OUString > aSupportedServices;
59 /// the function to create an instance of the component
60 ::cppu::ComponentFactoryFunc pComponentCreationFunc;
61 /// the function to create a factory for the component (usually <code>::cppu::createSingleComponentFactory</code>)
62 FactoryInstantiation pFactoryCreationFunc;
64 ComponentDescription(
65 const OUString& _rImplementationName,
66 const css::uno::Sequence< OUString >& _rSupportedServices,
67 ::cppu::ComponentFactoryFunc _pComponentCreationFunc,
68 FactoryInstantiation _pFactoryCreationFunc
70 :sImplementationName( _rImplementationName )
71 ,aSupportedServices( _rSupportedServices )
72 ,pComponentCreationFunc( _pComponentCreationFunc )
73 ,pFactoryCreationFunc( _pFactoryCreationFunc )
79 //= OModule
81 class OModuleImpl;
82 class COMPHELPER_DLLPUBLIC OModule
84 private:
85 oslInterlockedCount m_nClients; /// number of registered clients
86 std::unique_ptr<OModuleImpl> m_pImpl; /// impl class. lives as long as at least one client for the module is registered
88 protected:
89 mutable ::osl::Mutex m_aMutex; /// access safety
91 public:
92 OModule();
94 virtual ~OModule();
96 /** register a component implementing a service with the given data.
97 @param _rImplementationName
98 the implementation name of the component
99 @param _rServiceNames
100 the services the component supports
101 @param _pCreateFunction
102 a function for creating an instance of the component
103 @param _pFactoryFunction
104 a function for creating a factory for that component
106 void registerImplementation(
107 const OUString& _rImplementationName,
108 const css::uno::Sequence< OUString >& _rServiceNames,
109 ::cppu::ComponentFactoryFunc _pCreateFunction );
111 /** registers a component given by ComponentDescription
113 void registerImplementation( const ComponentDescription& _rComp );
115 /** creates a Factory for the component with the given implementation name.
116 <p>Usually used from within component_getFactory.<p/>
117 @param _pImplementationName
118 the implementation name of the component
119 @return
120 the XInterface access to a factory for the component
122 css::uno::Reference< css::uno::XInterface > getComponentFactory(
123 const OUString& _rImplementationName );
125 /** version of getComponentFactory which directly takes the char argument you got in your component_getFactory call
127 void* getComponentFactory( const sal_Char* _pImplementationName );
129 public:
130 class ClientAccess { friend class OModuleClient; private: ClientAccess() { } };
131 /// register a client for the module
132 void registerClient( ClientAccess );
133 /// revoke a client for the module
134 void revokeClient( ClientAccess );
136 protected:
138 /** called when the last client has been revoked
139 @precond
140 <member>m_aMutex</member> is locked
142 virtual void onLastClient();
144 private:
145 OModule( const OModule& ) = delete;
146 OModule& operator=( const OModule& ) = delete;
150 //= OModuleClient
152 /** base class for objects which uses any global module-specific resources
154 class COMPHELPER_DLLPUBLIC OModuleClient
156 protected:
157 OModule& m_rModule;
159 public:
160 OModuleClient( OModule& _rModule ) :m_rModule( _rModule ) { m_rModule.registerClient( OModule::ClientAccess() ); }
161 ~OModuleClient() { m_rModule.revokeClient( OModule::ClientAccess() ); }
165 //= OAutoRegistration
167 template <class TYPE>
168 class OAutoRegistration
170 public:
171 /** automatically provides all component information to an OModule instance
172 <p>Assumed that the template argument has the three methods
173 <ul>
174 <li><code>static OUString getImplementationName_static()</code><li/>
175 <li><code>static css::uno::Sequence< OUString > getSupportedServiceNames_static()</code><li/>
176 <li><code>static css::uno::Reference< css::uno::XInterface >
177 Create(const css::uno::Reference< css::lang::XMultiServiceFactory >&)</code>
178 </li>
179 <ul/>
180 the instantiation of this object will automatically register the class via <member>OModule::registerImplementation</member>.
181 <p/>
182 The factory creation function used is <code>::cppu::createSingleComponentFactory</code>.
184 OAutoRegistration( OModule& _rModule );
187 template <class TYPE>
188 OAutoRegistration<TYPE>::OAutoRegistration( OModule& _rModule )
190 _rModule.registerImplementation(
191 TYPE::getImplementationName_static(),
192 TYPE::getSupportedServiceNames_static(),
193 TYPE::Create
197 } // namespace comphelper
200 #endif // INCLUDED_COMPHELPER_COMPONENTMODULE_HXX
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */