Uno/Cpp/Snippet/Thread Unsafe Component

From Apache OpenOffice Wiki
< Uno‎ | Cpp
Revision as of 15:54, 12 June 2006 by Kr (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
// A simple Thread Unsafe component


#include "cppuhelper/implbase1.hxx"
#include "cppuhelper/implementationentry.hxx"

#include "com/sun/star/lang/XMultiComponentFactory.hpp"
#include "com/sun/star/lang/XServiceInfo.hpp"


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;
}

class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
{
public:
	explicit TestComponent() {};
	virtual ~TestComponent() {};

	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)
{
	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";
}
Personal tools