Difference between revisions of "Documentation/DevGuide/WritingUNO/C++/Class Definition with Helper Template Classes"
OOoWikiBot (Talk | contribs) m (FINAL VERSION FOR L10N) |
|||
Line 13: | Line 13: | ||
The implementation of <code>my_module.MyService2</code> uses the <code>::cppu::WeakImplHelper3<></code> 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. | The implementation of <code>my_module.MyService2</code> uses the <code>::cppu::WeakImplHelper3<></code> 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. | ||
<!--[SOURCE:Components/CppComponent/service2_impl.cxx]--> | <!--[SOURCE:Components/CppComponent/service2_impl.cxx]--> | ||
− | < | + | <syntaxhighlight lang="cpp"> |
#include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces | #include <cppuhelper/implbase3.hxx> // "3" implementing three interfaces | ||
#include <cppuhelper/factory.hxx> | #include <cppuhelper/factory.hxx> | ||
Line 38: | Line 38: | ||
}; | }; | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
The next section focuses on coding <idl>com.sun.star.lang.XServiceInfo</idl>, <idl>com.sun.star.lang.XInitialization</idl> and the sample interface <code>my_module.XSomething</code>. | The next section focuses on coding <idl>com.sun.star.lang.XServiceInfo</idl>, <idl>com.sun.star.lang.XInitialization</idl> and the sample interface <code>my_module.XSomething</code>. | ||
− | The cppuhelper shared library provides additional implementation helper classes, for example, supporting <idl>com.sun.star.lang.XComponent</idl>. Browse the ''::cppu namespace'' in the C++ reference of the SDK or on [ | + | The cppuhelper shared library provides additional implementation helper classes, for example, supporting <idl>com.sun.star.lang.XComponent</idl>. Browse the ''::cppu namespace'' in the C++ reference of the SDK or on [https://www.openoffice.org/udk/ www.openoffice.org/udk/]. |
=== XServiceInfo === | === XServiceInfo === | ||
− | An UNO service implementation supports <idl>com.sun.star.lang.XServiceInfo</idl> providing information about its implementation name and supported services. The implementation name is a unique name referencing the specific implementation. In this case, <code>my_module.my_sc_impl.MyService1</code> and <code>my_module.my_sc_impl.MyService2</code> respectively. The implementation name is used later when registering the implementation into the ''simple_component.rdb'' registry used for {{ | + | An UNO service implementation supports <idl>com.sun.star.lang.XServiceInfo</idl> providing information about its implementation name and supported services. The implementation name is a unique name referencing the specific implementation. In this case, <code>my_module.my_sc_impl.MyService1</code> and <code>my_module.my_sc_impl.MyService2</code> respectively. The implementation name is used later when registering the implementation into the ''simple_component.rdb'' registry used for {{AOo}}. 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 <idl>com.sun.star.lang.XServiceInfo</idl> interface. This interface has three methods, and can be coded for one supported service as follows: | Our service instance has to support the <idl>com.sun.star.lang.XServiceInfo</idl> interface. This interface has three methods, and can be coded for one supported service as follows: | ||
<!--[SOURCE:Components/CppComponent/service2_impl.cxx]--> | <!--[SOURCE:Components/CppComponent/service2_impl.cxx]--> | ||
− | < | + | <syntaxhighlight lang="cpp"> |
// XServiceInfo implementation | // XServiceInfo implementation | ||
OUString MyService2Impl::getImplementationName() | OUString MyService2Impl::getImplementationName() | ||
Line 69: | Line 69: | ||
return getSupportedServiceNames_MyService2Impl(); | return getSupportedServiceNames_MyService2Impl(); | ||
} | } | ||
− | </ | + | </syntaxhighlight> |
{{PDL1}} | {{PDL1}} | ||
[[Category:Documentation/Developer's Guide/Writing UNO Components]] | [[Category:Documentation/Developer's Guide/Writing UNO Components]] |
Latest revision as of 14:11, 24 December 2020
- 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
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). |