Class Definition with Helper Template Classes

From Apache OpenOffice Wiki
Jump to: navigation, search



XInterface, XTypeProvider and XWeak

The SDK offers helpers for ease of developing. There are implementation helper template classes that deal with the implementation of com.sun.star.uno.XInterface and com.sun.star.lang.XTypeProvider, as well as com.sun.star.uno.XWeak. These classes let you focus on the interfaces you want to implement.

The implementation of my_module.MyService2 uses the ::cppu::WeakImplHelper3<> helper. The "3" stands for the number of interfaces to implement. The class declaration inherits from this template class which takes the interfaces to implement as template parameters.

  #include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces
  #include <cppuhelper/factory.hxx>
  #include <cppuhelper/implementationentry.hxx>
 
  #include <com/sun/star/lang/XServiceInfo.hpp>
  #include <com/sun/star/lang/XInitialization.hpp>
  #include <com/sun/star/lang/IllegalArgumentException.hpp>
  #include <my_module/XSomething.hpp>
 
 
  using namespace ::rtl; // for OUString
  using namespace ::com::sun::star; // for sdk interfaces
  using namespace ::com::sun::star::uno; // for basic types
 
 
  namespace my_sc_impl {
 
  class MyService2Impl : public ::cppu::WeakImplHelper3< ::my_module::XSomething, 
                                                lang::XServiceInfo, 
                                                lang::XInitialization > 
  {
      ...
  };
  }

The next section focuses on coding com.sun.star.lang.XServiceInfo, com.sun.star.lang.XInitialization and the sample interface my_module.XSomething.

The cppuhelper shared library provides additional implementation helper classes, for example, supporting com.sun.star.lang.XComponent. Browse the ::cppu namespace in the C++ reference of the SDK or on www.openoffice.org/udk/.

XServiceInfo

An UNO service implementation supports com.sun.star.lang.XServiceInfo providing information about its implementation name and supported services. The implementation name is a unique name referencing the specific implementation. In this case, my_module.my_sc_impl.MyService1 and my_module.my_sc_impl.MyService2 respectively. The implementation name is used later when registering the implementation into the simple_component.rdb registry used for Apache OpenOffice. It links a service name entry to one implementation, because there may be more than one implementation. Multiple implementations of the same service may have different characteristics, such as runtime behavior and memory footprint.

Our service instance has to support the com.sun.star.lang.XServiceInfo interface. This interface has three methods, and can be coded for one supported service as follows:

  // XServiceInfo implementation
  OUString MyService2Impl::getImplementationName()
      throw (RuntimeException)
  {
      // unique implementation name
      return OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_impl.MyService2") );
  }
  sal_Bool MyService2Impl::supportsService( OUString const & serviceName )
      throw (RuntimeException)
  {
      // this object only supports one service, so the test is simple
      return serviceName.equalsAsciiL( 
  RTL_CONSTASCII_STRINGPARAM("my_module.MyService2") );
  }
  Sequence< OUString > MyService2Impl::getSupportedServiceNames()
      throw (RuntimeException)
  {
      return getSupportedServiceNames_MyService2Impl();
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages