bump product version to 4.1.6.2
[LibreOffice.git] / cppuhelper / test / testcmp / TestComponent.cxx
blob999417a57a244f622b397b873da99586c4ac3975
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #define CPPUHELPER_TEST_COMPONENT_IMPL
23 #include "TestComponent.hxx"
26 #include <string.h>
28 #include "osl/thread.h"
30 #include "cppuhelper/implbase1.hxx"
31 #include "cppuhelper/implementationentry.hxx"
32 #include "cppuhelper/supportsservice.hxx"
34 #include "com/sun/star/lang/XMultiComponentFactory.hpp"
35 #include "com/sun/star/lang/XServiceInfo.hpp"
37 #include "com/sun/star/uno/XComponentContext.hpp"
40 #include "cppu/EnvDcp.hxx"
42 #include <uno/environment.hxx>
44 using namespace ::com::sun::star;
47 #define LOG_LIFECYCLE_TestComponent
48 #ifdef LOG_LIFECYCLE_TestComponent
49 # include <iostream>
50 # define LOG_LIFECYCLE_TestComponent_emit(x) x
52 #else
53 # define LOG_LIFECYCLE_TestComponent_emit(x)
55 #endif
58 class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
60 rtl::OUString m_implName;
62 public:
63 static uno::Reference<uno::XInterface> create(
64 uno::Reference<uno::XComponentContext> const & xCtx
66 SAL_THROW((uno::Exception));
69 static uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames_Static();
71 explicit TestComponent(uno::Reference<uno::XComponentContext> const & xCtx);
72 virtual ~TestComponent();
74 uno::Any SAL_CALL queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException);
75 void SAL_CALL release() throw ();
76 void SAL_CALL acquire() throw ();
78 // lang::XServiceInfo
79 virtual rtl::OUString SAL_CALL getImplementationName() throw (uno::RuntimeException);
80 virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
81 throw (uno::RuntimeException);
82 virtual uno::Sequence<rtl::OUString> SAL_CALL getSupportedServiceNames()
83 throw (uno::RuntimeException);
85 protected:
86 uno::Reference<uno::XComponentContext> m_xComponentContext;
90 uno::Reference<uno::XInterface> SAL_CALL TestComponent::create(
91 uno::Reference<uno::XComponentContext> const & xCtx
93 SAL_THROW((uno::Exception))
95 try
97 return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
99 catch (std::bad_alloc &)
101 throw uno::RuntimeException(rtl::OUString("std::bad_alloc"),
102 uno::Reference<uno::XInterface>());
106 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
108 uno::Sequence<rtl::OUString> serviceNames(1);
109 serviceNames[0] = rtl::OUString("com.sun.star.lang.ServiceInfo");
111 return serviceNames;
115 TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
116 : m_xComponentContext(xCtx)
118 LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
121 TestComponent::~TestComponent()
123 LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
126 rtl::OUString SAL_CALL TestComponent::getImplementationName()
127 throw (uno::RuntimeException)
129 return m_implName;
132 void SAL_CALL TestComponent::acquire() throw ()
134 cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
137 void SAL_CALL TestComponent::release() throw ()
139 cppu::WeakImplHelper1<lang::XServiceInfo>::release();
142 uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException)
144 return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
147 sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
148 throw (uno::RuntimeException)
150 return cppu::supportsService(this, ServiceName);
153 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
154 throw (uno::RuntimeException)
156 return getSupportedServiceNames_Static();
159 extern "C" sal_Bool SAL_CALL component_writeInfo(
160 void * /*serviceManager*/,
161 void * /*registryKey*/
164 g_envDcp = uno::Environment::getCurrent().getTypeName();
166 return true;
169 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
170 char const * pImplName,
171 void * /*serviceManager*/,
172 void * /*registryKey*/
175 g_envDcp = uno::Environment::getCurrent().getTypeName();
177 uno::Reference< lang::XSingleComponentFactory > xFactory;
179 rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
181 rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
182 rtl::OUString cmpName("impl.test.TestComponent");
184 if (uImplName.equals(cmpName))
186 xFactory = cppu::createSingleComponentFactory(
187 TestComponent::create,
188 uImplName,
189 TestComponent::getSupportedServiceNames_Static());
191 xFactory->acquire();
194 return xFactory.get();
197 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironmentExt(
198 sal_Char const ** envTypeName,
199 uno_Environment ** /*ppEnv*/,
200 sal_Char const * pImplName,
201 uno_Environment * /*pSrcEnv*/
204 rtl::OString purpose;
206 if (pImplName) // this is the purpose for a specified impl
208 rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
209 purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
212 if (!purpose.getLength())
214 char * pPurpose = getenv("TestComponent.uno");
215 if (pPurpose)
216 purpose = rtl::OString(pPurpose);
219 if (purpose.getLength() == 0)
220 *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
222 else
224 char buff[256];
225 strcpy(buff, CPPU_STRINGIFY(CPPU_ENV));
226 strcat(buff, purpose.getStr());
228 *envTypeName = strdup(buff);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */