1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 INCLUDED_FRAMEWORK_INC_MACROS_XSERVICEINFO_HXX
21 #define INCLUDED_FRAMEWORK_INC_MACROS_XSERVICEINFO_HXX
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 <cppuhelper/supportsservice.hxx>
39 #include <rtl/ustring.hxx>
44 macros for declaration and definition of XServiceInfo
45 Please use follow public macros only!
47 1) DECLARE_XSERVICEINFO => use it to declare XServiceInfo in your header
48 2) DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for multi service mode
49 3) DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) => use it to define XServiceInfo for one instance service mode
50 4) DEFINE_INIT_SERVICE( CLASS ) => use it to implement your own impl_initService() method, which is necessary for initializeing object by using his own reference!
53 #define PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
55 OUString SAL_CALL CLASS::getImplementationName() throw( css::uno::RuntimeException, std::exception ) \
57 return impl_getStaticImplementationName(); \
60 sal_Bool SAL_CALL CLASS::supportsService( const OUString& sServiceName ) throw( css::uno::RuntimeException, std::exception ) \
62 return cppu::supportsService(this, sServiceName); \
65 css::uno::Sequence< OUString > SAL_CALL CLASS::getSupportedServiceNames() throw( css::uno::RuntimeException, std::exception ) \
67 return impl_getStaticSupportedServiceNames(); \
70 css::uno::Sequence< OUString > CLASS::impl_getStaticSupportedServiceNames() \
72 css::uno::Sequence< OUString > seqServiceNames( 1 ); \
73 seqServiceNames.getArray() [0] = SERVICENAME; \
74 return seqServiceNames; \
77 OUString CLASS::impl_getStaticImplementationName() \
79 return IMPLEMENTATIONNAME; \
82 #define PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
83 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
84 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
85 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
86 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
87 /* use "impl_initService()" method. */ \
88 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ) \
90 /* create new instance of service */ \
91 CLASS* pClass = new CLASS( xServiceManager ); \
92 /* hold it alive by increasing his ref count!!! */ \
93 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
94 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
95 pClass->impl_initService(); \
96 /* return new created service as reference */ \
100 #define PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
101 PRIVATE_DEFINE_XSERVICEINFO_BASE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
102 /* Attention: To avoid against wrong ref counts during our own initialize procedure, we must */ \
103 /* use right EXTERNAL handling of them. That's why you should do nothing in your ctor, which could*/ \
104 /* work on your ref count! All other things are allowed. Do work with your own reference - please */ \
105 /* use "impl_initService()" method. */ \
106 css::uno::Reference< css::uno::XInterface > SAL_CALL CLASS::impl_createInstance( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager )\
107 throw( css::uno::Exception ) \
109 /* retrieve component context from the given service manager */ \
110 css::uno::Reference< css::uno::XComponentContext > xComponentContext( \
111 comphelper::getComponentContext( xServiceManager ) ); \
112 /* create new instance of service */ \
113 CLASS* pClass = new CLASS( xComponentContext ); \
114 /* hold it alive by increasing his ref count!!! */ \
115 css::uno::Reference< css::uno::XInterface > xService( static_cast< XINTERFACECAST* >(pClass), css::uno::UNO_QUERY ); \
116 /* initialize new service instance ... he can use his own refcount ... we hold it! */ \
117 pClass->impl_initService(); \
118 /* return new created service as reference */ \
122 #define PRIVATE_DEFINE_SINGLEFACTORY( CLASS ) \
123 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
125 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createSingleFactory ( xServiceManager , \
126 CLASS::impl_getStaticImplementationName() , \
127 CLASS::impl_createInstance , \
128 CLASS::impl_getStaticSupportedServiceNames() \
134 #define PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS ) \
135 css::uno::Reference< css::lang::XSingleServiceFactory > CLASS::impl_createFactory( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) \
137 css::uno::Reference< css::lang::XSingleServiceFactory > xReturn ( cppu::createOneInstanceFactory ( xServiceManager , \
138 CLASS::impl_getStaticImplementationName() , \
139 CLASS::impl_createInstance , \
140 CLASS::impl_getStaticSupportedServiceNames() \
146 #define DECLARE_XSERVICEINFO_NOFACTORY \
147 /* interface XServiceInfo */ \
148 virtual OUString SAL_CALL getImplementationName ( ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE; \
149 virtual sal_Bool SAL_CALL supportsService ( const OUString& sServiceName ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE; \
150 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames ( ) throw( css::uno::RuntimeException, std::exception ) SAL_OVERRIDE; \
151 /* Helper for XServiceInfo */ \
152 static css::uno::Sequence< OUString > SAL_CALL impl_getStaticSupportedServiceNames( ); \
153 static OUString SAL_CALL impl_getStaticImplementationName ( ); \
154 /* Helper for initialization of service by using own reference! */ \
155 virtual void SAL_CALL impl_initService ( ); \
157 #define DECLARE_XSERVICEINFO \
158 DECLARE_XSERVICEINFO_NOFACTORY \
159 /* Helper for registry */ \
160 static css::uno::Reference< css::uno::XInterface > SAL_CALL impl_createInstance ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ) throw( css::uno::Exception ); \
161 static css::uno::Reference< css::lang::XSingleServiceFactory > SAL_CALL impl_createFactory ( const css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager ); \
163 #define DEFINE_XSERVICEINFO_MULTISERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
164 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
165 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
167 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
168 PRIVATE_DEFINE_XSERVICEINFO_OLDSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
169 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
171 #define DEFINE_XSERVICEINFO_MULTISERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
172 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
173 PRIVATE_DEFINE_SINGLEFACTORY( CLASS )
175 #define DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
176 PRIVATE_DEFINE_XSERVICEINFO_NEWSTYLE( CLASS, XINTERFACECAST, SERVICENAME, IMPLEMENTATIONNAME ) \
177 PRIVATE_DEFINE_ONEINSTANCEFACTORY( CLASS )
180 // implementation of service initialize!
181 // example of using: DEFINE_INIT_SERVICE( MyClassName,
184 // Reference< XInterface > xThis( this, UNO_QUERY );
185 // myMember* pMember = new myMember( xThis );
189 #define DEFINE_INIT_SERVICE( CLASS, FUNCTIONBODY ) \
190 void SAL_CALL CLASS::impl_initService() \
195 } // namespace framework
197 #endif // INCLUDED_FRAMEWORK_INC_MACROS_XSERVICEINFO_HXX
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */