The Free and Open Productivity Suite
Follow us on Twitter: @ApacheOO

Developer's Guide

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 | Website Feedback | Contact Us | Donate | Thanks

Apache, the Apache feather logo, and OpenOffice are trademarks of The Apache Software Foundation. OpenOffice.org 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.