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