xref: /trunk/main/dbaccess/source/inc/registrationhelper.hxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _REGISTRATIONHELPER_INCLUDED_INDIRECTLY_
29 #error "don't include this file directly! use dbu_reghelper.hxx instead!"
30 #endif
31 
32 typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
33         (
34             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
35             const ::rtl::OUString & _rComponentName,
36             ::cppu::ComponentInstantiation _pCreateFunction,
37             const ::com::sun::star::uno::Sequence< ::rtl::OUString > & _rServiceNames,
38             rtl_ModuleCount* _p
39         );
40 
41 //==========================================================================
42 class OModuleRegistration
43 {
44     static  ::com::sun::star::uno::Sequence< ::rtl::OUString >*
45         s_pImplementationNames;
46     static  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > >*
47         s_pSupportedServices;
48     static  ::com::sun::star::uno::Sequence< sal_Int64 >*
49         s_pCreationFunctionPointers;
50     static  ::com::sun::star::uno::Sequence< sal_Int64 >*
51         s_pFactoryFunctionPointers;
52 
53     // no direct instantiation, only static members/methods
54     OModuleRegistration() { }
55 
56 public:
57     /** register a component implementing a service with the given data.
58         @param      _rImplementationName        the implementation name of the component
59         @param      _rServiceNames              the services the component supports
60         @param      _pCreateFunction            a function for creating an instance of the component
61         @param      _pFactoryFunction           a function for creating a factory for that component
62         @see revokeComponent
63     */
64     static void registerComponent(
65         const ::rtl::OUString& _rImplementationName,
66         const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
67         ::cppu::ComponentInstantiation _pCreateFunction,
68         FactoryInstantiation _pFactoryFunction);
69 
70     /** revoke the registration for the specified component
71         @param      _rImplementationName        the implementation name of the component
72     */
73     static void revokeComponent(
74         const ::rtl::OUString& _rImplementationName);
75 
76     /** creates a Factory for the component with the given implementation name. Usually used from within component_getFactory.
77         @param      _rxServiceManager       a pointer to an XMultiServiceFactory interface as got in component_getFactory
78         @param      _pImplementationName    the implementation name of the component
79         @return                             the XInterface access to a factory for the component
80     */
81     static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
82         const ::rtl::OUString& _rImplementationName,
83         const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
84         );
85 };
86 
87 //==========================================================================
88 template <class TYPE>
89 class OMultiInstanceAutoRegistration
90 {
91 public:
92     /** assumed that the template argument has the three methods<BR>
93         <code>static ::rtl::OUString getImplementationName_Static()</code><BR>
94         <code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><BR>
95         and<BR>
96         <code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
97             Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code><BR>
98         the instantiation of this object will automatically register the class via <code>OModuleRegistration::registerComponent</code>.
99         The factory creation function used is <code>::cppu::createSingleFactory</code>.<BR>
100         @see OOneInstanceAutoRegistration
101     */
102     OMultiInstanceAutoRegistration();
103     ~OMultiInstanceAutoRegistration();
104 };
105 
106 template <class TYPE>
107 OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
108 {
109     OModuleRegistration::registerComponent(
110         TYPE::getImplementationName_Static(),
111         TYPE::getSupportedServiceNames_Static(),
112         TYPE::Create,
113         ::cppu::createSingleFactory
114         );
115 }
116 
117 template <class TYPE>
118 OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
119 {
120     OModuleRegistration::revokeComponent(TYPE::getImplementationName_Static());
121 }
122 
123 //==========================================================================
124 template <class TYPE>
125 class OOneInstanceAutoRegistration
126 {
127 public:
128     /** provided that the template argument has three methods<BR>
129         <code>static ::rtl::OUString getImplementationName_Static()</code><BR>
130         <code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><BR>
131         and<BR>
132         <code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
133             Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code><BR>
134         the instantiation of this object will automatically register the class via <code>OModuleRegistration::registerComponent</code>.
135         The factory creation function used is <code>::cppu::createSingleFactory</code>.<BR>
136         @see OMultiInstanceAutoRegistration
137     */
138     OOneInstanceAutoRegistration();
139     ~OOneInstanceAutoRegistration();
140 };
141 
142 template <class TYPE>
143 OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
144 {
145     OModuleRegistration::registerComponent(
146         TYPE::getImplementationName_Static(),
147         TYPE::getSupportedServiceNames_Static(),
148         TYPE::Create,
149         ::cppu::createOneInstanceFactory
150         );
151 }
152 
153 template <class TYPE>
154 OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
155 {
156     OModuleRegistration::revokeComponent(TYPE::getImplementationName_Static());
157 }
158 
159