Update ooo320-m1
[ooovba.git] / framework / inc / macros / xserviceinfo.hxx
blobee2880a5547ece2299d892c24c45a87dddb6a2a9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xserviceinfo.hxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
32 #define __FRAMEWORK_MACROS_XSERVICEINFO_HXX_
34 //_________________________________________________________________________________________________________________
35 // my own includes
36 //_________________________________________________________________________________________________________________
38 #include <general.h>
40 //_________________________________________________________________________________________________________________
41 // interface includes
42 //_________________________________________________________________________________________________________________
43 #include <com/sun/star/uno/Exception.hpp>
44 #include <com/sun/star/uno/RuntimeException.hpp>
45 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
48 //_________________________________________________________________________________________________________________
49 // other includes
50 //_________________________________________________________________________________________________________________
51 #include <com/sun/star/uno/Any.hxx>
52 #include <com/sun/star/uno/Reference.hxx>
53 #include <com/sun/star/uno/Sequence.hxx>
54 #include <com/sun/star/uno/Type.hxx>
55 #include <com/sun/star/uno/XComponentContext.hpp>
56 #include <com/sun/star/beans/XPropertySet.hpp>
57 #include <cppuhelper/factory.hxx>
58 #include <comphelper/sequence.hxx>
59 #include <rtl/ustring.hxx>
60 #include <rtl/logfile.hxx>
62 //_________________________________________________________________________________________________________________
63 // namespace
64 //_________________________________________________________________________________________________________________
66 namespace framework{
68 /*_________________________________________________________________________________________________________________
70 macros for declaration and definition of XServiceInfo
71 Please use follow public macros only!
73 1) DECLARE_XSERVICEINFO => use it to declare XServiceInfo in your header
74 2) DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for multi service mode
75 3) DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for one instance service mode
76 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!
78 _________________________________________________________________________________________________________________*/
80 //*****************************************************************************************************************
81 // private
82 // implementation of XServiceInfo and helper functions
83 //*****************************************************************************************************************
84 #define PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
85 /*===========================================================================================================*/ \
86 /* XServiceInfo */ \
87 /*===========================================================================================================*/ \
88 ::rtl::OUString SAL_CALL CLASS::getImplementationName() throw( css::uno::RuntimeException ) \
89 { \
90 return impl_getStaticImplementationName(); \
91 } \
93 /*===========================================================================================================*/ \
94 /* XServiceInfo */ \
95 /*===========================================================================================================*/ \
96 sal_Bool SAL_CALL CLASS::supportsService( const ::rtl::OUString& sServiceName ) throw( css::uno::RuntimeException ) \
97 { \
98 return ::comphelper::findValue(getSupportedServiceNames(), sServiceName, sal_True).getLength() != 0; \
99 } \
101 /*===========================================================================================================*/ \
102 /* XServiceInfo */ \
103 /*===========================================================================================================*/ \
104 css::uno::Sequence< ::rtl::OUString > SAL_CALL CLASS::getSupportedServiceNames() throw( css::uno::RuntimeException ) \
106 return impl_getStaticSupportedServiceNames(); \
109 /*===========================================================================================================*/ \
110 /* Helper for XServiceInfo */ \
111 /*===========================================================================================================*/ \
112 css::uno::Sequence< ::rtl::OUString > CLASS::impl_getStaticSupportedServiceNames() \
114 css::uno::Sequence< ::rtl::OUString > seqServiceNames( 1 ); \
115 seqServiceNames.getArray() [0] = SERVICENAME ; \
116 return seqServiceNames; \
119 /*===========================================================================================================*/ \
120 /* Helper for XServiceInfo */ \
121 /*===========================================================================================================*/ \
122 ::rtl::OUString CLASS::impl_getStaticImplementationName() \
124 return IMPLEMENTATIONNAME ; \
127 #define PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
128 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
129 /*===========================================================================================================*/ \
130 /* Helper for registry */ \
131 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
132 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
133 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
134 /* use "impl_initService()" method. */ \
135 /*===========================================================================================================*/ \
136 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ) \
138 RTL_LOGFILE_CONTEXT_AUTHOR( aLogger, "framework","Ocke.Janssen@sun.com",U2B(IMPLEMENTATIONNAME).getStr()); \
139 /* create new instance of service */ \
140 CLASS* pClass = new CLASS( xServiceManager ); \
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 #define PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
150 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
151 /*===========================================================================================================*/ \
152 /* Helper for registry */ \
153 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
154 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
155 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
156 /* use "impl_initService()" method. */ \
157 /*===========================================================================================================*/ \
158 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )\
159 throw( css::uno::Exception ) \
161 /* retrieve component context from the given service manager */ \
162 static const ::rtl::OUString PROP_DEFAULTCONTEXT = ::rtl::OUString::createFromAscii("DefaultContext"); \
163 css::uno::Reference< css::beans::XPropertySet > xSMGRProps(xServiceManager, css::uno::UNO_QUERY_THROW); \
164 css::uno::Reference< css::uno::XComponentContext > xComponentContext; \
165 xSMGRProps->getPropertyValue( PROP_DEFAULTCONTEXT ) >>= xComponentContext; \
166 /* create new instance of service */ \
167 CLASS* pClass = new CLASS( xComponentContext ); \
168 /* hold it alive by increasing his ref count!!! */ \
169 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
170 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
171 pClass->impl_initService(); \
172 /* return new created service as reference */ \
173 return xService; \
176 //*****************************************************************************************************************
177 // private
178 // definition of helper function createFactory() for multiple services
179 //*****************************************************************************************************************
180 #define PRIVATE_DEFINE_SINGLEFACTORY( CLASS ) \
181 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
183 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createSingleFactory ( xServiceManager , \
184 CLASS::impl_getStaticImplementationName() , \
185 CLASS::impl_createInstance , \
186 CLASS::impl_getStaticSupportedServiceNames() \
188 ); \
189 return xReturn; \
192 //*****************************************************************************************************************
193 // private
194 // definition of helper function createFactory() for one instance services
195 //*****************************************************************************************************************
196 #define PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS ) \
197 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
199 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createOneInstanceFactory ( xServiceManager , \
200 CLASS::impl_getStaticImplementationName() , \
201 CLASS::impl_createInstance , \
202 CLASS::impl_getStaticSupportedServiceNames() \
204 ); \
205 return xReturn; \
208 //*****************************************************************************************************************
209 // public
210 // declaration of XServiceInfo and helper functions
211 //*****************************************************************************************************************
212 #define DECLARE_XSERVICEINFO \
213 /* interface XServiceInfo */ \
214 virtual ::rtl::OUString SAL_CALL getImplementationName ( ) throw( css::uno::RuntimeException ); \
215 virtual sal_Bool SAL_CALL supportsService ( const ::rtl::OUString& sServiceName ) throw( css::uno::RuntimeException ); \
216 virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames ( ) throw( css::uno::RuntimeException ); \
217 /* Helper for XServiceInfo */ \
218 static css::uno::Sequence< ::rtl::OUString > SAL_CALL impl_getStaticSupportedServiceNames( ); \
219 static ::rtl::OUString SAL_CALL impl_getStaticImplementationName ( ); \
220 /* Helper for registry */ \
221 static css::uno::Reference< css::uno::XInterface > SAL_CALL impl_createInstance ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ); \
222 static css::uno::Reference< css::lang::XSingleServiceFactory > SAL_CALL impl_createFactory ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ); \
223 /* Helper for initialization of service by using own reference! */ \
224 virtual void SAL_CALL impl_initService ( ); \
226 //*****************************************************************************************************************
227 // public
228 // implementation of XServiceInfo
229 //*****************************************************************************************************************
230 #define DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
231 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
232 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
234 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
235 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
236 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
238 #define DEFINE_XSERVICEINFO_MULTISERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
239 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
240 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
242 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
243 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
244 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
246 //*****************************************************************************************************************
247 // public
248 // implementation of service initialize!
249 // example of using: DEFINE_INIT_SERVICE( MyClassName,
250 // {
251 // ...
252 // Reference< XInterface > xThis( this, UNO_QUERY );
253 // myMember* pMember = new myMember( xThis );
254 // ...
255 // }
256 // )
257 //*****************************************************************************************************************
258 #define DEFINE_INIT_SERVICE( CLASS, FUNCTIONBODY ) \
259 void SAL_CALL CLASS::impl_initService() \
261 FUNCTIONBODY \
264 #define DEFINE_INIT_SERVICE_WITH_BASECLASS( CLASS, BASECLASS, FUNCTIONBODY ) \
265 void SAL_CALL CLASS::impl_initService() \
267 BASECLASS::impl_initService(); \
269 FUNCTIONBODY \
273 } // namespace framework
275 #endif // #ifndef __FRAMEWORK_MACROS_XSERVICEINFO_HXX_