Language

The Free and Open Productivity Suite
Released: Apache OpenOffice 4.1.15

API

SDK

Tips ‘n’ Tricks

Miscellaneous

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 );
    
    but
         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 );
    
    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!


Last update: 16-Oct-2012

Apache Software Foundation

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

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