xref: /trunk/main/toolkit/inc/toolkit/helper/macros.hxx (revision 50e6b072)
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 _TOOLKIT_HELPER_MACROS_HXX_
25 #define _TOOLKIT_HELPER_MACROS_HXX_
26 
27 // -------------------------------------------------------------------------------------
28 
29 #define IMPL_XUNOTUNNEL( ClassName ) \
30 sal_Int64 ClassName::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rIdentifier ) throw(::com::sun::star::uno::RuntimeException) \
31 { \
32 	if( ( rIdentifier.getLength() == 16 ) && ( 0 == rtl_compareMemory( ClassName::GetUnoTunnelId().getConstArray(), rIdentifier.getConstArray(), 16 ) ) ) \
33 	{ \
34 		return sal::static_int_cast< sal_Int64 >(reinterpret_cast< sal_IntPtr >(this)); \
35 	} \
36 	return 0; \
37 } \
38 const ::com::sun::star::uno::Sequence< sal_Int8 >& ClassName::GetUnoTunnelId() throw() \
39 { \
40 	static ::com::sun::star::uno::Sequence< sal_Int8 > * pSeq = NULL; \
41 	if( !pSeq ) \
42 	{ \
43 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ); \
44 		if( !pSeq ) \
45 		{ \
46 			static ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( 16 ); \
47 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True ); \
48 			pSeq = &aSeq; \
49 		} \
50 	} \
51 	return *pSeq; \
52 } \
53 ClassName* ClassName::GetImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rxIFace ) throw() \
54 { \
55 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( rxIFace, ::com::sun::star::uno::UNO_QUERY ); \
56 	return xUT.is() ? reinterpret_cast<ClassName*>(sal::static_int_cast<sal_IntPtr>(xUT->getSomething( ClassName::GetUnoTunnelId() ))) : NULL; \
57 }
58 
59 #define IMPL_XUNOTUNNEL2( ClassName, BaseClass ) \
60 sal_Int64 ClassName::getSomething( const ::com::sun::star::uno::Sequence< sal_Int8 >& rIdentifier ) throw(::com::sun::star::uno::RuntimeException) \
61 { \
62 	if( ( rIdentifier.getLength() == 16 ) && ( 0 == rtl_compareMemory( ClassName::GetUnoTunnelId().getConstArray(), rIdentifier.getConstArray(), 16 ) ) ) \
63 	{ \
64 		return sal::static_int_cast< sal_Int64 >(reinterpret_cast< sal_IntPtr >(this)); \
65 	} \
66 	return BaseClass::getSomething( rIdentifier ); \
67 } \
68 const ::com::sun::star::uno::Sequence< sal_Int8 >& ClassName::GetUnoTunnelId() throw() \
69 { \
70 	static ::com::sun::star::uno::Sequence< sal_Int8 > * pSeq = NULL; \
71 	if( !pSeq ) \
72 	{ \
73 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ); \
74 		if( !pSeq ) \
75 		{ \
76 			static ::com::sun::star::uno::Sequence< sal_Int8 > aSeq( 16 ); \
77 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True ); \
78 			pSeq = &aSeq; \
79 		} \
80 	} \
81 	return *pSeq; \
82 } \
83 ClassName* ClassName::GetImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& rxIFace ) throw() \
84 { \
85 	::com::sun::star::uno::Reference< ::com::sun::star::lang::XUnoTunnel > xUT( rxIFace, ::com::sun::star::uno::UNO_QUERY ); \
86 	return xUT.is() ? reinterpret_cast<ClassName*>(sal::static_int_cast<sal_IntPtr>(xUT->getSomething( ClassName::GetUnoTunnelId() ))) : NULL; \
87 }
88 
89 // -------------------------------------------------------------------------------------
90 
91 #define IMPL_IMPLEMENTATION_ID( ClassName )	\
92 ::com::sun::star::uno::Sequence< sal_Int8 > ClassName::getImplementationId() throw(::com::sun::star::uno::RuntimeException) \
93 { \
94 	static ::cppu::OImplementationId* pId = NULL; \
95 	if( !pId ) \
96 	{ \
97 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ); \
98 		if( ! pId ) \
99 		{ \
100 			static ::cppu::OImplementationId id( sal_False ); \
101 			pId = &id; \
102 		} \
103 	} \
104 	return (*pId).getImplementationId(); \
105 }
106 
107 #define IMPL_XTYPEPROVIDER_START( ClassName )	\
108 IMPL_IMPLEMENTATION_ID( ClassName ) \
109 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > ClassName::getTypes() throw(::com::sun::star::uno::RuntimeException) \
110 { \
111 	static ::cppu::OTypeCollection* pCollection = NULL; \
112 	if( !pCollection ) \
113 	{ \
114 		::osl::Guard< ::osl::Mutex > aGuard( ::osl::Mutex::getGlobalMutex() ); \
115 		if( !pCollection ) \
116 		{ \
117 			static ::cppu::OTypeCollection collection( \
118 			getCppuType( ( ::com::sun::star::uno::Reference< ::com::sun::star::lang::XTypeProvider>* ) NULL ),
119 
120 // -------------------------------------------------------------------------------------
121 
122 #define IMPL_XTYPEPROVIDER_END \
123 			); \
124 			pCollection = &collection; \
125 		} \
126 	} \
127 	return (*pCollection).getTypes(); \
128 }
129 
130 // -------------------------------------------------------------------------------------
131 
132 #define DECL_LISTENERMULTIPLEXER_START( ClassName, InterfaceName ) \
133 class ClassName : public ListenerMultiplexerBase, public InterfaceName \
134 { \
135 public: \
136 	ClassName( ::cppu::OWeakObject& rSource ); \
137     ::com::sun::star::uno::Any	SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException); \
138 	void						SAL_CALL acquire() throw()	{ ListenerMultiplexerBase::acquire(); } \
139 	void						SAL_CALL release() throw()	{ ListenerMultiplexerBase::release(); } \
140     void						SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw(::com::sun::star::uno::RuntimeException);
141 
142 // -------------------------------------------------------------------------------------
143 
144 #define DECL_LISTENERMULTIPLEXER_START_DLLPUB( ClassName, InterfaceName ) \
145 class TOOLKIT_DLLPUBLIC ClassName : public ListenerMultiplexerBase, public InterfaceName \
146 { \
147 public: \
148 	ClassName( ::cppu::OWeakObject& rSource ); \
149     ::com::sun::star::uno::Any	SAL_CALL queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException); \
150 	void						SAL_CALL acquire() throw()	{ ListenerMultiplexerBase::acquire(); } \
151 	void						SAL_CALL release() throw()	{ ListenerMultiplexerBase::release(); } \
152     void						SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source ) throw(::com::sun::star::uno::RuntimeException);
153 
154 // -------------------------------------------------------------------------------------
155 
156 #define DECL_LISTENERMULTIPLEXER_END \
157 };
158 
159 // -------------------------------------------------------------------------------------
160 
161 #define IMPL_LISTENERMULTIPLEXER_BASEMETHODS( ClassName, InterfaceName ) \
162 ClassName::ClassName( ::cppu::OWeakObject& rSource ) \
163 	: ListenerMultiplexerBase( rSource ) \
164 { \
165 } \
166 ::com::sun::star::uno::Any ClassName::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException) \
167 { \
168 	::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType, \
169 										SAL_STATIC_CAST( ::com::sun::star::lang::XEventListener*, this ), \
170 										SAL_STATIC_CAST( InterfaceName*, this ) ); \
171 	return (aRet.hasValue() ? aRet : ListenerMultiplexerBase::queryInterface( rType )); \
172 } \
173 void ClassName::disposing( const ::com::sun::star::lang::EventObject& ) throw(::com::sun::star::uno::RuntimeException) \
174 { \
175 }
176 
177 // -------------------------------------------------------------------------------------
178 
179 #if OSL_DEBUG_LEVEL > 0
180     #define DISPLAY_EXCEPTION( ClassName, MethodName, e )    \
181         ::rtl::OString sMessage( #ClassName "::" #MethodName ": caught an exception!\n" ); \
182         sMessage += ::rtl::OString( e.Message.getStr(), e.Message.getLength(), RTL_TEXTENCODING_ASCII_US ); \
183         OSL_ENSURE( sal_False, sMessage.getStr() );
184 #else
185     #define DISPLAY_EXCEPTION( ClassName, MethodName, e )
186 #endif
187 
188 #define IMPL_LISTENERMULTIPLEXER_LISTENERMETHOD_BODY( ClassName, InterfaceName, MethodName, EventType ) \
189 { \
190 	EventType aMulti( evt ); \
191 	aMulti.Source = &GetContext(); \
192 	::cppu::OInterfaceIteratorHelper aIt( *this ); \
193 	while( aIt.hasMoreElements() ) \
194     { \
195         ::com::sun::star::uno::Reference< InterfaceName > xListener( \
196             static_cast< InterfaceName* >( aIt.next() ) ); \
197         try \
198         { \
199 		    xListener->MethodName( aMulti ); \
200         } \
201         catch( ::com::sun::star::lang::DisposedException e ) \
202         { \
203             OSL_ENSURE( e.Context.is(), "caught DisposedException with empty Context field" ); \
204             if ( e.Context == xListener || !e.Context.is() ) \
205                 aIt.remove(); \
206         } \
207         catch( ::com::sun::star::uno::RuntimeException e ) \
208         { \
209             DISPLAY_EXCEPTION( ClassName, MethodName, e ) \
210         } \
211     } \
212 }
213 
214 #define IMPL_LISTENERMULTIPLEXER_LISTENERMETHOD_EXCEPTION( ClassName, InterfaceName, MethodName, EventType, Exception ) \
215 void ClassName::MethodName( const EventType& evt ) throw(::com::sun::star::uno::RuntimeException, Exception) \
216 IMPL_LISTENERMULTIPLEXER_LISTENERMETHOD_BODY( ClassName, InterfaceName, MethodName, EventType )
217 
218 #define IMPL_LISTENERMULTIPLEXER_LISTENERMETHOD( ClassName, InterfaceName, MethodName, EventType ) \
219 void ClassName::MethodName( const EventType& evt ) throw(::com::sun::star::uno::RuntimeException) \
220 IMPL_LISTENERMULTIPLEXER_LISTENERMETHOD_BODY( ClassName, InterfaceName, MethodName, EventType )
221 
222 // -------------------------------------------------------------------------------------
223 
224 #define DECLIMPL_SUPPORTS_SERVICE( ) \
225     sal_Bool SAL_CALL supportsService( const ::rtl::OUString& rServiceName ) throw(::com::sun::star::uno::RuntimeException) \
226 	{ \
227 		::com::sun::star::uno::Sequence< ::rtl::OUString > aServiceNames( getSupportedServiceNames() ); \
228 		const ::rtl::OUString* pSupported = aServiceNames.getConstArray(); \
229 		const ::rtl::OUString* pSupportedEnd = pSupported + aServiceNames.getLength(); \
230 		for ( ; pSupported != pSupportedEnd; ++pSupported ) \
231 			if ( *pSupported == rServiceName ) \
232 				return sal_True; \
233 		return sal_False; \
234 	}
235 
236 // -------------------------------------------------------------------------------------
237 
238 #define DECLIMPL_SERVICEINFO_DERIVED( ImplName, BaseClass, ServiceName ) \
239 	::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException) { return ::rtl::OUString::createFromAscii( "stardiv.Toolkit." #ImplName ); } \
240     ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)	\
241 							{ \
242 								::com::sun::star::uno::Sequence< ::rtl::OUString > aNames = BaseClass::getSupportedServiceNames( ); \
243 								aNames.realloc( aNames.getLength() + 1 ); \
244 								aNames[ aNames.getLength() - 1 ] = ::rtl::OUString::createFromAscii( ServiceName ); \
245 								return aNames; \
246 							} \
247 
248 // -------------------------------------------------------------------------------------
249 
250 #define DECLIMPL_SERVICEINFO( ImplName, ServiceName ) \
251 	::rtl::OUString SAL_CALL getImplementationName(  ) throw(::com::sun::star::uno::RuntimeException) { return ::rtl::OUString::createFromAscii( "stardiv.Toolkit." #ImplName ); } \
252     ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)	\
253 							{ \
254 								::com::sun::star::uno::Sequence< ::rtl::OUString > aNames( 1 ); \
255 								aNames[ 0 ] = ::rtl::OUString::createFromAscii( ServiceName ); \
256 								return aNames; \
257 							} \
258 	DECLIMPL_SUPPORTS_SERVICE( )
259 
260 
261 
262 
263 
264 
265 #endif // _TOOLKIT_HELPER_MACROS_HXX_
266 
267