Branch libreoffice-5-0-4
[LibreOffice.git] / cppuhelper / test / testcmp / TestComponent.cxx
blob64d2b9ff36afd1798922d00a41810d432b8bd85d
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>
43 #include <uno/lbnames.h>
45 using namespace ::com::sun::star;
48 #define LOG_LIFECYCLE_TestComponent
49 #ifdef LOG_LIFECYCLE_TestComponent
50 # include <iostream>
51 # define LOG_LIFECYCLE_TestComponent_emit(x) x
53 #else
54 # define LOG_LIFECYCLE_TestComponent_emit(x)
56 #endif
59 class TestComponent: public cppu::WeakImplHelper1<lang::XServiceInfo>
61 rtl::OUString m_implName;
63 public:
64 static uno::Reference<uno::XInterface> create(
65 uno::Reference<uno::XComponentContext> const & xCtx
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 try
95 return static_cast<cppu::OWeakObject *>(new TestComponent(xCtx));
97 catch (std::bad_alloc &)
99 throw uno::RuntimeException("std::bad_alloc");
103 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames_Static()
105 uno::Sequence<rtl::OUString> serviceNames(1);
106 serviceNames[0] = rtl::OUString("com.sun.star.lang.ServiceInfo");
108 return serviceNames;
112 TestComponent::TestComponent(uno::Reference<uno::XComponentContext> const & xCtx)
113 : m_xComponentContext(xCtx)
115 LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::TestComponent()", this));
118 TestComponent::~TestComponent()
120 LOG_LIFECYCLE_TestComponent_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestComponent::~TestComponent", this));
123 rtl::OUString SAL_CALL TestComponent::getImplementationName()
124 throw (uno::RuntimeException)
126 return m_implName;
129 void SAL_CALL TestComponent::acquire() throw ()
131 cppu::WeakImplHelper1<lang::XServiceInfo>::acquire();
134 void SAL_CALL TestComponent::release() throw ()
136 cppu::WeakImplHelper1<lang::XServiceInfo>::release();
139 uno::Any SAL_CALL TestComponent::queryInterface(uno::Type const & rType ) throw (::com::sun::star::uno::RuntimeException)
141 return cppu::WeakImplHelper1<lang::XServiceInfo>::queryInterface(rType);
144 sal_Bool SAL_CALL TestComponent::supportsService(rtl::OUString const & ServiceName)
145 throw (uno::RuntimeException)
147 return cppu::supportsService(this, ServiceName);
150 uno::Sequence<rtl::OUString> SAL_CALL TestComponent::getSupportedServiceNames()
151 throw (uno::RuntimeException)
153 return getSupportedServiceNames_Static();
156 extern "C" sal_Bool SAL_CALL component_writeInfo(
157 void * /*serviceManager*/,
158 void * /*registryKey*/
161 g_envDcp = uno::Environment::getCurrent().getTypeName();
163 return true;
166 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
167 char const * pImplName,
168 void * /*serviceManager*/,
169 void * /*registryKey*/
172 g_envDcp = uno::Environment::getCurrent().getTypeName();
174 uno::Reference< lang::XSingleComponentFactory > xFactory;
176 rtl::OUString uTmp(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
178 rtl::OUString uImplName(cppu::EnvDcp::getTypeName(uTmp));
179 rtl::OUString cmpName("impl.test.TestComponent");
181 if (uImplName.equals(cmpName))
183 xFactory = cppu::createSingleComponentFactory(
184 TestComponent::create,
185 uImplName,
186 TestComponent::getSupportedServiceNames_Static());
188 xFactory->acquire();
191 return xFactory.get();
194 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironmentExt(
195 sal_Char const ** envTypeName,
196 uno_Environment ** /*ppEnv*/,
197 sal_Char const * pImplName,
198 uno_Environment * /*pSrcEnv*/
201 rtl::OString purpose;
203 if (pImplName) // this is the purpose for a specified impl
205 rtl::OUString uImplName(pImplName, rtl_str_getLength(pImplName), RTL_TEXTENCODING_ASCII_US);
206 purpose = rtl::OUStringToOString(cppu::EnvDcp::getPurpose(uImplName), RTL_TEXTENCODING_ASCII_US);
209 if (!purpose.getLength())
211 char * pPurpose = getenv("TestComponent.uno");
212 if (pPurpose)
213 purpose = rtl::OString(pPurpose);
216 if (purpose.getLength() == 0)
217 *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
219 else
221 char buff[256];
222 strcpy(buff, CPPU_CURRENT_LANGUAGE_BINDING_NAME);
223 strcat(buff, purpose.getStr());
225 *envTypeName = strdup(buff);
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */