The Free and Open Productivity Suite
Apache OpenOffice 4.1.6 released

Developer's Guide



Tips 'n' Tricks


This page might be outdated, please reffer to the Apache OpenOffice wiki for a more current information.

C++ UNO Coding Hints

Author: Daniel Bölzle

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:

  1. 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 );
         sal_Unicode c = 'a';
         return Any( &c, ::getCppuCharType() );

  2. don't use:

         Reference< XFoo > x;
         x = Reference< XFoo >( xSomething, UNO_QUERY );
    for better performance use:
         Reference< XFoo > x;
         x.set( xSomething, UNO_QUERY );

  3. The following code:

         Any any;
         Reference< XFoo > x;
         any >>= x;
    can alternatively be written as:
         Any any;
         Reference< XFoo > x( any, UNO_QUERY );
         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 (!
         throw RuntimeException(..."got no XFoo!"...);


     Reference< XFoo >( xBar, UNO_QUERY_THROW )->foo();

Anything more you want to appear here? Write to us!

Last update: 16-Oct-2012

Copyright & License | Privacy | Contact Us | Donate | Thanks

Apache and the Apache feather logo are trademarks of The Apache Software Foundation. OpenOffice, and the seagull logo are registered trademarks of The Apache Software Foundation. Other names appearing on the site may be trademarks of their respective owners.