xref: /trunk/main/comphelper/inc/comphelper/uno3.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _COMPHELPER_UNO3_HXX_
29 #define _COMPHELPER_UNO3_HXX_
30 
31 #include <osl/interlck.h>
32 #include <comphelper/types.hxx>
33 #include <com/sun/star/uno/XAggregation.hpp>
34 #include <comphelper/sequence.hxx>
35 #include <cppuhelper/typeprovider.hxx>
36 
37 //.........................................................................
38 namespace comphelper
39 {
40 //.........................................................................
41 
42 //=========================================================================
43 
44     /// manipulate ref counts without calling acquire/release
45     inline oslInterlockedCount increment(oslInterlockedCount& _counter) { return osl_incrementInterlockedCount(&_counter); }
46     inline oslInterlockedCount decrement(oslInterlockedCount& _counter) { return osl_decrementInterlockedCount(&_counter); }
47 
48 //=========================================================================
49 
50     /** used for declaring UNO3-Defaults, i.e. acquire/release
51     */
52     #define DECLARE_UNO3_DEFAULTS(classname, baseclass) \
53         virtual void    SAL_CALL acquire() throw() { baseclass::acquire(); }    \
54         virtual void    SAL_CALL release() throw() { baseclass::release(); }    \
55         void            SAL_CALL PUT_SEMICOLON_AT_THE_END()
56 
57     /** used for declaring UNO3-Defaults, i.e. acquire/release if you want to forward all queryInterfaces to the base class,
58         (e.g. if you overload queryAggregation)
59     */
60     #define DECLARE_UNO3_AGG_DEFAULTS(classname, baseclass) \
61         virtual void            SAL_CALL acquire() throw() { baseclass::acquire(); } \
62         virtual void            SAL_CALL release() throw() { baseclass::release(); }    \
63         virtual ::com::sun::star::uno::Any  SAL_CALL queryInterface(const ::com::sun::star::uno::Type& _rType) throw (::com::sun::star::uno::RuntimeException) \
64             { return baseclass::queryInterface(_rType); } \
65         void                    SAL_CALL PUT_SEMICOLON_AT_THE_END()
66 
67     /** Use this macro to forward XComponent methods to base class
68 
69         When using the ::cppu::WeakComponentImplHelper base classes to
70         implement a UNO interface, a problem occurs when the interface
71         itself already derives from XComponent (like e.g. awt::XWindow
72         or awt::XControl): ::cppu::WeakComponentImplHelper is then
73         still abstract. Using this macro in the most derived class
74         definition provides overrides for the XComponent methods,
75         forwarding them to the given baseclass.
76 
77         @param classname
78         Name of the class this macro is issued within
79 
80         @param baseclass
81         Name of the baseclass that should have the XInterface methods
82         forwarded to - that's usually the WeakComponentImplHelperN base
83 
84         @param implhelper
85         Name of the baseclass that should have the XComponent methods
86         forwarded to - in the case of the WeakComponentImplHelper,
87         that would be ::cppu::WeakComponentImplHelperBase
88     */
89     #define DECLARE_UNO3_XCOMPONENT_DEFAULTS(classname, baseclass, implhelper) \
90         virtual void SAL_CALL acquire() throw() { baseclass::acquire(); }   \
91         virtual void SAL_CALL release() throw() { baseclass::release(); }   \
92         virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException) \
93         {                                                                               \
94             implhelper::dispose();                                                      \
95         }                                                                               \
96         virtual void SAL_CALL addEventListener(                                         \
97             ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
98         {                                                                               \
99             implhelper::addEventListener(xListener);                                        \
100         }                                                                               \
101         virtual void SAL_CALL removeEventListener(                                      \
102             ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
103         {                                                                               \
104             implhelper::removeEventListener(xListener);                                 \
105         }                                                                               \
106         void         SAL_CALL PUT_SEMICOLON_AT_THE_END()
107 
108 
109     /** Use this macro to forward XComponent methods to base class
110 
111         When using the ::cppu::WeakComponentImplHelper base classes to
112         implement a UNO interface, a problem occurs when the interface
113         itself already derives from XComponent (like e.g. awt::XWindow
114         or awt::XControl): ::cppu::WeakComponentImplHelper is then
115         still abstract. Using this macro in the most derived class
116         definition provides overrides for the XComponent methods,
117         forwarding them to the given baseclass.
118 
119         @param classname
120         Name of the class this macro is issued within
121 
122         @param baseclass
123         Name of the baseclass that should have the XInterface methods
124         forwarded to - that's usually the WeakComponentImplHelperN base
125 
126         @param implhelper
127         Name of the baseclass that should have the XComponent methods
128         forwarded to - in the case of the WeakComponentImplHelper,
129         that would be ::cppu::WeakComponentImplHelperBase
130     */
131     #define DECLARE_UNO3_XCOMPONENT_AGG_DEFAULTS(classname, baseclass, implhelper) \
132         virtual void SAL_CALL acquire() throw() { baseclass::acquire(); }   \
133         virtual void SAL_CALL release() throw() { baseclass::release(); }   \
134         virtual ::com::sun::star::uno::Any  SAL_CALL queryInterface(const ::com::sun::star::uno::Type& _rType) throw (::com::sun::star::uno::RuntimeException) \
135             { return baseclass::queryInterface(_rType); }                               \
136         virtual void SAL_CALL dispose() throw (::com::sun::star::uno::RuntimeException) \
137         {                                                                               \
138             implhelper::dispose();                                                      \
139         }                                                                               \
140         virtual void SAL_CALL addEventListener(                                         \
141             ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
142         {                                                                               \
143             implhelper::addEventListener(xListener);                                        \
144         }                                                                               \
145         virtual void SAL_CALL removeEventListener(                                      \
146             ::com::sun::star::uno::Reference< ::com::sun::star::lang::XEventListener > const & xListener ) throw (::com::sun::star::uno::RuntimeException) \
147         {                                                                               \
148             implhelper::removeEventListener(xListener);                                 \
149         }                                                                               \
150         void         SAL_CALL PUT_SEMICOLON_AT_THE_END()
151 
152 
153     //=====================================================================
154     //= deriving from multiple XInterface-derived classes
155     //=====================================================================
156     //= forwarding/merging XInterface funtionality
157     //=====================================================================
158     #define DECLARE_XINTERFACE( )   \
159         virtual ::com::sun::star::uno::Any SAL_CALL queryInterface( const ::com::sun::star::uno::Type& aType ) throw (::com::sun::star::uno::RuntimeException); \
160         virtual void SAL_CALL acquire() throw(); \
161         virtual void SAL_CALL release() throw();
162 
163     #define IMPLEMENT_FORWARD_REFCOUNT( classname, refcountbase ) \
164         void SAL_CALL classname::acquire() throw() { refcountbase::acquire(); } \
165         void SAL_CALL classname::release() throw() { refcountbase::release(); }
166 
167     #define IMPLEMENT_FORWARD_XINTERFACE2( classname, refcountbase, baseclass2 ) \
168         IMPLEMENT_FORWARD_REFCOUNT( classname, refcountbase ) \
169         ::com::sun::star::uno::Any SAL_CALL classname::queryInterface( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException) \
170         { \
171             ::com::sun::star::uno::Any aReturn = refcountbase::queryInterface( _rType ); \
172             if ( !aReturn.hasValue() ) \
173                 aReturn = baseclass2::queryInterface( _rType ); \
174             return aReturn; \
175         }
176 
177     #define IMPLEMENT_FORWARD_XINTERFACE3( classname, refcountbase, baseclass2, baseclass3 ) \
178         IMPLEMENT_FORWARD_REFCOUNT( classname, refcountbase ) \
179         ::com::sun::star::uno::Any SAL_CALL classname::queryInterface( const ::com::sun::star::uno::Type& _rType ) throw (::com::sun::star::uno::RuntimeException) \
180         { \
181             ::com::sun::star::uno::Any aReturn = refcountbase::queryInterface( _rType ); \
182             if ( !aReturn.hasValue() ) \
183             { \
184                 aReturn = baseclass2::queryInterface( _rType ); \
185                 if ( !aReturn.hasValue() ) \
186                     aReturn = baseclass3::queryInterface( _rType ); \
187             } \
188             return aReturn; \
189         }
190 
191     //=====================================================================
192     //= forwarding/merging XTypeProvider funtionality
193     //=====================================================================
194     #define DECLARE_XTYPEPROVIDER( )    \
195         virtual ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL getTypes(  ) throw (::com::sun::star::uno::RuntimeException); \
196         virtual ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL getImplementationId(  ) throw (::com::sun::star::uno::RuntimeException);
197 
198     #define IMPLEMENT_GET_IMPLEMENTATION_ID( classname ) \
199         ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL classname::getImplementationId(  ) throw (::com::sun::star::uno::RuntimeException) \
200         { \
201             static ::cppu::OImplementationId* pId = NULL; \
202             if (!pId) \
203             { \
204                 ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() ); \
205                 if (!pId) \
206                 { \
207                     static ::cppu::OImplementationId aId; \
208                     pId = &aId; \
209                 } \
210             } \
211             return pId->getImplementationId(); \
212         }
213 
214     #define IMPLEMENT_FORWARD_XTYPEPROVIDER2( classname, baseclass1, baseclass2 ) \
215         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL classname::getTypes(  ) throw (::com::sun::star::uno::RuntimeException) \
216         { \
217             return ::comphelper::concatSequences( \
218                 baseclass1::getTypes(), \
219                 baseclass2::getTypes() \
220             ); \
221         } \
222         \
223         IMPLEMENT_GET_IMPLEMENTATION_ID( classname )
224 
225     #define IMPLEMENT_FORWARD_XTYPEPROVIDER3( classname, baseclass1, baseclass2, baseclass3 ) \
226         ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Type > SAL_CALL classname::getTypes(  ) throw (::com::sun::star::uno::RuntimeException) \
227         { \
228             return ::comphelper::concatSequences( \
229                 baseclass1::getTypes(), \
230                 baseclass2::getTypes(), \
231                 baseclass3::getTypes() \
232             ); \
233         } \
234         \
235         IMPLEMENT_GET_IMPLEMENTATION_ID( classname )
236 
237 //=========================================================================
238 
239     /** ask for an iface of an aggregated object
240         usage:<br/>
241             Reference<XFoo> xFoo;<br/>
242             if (query_aggregation(xAggregatedObject, xFoo))<br/>
243                 ....
244     */
245     template <class iface>
246     sal_Bool query_aggregation(const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XAggregation >& _rxAggregate, ::com::sun::star::uno::Reference<iface>& _rxOut)
247     {
248         _rxOut = static_cast<iface*>(NULL);
249         if (_rxAggregate.is())
250         {
251             ::com::sun::star::uno::Any aCheck = _rxAggregate->queryAggregation(
252                 iface::static_type());
253             if (aCheck.hasValue())
254                 _rxOut = *(::com::sun::star::uno::Reference<iface>*)aCheck.getValue();
255         }
256         return _rxOut.is();
257     }
258 
259     /** ask for an iface of an object
260         usage:<br/>
261             Reference<XFoo> xFoo;<br/>
262             if (query_interface(xAnything, xFoo))<br/>
263                 ....
264     */
265     template <class iface>
266     sal_Bool query_interface(const InterfaceRef& _rxObject, ::com::sun::star::uno::Reference<iface>& _rxOut)
267     {
268         _rxOut = static_cast<iface*>(NULL);
269         if (_rxObject.is())
270         {
271             ::com::sun::star::uno::Any aCheck = _rxObject->queryInterface(
272                 iface::static_type());
273             if(aCheck.hasValue())
274             {
275                 _rxOut = *(::com::sun::star::uno::Reference<iface>*)aCheck.getValue();
276                 return _rxOut.is();
277             }
278         }
279         return sal_False;
280     }
281     #define FORWARD_DECLARE_INTERFACE(NAME,XFACE) \
282         namespace com \
283         {   \
284             namespace sun \
285             {\
286                 namespace star \
287                 {\
288                     namespace NAME \
289                     {\
290                         class XFACE; \
291                     }\
292                 }\
293             }\
294         }\
295 
296 
297 //.........................................................................
298 }   // namespace comphelper
299 //.........................................................................
300 
301 #endif // _COMPHELPER_UNO3_HXX_
302 
303