Difference between revisions of "Documentation/DevGuide/WritingUNO/C++/Providing a Single Factory Using a Helper Method"
(Initial author Sun Microsystems, Inc.) |
m (1 revision(s)) |
(No difference)
|
Revision as of 13:07, 15 February 2008
- Class Definition with Helper Template Classes
- Implementing your own Interfaces
- Providing a Single Factory Using a Helper Method
- Write Registration Info Using a Helper Method
- Provide Implementation Environment
- Implementing without Helpers
- Storing the Service Manager for Further Use
- Create Instance with Arguments
- Multiple Components in One Dynamic Link Library
- Building and Testing C++ Components
C++ component libraries must export an external "C" function called component_getFactory()
that supplies a factory object for the given implementation. Use ::cppu::component_getFactoryHelper()
to create this function. The declarations for it are included through cppuhelper/implementationentry.hxx.
The component_getFactory()
method appears at the end of the following listing. This method assumes that the component includes a static ::cppu::ImplementationEntry
array s_component_entries[]
, which contains a number of function pointers. The listing shows how to write the component, so that the function pointers for all services of a multi-service component are correctly initialized.
#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 > { OUString m_arg; public: // focus on three given interfaces, // no need to implement XInterface, XTypeProvider, XWeak // XInitialization will be called upon createInstanceWithArguments[AndContext]() virtual void SAL_CALL initialize( Sequence< Any > const & args ) throw (Exception); // XSomething virtual OUString SAL_CALL methodOne( OUString const & str ) throw (RuntimeException); // XServiceInfo virtual OUString SAL_CALL getImplementationName() throw (RuntimeException); virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) throw (RuntimeException); virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw (RuntimeException); }; // Implementation of XSomething, XServiceInfo and XInitilization omitted here: ... // component operations from service1_impl.cxx extern Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl(); extern OUString SAL_CALL getImplementationName_MyService1Impl(); extern Reference< XInterface > SAL_CALL create_MyService1Impl( Reference< XComponentContext > const & xContext ) SAL_THROW( () ); // component operations for MyService2Impl static Sequence< OUString > getSupportedServiceNames_MyService2Impl() { Sequence<OUString> names(1); names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2")); return names; } static OUString getImplementationName_MyService2Impl() { return OUString( RTL_CONSTASCII_USTRINGPARAM( "my_module.my_sc_implementation.MyService2") ); } Reference< XInterface > SAL_CALL create_MyService2Impl( Reference< XComponentContext > const & xContext ) SAL_THROW( () ) { return static_cast< lang::XTypeProvider * >( new MyService2Impl() ); } { { create_MyService1Impl, getImplementationName_MyService1Impl, getSupportedServiceNames_MyService1Impl, ::cppu::createSingleComponentFactory, 0, 0 }, { create_MyService2Impl, getImplementationName_MyService2Impl, getSupportedServiceNames_MyService2Impl, ::cppu::createSingleComponentFactory, 0, 0 }, { 0, 0, 0, 0, 0, 0 } }; } extern "C" { void * SAL_CALL component_getFactory( sal_Char const * implName, lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry ) { return ::cppu::component_getFactoryHelper( implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries ); } // getImplementationEnvironment and component_writeInfo are described later, we omit them here ... }
The static variable s_component_entries
defines a null-terminated array of entries concerning the service implementations of the shared library. A service implementation entry consists of function pointers for
- object creation:
create_MyServiceXImpl()
- implementation name:
getImplementationName_MyServiceXImpl()
- supported service names:
getSupportedServiceNames_MyServiceXImpl()
- factory helper to be used:
::cppu::createComponentFactory()
The last two values are reserved for future use and therefore can be 0.
Content on this page is licensed under the Public Documentation License (PDL). |