Difference between revisions of "Development/Cpp/Helper/ServiceDecl"

From Apache OpenOffice Wiki
< Development‎ | Cpp‎ | Helper
Jump to: navigation, search
m
 
Line 85: Line 85:
 
</source>
 
</source>
  
[[Category:Development]]
+
 
 
[[Category:Cpp]]
 
[[Category:Cpp]]
 
[[Category:Helper]]
 
[[Category:Helper]]

Latest revision as of 13:45, 28 March 2010

Class Name Purpose Location
ServiceDecl is a helper class for providing the UNO component service factory boilerplate (the three exported C functions and associated code) comphelper/servicedecl.hxx


Description

A ServiceDecl provides the service factory entry points for a UNO component. A UNO component, i.e. a standalone shared library, that exports only these three C functions

void SAL_CALL component_getImplementationEnvironment( const sal_Char**  ppEnvTypeName,
                                                      uno_Environment** ppEnv );
sal_Bool SAL_CALL component_writeInfo( ::com::sun::star::lang::XMultiServiceFactory*    pServiceManager,
                                       ::com::sun::star::registry::XRegistryKey*        pRegistryKey );
void* SAL_CALL component_getFactory( sal_Char const*                                pImplName,
                                     ::com::sun::star::lang::XMultiServiceFactory*  pServiceManager,
                                     ::com::sun::star::registry::XRegistryKey*      pRegistryKey );

needs some boilerplate to provide the service factory, which is repeated almost literally in every implementation (see the Developer Guide for the canonical example).

The described class encapsulates almost everything of this, and provides a set of macros on top, that define the C entry functions,

Interface

See the excellent inline header file commentary here for details of use (should the example section below not suffice).

Example

The following example provides two services to the UNO runtime.

#include "comphelper/servicedecl.hxx"
 
class Foo : public SomeUNOInterface {...};
class Bar : public SomeOtherUNOInterface {...};
 
namespace sdecl = comphelper::service_decl;
const sdecl::ServiceDecl serviceDecl2(
      sdecl::class_<Foo>(),
        "com.sun.star.comp.stuff.MyImplName1",
        "com.sun.star.stuff.MyServiceName1" );
 
const sdecl::ServiceDecl serviceDecl2(
      sdecl::class_<Bar>(),
        "com.sun.star.comp.stuff.MyImplName2",
        "com.sun.star.stuff.MyServiceName2" );
 
// The C shared lib entry points
COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1, serviceDecl2)

This is the perfect world example. In real world, we still have ports using gcc3.3 or less with that old parser bug. To be portable, the code should rather look like this (only slightly more obfuscated):

#include "comphelper/servicedecl.hxx"
 
class Foo : public SomeUNOInterface {...};
class Bar : public SomeOtherUNOInterface {...};
 
namespace sdecl = comphelper::service_decl;
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3)
 sdecl::class_<Foo> serviceImpl;
 const sdecl::ServiceDecl serviceDecl1(
     serviceImpl,
#else
 const sdecl::ServiceDecl serviceDecl2(
     sdecl::class_<Foo>(),
#endif
     "com.sun.star.comp.stuff.MyImplName1",
     "com.sun.star.stuff.MyServiceName1" );
 
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3)
 sdecl::class_<Bar> serviceImpl;
 const sdecl::ServiceDecl serviceDecl2(
     serviceImpl,
#else
 const sdecl::ServiceDecl serviceDecl2(
     sdecl::class_<Bar>(),
#endif
     "com.sun.star.comp.stuff.MyImplName2",
     "com.sun.star.stuff.MyServiceName2" );
 
// The C shared lib entry points
COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1, serviceDecl2)
Personal tools