Bump version to 5.0-43
[LibreOffice.git] / cppuhelper / source / macro_expander.cxx
blob3b950622ca6527a47513d977c2f3dff2c7050338
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 .
21 #include <rtl/bootstrap.hxx>
23 #include <uno/lbnames.h>
24 #include <uno/mapping.hxx>
26 #include <cppuhelper/factory.hxx>
27 #include <cppuhelper/compbase2.hxx>
28 #include <cppuhelper/component_context.hxx>
29 #include <cppuhelper/supportsservice.hxx>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/util/XMacroExpander.hpp>
33 #include <com/sun/star/uno/RuntimeException.hpp>
35 #include "macro_expander.hxx"
36 #include "paths.hxx"
38 #define SERVICE_NAME_A "com.sun.star.lang.MacroExpander"
39 #define SERVICE_NAME_B "com.sun.star.lang.BootstrapMacroExpander"
40 #define IMPL_NAME "com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander"
42 using namespace ::osl;
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
46 using rtl::Bootstrap;
47 using rtl::OUString;
49 namespace cppu
52 Bootstrap const & get_unorc()
54 static rtlBootstrapHandle s_bstrap = 0;
55 if (! s_bstrap)
57 OUString iniName(getUnoIniUri());
58 rtlBootstrapHandle bstrap = rtl_bootstrap_args_open( iniName.pData );
60 ClearableMutexGuard guard( Mutex::getGlobalMutex() );
61 if (s_bstrap)
63 guard.clear();
64 rtl_bootstrap_args_close( bstrap );
66 else
68 s_bstrap = bstrap;
71 return *reinterpret_cast<Bootstrap const *>(&s_bstrap);
76 namespace cppuhelper { namespace detail {
78 rtl::OUString expandMacros(rtl::OUString const & text) {
79 rtl::OUString t(text);
80 rtl_bootstrap_expandMacros_from_handle(
81 cppu::get_unorc().getHandle(), &t.pData);
82 return t;
85 } }
87 namespace
90 class ImplNames
92 private:
93 Sequence<OUString> m_aNames;
94 public:
95 ImplNames() : m_aNames(2)
97 m_aNames[0] = SERVICE_NAME_A;
98 m_aNames[1] = SERVICE_NAME_B;
100 const Sequence<OUString>& getNames() const { return m_aNames; }
103 class theImplNames : public rtl::Static<ImplNames, theImplNames> {};
105 inline OUString s_impl_name()
107 return OUString(IMPL_NAME);
110 inline Sequence< OUString > const & s_get_service_names()
112 return theImplNames::get().getNames();
115 typedef ::cppu::WeakComponentImplHelper2<
116 util::XMacroExpander, lang::XServiceInfo > t_uno_impl;
118 struct mutex_holder
120 Mutex m_mutex;
123 class Bootstrap_MacroExpander : public mutex_holder, public t_uno_impl
125 protected:
126 virtual void SAL_CALL disposing() SAL_OVERRIDE;
128 public:
129 inline Bootstrap_MacroExpander()
130 : t_uno_impl( m_mutex )
132 virtual ~Bootstrap_MacroExpander();
134 // XMacroExpander impl
135 virtual OUString SAL_CALL expandMacros( OUString const & exp )
136 throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception) SAL_OVERRIDE;
137 // XServiceInfo impl
138 virtual OUString SAL_CALL getImplementationName()
139 throw (RuntimeException, std::exception) SAL_OVERRIDE;
140 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
141 throw (RuntimeException, std::exception) SAL_OVERRIDE;
142 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
143 throw (RuntimeException, std::exception) SAL_OVERRIDE;
147 void Bootstrap_MacroExpander::disposing()
150 Bootstrap_MacroExpander::~Bootstrap_MacroExpander()
153 // XServiceInfo impl
155 OUString Bootstrap_MacroExpander::getImplementationName()
156 throw (RuntimeException, std::exception)
158 return s_impl_name();
161 sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
162 throw (RuntimeException, std::exception)
164 return cppu::supportsService(this, serviceName);
167 Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
168 throw (RuntimeException, std::exception)
170 return s_get_service_names();
173 // XMacroExpander impl
175 OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
176 throw (lang::IllegalArgumentException, uno::RuntimeException, std::exception)
178 return cppuhelper::detail::expandMacros( exp );
182 Reference< XInterface > SAL_CALL service_create(
183 SAL_UNUSED_PARAMETER Reference< XComponentContext > const & )
185 return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander );
190 namespace cppuhelper { namespace detail {
192 Reference< lang::XSingleComponentFactory > create_bootstrap_macro_expander_factory()
194 Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
195 service_create,
196 s_impl_name(),
197 s_get_service_names() ));
199 uno::Environment curr_env(Environment::getCurrent());
200 uno::Environment target_env(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
202 uno::Mapping target2curr(target_env, curr_env);
204 return Reference<lang::XSingleComponentFactory>(
205 static_cast<lang::XSingleComponentFactory *>(
206 target2curr.mapInterface(free.get(), cppu::UnoType<decltype(free)>::get())),
207 SAL_NO_ACQUIRE);
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */