1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include <sal/config.h>
22 #include <codemaker/commoncpp.hxx>
23 #include <codemaker/global.hxx>
25 #include "skeletoncommon.hxx"
26 #include "skeletoncpp.hxx"
29 #include <string_view>
31 using namespace ::codemaker::cpp
;
33 namespace skeletonmaker::cpp
{
35 static void generateIncludes(std::ostream
& o
,
36 const std::set
< OUString
>& interfaces
,
37 std::u16string_view propertyhelper
, const bool serviceobject
,
38 const bool supportxcomponent
)
40 o
<< "#include \"sal/config.h\"\n";
42 o
<< "#include \"cppuhelper/factory.hxx\"\n"
43 "#include \"cppuhelper/implementationentry.hxx\"\n";
45 o
<< "#include \"com/sun/star/uno/XComponentContext.hpp\"\n";
47 if (supportxcomponent
) {
48 o
<< "#include \"cppuhelper/compbase" << interfaces
.size() << ".hxx\"\n";
49 o
<< "#include \"cppuhelper/basemutex.hxx\"\n";
51 o
<< "#include \"cppuhelper/implbase" << interfaces
.size() << ".hxx\"\n";
54 if (propertyhelper
.size() > 1) {
55 if (propertyhelper
== u
"_")
56 o
<< "#include \"cppuhelper/rpopshlp.hxx\"\n";
58 o
<< "#include \"cppuhelper/propertysetmixin.hxx\"\n";
61 for (const auto& rIface
: interfaces
)
64 << rIface
.replace('.', '/')
69 static short generateNamespace(std::ostream
& o
,
70 const OString
& implname
,
75 sal_Int32 index
= implname
.lastIndexOf('.');
77 o
<< "\n\n// component helper namespace\n";
84 buf
.append("comp_" + implname
);
85 nm
= buf
.makeStringAndClear();
86 o
<< "namespace comp_" << implname
<< " {\n\n";
94 OString
token(implname
.getToken(0, '.', nPos
));
95 if (nPos
< 0 && serviceobject
) {
96 buf
.append("::comp_" + token
);
97 o
<< "namespace comp_" << token
<< " { ";
100 buf
.append("::" + token
);
101 o
<< "namespace " << token
<< " { ";
104 } while( nPos
<= index
);
105 nm
= buf
.makeStringAndClear();
111 static OString
generateCompHelperDeclaration(std::ostream
& o
,
112 const OString
& implname
)
115 short nbrackets
= generateNamespace(o
, implname
, true, nm
);
117 o
<< "namespace css = ::com::sun::star;\n\n";
119 // generate component/service helper functions
120 o
<< "// component and service helper functions:\n"
121 "OUString SAL_CALL _getImplementationName();\n"
122 "css::uno::Sequence< OUString > SAL_CALL "
123 "_getSupportedServiceNames();\n"
124 "css::uno::Reference< css::uno::XInterface > SAL_CALL _create("
125 " css::uno::Reference< css::uno::XComponentContext > const & "
129 for (short i
=0; i
< nbrackets
; i
++)
131 o
<< "// closing component helper namespace\n\n";
136 static void generateCompHelperDefinition(std::ostream
& o
,
137 const OString
& implname
,
138 const OString
& classname
,
139 const std::set
< OUString
>& services
)
142 short nbrackets
= generateNamespace(o
, implname
, true, nm
);
144 o
<< "OUString SAL_CALL _getImplementationName() {\n"
145 " return OUString(\n"
146 " \"" << implname
<< "\");\n}\n\n";
148 o
<< "css::uno::Sequence< OUString > SAL_CALL "
149 "_getSupportedServiceNames()\n{\n css::uno::Sequence< "
150 "OUString > s(" << services
.size() << ");\n";
153 for (const auto& rService
: services
)
155 o
<< " s[" << i
++ << "] = OUString(\""
156 << rService
<< "\");\n";
158 o
<< " return s;\n}\n\n";
160 o
<< "css::uno::Reference< css::uno::XInterface > SAL_CALL _create("
161 "\n const css::uno::Reference< css::uno::XComponentContext > & "
163 " return static_cast< ::cppu::OWeakObject * >(new "
164 << classname
<< "(context));\n}\n\n";
167 for (short j
=0; j
< nbrackets
; j
++)
169 o
<< "// closing component helper namespace\n\n";
173 static void generateCompFunctions(std::ostream
& o
, const OString
& nmspace
)
175 o
<< "static ::cppu::ImplementationEntry const entries[] = {\n"
176 " { &" << nmspace
<< "::_create,\n &"
177 << nmspace
<< "::_getImplementationName,\n &"
178 << nmspace
<< "::_getSupportedServiceNames,\n"
179 " &::cppu::createSingleComponentFactory, 0, 0 },\n"
180 " { 0, 0, 0, 0, 0, 0 }\n};\n\n";
182 o
<< "extern \"C\" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(\n"
183 " const char * implName, void * serviceManager, void * registryKey)\n{\n"
184 " return ::cppu::component_getFactoryHelper(\n"
185 " implName, serviceManager, registryKey, entries);\n}\n\n";
187 o
<< "extern \"C\" sal_Bool SAL_CALL component_writeInfo(\n"
188 " void * serviceManager, void * registryKey)\n{\n"
189 " return ::cppu::component_writeInfoHelper("
190 "serviceManager, registryKey, entries);\n}\n";
193 void generateXPropertySetBodies(std::ostream
& o
,
194 const OString
& classname
,
195 const OString
& propertyhelper
)
197 o
<< "// com.sun.star.beans.XPropertySet:\n";
199 o
<< "css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL "
200 << classname
<< "getPropertySetInfo() throw ("
201 "css::uno::RuntimeException)\n{\n return ::cppu::PropertySetMixin< "
203 << " >::getPropertySetInfo();\n}\n\n";
205 o
<< "void SAL_CALL " << classname
<< "setPropertyValue(const OUString"
206 " & aPropertyName, const css::uno::Any & aValue) throw ("
207 "css::uno::RuntimeException, css::beans::UnknownPropertyException, "
208 "css::beans::PropertyVetoException, css::lang::IllegalArgumentException, "
209 "css::lang::WrappedTargetException)\n{\n ::cppu::PropertySetMixin< "
210 << propertyhelper
<< " >::setPropertyValue(aPropertyName, aValue);\n}\n\n";
213 o
<< "css::uno::Any SAL_CALL " << classname
<< "getPropertyValue(const "
214 "OUString & aPropertyName) throw (css::uno::RuntimeException, "
215 "css::beans::UnknownPropertyException, css::lang::WrappedTargetException)"
216 "\n{\n return ::cppu::PropertySetMixin< "
217 << propertyhelper
<< " >::getPropertyValue(aPropertyName);\n}\n\n";
219 o
<< "void SAL_CALL " << classname
<< "addPropertyChangeListener(const "
220 "OUString & aPropertyName, const css::uno::Reference< "
221 "css::beans::XPropertyChangeListener > & xListener) throw ("
222 "css::uno::RuntimeException, css::beans::UnknownPropertyException, "
223 "css::lang::WrappedTargetException)\n{\n ::cppu::PropertySetMixin< "
225 << " >::addPropertyChangeListener(aPropertyName, xListener);\n}\n\n";
227 o
<< "void SAL_CALL " << classname
<< "removePropertyChangeListener(const "
228 "OUString & aPropertyName, const css::uno::Reference< "
229 "css::beans::XPropertyChangeListener > & xListener) throw ("
230 "css::uno::RuntimeException, css::beans::UnknownPropertyException, "
231 "css::lang::WrappedTargetException)\n{\n ::cppu::PropertySetMixin< "
233 << " >::removePropertyChangeListener(aPropertyName, xListener);\n}\n\n";
235 o
<< "void SAL_CALL " << classname
<< "addVetoableChangeListener(const "
236 "OUString & aPropertyName, const css::uno::Reference< "
237 "css::beans::XVetoableChangeListener > & xListener) throw ("
238 "css::uno::RuntimeException, css::beans::UnknownPropertyException, "
239 "css::lang::WrappedTargetException)\n{\n ::cppu::PropertySetMixin< "
241 << " >::addVetoableChangeListener(aPropertyName, xListener);\n}\n\n";
243 o
<< "void SAL_CALL " << classname
<< "removeVetoableChangeListener(const "
244 "OUString & aPropertyName, const css::uno::Reference< "
245 "css::beans::XVetoableChangeListener > & xListener) throw ("
246 "css::uno::RuntimeException, css::beans::UnknownPropertyException, "
247 "css::lang::WrappedTargetException)\n{\n ::cppu::PropertySetMixin< "
249 << " >::removeVetoableChangeListener(aPropertyName, xListener);\n}\n\n";
252 void generateXFastPropertySetBodies(std::ostream
& o
,
253 const OString
& classname
,
254 const OString
& propertyhelper
)
256 o
<< "// com.sun.star.beans.XFastPropertySet:\n";
258 o
<< "void SAL_CALL " << classname
<< "setFastPropertyValue( ::sal_Int32 "
259 "nHandle, const css::uno::Any& aValue ) throw ("
260 "css::beans::UnknownPropertyException, css::beans::PropertyVetoException, "
261 "css::lang::IllegalArgumentException, css::lang::WrappedTargetException, "
262 "css::uno::RuntimeException)\n{\n ::cppu::PropertySetMixin< "
263 << propertyhelper
<< " >::setFastPropertyValue(nHandle, aValue);\n}\n\n";
266 o
<< "css::uno::Any SAL_CALL " << classname
<< "getFastPropertyValue( "
267 "::sal_Int32 nHandle ) throw (css::beans::UnknownPropertyException, "
268 "css::lang::WrappedTargetException, css::uno::RuntimeException)\n{\n"
269 " return ::cppu::PropertySetMixin< "
270 << propertyhelper
<< " >::getFastPropertyValue(nHandle);\n}\n\n";
273 void generateXPropertyAccessBodies(std::ostream
& o
,
274 const OString
& classname
,
275 const OString
& propertyhelper
)
277 o
<< " // com.sun.star.beans.XPropertyAccess:\n";
279 o
<< "css::uno::Sequence< css::beans::PropertyValue > SAL_CALL "
280 << classname
<< "getPropertyValues( ) throw ("
281 "css::uno::RuntimeException)\n{\n"
282 " return ::cppu::PropertySetMixin< "
283 << propertyhelper
<< " >::getPropertyValues();\n}\n\n";
285 o
<< "void SAL_CALL " << classname
<< "setPropertyValues( const "
286 "css::uno::Sequence< css::beans::PropertyValue >& aProps ) throw ("
287 "css::beans::UnknownPropertyException, css::beans::PropertyVetoException, "
288 "css::lang::IllegalArgumentException, css::lang::WrappedTargetException, "
289 "css::uno::RuntimeException)\n{\n"
290 " ::cppu::PropertySetMixin< "
291 << propertyhelper
<< " >::setPropertyValues(aProps);\n}\n\n";
294 void generateXLocalizable(std::ostream
& o
, const OString
& classname
)
296 o
<< "// css::lang::XLocalizable:\n"
297 "void SAL_CALL " << classname
<< "setLocale(const css::lang::"
298 "Locale & eLocale) throw (css::uno::RuntimeException)\n{\n"
299 " m_locale = eLocale;\n}\n\n"
300 "css::lang::Locale SAL_CALL " << classname
<< "getLocale() "
301 "throw (css::uno::RuntimeException)\n{\n return m_locale;\n}\n\n";
304 void generateXAddInBodies(std::ostream
& o
, const OString
& classname
)
306 o
<< "// css::sheet::XAddIn:\n";
308 o
<< "OUString SAL_CALL " << classname
<< "getProgrammaticFuntionName("
309 "const OUString & aDisplayName) throw (css::uno::RuntimeException)"
310 "\n{\n OUString ret;\n try {\n css::uno::Reference< "
311 "css::container::XNameAccess > xNAccess(m_xHAccess, css::uno::UNO_QUERY);\n"
312 " css::uno::Sequence< OUString > functions = "
313 "xNAccess->getElementNames();\n sal_Int32 len = functions."
314 "getLength();\n OUString sDisplayName;\n"
315 " for (sal_Int32 i=0; i < len; ++i) {\n"
316 " sDisplayName = getAddinProperty(functions[i], "
318 "sDISPLAYNAME);\n if (sDisplayName.equals(aDisplayName))\n"
319 " return functions[i];\n }\n }\n"
320 " catch ( const css::uno::RuntimeException & e ) {\n throw e;\n }\n"
321 " catch ( css::uno::Exception & ) {\n }\n return ret;\n}\n\n";
323 o
<< "OUString SAL_CALL " << classname
<< "getDisplayFunctionName(const "
324 "OUString & aProgrammaticName) throw (css::uno::RuntimeException)\n"
325 "{\n return getAddinProperty(aProgrammaticName, OUString(), "
326 "sDISPLAYNAME);\n}\n\n";
328 o
<< "OUString SAL_CALL " << classname
<< "getFunctionDescription(const "
329 "OUString & aProgrammaticName) throw (css::uno::RuntimeException)\n"
330 "{\n return getAddinProperty(aProgrammaticName, OUString(), "
331 "sDESCRIPTION);\n}\n\n";
333 o
<< "OUString SAL_CALL " << classname
<< "getDisplayArgumentName(const "
334 "OUString & aProgrammaticFunctionName, ::sal_Int32 nArgument) throw "
335 "(css::uno::RuntimeException)\n{\n return getAddinProperty("
336 "aProgrammaticFunctionName,\n m_functionMap["
337 "aProgrammaticFunctionName][nArgument],\n"
338 " sDISPLAYNAME);\n}\n\n";
340 o
<< "OUString SAL_CALL " << classname
<< "getArgumentDescription(const "
341 "OUString & aProgrammaticFunctionName, ::sal_Int32 nArgument) throw "
342 "(css::uno::RuntimeException)\n{\n return getAddinProperty("
343 "aProgrammaticFunctionName,\n "
344 "m_functionMap[aProgrammaticFunctionName][nArgument],\n"
345 " sDESCRIPTION);\n}\n\n";
347 o
<< "OUString SAL_CALL " << classname
<< "getProgrammaticCategoryName("
348 "const OUString & aProgrammaticFunctionName) throw ("
349 "css::uno::RuntimeException)\n{\n return getAddinProperty("
350 "aProgrammaticFunctionName, OUString(), sCATEGORY);\n}\n\n";
352 o
<< "OUString SAL_CALL " << classname
<< "getDisplayCategoryName(const "
353 "OUString & aProgrammaticFunctionName) throw ("
354 "css::uno::RuntimeException)\n{\n return getAddinProperty("
355 "aProgrammaticFunctionName, OUString(), "
356 "sCATEGORYDISPLAYNAME);\n}\n\n";
359 void generateXCompatibilityNamesBodies(std::ostream
& o
, const OString
& classname
)
361 o
<< "// css::sheet::XCompatibilityNames:\n"
362 "css::uno::Sequence< css::sheet::LocalizedName > SAL_CALL " << classname
363 << "getCompatibilityNames(const OUString & aProgrammaticName) throw "
364 "(css::uno::RuntimeException)\n{\n css::uno::Sequence< "
365 "css::sheet::LocalizedName > seqLocalizedNames;\n try {\n "
366 "OUStringBuffer buf("
367 "aProgrammaticName);\n buf.appendAscii(\"/CompatibilityName\");\n"
368 " OUString hname(buf.makeStringAndClear());\n\n "
369 "if ( m_xCompAccess->hasByHierarchicalName(hname) ) {\n"
370 " css::uno::Reference< css::container::XNameAccess > "
372 " m_xCompAccess->getByHierarchicalName(hname), "
373 "css::uno::UNO_QUERY);\n\n css::uno::Sequence< OUString"
374 " > elems = \n xNameAccess->getElementNames();"
375 "\n ::sal_Int32 len = elems.getLength();\n\n "
376 "seqLocalizedNames.realloc(len);\n\n OUString "
377 "sCompatibilityName;\n for (::sal_Int32 i=0; i < len; ++i) {\n"
378 " OUString sLocale(elems[i]);\n "
379 "xNameAccess->getByName(sLocale) >>= sCompatibilityName;\n\n"
380 " css::lang::Locale aLocale;\n "
381 "::sal_Int32 nIndex = 0, nToken = 0;\n "
382 /* FIXME-BCP47: this will break. */
383 "do {\n OUString aToken = sLocale.getToken(0, '-', "
384 "nIndex);\n switch (nToken++) {\n "
385 "case 0:\n aLocale.Language = aToken;\n"
386 " break;\n case 1:\n"
387 " aLocale.Country = aToken;\n "
388 " break;\n default:\n "
389 "aLocale.Variant = sLocale.copy(nIndex-aToken.getLength()-1);\n"
390 " nIndex = -1;\n }\n"
391 " } while ( nIndex >= 0 );\n\n "
392 "seqLocalizedNames[i].Locale = aLocale;\n "
393 "seqLocalizedNames[i].Name = sCompatibilityName;\n }"
394 "\n }\n }\n catch ( const css::uno::RuntimeException & e ) {\n "
395 "throw e;\n }\n catch ( css::uno::Exception & ) {\n }\n\n"
396 " return seqLocalizedNames;\n}\n\n";
399 void generateXInitialization(std::ostream
& o
, const OString
& classname
)
401 o
<< "// css::lang::XInitialization:\n"
402 "void SAL_CALL " << classname
<< "initialize( const css::uno::Sequence< "
403 "css::uno::Any >& aArguments ) "
404 "throw (css::uno::Exception, css::uno::RuntimeException)\n{\n"
405 " css::uno::Reference < css::frame::XFrame > xFrame;\n"
406 " if ( aArguments.getLength() ) {\n aArguments[0] >>= xFrame;\n"
407 " m_xFrame = xFrame;\n }\n}\n\n";
410 void generateXDispatch(std::ostream
& o
,
411 const OString
& classname
,
412 const ProtocolCmdMap
& protocolCmdMap
)
414 // com.sun.star.frame.XDispatch
416 o
<< "// css::frame::XDispatch:\n"
417 "void SAL_CALL " << classname
<< "dispatch( const css::util::URL& aURL, const "
418 "css::uno::Sequence< css::beans::PropertyValue >& aArguments ) throw"
419 "(css::uno::RuntimeException)\n{\n";
421 for (const auto& rEntry
: protocolCmdMap
) {
422 o
<< " if ( aURL.Protocol.equalsAscii(\"" << rEntry
.first
423 << "\") == 0 )\n {\n";
425 for (const auto& rCmd
: rEntry
.second
) {
426 o
<< " if ( aURL.Path.equalsAscii(\"" << rCmd
<< "\") )\n"
427 " {\n // add your own code here\n"
436 o
<< "void SAL_CALL " << classname
<< "addStatusListener( const css::uno::Reference< "
437 "css::frame::XStatusListener >& xControl, const css::util::URL& aURL ) "
438 "throw (css::uno::RuntimeException)\n{\n"
439 " // add your own code here\n}\n\n";
441 // removeStatusListener
442 o
<< "void SAL_CALL " << classname
<< "removeStatusListener( const css::uno::Reference"
443 "< css::frame::XStatusListener >& xControl, const css::util::URL& aURL ) "
444 "throw (css::uno::RuntimeException)\n{\n"
445 " // add your own code here\n}\n\n";
448 void generateXDispatchProvider(std::ostream
& o
,
449 const OString
& classname
,
450 const ProtocolCmdMap
& protocolCmdMap
)
453 // com.sun.star.frame.XDispatchProvider
455 o
<< "// css::frame::XDispatchProvider:\n"
456 "css::uno::Reference< css::frame::XDispatch > SAL_CALL " << classname
457 << "queryDispatch( const css::util::URL& aURL,"
458 " const OUString& sTargetFrameName, sal_Int32 nSearchFlags ) "
459 "throw(css::uno::RuntimeException)\n{\n css::uno::Reference< "
460 "css::frame::XDispatch > xRet;\n"
461 " if ( !m_xFrame.is() )\n return 0;\n\n";
463 for (const auto& rEntry
: protocolCmdMap
) {
464 o
<< " if ( aURL.Protocol.equalsAscii(\"" << rEntry
.first
465 << "\") == 0 )\n {\n";
467 for (const auto& rCmd
: rEntry
.second
) {
468 o
<< " if ( aURL.Path.equalsAscii(\"" << rCmd
<< "\") == 0 )\n"
474 o
<< " return xRet;\n}\n\n";
477 o
<< "css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL "
478 << classname
<< "queryDispatches( const css::uno::Sequence< "
479 "css::frame::DispatchDescriptor >& seqDescripts ) throw("
480 "css::uno::RuntimeException)\n{\n"
481 " sal_Int32 nCount = seqDescripts.getLength();\n"
482 " css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > "
483 "lDispatcher(nCount);\n\n"
484 " for( sal_Int32 i=0; i<nCount; ++i ) {\n"
485 " lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL,\n"
486 " seqDescripts[i].FrameName,\n"
487 " seqDescripts[i].SearchFlags );\n"
488 " }\n\n return lDispatcher;\n}\n\n";
491 static void generateAddinConstructorAndHelper(std::ostream
& o
,
492 ProgramOptions
const & options
,
493 rtl::Reference
< TypeManager
> const & manager
, const OString
& classname
,
494 const std::set
< OUString
>& interfaces
)
496 o
<< classname
<< "::" << classname
497 << "(css::uno::Reference< css::uno::XComponentContext > const & context) :\n"
498 " m_xContext(context), m_locale()\n{\n";
500 if (options
.backwardcompatible
) {
503 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
505 o
<< " css::uno::Reference< css::lang::XMultiServiceFactory > xProvider"
506 "(\n m_xContext->getServiceManager()->createInstanceWithContext"
508 " \"com.sun.star.configuration.ConfigurationProvider\"),"
509 "\n m_xContext ), css::uno::UNO_QUERY );\n\n";
511 o
<< " OUString sReadOnlyView(\n"
512 " \"com.sun.star.configuration.ConfigurationAccess\");\n\n";
514 o
<< " OUStringBuffer sPath(OUString(\n"
515 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\"));\n"
516 " sPath.appendAscii(sADDIN_SERVICENAME);\n"
517 " sPath.appendAscii(\"/AddInFunctions\");\n\n"
518 " // create arguments: nodepath\n"
519 " css::beans::PropertyValue aArgument;\n"
520 " aArgument.Name = OUString(\"nodepath\");\n"
521 " aArgument.Value <<= sPath.makeStringAndClear();\n\n"
522 " css::uno::Sequence< css::uno::Any > aArguments(1);\n"
523 " aArguments[0] <<= aArgument;\n\n";
525 o
<< " // create the default view using default UI locale\n"
526 " css::uno::Reference< css::uno::XInterface > xIface =\n"
527 " xProvider->createInstanceWithArguments(sReadOnlyView, "
529 " m_xHAccess.set(xIface, css::uno::UNO_QUERY);"
532 o
<< " // extend arguments to create a view for all locales to get "
533 "simple\n // access to the compatibilityname property\n"
534 " aArgument.Name = OUString(\"locale\");\n"
535 " aArgument.Value <<= OUString(\"*\");\n"
536 " aArguments.realloc(2);\n"
537 " aArguments[1] <<= aArgument;\n\n"
538 " // create view for all locales\n"
539 " xIface = xProvider->createInstanceWithArguments(sReadOnlyView, "
541 " m_xCompAccess.set(xIface, css::uno::UNO_QUERY);\n";
543 o
<< " }\n catch ( css::uno::Exception & ) {\n }\n}\n\n";
545 o
<< "// addin configuration property helper function:\nOUString "
546 "SAL_CALL " << classname
<< "::getAddinProperty(const OUString &"
547 " funcName, const OUString & paramName, const char * propName) "
548 "throw (css::uno::RuntimeException)\n{\n"
549 " OUString ret;\n try {\n "
550 "OUStringBuffer buf(funcName);\n"
551 " if (!paramName.isEmpty()) {\n"
552 " buf.appendAscii(\"/Parameters/\");\n"
553 " buf.append(paramName);\n }\n\n"
554 " css::uno::Reference< css::beans::XPropertySet > xPropSet(\n"
555 " m_xHAccess->getByHierarchicalName(\n"
556 " buf.makeStringAndClear()), css::uno::UNO_QUERY);\n"
557 " xPropSet->getPropertyValue(\n "
558 "OUString(propName)) >>= ret;\n }\n"
559 " catch ( const css::uno::RuntimeException & e ) {\n throw e;\n }\n"
560 " catch ( css::uno::Exception & ) {\n }\n return ret;\n";
565 static void generateMemberInitialization(std::ostream
& o
,
566 ProgramOptions
const & options
,
567 rtl::Reference
< TypeManager
> const & manager
,
568 AttributeInfo
const & members
)
570 for (const auto& rMember
: members
)
573 if ((manager
->decompose(rMember
.type
, true, nullptr, &rank
, nullptr, nullptr)
574 <= codemaker::UnoType::Sort::Char
)
577 o
<< ",\n m_" << rMember
.name
<< "(";
578 printType(o
, options
, manager
, rMember
.type
, 16, true);
584 static void generateMemberDeclaration(std::ostream
& o
,
585 ProgramOptions
const & options
,
586 rtl::Reference
< TypeManager
> const & manager
,
587 AttributeInfo
const & members
)
589 for (const auto& rMember
: members
)
592 printType(o
, options
, manager
, rMember
.type
, 1);
593 o
<< " m_" << rMember
.name
<< ";\n";
597 static OString
generateClassDefinition(std::ostream
& o
,
598 ProgramOptions
const & options
,
599 rtl::Reference
< TypeManager
> const & manager
,
600 OString
const & classname
,
601 std::set
< OUString
> const & interfaces
,
602 AttributeInfo
const & properties
,
603 AttributeInfo
const & attributes
,
604 std::set
< OUString
> const & propinterfaces
,
605 OUString
const & propertyhelper
, bool supportxcomponent
)
607 OStringBuffer
parentname(64);
608 o
<< "class " << classname
<< ":\n";
610 if (!interfaces
.empty()) {
611 if (supportxcomponent
) {
612 parentname
.append("::cppu::WeakComponentImplHelper" +
613 OString::number(static_cast<sal_Int32
>(interfaces
.size())));
614 o
<< " private ::cppu::BaseMutex,\n"
615 " public ::cppu::WeakComponentImplHelper"
616 << interfaces
.size() << "<";
618 parentname
.append("::cppu::WeakImplHelper" +
619 OString::number(static_cast<sal_Int32
>(interfaces
.size())));
620 o
<< " public ::cppu::WeakImplHelper" << interfaces
.size() << "<";
623 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
624 while (iter
!= interfaces
.end())
626 o
<< "\n " << scopedCppName(u2b(*iter
));
628 if (iter
!= interfaces
.end())
635 if (propertyhelper
.getLength() > 1) {
636 o
<< ",\n public ::cppu::PropertySetMixin< "
637 << scopedCppName(u2b(propertyhelper
)) << " >";
640 o
<< "\n{\npublic:\n"
641 " explicit " << classname
<< "("
642 "css::uno::Reference< css::uno::XComponentContext > const & context);\n\n";
644 // generate component/service helper functions
645 // o << " // component and service helper functions:\n"
646 // << " static OUString SAL_CALL _getImplementationName();\n"
647 // << " static css::uno::Sequence< OUString > SAL_CALL "
648 // << "_getSupportedServiceNames();\n"
649 // << " static css::uno::Reference< css::uno::XInterface > SAL_CALL _create("
650 // << "\n css::uno::Reference< css::uno::XComponentContext > const & "
651 // << "context);\n\n";
653 // override queryInterface
654 if (propertyhelper
.getLength() > 1) {
655 o
<< " // css::uno::XInterface:\n"
656 " virtual css::uno::Any SAL_CALL queryInterface("
657 "css::uno::Type const & type) throw ("
658 "css::uno::RuntimeException);\n";
660 OStringBuffer
buffer(256);
661 buffer
.append(parentname
+ "< ");
662 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
663 while (iter
!= interfaces
.end())
665 buffer
.append(scopedCppName(u2b(*iter
)));
667 if (iter
!= interfaces
.end())
672 OString
parent(buffer
.makeStringAndClear());
673 o
<< " virtual void SAL_CALL acquire() throw ()\n { "
674 << parent
<< "::acquire(); }\n";
675 o
<< " virtual void SAL_CALL release() throw ()\n { "
676 << parent
<< "::release(); }\n\n";
679 codemaker::GeneratedTypeSet generated
;
680 for (const auto& rIface
: interfaces
)
682 printMethods(o
, options
, manager
, rIface
, generated
, "", "", " ",
683 true, propertyhelper
);
686 o
<< "private:\n " << classname
<< "(const " << classname
<< " &); // not defined\n"
687 " " << classname
<< "& operator=(const " << classname
<< " &); // not defined\n\n"
688 " // destructor is private and will be called indirectly by the release call"
689 " virtual ~" << classname
<< "() {}\n\n";
691 if (options
.componenttype
== 2) {
692 o
<< " typedef boost::unordered_map< ::sal_Int32, OUString, "
693 "boost::hash<::sal_Int32> > ParamMap;\n"
694 " typedef boost::unordered_map< OUString, ParamMap, "
695 "OUStringHash > FunctionMap;\n\n"
696 " OUString SAL_CALL getAddinProperty(const OUString & "
697 "funcName, const OUString & paramName, const char * propName) "
698 "throw (css::uno::RuntimeException);\n\n";
701 if (supportxcomponent
) {
702 o
<< " // override WeakComponentImplHelperBase::disposing()\n"
703 " // This function is called upon disposing the component,\n"
704 " // if your component needs special work when it becomes\n"
705 " // disposed, do it here.\n"
706 " virtual void SAL_CALL disposing();\n\n";
710 o
<< " css::uno::Reference< css::uno::XComponentContext > m_xContext;\n";
711 if (!supportxcomponent
&& !attributes
.empty())
712 o
<< " mutable ::osl::Mutex m_aMutex;\n";
714 // additional member for add-ons
715 if (options
.componenttype
== 3) {
716 o
<< " css::uno::Reference< css::frame::XFrame > m_xFrame;\n";
719 if (options
.componenttype
== 2) {
720 if (options
.backwardcompatible
) {
721 o
<<" css::uno::Reference< css::container::XHierarchicalNameAccess > "
723 " css::uno::Reference< css::container::XHierarchicalNameAccess > "
725 " FunctionMap m_functionMap;\n";
727 o
<< " css::lang::Locale m_locale;\n";
730 generateMemberDeclaration(o
, options
, manager
, properties
);
731 generateMemberDeclaration(o
, options
, manager
, attributes
);
733 // if (!properties.empty())
735 // AttributeInfo::const_iterator iter = properties.begin();
736 // while (iter != properties.end())
739 // printType(o, options, manager, iter->second.first.replace('.','/'),
741 // o << " m_" << iter->first << ";\n";
745 // if (!attributes.empty())
747 // AttributeInfo::const_iterator iter = attributes.begin();
748 // while (iter != attributes.end())
751 // printType(o, options, manager, iter->second.first.replace('.','/'),
753 // o << " m_" << iter->first << ";\n";
760 // generate constructor
761 if (options
.componenttype
== 2) {
762 generateAddinConstructorAndHelper(o
, options
, manager
,
763 classname
, interfaces
);
765 o
<< classname
<< "::" << classname
766 << "(css::uno::Reference< css::uno::XComponentContext > const & context) :\n";
767 if (supportxcomponent
) {
768 o
<< " ::cppu::WeakComponentImplHelper" << interfaces
.size() << "<";
769 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
770 while (iter
!= interfaces
.end()) {
771 o
<< "\n " << scopedCppName(u2b(*iter
));
773 if (iter
!= interfaces
.end())
776 o
<< ">(m_aMutex),\n";
779 if (propertyhelper
.getLength() > 1) {
780 o
<< " ::cppu::PropertySetMixin< "
781 << scopedCppName(u2b(propertyhelper
)) << " >(\n"
782 " context, static_cast< Implements >(\n ";
783 OStringBuffer
buffer(128);
784 if (propinterfaces
.find("com/sun/star/beans/XPropertySet")
785 != propinterfaces
.end()) {
786 buffer
.append("IMPLEMENTS_PROPERTY_SET");
788 if (propinterfaces
.find("com/sun/star/beans/XFastPropertySet")
789 != propinterfaces
.end()) {
790 if (!buffer
.isEmpty())
791 buffer
.append(" | IMPLEMENTS_FAST_PROPERTY_SET");
793 buffer
.append("IMPLEMENTS_FAST_PROPERTY_SET");
795 if (propinterfaces
.find("com/sun/star/beans/XPropertyAccess")
796 != propinterfaces
.end()) {
797 if (!buffer
.isEmpty())
798 buffer
.append(" | IMPLEMENTS_PROPERTY_ACCESS");
800 buffer
.append("IMPLEMENTS_PROPERTY_ACCESS");
802 o
<< buffer
.makeStringAndClear()
803 << "), css::uno::Sequence< OUString >()),\n";
805 o
<< " m_xContext(context)";
807 generateMemberInitialization(o
, options
, manager
, properties
);
808 generateMemberInitialization(o
, options
, manager
, attributes
);
813 // generate service/component helper function implementations
814 // generateServiceHelper(o, options.implname, classname, services);
816 if (supportxcomponent
) {
817 o
<< "// override WeakComponentImplHelperBase::disposing()\n"
818 "// This function is called upon disposing the component,\n"
819 "// if your component needs special work when it becomes\n"
820 "// disposed, do it here.\n"
821 "void SAL_CALL " << classname
<< "::disposing()\n{\n\n}\n\n";
824 return parentname
.makeStringAndClear();
827 static void generateXServiceInfoBodies(std::ostream
& o
,
828 OString
const & classname
,
829 OString
const & comphelpernamespace
)
831 o
<< "// com.sun.star.uno.XServiceInfo:\n"
832 "OUString SAL_CALL " << classname
<< "getImplementationName() "
833 "throw (css::uno::RuntimeException)\n{\n "
834 "return " << comphelpernamespace
<< "::_getImplementationName();\n}\n\n";
836 o
<< "sal_Bool SAL_CALL " << classname
837 << "supportsService(OUString const & "
838 "serviceName) throw (css::uno::RuntimeException)\n{\n "
839 "css::uno::Sequence< OUString > serviceNames = "
840 << comphelpernamespace
<< "::_getSupportedServiceNames();\n "
841 "for (::sal_Int32 i = 0; i < serviceNames.getLength(); ++i) {\n "
842 " if (serviceNames[i] == serviceName)\n return sal_True;\n"
843 " }\n return sal_False;\n}\n\n";
845 o
<< "css::uno::Sequence< OUString > SAL_CALL " << classname
846 << "getSupportedServiceNames() throw (css::uno::RuntimeException)\n{\n "
847 "return " << comphelpernamespace
848 << "::_getSupportedServiceNames();\n}\n\n";
852 static void generateMethodBodies(std::ostream
& o
,
853 ProgramOptions
const & options
,
854 rtl::Reference
< TypeManager
> const & manager
,
855 std::set
< OUString
> const & interfaces
,
856 std::string_view classname
,
857 OString
const & comphelpernamespace
,
858 OUString
const & propertyhelper
)
860 OString name
= OString::Concat(classname
) + "::";
861 codemaker::GeneratedTypeSet generated
;
862 for (const auto& rIface
: interfaces
) {
863 if ( rIface
== "com.sun.star.lang.XServiceInfo" ) {
864 generateXServiceInfoBodies(o
, name
, comphelpernamespace
);
865 generated
.add(u2b(rIface
));
867 printMethods(o
, options
, manager
, rIface
, generated
, "_",
868 name
, "", true, propertyhelper
);
873 static void generateQueryInterface(std::ostream
& o
,
874 ProgramOptions
const & options
,
875 rtl::Reference
< TypeManager
> const & manager
,
876 const std::set
< OUString
>& interfaces
,
877 OString
const & parentname
,
878 OString
const & classname
,
879 std::u16string_view propertyhelper
)
881 if (propertyhelper
.empty())
884 o
<< "css::uno::Any " << classname
885 << "::queryInterface(css::uno::Type const & type) throw ("
886 "css::uno::RuntimeException)\n{\n ";
888 if (!propertyhelper
.empty())
891 o
<< "css::uno::Any a(";
893 o
<< parentname
<< "<";
894 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
895 while (iter
!= interfaces
.end())
897 o
<< "\n " << scopedCppName(u2b(*iter
));
899 if (iter
!= interfaces
.end())
905 if (!propertyhelper
.empty()) {
906 o
<< "::queryInterface(type);\n";
908 o
<< "::queryInterface(type));\n";
909 o
<< " return a.hasValue() ? a\n : (";
910 if (propertyhelper
== u
"_") {
911 o
<< "::cppu::OPropertySetHelper::queryInterface(type));\n";
913 o
<< "::cppu::PropertySetMixin<\n ";
914 printType(o
, options
, manager
, propertyhelper
, 0);
915 o
<< " >::queryInterface(\n type));\n";
921 void generateSkeleton(ProgramOptions
const & options
,
922 rtl::Reference
< TypeManager
> const & manager
,
923 std::vector
< OString
> const & types
)
925 // special handling of calc add-ins
926 if (options
.componenttype
== 2) {
927 generateCalcAddin(options
, manager
, types
);
931 std::set
< OUString
> interfaces
;
932 std::set
< OUString
> services
;
933 AttributeInfo properties
;
934 AttributeInfo attributes
;
935 std::set
< OUString
> propinterfaces
;
936 bool serviceobject
= false;
937 bool supportxcomponent
= false;
939 for (const auto& rType
: types
) {
940 checkType(manager
, b2u(rType
), interfaces
, services
, properties
);
943 if (options
.componenttype
== 3) {
944 // the Protocolhandler service is mandatory for a protocol handler add-on,
945 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
946 // certain functions and the generated impl object implements XDispatch
947 // directly for simplicity reasons.
948 checkType(manager
, "com.sun.star.frame.ProtocolHandler",
949 interfaces
, services
, properties
);
950 checkType(manager
, "com.sun.star.frame.XDispatch",
951 interfaces
, services
, properties
);
954 // check if service object or simple UNO object
955 if (!services
.empty())
956 serviceobject
= true;
958 OUString propertyhelper
= checkPropertyHelper(
959 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
961 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
963 if (interfaces
.size() > 12)
964 throw CannotDumpException(
965 "the skeletonmaker supports components with 12 interfaces "
966 "only (limitation of the UNO implementation helpers)!");
969 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
971 OString compFileName
;
973 std::ostream
* pofs
= nullptr;
974 bool standardout
= getOutputStream(options
, ".cxx",
975 &pofs
, compFileName
, tmpFileName
);
978 if (!standardout
&& options
.license
) {
979 printLicenseHeader(*pofs
);
982 generateIncludes(*pofs
, interfaces
, propertyhelper
, serviceobject
,
985 if (options
.componenttype
== 3) {
986 *pofs
<< "#include \"com/sun/star/frame/XFrame.hpp\"\n";
994 nmspace
= generateCompHelperDeclaration(*pofs
, options
.implname
);
997 "\n\n/// anonymous implementation namespace\nnamespace {\n\n"
998 "namespace css = ::com::sun::star;\n\n";
1000 nm
= generateNamespace(*pofs
, options
.implname
, false, nmspace
);
1001 *pofs
<< "namespace css = ::com::sun::star;\n\n";
1004 sal_Int32 index
= 0;
1005 OString
classname(options
.implname
);
1006 if ((index
= classname
.lastIndexOf('.')) > 0)
1007 classname
= classname
.copy(index
+1);
1010 generateClassDefinition(*pofs
,
1011 options
, manager
, classname
, interfaces
, properties
,
1012 attributes
, propinterfaces
, propertyhelper
, supportxcomponent
));
1014 generateQueryInterface(*pofs
, options
, manager
, interfaces
, parentname
,
1015 classname
, propertyhelper
);
1017 generateMethodBodies(*pofs
, options
, manager
, interfaces
, classname
,
1018 nmspace
, propertyhelper
);
1020 if (serviceobject
) {
1022 *pofs
<< "} // closing anonymous implementation namespace\n\n";
1024 generateCompHelperDefinition(*pofs
, options
.implname
,
1025 classname
, services
);
1026 generateCompFunctions(*pofs
, nmspace
);
1029 for (short i
=0; i
< nm
; i
++)
1031 *pofs
<< (nm
> 0 ? "// closing namespace\n\n" : "\n");
1036 if (static_cast<std::ofstream
*>(pofs
)->is_open())
1037 static_cast<std::ofstream
*>(pofs
)->close();
1039 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, false));
1041 } catch (CannotDumpException
& e
) {
1042 std::cerr
<< "ERROR: " << e
.getMessage() << "\n";
1043 if ( !standardout
) {
1044 if (static_cast<std::ofstream
*>(pofs
)->is_open())
1045 static_cast<std::ofstream
*>(pofs
)->close();
1047 // remove existing type file if something goes wrong to ensure
1049 if (fileExists(compFileName
))
1050 removeTypeFile(compFileName
);
1052 // remove tmp file if something goes wrong
1053 removeTypeFile(tmpFileName
);
1058 void generateCalcAddin(ProgramOptions
const & options
,
1059 rtl::Reference
< TypeManager
> const & manager
,
1060 std::vector
< OString
> const & types
)
1062 std::set
< OUString
> interfaces
;
1063 std::set
< OUString
> services
;
1064 AttributeInfo properties
;
1065 AttributeInfo attributes
;
1066 std::set
< OUString
> propinterfaces
;
1067 bool serviceobject
= false;
1068 bool supportxcomponent
= false;
1071 for (const auto& rType
: types
) {
1072 checkType(manager
, b2u(rType
), interfaces
, services
, properties
);
1075 OUString sAddinService
;
1076 if (services
.size() != 1) {
1077 throw CannotDumpException(
1078 "for calc add-in components one and only one service type is necessary!"
1079 " Please reference a valid type with the '-t' option.");
1083 // get the one and only add-in service for later use
1084 std::set
< OUString
>::const_iterator iter2
= services
.begin();
1085 sAddinService
= *iter2
;
1086 if (sAddinService
== "com.sun.star.sheet.AddIn") {
1087 sAddinService
= *(++iter2
);
1090 // if backwardcompatible==true the AddIn service needs to be added to the
1091 // supported service list, the necessary interfaces are mapped to the add-in
1092 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
1093 // taken from the configuration from Calc directly, this simplifies the
1095 if (options
.backwardcompatible
) {
1096 checkType(manager
, "com.sun.star.sheet.AddIn",
1097 interfaces
, services
, properties
);
1099 // special case for the optional XLocalization interface. It should be
1100 // implemented always. But it is parent of the XAddIn and we need it only
1101 // if backwardcompatible is false.
1102 interfaces
.insert("com.sun.star.lang.XLocalizable");
1105 OUString propertyhelper
= checkPropertyHelper(
1106 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
1108 if (!propertyhelper
.isEmpty())
1109 std::cerr
<< "WARNING: interfaces specifying calc add-in functions "
1110 "shouldn't support attributes!\n";
1112 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
1114 if (interfaces
.size() > 12) {
1115 throw CannotDumpException(
1116 "the skeletonmaker supports components with 12 interfaces "
1117 "only (limitation of the UNO implementation helpers)!");
1120 // check if service object or simple UNO object
1121 if (!services
.empty())
1122 serviceobject
= true;
1124 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
1125 if (supportxcomponent
)
1126 std::cerr
<< "WARNING: add-ins shouldn't support "
1127 "com.sun.star.uno.XComponent!\n";
1129 OString compFileName
;
1130 OString tmpFileName
;
1131 std::ostream
* pofs
= nullptr;
1132 bool standardout
= getOutputStream(options
, ".cxx",
1133 &pofs
, compFileName
, tmpFileName
);
1136 if (!standardout
&& options
.license
) {
1137 printLicenseHeader(*pofs
);
1140 generateIncludes(*pofs
, interfaces
, propertyhelper
, serviceobject
,
1144 "#include \"com/sun/star/beans/PropertyValue.hpp\"\n"
1145 "#include \"com/sun/star/beans/XPropertySet.hpp\"\n"
1146 "#include \"com/sun/star/container/XNameAccess.hpp\"\n"
1147 "#include \"com/sun/star/container/XHierarchicalNameAccess.hpp\"\n\n"
1148 "#include \"rtl/ustrbuf.hxx\"\n\n"
1149 "#include <boost/unordered_map.hpp>\n"
1153 OString
nmspace(generateCompHelperDeclaration(*pofs
, options
.implname
));
1156 "\n\n// anonymous implementation namespace\nnamespace {\n\n"
1157 "namespace css = ::com::sun::star;\n\n";
1159 sal_Int32 index
= 0;
1160 OString
classname(options
.implname
);
1161 if ((index
= classname
.lastIndexOf('.')) > 0) {
1162 classname
= classname
.copy(index
+1);
1165 if (options
.backwardcompatible
) {
1166 *pofs
<< "static const char * sADDIN_SERVICENAME = \""
1167 << sAddinService
<< "\";\n\n";
1168 *pofs
<< "static const char * sDISPLAYNAME = \"DisplayName\";\n"
1169 "static const char * sDESCRIPTION = \"Description\";\n"
1170 "static const char * sCATEGORY = \"Category\";\n"
1171 "static const char * sCATEGORYDISPLAYNAME = \"CategoryDisplayName\";"
1176 generateClassDefinition(*pofs
,
1177 options
, manager
, classname
, interfaces
, properties
,
1178 attributes
, propinterfaces
, propertyhelper
, supportxcomponent
));
1180 generateQueryInterface(*pofs
, options
, manager
, interfaces
, parentname
,
1181 classname
, propertyhelper
);
1183 generateMethodBodies(*pofs
, options
, manager
, interfaces
, classname
,
1184 nmspace
, propertyhelper
);
1187 *pofs
<< "} // closing anonymous implementation namespace\n\n";
1189 generateCompHelperDefinition(*pofs
, options
.implname
, classname
,
1192 generateCompFunctions(*pofs
, nmspace
);
1196 if (static_cast<std::ofstream
*>(pofs
)->is_open())
1197 static_cast<std::ofstream
*>(pofs
)->close();
1199 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, false));
1201 } catch (CannotDumpException
& e
) {
1202 std::cerr
<< "ERROR: " << e
.getMessage() << "\n";
1203 if ( !standardout
) {
1204 if (static_cast<std::ofstream
*>(pofs
)->is_open())
1205 static_cast<std::ofstream
*>(pofs
)->close();
1207 // remove existing type file if something goes wrong to ensure
1209 if (fileExists(compFileName
))
1210 removeTypeFile(compFileName
);
1212 // remove tmp file if something goes wrong
1213 removeTypeFile(tmpFileName
);
1221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */