bump product version to 4.1.6.2
[LibreOffice.git] / framework / inc / macros / xserviceinfo.hxx
blob6bf913af1e1411bf498ccb66048de9dc28a17dd4
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 __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
21 #define __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
23 #include <general.h>
25 #include <com/sun/star/uno/Exception.hpp>
26 #include <com/sun/star/uno/RuntimeException.hpp>
27 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <com/sun/star/uno/Type.hxx>
34 #include <com/sun/star/uno/XComponentContext.hpp>
35 #include <cppuhelper/factory.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <comphelper/sequence.hxx>
38 #include <rtl/ustring.hxx>
39 #include <rtl/logfile.hxx>
41 namespace framework{
43 /*_________________________________________________________________________________________________________________
45 macros for declaration and definition of XServiceInfo
46 Please use follow public macros only!
48 1) DECLARE_XSERVICEINFO => use it to declare XServiceInfo in your header
49 2) DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for multi service mode
50 3) DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for one instance service mode
51 4) DEFINE_INIT_SERVICE( CLASS ) => use it to implement your own impl_initService() method, which is neccessary for initializeing object by using his own reference!
53 _________________________________________________________________________________________________________________*/
55 //*****************************************************************************************************************
56 // private
57 // implementation of XServiceInfo and helper functions
58 //*****************************************************************************************************************
59 #define PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
60 /*===========================================================================================================*/ \
61 /* XServiceInfo */ \
62 /*===========================================================================================================*/ \
63 OUString SAL_CALL CLASS::getImplementationName() throw( css::uno::RuntimeException ) \
64 { \
65 return impl_getStaticImplementationName(); \
66 } \
68 /*===========================================================================================================*/ \
69 /* XServiceInfo */ \
70 /*===========================================================================================================*/ \
71 sal_Bool SAL_CALL CLASS::supportsService( const OUString& sServiceName ) throw( css::uno::RuntimeException ) \
72 { \
73 return ::comphelper::findValue(getSupportedServiceNames(), sServiceName, sal_True).getLength() != 0; \
74 } \
76 /*===========================================================================================================*/ \
77 /* XServiceInfo */ \
78 /*===========================================================================================================*/ \
79 css::uno::Sequence< OUString > SAL_CALL CLASS::getSupportedServiceNames() throw( css::uno::RuntimeException ) \
80 { \
81 return impl_getStaticSupportedServiceNames(); \
82 } \
84 /*===========================================================================================================*/ \
85 /* Helper for XServiceInfo */ \
86 /*===========================================================================================================*/ \
87 css::uno::Sequence< OUString > CLASS::impl_getStaticSupportedServiceNames() \
88 { \
89 css::uno::Sequence< OUString > seqServiceNames( 1 ); \
90 seqServiceNames.getArray() [0] = SERVICENAME ; \
91 return seqServiceNames; \
92 } \
94 /*===========================================================================================================*/ \
95 /* Helper for XServiceInfo */ \
96 /*===========================================================================================================*/ \
97 OUString CLASS::impl_getStaticImplementationName() \
98 { \
99 return IMPLEMENTATIONNAME ; \
102 #define PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
103 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
104 /*===========================================================================================================*/ \
105 /* Helper for registry */ \
106 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
107 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
108 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
109 /* use "impl_initService()" method. */ \
110 /*===========================================================================================================*/ \
111 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ) \
113 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework","Ocke.Janssen@sun.com",U2B(IMPLEMENTATIONNAME).getStr()); \
114 /* create new instance of service */ \
115 CLASS* pClass = new CLASS( xServiceManager ); \
116 /* hold it alive by increasing his ref count!!! */ \
117 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
118 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
119 pClass->impl_initService(); \
120 /* return new created service as reference */ \
121 return xService; \
124 #define PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
125 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
126 /*===========================================================================================================*/ \
127 /* Helper for registry */ \
128 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
129 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
130 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
131 /* use "impl_initService()" method. */ \
132 /*===========================================================================================================*/ \
133 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )\
134 throw( css::uno::Exception ) \
136 /* retrieve component context from the given service manager */ \
137 css::uno::Reference< css::uno::XComponentContext > xComponentContext( \
138 comphelper::getComponentContext( xServiceManager ) ); \
139 /* create new instance of service */ \
140 CLASS* pClass = new CLASS( xComponentContext ); \
141 /* hold it alive by increasing his ref count!!! */ \
142 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
143 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
144 pClass->impl_initService(); \
145 /* return new created service as reference */ \
146 return xService; \
149 //*****************************************************************************************************************
150 // private
151 // definition of helper function createFactory() for multiple services
152 //*****************************************************************************************************************
153 #define PRIVATE_DEFINE_SINGLEFACTORY( CLASS ) \
154 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
156 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createSingleFactory ( xServiceManager , \
157 CLASS::impl_getStaticImplementationName() , \
158 CLASS::impl_createInstance , \
159 CLASS::impl_getStaticSupportedServiceNames() \
161 ); \
162 return xReturn; \
165 //*****************************************************************************************************************
166 // private
167 // definition of helper function createFactory() for one instance services
168 //*****************************************************************************************************************
169 #define PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS ) \
170 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
172 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createOneInstanceFactory ( xServiceManager , \
173 CLASS::impl_getStaticImplementationName() , \
174 CLASS::impl_createInstance , \
175 CLASS::impl_getStaticSupportedServiceNames() \
177 ); \
178 return xReturn; \
181 //*****************************************************************************************************************
182 // public
183 // declaration of XServiceInfo and helper functions
184 //*****************************************************************************************************************
186 #define DECLARE_XSERVICEINFO_NOFACTORY \
187 /* interface XServiceInfo */ \
188 virtual OUString SAL_CALL getImplementationName ( ) throw( css::uno::RuntimeException ); \
189 virtual sal_Bool SAL_CALL supportsService ( const OUString& sServiceName ) throw( css::uno::RuntimeException ); \
190 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames ( ) throw( css::uno::RuntimeException ); \
191 /* Helper for XServiceInfo */ \
192 static css::uno::Sequence< OUString > SAL_CALL impl_getStaticSupportedServiceNames( ); \
193 static OUString SAL_CALL impl_getStaticImplementationName ( ); \
194 /* Helper for initialization of service by using own reference! */ \
195 virtual void SAL_CALL impl_initService ( ); \
197 #define DECLARE_XSERVICEINFO \
198 DECLARE_XSERVICEINFO_NOFACTORY \
199 /* Helper for registry */ \
200 static css::uno::Reference< css::uno::XInterface > SAL_CALL impl_createInstance ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ); \
201 static css::uno::Reference< css::lang::XSingleServiceFactory > SAL_CALL impl_createFactory ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ); \
203 //*****************************************************************************************************************
204 // public
205 // implementation of XServiceInfo
206 //*****************************************************************************************************************
207 #define DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
208 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
209 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
211 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
212 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
213 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
215 #define DEFINE_XSERVICEINFO_MULTISERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
216 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
217 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
219 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
220 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
221 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
223 //*****************************************************************************************************************
224 // public
225 // implementation of service initialize!
226 // example of using: DEFINE_INIT_SERVICE( MyClassName,
227 // {
228 // ...
229 // Reference< XInterface > xThis( this, UNO_QUERY );
230 // myMember* pMember = new myMember( xThis );
231 // ...
232 // }
233 // )
234 //*****************************************************************************************************************
235 #define DEFINE_INIT_SERVICE( CLASS, FUNCTIONBODY ) \
236 void SAL_CALL CLASS::impl_initService() \
238 FUNCTIONBODY \
241 } // namespace framework
243 #endif // #ifndef __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */