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

From Apache OpenOffice Wiki
< Development‎ | Cpp‎ | Helper
Jump to: navigation, search
m (example fix)
m
Line 30: Line 30:
  
 
<source lang="cpp">
 
<source lang="cpp">
#include <comphelper/servicedecl.hxx>
+
#include "comphelper/servicedecl.hxx"
  
 
class Foo : public SomeUNOInterface {...};
 
class Foo : public SomeUNOInterface {...};
Line 47: Line 47:
  
 
// The C shared lib entry points
 
// The C shared lib entry points
COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1,serviceDecl2)
+
COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1, serviceDecl2)
 
</source>
 
</source>
  
Line 53: Line 53:
  
 
<source lang="cpp">
 
<source lang="cpp">
#include <comphelper/servicedecl.hxx>
+
#include "comphelper/servicedecl.hxx"
  
 
class Foo : public SomeUNOInterface {...};
 
class Foo : public SomeUNOInterface {...};
Line 60: Line 60:
 
namespace sdecl = comphelper::service_decl;
 
namespace sdecl = comphelper::service_decl;
 
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3)
 
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  sdecl::class_<SomeUNOInterface> serviceImpl;
+
  sdecl::class_<Foo> serviceImpl;
 
  const sdecl::ServiceDecl serviceDecl1(
 
  const sdecl::ServiceDecl serviceDecl1(
 
     serviceImpl,
 
     serviceImpl,
 
#else
 
#else
 
  const sdecl::ServiceDecl serviceDecl2(
 
  const sdecl::ServiceDecl serviceDecl2(
     sdecl::class_<SomeUNOInterface>(),
+
     sdecl::class_<Foo>(),
 
#endif
 
#endif
 
     "com.sun.star.comp.stuff.MyImplName1",
 
     "com.sun.star.comp.stuff.MyImplName1",
Line 71: Line 71:
  
 
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3)
 
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3)
  sdecl::class_<SomeOtherUNOInterface> serviceImpl;
+
  sdecl::class_<Bar> serviceImpl;
 
  const sdecl::ServiceDecl serviceDecl2(
 
  const sdecl::ServiceDecl serviceDecl2(
 
     serviceImpl,
 
     serviceImpl,
 
#else
 
#else
 
  const sdecl::ServiceDecl serviceDecl2(
 
  const sdecl::ServiceDecl serviceDecl2(
     sdecl::class_<SomeOtherUNOInterface>(),
+
     sdecl::class_<Bar>(),
 
#endif
 
#endif
 
     "com.sun.star.comp.stuff.MyImplName2",
 
     "com.sun.star.comp.stuff.MyImplName2",
Line 82: Line 82:
  
 
// The C shared lib entry points
 
// The C shared lib entry points
COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1,serviceDecl2)
+
COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1, serviceDecl2)
 
</source>
 
</source>
  

Revision as of 23:35, 18 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