Uno/Cpp/Snippet/Thread Unsafe Component
From Apache OpenOffice Wiki
< Uno | Cpp
Revision as of 17:41, 23 February 2008 by SergeMoutou (talk | contribs)
//
// A simple Thread Unsafe component
//
// setenv UNO_TYPES <office>/program/types.rdb
// setenv UNO_SERVICES <office>/program/services.rdb
//
// uno -c impl.test.TestComponent -l file:///usr/local/kr/Projects/threading/utf2/SRC680M124/cppuhelper/unxlngi6/lib/TestComponent.uno.so
//
#include <uno/environment.hxx>
#include "cppuhelper/implbase1.hxx"
#include "cppuhelper/implementationentry.hxx"
#include "com/sun/star/lang/XMultiComponentFactory.hpp"
#include "com/sun/star/lang/XServiceInfo.hpp"
#include <stdio.h>
using namespace ::com::sun::star;
static rtl::OUString impl_name(RTL_CONSTASCII_USTRINGPARAM("impl.test.TestComponent"));
static uno::Sequence<rtl::OUString> getSupportedServiceNames()
{
uno::Sequence<rtl::OUString> serviceNames(1);
serviceNames[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.lang.ServiceInfo"));
return serviceNames;
}
static void print_curr_EnvDcp(char const * str)
{
uno::Environment env(uno::getCurrentEnvironment());
rtl::OString envDcp(OUStringToOString(env.getTypeName(), RTL_TEXTENCODING_ASCII_US));
fprintf(stderr, "%s \"%s\"\n", str, envDcp.getStr());
}
class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
{
public:
explicit TestComponent() { print_curr_EnvDcp(__FUNCTION__); }
virtual ~TestComponent() { print_curr_EnvDcp(__FUNCTION__); }
uno::Any SAL_CALL queryInterface(uno::Type const & rType ) throw (uno::RuntimeException)
{
return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
}
void SAL_CALL release() throw () { cppu::WeakImplHelper1<lang::XServiceInfo>::release(); }
void SAL_CALL acquire() throw () { cppu::WeakImplHelper1<lang::XServiceInfo>::acquire(); }
// lang::XServiceInfo
virtual rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException) { return impl_name; }
virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName) throw (uno::RuntimeException);
virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames() throw (uno::RuntimeException)
{
return getSupportedServiceNames();
}
};
static uno::Reference<uno::XInterface> SAL_CALL create(uno::Reference<uno::XComponentContext> const &)
{
try
{
return static_cast<cppu::OWeakObject *>(new TestComponent());
}
catch (std::bad_alloc &)
{
throw uno::RuntimeException(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("std::bad_alloc")),
uno::Reference<uno::XInterface>());
}
}
sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName) throw (uno::RuntimeException)
{
uno::Sequence<rtl::OUString> serviceNames = getSupportedServiceNames();
for (sal_Int32 n = 0; n < serviceNames.getLength(); ++n)
{
if (serviceNames[n] == ServiceName)
return true;
}
return false;
}
extern "C" void * SAL_CALL component_getFactory(char const * implName,
void * serviceManager,
void * registryKey)
{
print_curr_EnvDcp(__FUNCTION__);
lang::XSingleComponentFactory * pFactory = NULL;
rtl::OUString impName(implName, rtl_str_getLength(implName), RTL_TEXTENCODING_ASCII_US);
if (impName.equals(impl_name))
{
uno::Reference< lang::XSingleComponentFactory > xFactory = cppu::createSingleComponentFactory(
create,
impName,
getSupportedServiceNames());
xFactory->acquire();
pFactory = xFactory.get();
}
return pFactory;
}
extern "C" void SAL_CALL component_getImplementationEnvironment(sal_Char const ** ppEnvTypeName,
uno_Environment ** ppEnv)
{
*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME ":unsafe";
}