LibreOffice Module cppuhelper (master)  1
macro_expander.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
21 #include <rtl/bootstrap.hxx>
22 
23 #include <uno/lbnames.h>
24 #include <uno/mapping.hxx>
25 
26 #include <cppuhelper/factory.hxx>
27 #include <cppuhelper/compbase.hxx>
29 
30 #include <com/sun/star/lang/XServiceInfo.hpp>
31 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
32 #include <com/sun/star/util/XMacroExpander.hpp>
33 
34 #include "macro_expander.hxx"
35 #include "paths.hxx"
36 
37 constexpr OUStringLiteral SERVICE_NAME_A = u"com.sun.star.lang.MacroExpander";
38 constexpr OUStringLiteral SERVICE_NAME_B = u"com.sun.star.lang.BootstrapMacroExpander";
39 constexpr OUStringLiteral IMPL_NAME = u"com.sun.star.lang.comp.cppuhelper.BootstrapMacroExpander";
40 
41 using namespace ::osl;
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 
45 using rtl::Bootstrap;
46 
47 namespace cppu
48 {
49 
50 static Bootstrap const & get_unorc()
51 {
52  static rtlBootstrapHandle s_bstrap = rtl_bootstrap_args_open(getUnoIniUri().pData);
53  return *reinterpret_cast<Bootstrap const *>(&s_bstrap);
54 }
55 
56 }
57 
58 namespace cppuhelper::detail {
59 
60 OUString expandMacros(OUString const & text) {
61  OUString t(text);
62  rtl_bootstrap_expandMacros_from_handle(
63  cppu::get_unorc().getHandle(), &t.pData);
64  return t;
65 }
66 
67 }
68 
69 namespace
70 {
71 
72 OUString s_impl_name()
73 {
74  return IMPL_NAME;
75 }
76 
77 Sequence< OUString > const & s_get_service_names()
78 {
79  static const Sequence< OUString > IMPL_NAMES {
82  };
83  return IMPL_NAMES;
84 }
85 
86 typedef cppu::WeakComponentImplHelper<
87  util::XMacroExpander, lang::XServiceInfo > t_uno_impl;
88 
89 struct mutex_holder
90 {
91  Mutex m_mutex;
92 };
93 
94 class Bootstrap_MacroExpander : public mutex_holder, public t_uno_impl
95 {
96 protected:
97  virtual void SAL_CALL disposing() override;
98 
99 public:
100  Bootstrap_MacroExpander()
101  : t_uno_impl( m_mutex )
102  {}
103 
104  // XMacroExpander impl
105  virtual OUString SAL_CALL expandMacros( OUString const & exp ) override;
106  // XServiceInfo impl
107  virtual OUString SAL_CALL getImplementationName() override;
108  virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName ) override;
109  virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
110 };
111 
112 
113 void Bootstrap_MacroExpander::disposing()
114 {}
115 
116 // XServiceInfo impl
117 
118 OUString Bootstrap_MacroExpander::getImplementationName()
119 {
120  return s_impl_name();
121 }
122 
123 sal_Bool Bootstrap_MacroExpander::supportsService( OUString const & serviceName )
124 {
125  return cppu::supportsService(this, serviceName);
126 }
127 
128 Sequence< OUString > Bootstrap_MacroExpander::getSupportedServiceNames()
129 {
130  return s_get_service_names();
131 }
132 
133 // XMacroExpander impl
134 
135 OUString Bootstrap_MacroExpander::expandMacros( OUString const & exp )
136 {
137  return cppuhelper::detail::expandMacros( exp );
138 }
139 
140 
141 Reference< XInterface > service_create(
142  SAL_UNUSED_PARAMETER Reference< XComponentContext > const & )
143 {
144  return static_cast< ::cppu::OWeakObject * >( new Bootstrap_MacroExpander );
145 }
146 
147 }
148 
149 namespace cppuhelper::detail {
150 
151 Reference< lang::XSingleComponentFactory > create_bootstrap_macro_expander_factory()
152 {
153  Reference< lang::XSingleComponentFactory > free(::cppu::createSingleComponentFactory(
154  service_create,
155  s_impl_name(),
156  s_get_service_names() ));
157 
158  uno::Environment curr_env(Environment::getCurrent());
159  uno::Environment target_env(CPPU_CURRENT_LANGUAGE_BINDING_NAME);
160 
161  uno::Mapping target2curr(target_env, curr_env);
162 
163  return Reference<lang::XSingleComponentFactory>(
164  static_cast<lang::XSingleComponentFactory *>(
165  target2curr.mapInterface(free.get(), cppu::UnoType<decltype(free)>::get())),
166  SAL_NO_ACQUIRE);
167 }
168 
169 }
170 
171 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XInterface >(SAL_CALL *ComponentFactoryFunc)(css CPPUHELPER_DLLPUBLIC css::uno::Reference< css::lang::XSingleComponentFactory > SAL_CALL createSingleComponentFactory(ComponentFactoryFunc fptr,::rtl::OUString const &rImplementationName, css::uno::Sequence< ::rtl::OUString > const &rServiceNames, rtl_ModuleCount *pModCount=NULL)
Function pointer declaration.
Definition: factory.hxx:146
std::unique_ptr< ContentProperties > pData
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
A helper for implementations of com.sun.star.lang.XServiceInfo.
OUString getUnoIniUri()
Definition: paths.cxx:54
Reference< lang::XSingleComponentFactory > create_bootstrap_macro_expander_factory()
constexpr OUStringLiteral SERVICE_NAME_A
float u
unsigned char sal_Bool
Mutex m_mutex
XPropertyListType t
Base class to implement a UNO object supporting weak references, i.e.
Definition: weak.hxx:47
constexpr OUStringLiteral SERVICE_NAME_B
constexpr OUStringLiteral IMPL_NAME
static Bootstrap const & get_unorc()
OUString expandMacros(OUString const &text)
Helper function to expand macros based on the unorc/uno.ini.