Difference between revisions of "Development/Cpp/Helper/ServiceDecl"
m (→Description) |
B michaelsen (Talk | contribs) |
||
(7 intermediate revisions by 3 users not shown) | |||
Line 7: | Line 7: | ||
A {{CppClass|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 | A {{CppClass|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 | ||
− | < | + | <source lang="cpp"> |
void SAL_CALL component_getImplementationEnvironment( const sal_Char** ppEnvTypeName, | void SAL_CALL component_getImplementationEnvironment( const sal_Char** ppEnvTypeName, | ||
uno_Environment** ppEnv ); | uno_Environment** ppEnv ); | ||
Line 15: | Line 15: | ||
::com::sun::star::lang::XMultiServiceFactory* pServiceManager, | ::com::sun::star::lang::XMultiServiceFactory* pServiceManager, | ||
::com::sun::star::registry::XRegistryKey* pRegistryKey ); | ::com::sun::star::registry::XRegistryKey* pRegistryKey ); | ||
− | </ | + | </source> |
− | needs some boilerplate to provide the service factory, which is repeated almost literally in every implementation (see the [ | + | needs some boilerplate to provide the service factory, which is repeated almost literally in every implementation (see the [[Documentation/DevGuide/WritingUNO/Providing_a_Single_Factory_Using_a_Helper_Method|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, | The described class encapsulates almost everything of this, and provides a set of macros on top, that define the C entry functions, | ||
Line 23: | Line 23: | ||
=== Interface === | === Interface === | ||
+ | See the excellent inline header file commentary [http://util.openoffice.org/source/browse/util/comphelper/inc/comphelper/servicedecl.hxx?rev=1.4&view=markup here] for details of use (should the example section below not suffice). | ||
=== Example === | === Example === | ||
Line 28: | Line 29: | ||
The following example provides two services to the UNO runtime. | The following example provides two services to the UNO runtime. | ||
− | < | + | <source lang="cpp"> |
− | #include | + | #include "comphelper/servicedecl.hxx" |
class Foo : public SomeUNOInterface {...}; | class Foo : public SomeUNOInterface {...}; | ||
Line 36: | Line 37: | ||
namespace sdecl = comphelper::service_decl; | namespace sdecl = comphelper::service_decl; | ||
const sdecl::ServiceDecl serviceDecl2( | const sdecl::ServiceDecl serviceDecl2( | ||
− | sdecl::class_< | + | sdecl::class_<Foo>(), |
"com.sun.star.comp.stuff.MyImplName1", | "com.sun.star.comp.stuff.MyImplName1", | ||
"com.sun.star.stuff.MyServiceName1" ); | "com.sun.star.stuff.MyServiceName1" ); | ||
const sdecl::ServiceDecl serviceDecl2( | const sdecl::ServiceDecl serviceDecl2( | ||
− | sdecl::class_< | + | sdecl::class_<Bar>(), |
"com.sun.star.comp.stuff.MyImplName2", | "com.sun.star.comp.stuff.MyImplName2", | ||
"com.sun.star.stuff.MyServiceName2" ); | "com.sun.star.stuff.MyServiceName2" ); | ||
// The C shared lib entry points | // The C shared lib entry points | ||
− | COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1,serviceDecl2) | + | COMPHELPER_SERVICEDECL_EXPORTS2(serviceDecl1, serviceDecl2) |
− | </ | + | </source> |
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): | 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): | ||
− | < | + | <source lang="cpp"> |
− | #include | + | #include "comphelper/servicedecl.hxx" |
class Foo : public SomeUNOInterface {...}; | class Foo : public SomeUNOInterface {...}; | ||
Line 59: | 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_< | + | 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_< | + | sdecl::class_<Foo>(), |
#endif | #endif | ||
"com.sun.star.comp.stuff.MyImplName1", | "com.sun.star.comp.stuff.MyImplName1", | ||
Line 70: | Line 71: | ||
#if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3) | #if defined (__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ <= 3) | ||
− | sdecl::class_< | + | 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_< | + | sdecl::class_<Bar>(), |
#endif | #endif | ||
"com.sun.star.comp.stuff.MyImplName2", | "com.sun.star.comp.stuff.MyImplName2", | ||
Line 81: | 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> |
+ | |||
− | |||
[[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
|
Contents
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)