xref: /AOO41X/main/dbaccess/source/inc/registrationhelper.hxx (revision 2e2212a7c22e96cf6f6fab0dd042c34a45a64bd6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _REGISTRATIONHELPER_INCLUDED_INDIRECTLY_
25 #error "don't include this file directly! use dbu_reghelper.hxx instead!"
26 #endif
27 
28 typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
29         (
30             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
31             const ::rtl::OUString & _rComponentName,
32             ::cppu::ComponentInstantiation _pCreateFunction,
33             const ::com::sun::star::uno::Sequence< ::rtl::OUString > & _rServiceNames,
34             rtl_ModuleCount* _p
35         );
36 
37 //==========================================================================
38 class OModuleRegistration
39 {
40     static  ::com::sun::star::uno::Sequence< ::rtl::OUString >*
41         s_pImplementationNames;
42     static  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > >*
43         s_pSupportedServices;
44     static  ::com::sun::star::uno::Sequence< sal_Int64 >*
45         s_pCreationFunctionPointers;
46     static  ::com::sun::star::uno::Sequence< sal_Int64 >*
47         s_pFactoryFunctionPointers;
48 
49     // no direct instantiation, only static members/methods
OModuleRegistration()50     OModuleRegistration() { }
51 
52 public:
53     /** register a component implementing a service with the given data.
54         @param      _rImplementationName        the implementation name of the component
55         @param      _rServiceNames              the services the component supports
56         @param      _pCreateFunction            a function for creating an instance of the component
57         @param      _pFactoryFunction           a function for creating a factory for that component
58         @see revokeComponent
59     */
60     static void registerComponent(
61         const ::rtl::OUString& _rImplementationName,
62         const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
63         ::cppu::ComponentInstantiation _pCreateFunction,
64         FactoryInstantiation _pFactoryFunction);
65 
66     /** revoke the registration for the specified component
67         @param      _rImplementationName        the implementation name of the component
68     */
69     static void revokeComponent(
70         const ::rtl::OUString& _rImplementationName);
71 
72     /** creates a Factory for the component with the given implementation name. Usually used from within component_getFactory.
73         @param      _rxServiceManager       a pointer to an XMultiServiceFactory interface as got in component_getFactory
74         @param      _pImplementationName    the implementation name of the component
75         @return                             the XInterface access to a factory for the component
76     */
77     static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
78         const ::rtl::OUString& _rImplementationName,
79         const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
80         );
81 };
82 
83 //==========================================================================
84 template <class TYPE>
85 class OMultiInstanceAutoRegistration
86 {
87 public:
88     /** assumed that the template argument has the three methods<BR>
89         <code>static ::rtl::OUString getImplementationName_Static()</code><BR>
90         <code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><BR>
91         and<BR>
92         <code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
93             Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code><BR>
94         the instantiation of this object will automatically register the class via <code>OModuleRegistration::registerComponent</code>.
95         The factory creation function used is <code>::cppu::createSingleFactory</code>.<BR>
96         @see OOneInstanceAutoRegistration
97     */
98     OMultiInstanceAutoRegistration();
99     ~OMultiInstanceAutoRegistration();
100 };
101 
102 template <class TYPE>
OMultiInstanceAutoRegistration()103 OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
104 {
105     OModuleRegistration::registerComponent(
106         TYPE::getImplementationName_Static(),
107         TYPE::getSupportedServiceNames_Static(),
108         TYPE::Create,
109         ::cppu::createSingleFactory
110         );
111 }
112 
113 template <class TYPE>
~OMultiInstanceAutoRegistration()114 OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
115 {
116     OModuleRegistration::revokeComponent(TYPE::getImplementationName_Static());
117 }
118 
119 //==========================================================================
120 template <class TYPE>
121 class OOneInstanceAutoRegistration
122 {
123 public:
124     /** provided that the template argument has three methods<BR>
125         <code>static ::rtl::OUString getImplementationName_Static()</code><BR>
126         <code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><BR>
127         and<BR>
128         <code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
129             Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code><BR>
130         the instantiation of this object will automatically register the class via <code>OModuleRegistration::registerComponent</code>.
131         The factory creation function used is <code>::cppu::createSingleFactory</code>.<BR>
132         @see OMultiInstanceAutoRegistration
133     */
134     OOneInstanceAutoRegistration();
135     ~OOneInstanceAutoRegistration();
136 };
137 
138 template <class TYPE>
OOneInstanceAutoRegistration()139 OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
140 {
141     OModuleRegistration::registerComponent(
142         TYPE::getImplementationName_Static(),
143         TYPE::getSupportedServiceNames_Static(),
144         TYPE::Create,
145         ::cppu::createOneInstanceFactory
146         );
147 }
148 
149 template <class TYPE>
~OOneInstanceAutoRegistration()150 OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
151 {
152     OModuleRegistration::revokeComponent(TYPE::getImplementationName_Static());
153 }
154 
155