1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef _COMPHELPER_UNO3_HXX_
21 #define _COMPHELPER_UNO3_HXX_
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>
30 //.........................................................................
33 //.........................................................................
35 //=========================================================================
37 /// manipulate ref counts without calling acquire/release
38 inline oslInterlockedCount
increment(oslInterlockedCount
& _counter
) { return osl_atomic_increment(&_counter
); }
39 inline oslInterlockedCount
decrement(oslInterlockedCount
& _counter
) { return osl_atomic_decrement(&_counter
); }
41 //=========================================================================
43 /** used for declaring UNO3-Defaults, i.e. acquire/release
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()
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)
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()
60 /** Use this macro to forward XComponent methods to base class
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.
71 Name of the class this macro is issued within
74 Name of the baseclass that should have the XInterface methods
75 forwarded to - that's usually the WeakComponentImplHelperN base
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
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) \
87 implhelper::dispose(); \
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) \
92 implhelper::addEventListener(xListener); \
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) \
97 implhelper::removeEventListener(xListener); \
99 void SAL_CALL PUT_SEMICOLON_AT_THE_END()
102 /** Use this macro to forward XComponent methods to base class
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.
113 Name of the class this macro is issued within
116 Name of the baseclass that should have the XInterface methods
117 forwarded to - that's usually the WeakComponentImplHelperN base
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
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) \
131 implhelper::dispose(); \
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) \
136 implhelper::addEventListener(xListener); \
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) \
141 implhelper::removeEventListener(xListener); \
143 void SAL_CALL PUT_SEMICOLON_AT_THE_END()
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();
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(); }
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) \
164 ::com::sun::star::uno::Any aReturn = refcountbase::queryInterface( _rType ); \
165 if ( !aReturn.hasValue() ) \
166 aReturn = baseclass2::queryInterface( _rType ); \
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) \
174 ::com::sun::star::uno::Any aReturn = refcountbase::queryInterface( _rType ); \
175 if ( !aReturn.hasValue() ) \
177 aReturn = baseclass2::queryInterface( _rType ); \
178 if ( !aReturn.hasValue() ) \
179 aReturn = baseclass3::queryInterface( _rType ); \
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);
191 #define IMPLEMENT_GET_IMPLEMENTATION_ID( classname ) \
194 class the##classname##ImplementationId : public rtl::Static< ::cppu::OImplementationId, the##classname##ImplementationId> {}; \
196 ::com::sun::star::uno::Sequence< sal_Int8 > SAL_CALL classname::getImplementationId( ) throw (::com::sun::star::uno::RuntimeException) \
198 return the##classname##ImplementationId::get().getImplementationId(); \
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) \
204 return ::comphelper::concatSequences( \
205 baseclass1::getTypes(), \
206 baseclass2::getTypes() \
210 IMPLEMENT_GET_IMPLEMENTATION_ID( classname )
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) \
215 return ::comphelper::concatSequences( \
216 baseclass1::getTypes(), \
217 baseclass2::getTypes(), \
218 baseclass3::getTypes() \
222 IMPLEMENT_GET_IMPLEMENTATION_ID( classname )
224 //=========================================================================
226 /** ask for an iface of an aggregated object
228 Reference<XFoo> xFoo;<br/>
229 if (query_aggregation(xAggregatedObject, xFoo))<br/>
232 template <class iface
>
233 sal_Bool
query_aggregation(const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XAggregation
>& _rxAggregate
, ::com::sun::star::uno::Reference
<iface
>& _rxOut
)
235 _rxOut
= static_cast<iface
*>(NULL
);
236 if (_rxAggregate
.is())
238 ::com::sun::star::uno::Any aCheck
= _rxAggregate
->queryAggregation(
239 iface::static_type());
240 if (aCheck
.hasValue())
241 _rxOut
= *(::com::sun::star::uno::Reference
<iface
>*)aCheck
.getValue();
246 /** ask for an iface of an object
248 Reference<XFoo> xFoo;<br/>
249 if (query_interface(xAnything, xFoo))<br/>
252 template <class iface
>
253 sal_Bool
query_interface(const InterfaceRef
& _rxObject
, ::com::sun::star::uno::Reference
<iface
>& _rxOut
)
255 _rxOut
= static_cast<iface
*>(NULL
);
258 ::com::sun::star::uno::Any aCheck
= _rxObject
->queryInterface(
259 iface::static_type());
260 if(aCheck
.hasValue())
262 _rxOut
= *(::com::sun::star::uno::Reference
<iface
>*)aCheck
.getValue();
269 //.........................................................................
270 } // namespace comphelper
271 //.........................................................................
273 #endif // _COMPHELPER_UNO3_HXX_
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */