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 _COMPHELPER_UNO3_HXX_
25 #define _COMPHELPER_UNO3_HXX_
26 
27 #include <osl/interlck.h>
28 #include <comphelper/types.hxx>
29 #include <com/sun/star/uno/XAggregation.hpp>
30 #include <comphelper/sequence.hxx>
31 #include <cppuhelper/typeprovider.hxx>
32 
33 //.........................................................................
34 namespace comphelper
35 {
36 //.........................................................................
37 
38 //=========================================================================
39 
40 	/// manipulate ref counts without calling acquire/release
increment(oslInterlockedCount & _counter)41 	inline oslInterlockedCount increment(oslInterlockedCount& _counter) { return osl_incrementInterlockedCount(&_counter); }
decrement(oslInterlockedCount & _counter)42 	inline oslInterlockedCount decrement(oslInterlockedCount& _counter) { return osl_decrementInterlockedCount(&_counter); }
43 
44 //=========================================================================
45 
46 	/** used for declaring UNO3-Defaults, i.e. acquire/release
47 	*/
48 	#define DECLARE_UNO3_DEFAULTS(classname, baseclass) \
49 		virtual void	SAL_CALL acquire() throw() { baseclass::acquire(); }	\
50 		virtual void	SAL_CALL release() throw() { baseclass::release(); }	\
51 		void			SAL_CALL PUT_SEMICOLON_AT_THE_END()
52 
53 	/** used for declaring UNO3-Defaults, i.e. acquire/release if you want to forward all queryInterfaces to the base class,
54 		(e.g. if you overload queryAggregation)
55 	*/
56 	#define DECLARE_UNO3_AGG_DEFAULTS(classname, baseclass) \
57 		virtual void			SAL_CALL acquire() throw() { baseclass::acquire(); } \
58 		virtual void			SAL_CALL release() throw() { baseclass::release(); }	\
59 		virtual ::com::sun::star::uno::Any	SAL_CALL queryInterface(const ::com::sun::star::uno::Type& _rType) throw (::com::sun::star::uno::RuntimeException) \
60 			{ return baseclass::queryInterface(_rType); } \
61 		void					SAL_CALL PUT_SEMICOLON_AT_THE_END()
62 
63 	/** Use this macro to forward XComponent methods to base class
64 
65     	When using the ::cppu::WeakComponentImplHelper base classes to
66     	implement a UNO interface, a problem occurs when the interface
67     	itself already derives from XComponent (like e.g. awt::XWindow
68     	or awt::XControl): ::cppu::WeakComponentImplHelper is then
69     	still abstract. Using this macro in the most derived class
70     	definition provides overrides for the XComponent methods,
71     	forwarding them to the given baseclass.
72 
73         @param classname
74         Name of the class this macro is issued within
75 
76         @param baseclass
77         Name of the baseclass that should have the XInterface methods
78         forwarded to - that's usually the WeakComponentImplHelperN base
79 
80         @param implhelper
81         Name of the baseclass that should have the XComponent methods
82         forwarded to - in the case of the WeakComponentImplHelper,
83         that would be ::cppu::WeakComponentImplHelperBase
84 	*/
85 	#define DECLARE_UNO3_XCOMPONENT_DEFAULTS(classname, baseclass, implhelper) \
86 		virtual void SAL_CALL acquire() throw() { baseclass::acquire(); }	\
87 		virtual void SAL_CALL release() throw() { baseclass::release(); }	\
88         virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException) \
89         { 																				\
90 		    implhelper::dispose(); 														\
91 		}                                                                       		\
92         virtual void SAL_CALL addEventListener( 										\
93     		::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
94         {																				\
95 		    implhelper::addEventListener(xListener);										\
96 		}                                                                       		\
97         virtual void SAL_CALL removeEventListener( 										\
98     		::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
99         {																				\
100 		    implhelper::removeEventListener(xListener);                  				\
101 		}                                                                       		\
102 		void		 SAL_CALL PUT_SEMICOLON_AT_THE_END()
103 
104 
105 	/** Use this macro to forward XComponent methods to base class
106 
107     	When using the ::cppu::WeakComponentImplHelper base classes to
108     	implement a UNO interface, a problem occurs when the interface
109     	itself already derives from XComponent (like e.g. awt::XWindow
110     	or awt::XControl): ::cppu::WeakComponentImplHelper is then
111     	still abstract. Using this macro in the most derived class
112     	definition provides overrides for the XComponent methods,
113     	forwarding them to the given baseclass.
114 
115         @param classname
116         Name of the class this macro is issued within
117 
118         @param baseclass
119         Name of the baseclass that should have the XInterface methods
120         forwarded to - that's usually the WeakComponentImplHelperN base
121 
122         @param implhelper
123         Name of the baseclass that should have the XComponent methods
124         forwarded to - in the case of the WeakComponentImplHelper,
125         that would be ::cppu::WeakComponentImplHelperBase
126 	*/
127 	#define DECLARE_UNO3_XCOMPONENT_AGG_DEFAULTS(classname, baseclass, implhelper) \
128 		virtual void SAL_CALL acquire() throw() { baseclass::acquire(); }	\
129 		virtual void SAL_CALL release() throw() { baseclass::release(); }	\
130 		virtual ::com::sun::star::uno::Any	SAL_CALL queryInterface(const ::com::sun::star::uno::Type& _rType) throw (::com::sun::star::uno::RuntimeException) \
131 			{ return baseclass::queryInterface(_rType); } 								\
132         virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException) \
133         { 																				\
134 		    implhelper::dispose(); 														\
135 		}                                                                       		\
136         virtual void SAL_CALL addEventListener( 										\
137     		::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
138         {																				\
139 		    implhelper::addEventListener(xListener);										\
140 		}                                                                       		\
141         virtual void SAL_CALL removeEventListener( 										\
142     		::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
143         {																				\
144 		    implhelper::removeEventListener(xListener);                  				\
145 		}                                                                       		\
146 		void		 SAL_CALL PUT_SEMICOLON_AT_THE_END()
147 
148 
149 	//=====================================================================
150 	//= deriving from multiple XInterface-derived classes
151 	//=====================================================================
152 	//= forwarding/merging XInterface funtionality
153 	//=====================================================================
154 	#define DECLARE_XINTERFACE( )	\
155 		virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& aType ) throw (::com::sun::star::uno::RuntimeException); \
156 		virtual void SAL_CALL acquire() throw(); \
157 		virtual void SAL_CALL release() throw();
158 
159 	#define IMPLEMENT_FORWARD_REFCOUNT( classname, refcountbase ) \
160 		void SAL_CALL classname::acquire() throw() { refcountbase::acquire(); } \
161 		void SAL_CALL classname::release() throw() { refcountbase::release(); }
162 
163 	#define IMPLEMENT_FORWARD_XINTERFACE2( classname, refcountbase, baseclass2 ) \
164 		IMPLEMENT_FORWARD_REFCOUNT( classname, refcountbase ) \
165 		::com::sun::star::uno::Any SAL_CALL classname::queryInterface( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException) \
166 		{ \
167 			::com::sun::star::uno::Any aReturn = refcountbase::queryInterface( _rType ); \
168 			if ( !aReturn.hasValue() ) \
169 				aReturn = baseclass2::queryInterface( _rType ); \
170 			return aReturn; \
171 		}
172 
173 	#define IMPLEMENT_FORWARD_XINTERFACE3( classname, refcountbase, baseclass2, baseclass3 ) \
174 		IMPLEMENT_FORWARD_REFCOUNT( classname, refcountbase ) \
175 		::com::sun::star::uno::Any SAL_CALL classname::queryInterface( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException) \
176 		{ \
177 			::com::sun::star::uno::Any aReturn = refcountbase::queryInterface( _rType ); \
178 			if ( !aReturn.hasValue() ) \
179 			{ \
180 				aReturn = baseclass2::queryInterface( _rType ); \
181 				if ( !aReturn.hasValue() ) \
182 					aReturn = baseclass3::queryInterface( _rType ); \
183 			} \
184 			return aReturn; \
185 		}
186 
187 	//=====================================================================
188 	//= forwarding/merging XTypeProvider funtionality
189 	//=====================================================================
190 	#define DECLARE_XTYPEPROVIDER( )	\
191 		virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw (::com::sun::star::uno::RuntimeException); \
192 		virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId(  ) throw (::com::sun::star::uno::RuntimeException);
193 
194 	#define IMPLEMENT_GET_IMPLEMENTATION_ID( classname ) \
195 		::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL classname::getImplementationId(  ) throw (::com::sun::star::uno::RuntimeException) \
196 		{ \
197 			static ::cppu::OImplementationId* pId = NULL; \
198 			if (!pId) \
199 			{ \
200 				::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); \
201 				if (!pId) \
202 				{ \
203 					static ::cppu::OImplementationId aId; \
204 					pId = &aId; \
205 				} \
206 			} \
207 			return pId->getImplementationId(); \
208 		}
209 
210 	#define IMPLEMENT_FORWARD_XTYPEPROVIDER2( classname, baseclass1, baseclass2 ) \
211 		::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL classname::getTypes(  ) throw (::com::sun::star::uno::RuntimeException) \
212 		{ \
213 			return ::comphelper::concatSequences( \
214 				baseclass1::getTypes(), \
215 				baseclass2::getTypes() \
216 			); \
217 		} \
218 		\
219 		IMPLEMENT_GET_IMPLEMENTATION_ID( classname )
220 
221 	#define IMPLEMENT_FORWARD_XTYPEPROVIDER3( classname, baseclass1, baseclass2, baseclass3 ) \
222 		::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL classname::getTypes(  ) throw (::com::sun::star::uno::RuntimeException) \
223 		{ \
224 			return ::comphelper::concatSequences( \
225 				baseclass1::getTypes(), \
226 				baseclass2::getTypes(), \
227 				baseclass3::getTypes() \
228 			); \
229 		} \
230 		\
231 		IMPLEMENT_GET_IMPLEMENTATION_ID( classname )
232 
233 //=========================================================================
234 
235 	/** ask for an iface of an aggregated object
236 		usage:<br/>
237 			Reference<XFoo> xFoo;<br/>
238 			if (query_aggregation(xAggregatedObject, xFoo))<br/>
239 				....
240 	*/
241 	template <class iface>
query_aggregation(const::com::sun::star::uno::Reference<::com::sun::star::uno::XAggregation> & _rxAggregate,::com::sun::star::uno::Reference<iface> & _rxOut)242 	sal_Bool query_aggregation(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAggregation >& _rxAggregate, ::com::sun::star::uno::Reference<iface>& _rxOut)
243 	{
244 		_rxOut = static_cast<iface*>(NULL);
245 		if (_rxAggregate.is())
246 		{
247 			::com::sun::star::uno::Any aCheck = _rxAggregate->queryAggregation(
248                 iface::static_type());
249 			if (aCheck.hasValue())
250 				_rxOut = *(::com::sun::star::uno::Reference<iface>*)aCheck.getValue();
251 		}
252 		return _rxOut.is();
253 	}
254 
255 	/** ask for an iface of an object
256 		usage:<br/>
257 			Reference<XFoo> xFoo;<br/>
258 			if (query_interface(xAnything, xFoo))<br/>
259 				....
260 	*/
261 	template <class iface>
query_interface(const InterfaceRef & _rxObject,::com::sun::star::uno::Reference<iface> & _rxOut)262 	sal_Bool query_interface(const InterfaceRef& _rxObject, ::com::sun::star::uno::Reference<iface>& _rxOut)
263 	{
264 		_rxOut = static_cast<iface*>(NULL);
265 		if (_rxObject.is())
266 		{
267 			::com::sun::star::uno::Any aCheck = _rxObject->queryInterface(
268                 iface::static_type());
269 			if(aCheck.hasValue())
270 			{
271 				_rxOut = *(::com::sun::star::uno::Reference<iface>*)aCheck.getValue();
272 				return _rxOut.is();
273 			}
274 		}
275 		return sal_False;
276 	}
277 	#define FORWARD_DECLARE_INTERFACE(NAME,XFACE) \
278 		namespace com \
279 		{	\
280 			namespace sun \
281 			{\
282 				namespace star \
283 				{\
284 					namespace NAME \
285 					{\
286 						class XFACE; \
287 					}\
288 				}\
289 			}\
290 		}\
291 
292 
293 //.........................................................................
294 }	// namespace comphelper
295 //.........................................................................
296 
297 #endif // _COMPHELPER_UNO3_HXX_
298 
299