1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xserviceinfo.hxx,v $
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 //_________________________________________________________________________________________________________________
36 //_________________________________________________________________________________________________________________
40 //_________________________________________________________________________________________________________________
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 //_________________________________________________________________________________________________________________
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 <rtl/ustring.hxx>
60 //_________________________________________________________________________________________________________________
62 //_________________________________________________________________________________________________________________
66 /*_________________________________________________________________________________________________________________
68 macros for declaration and definition of XServiceInfo
69 Please use follow public macros only!
71 1) DECLARE_XSERVICEINFO => use it to declare XServiceInfo in your header
72 2) DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for multi service mode
73 3) DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for one instance service mode
74 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!
76 _________________________________________________________________________________________________________________*/
78 //*****************************************************************************************************************
80 // implementation of XServiceInfo and helper functions
81 //*****************************************************************************************************************
82 #define PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
83 /*===========================================================================================================*/ \
85 /*===========================================================================================================*/ \
86 ::rtl::OUString SAL_CALL CLASS::getImplementationName() throw( css::uno::RuntimeException ) \
88 return impl_getStaticImplementationName(); \
91 /*===========================================================================================================*/ \
93 /*===========================================================================================================*/ \
94 sal_Bool SAL_CALL CLASS::supportsService( const ::rtl::OUString& sServiceName ) throw( css::uno::RuntimeException ) \
96 /* Set default return value. */ \
97 sal_Bool bReturn = sal_False ; \
98 /* Get names of all supported servicenames. */ \
99 css::uno::Sequence< ::rtl::OUString > seqServiceNames = getSupportedServiceNames(); \
100 const ::rtl::OUString* pArray = seqServiceNames.getConstArray(); \
101 sal_Int32 nCounter = 0; \
102 sal_Int32 nLength = seqServiceNames.getLength(); \
103 /* Search for right name in list. */ \
105 ( nCounter < nLength ) && \
106 ( bReturn == sal_False ) \
109 /* Is name was found, say "YES, SERVICE IS SUPPORTED." and break loop. */ \
110 if ( pArray[nCounter] == sServiceName ) \
112 bReturn = sal_True ; \
114 /* Else step to next element in list. */ \
117 /* Return state of search. */ \
121 /*===========================================================================================================*/ \
123 /*===========================================================================================================*/ \
124 css::uno::Sequence< ::rtl::OUString > SAL_CALL CLASS::getSupportedServiceNames() throw( css::uno::RuntimeException ) \
126 return impl_getStaticSupportedServiceNames(); \
129 /*===========================================================================================================*/ \
130 /* Helper for XServiceInfo */ \
131 /*===========================================================================================================*/ \
132 css::uno::Sequence< ::rtl::OUString > CLASS::impl_getStaticSupportedServiceNames() \
134 css::uno::Sequence< ::rtl::OUString > seqServiceNames( 1 ); \
135 seqServiceNames.getArray() [0] = SERVICENAME ; \
136 return seqServiceNames; \
139 /*===========================================================================================================*/ \
140 /* Helper for XServiceInfo */ \
141 /*===========================================================================================================*/ \
142 ::rtl::OUString CLASS::impl_getStaticImplementationName() \
144 return IMPLEMENTATIONNAME ; \
147 #define PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
148 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
149 /*===========================================================================================================*/ \
150 /* Helper for registry */ \
151 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
152 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
153 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
154 /* use "impl_initService()" method. */ \
155 /*===========================================================================================================*/ \
156 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ) \
158 /* create new instance of service */ \
159 CLASS* pClass = new CLASS( xServiceManager ); \
160 /* hold it alive by increasing his ref count!!! */ \
161 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
162 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
163 pClass->impl_initService(); \
164 /* return new created service as reference */ \
168 #define PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
169 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
170 /*===========================================================================================================*/ \
171 /* Helper for registry */ \
172 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
173 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
174 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
175 /* use "impl_initService()" method. */ \
176 /*===========================================================================================================*/ \
177 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )\
178 throw( css::uno::Exception ) \
180 /* retrieve component context from the given service manager */ \
181 static const ::rtl::OUString PROP_DEFAULTCONTEXT = ::rtl::OUString::createFromAscii("DefaultContext"); \
182 css::uno::Reference< css::beans::XPropertySet > xSMGRProps(xServiceManager, css::uno::UNO_QUERY_THROW); \
183 css::uno::Reference< css::uno::XComponentContext > xComponentContext; \
184 xSMGRProps->getPropertyValue( PROP_DEFAULTCONTEXT ) >>= xComponentContext; \
185 /* create new instance of service */ \
186 CLASS* pClass = new CLASS( xComponentContext ); \
187 /* hold it alive by increasing his ref count!!! */ \
188 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
189 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
190 pClass->impl_initService(); \
191 /* return new created service as reference */ \
195 //*****************************************************************************************************************
197 // definition of helper function createFactory() for multiple services
198 //*****************************************************************************************************************
199 #define PRIVATE_DEFINE_SINGLEFACTORY( CLASS ) \
200 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
202 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createSingleFactory ( xServiceManager , \
203 CLASS::impl_getStaticImplementationName() , \
204 CLASS::impl_createInstance , \
205 CLASS::impl_getStaticSupportedServiceNames() \
211 //*****************************************************************************************************************
213 // definition of helper function createFactory() for one instance services
214 //*****************************************************************************************************************
215 #define PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS ) \
216 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
218 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createOneInstanceFactory ( xServiceManager , \
219 CLASS::impl_getStaticImplementationName() , \
220 CLASS::impl_createInstance , \
221 CLASS::impl_getStaticSupportedServiceNames() \
227 //*****************************************************************************************************************
229 // declaration of XServiceInfo and helper functions
230 //*****************************************************************************************************************
231 #define DECLARE_XSERVICEINFO \
232 /* interface XServiceInfo */ \
233 virtual ::rtl::OUString SAL_CALL getImplementationName ( ) throw( css::uno::RuntimeException ); \
234 virtual sal_Bool SAL_CALL supportsService ( const ::rtl::OUString& sServiceName ) throw( css::uno::RuntimeException ); \
235 virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames ( ) throw( css::uno::RuntimeException ); \
236 /* Helper for XServiceInfo */ \
237 static css::uno::Sequence< ::rtl::OUString > SAL_CALL impl_getStaticSupportedServiceNames( ); \
238 static ::rtl::OUString SAL_CALL impl_getStaticImplementationName ( ); \
239 /* Helper for registry */ \
240 static css::uno::Reference< css::uno::XInterface > SAL_CALL impl_createInstance ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ); \
241 static css::uno::Reference< css::lang::XSingleServiceFactory > SAL_CALL impl_createFactory ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ); \
242 /* Helper for initialization of service by using own reference! */ \
243 virtual void SAL_CALL impl_initService ( ); \
245 //*****************************************************************************************************************
247 // implementation of XServiceInfo
248 //*****************************************************************************************************************
249 #define DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
250 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
251 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
253 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
254 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
255 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
257 #define DEFINE_XSERVICEINFO_MULTISERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
258 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
259 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
261 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
262 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
263 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
265 //*****************************************************************************************************************
267 // implementation of service initialize!
268 // example of using: DEFINE_INIT_SERVICE( MyClassName,
271 // Reference< XInterface > xThis( this, UNO_QUERY );
272 // myMember* pMember = new myMember( xThis );
276 //*****************************************************************************************************************
277 #define DEFINE_INIT_SERVICE( CLASS, FUNCTIONBODY ) \
278 void SAL_CALL CLASS::impl_initService() \
283 #define DEFINE_INIT_SERVICE_WITH_BASECLASS( CLASS, BASECLASS, FUNCTIONBODY ) \
284 void SAL_CALL CLASS::impl_initService() \
286 BASECLASS::impl_initService(); \
292 } // namespace framework
294 #endif // #ifndef __FRAMEWORK_MACROS_XSERVICEINFO_HXX_