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