I want to show some common hints and peephole optimizations concerning
C++ Any
and Reference<>
objects,
although most of them have formerly been posted on interface-announce.
Here we go:
don't use:
Any ret;
ret <<= xFoo;
return ret;
for better performance use:
return makeAny( xFoo );
BTW: You can use makeAny()
and <<=, >>=
with every C++-UNO type and the C++ bool, except for sal_Unicode
which conflicts with sal_uInt16
, e.g.
return makeAny( static_cast< sal_Int64 >(42) );
return makeAny( Sequence< OUString >() );
return makeAny( false );
but
sal_Unicode c = 'a';
return Any( &c, ::getCppuCharType() );
don't use:
Reference< XFoo > x;
...
x = Reference< XFoo >( xSomething, UNO_QUERY );
for better performance use:
Reference< XFoo > x;
...
x.set( xSomething, UNO_QUERY );
The following code:
Any any;
...
Reference< XFoo > x;
any >>= x;
can alternatively be written as:
Any any;
...
Reference< XFoo > x( any, UNO_QUERY );
or
Reference< XFoo > x;
...
x.set( any, UNO_QUERY );
Additionally I recommend using UNO_QUERY_THROW
(instead of UNO_QUERY
). This will throw a
::com::sun::star::uno::RuntimeException
with message
"unsatisfied query for interface of type ...!"
in case a query was not successful. Using UNO_QUERY_THROW
often leads to shorter code, e.g.
Reference< XFoo > xFoo( xBar, UNO_QUERY );
if (! xFoo.is())
{
throw RuntimeException(..."got no XFoo!"...);
}
xFoo->foo();
=>
Reference< XFoo >( xBar, UNO_QUERY_THROW )->foo();
Anything more you want to appear here? Write to us!