LibreOffice Module vbahelper (master) 1
vbahelperinterface.hxx
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#ifndef INCLUDED_VBAHELPER_VBAHELPERINTERFACE_HXX
20#define INCLUDED_VBAHELPER_VBAHELPERINTERFACE_HXX
21
22#include <com/sun/star/container/XNameAccess.hpp>
23#include <com/sun/star/uno/Any.hxx>
24#include <com/sun/star/uno/Reference.hxx>
25#include <com/sun/star/uno/RuntimeException.hpp>
26#include <com/sun/star/uno/Sequence.hxx>
27#include <com/sun/star/uno/XComponentContext.hpp>
30#include <ooo/vba/XHelperInterface.hpp>
31#include <rtl/ustring.hxx>
32#include <sal/types.h>
33#include <utility>
35
36// use this class when you have an object like
37// interface XAnInterface which contains XHelperInterface in its inheritance hierarchy
38// interface XAnInterface
39// {
40// interface XHelperInterface;
41// [attribute, string] name;
42// }
43// or
44// interface XAnInterface : XHelperInterface;
45// {
46// [attribute, string] name;
47// }
48//
49// then this class can provide a default implementation of XHelperInterface,
50// you can use it like this
51// typedef InheritedHelperInterfaceImpl< XAnInterface > > AnInterfaceImpl_BASE;
52// class AnInterfaceImpl : public AnInterfaceImpl_BASE
53// {
54// public:
55// AnInterface( const Reference< HelperInterface >& xParent ) : AnInterfaceImpl_BASE( xParent ) {}
56// // implement XAnInterface methods only, no need to implement the XHelperInterface
57// // methods
58// virtual void setName( const OUString& );
59// virtual OUString getName();
60// }
61//
62
63template< typename... Ifc >
64class SAL_DLLPUBLIC_TEMPLATE InheritedHelperInterfaceImpl : public Ifc...
65{
66protected:
67 css::uno::WeakReference< ov::XHelperInterface > mxParent;
68 css::uno::Reference< css::uno::XComponentContext > mxContext;
69public:
70 InheritedHelperInterfaceImpl( const css::uno::Reference< ov::XHelperInterface >& xParent, css::uno::Reference< css::uno::XComponentContext > xContext ) : mxParent( xParent ), mxContext(std::move( xContext )) {}
71 virtual OUString getServiceImplName() = 0;
72 virtual css::uno::Sequence<OUString> getServiceNames() = 0;
73
74 // XHelperInterface Methods
75 virtual ::sal_Int32 SAL_CALL getCreator() override
76 {
77 return 0x53756E4F;
78 }
79 virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent( ) override { return mxParent; }
80
81 virtual css::uno::Any SAL_CALL Application( ) override {
82 // The application could certainly be passed around in the context - seems
83 // to make sense
84 css::uno::Reference< css::container::XNameAccess > xNameAccess( mxContext, css::uno::UNO_QUERY_THROW );
85 return xNameAccess->getByName( "Application" );
86 }
87
88 // XServiceInfo Methods
89 virtual OUString SAL_CALL getImplementationName( ) override { return getServiceImplName(); }
90 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override
91 {
92 css::uno::Sequence< OUString > sServices = getSupportedServiceNames();
93 const OUString* pStart = sServices.getConstArray();
94 const OUString* pEnd = pStart + sServices.getLength();
95 for ( ; pStart != pEnd ; ++pStart )
96 if ( *pStart == ServiceName )
97 return true;
98 return false;
99 }
100 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames( ) override
101 {
102 css::uno::Sequence< OUString > aNames = getServiceNames();
103 return aNames;
104 }
105 };
106
107template <typename... Ifc >
108class SAL_DLLPUBLIC_TEMPLATE InheritedHelperInterfaceWeakImpl : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper< Ifc... > >
109{
110 typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper< Ifc... > > Base;
111public:
112 InheritedHelperInterfaceWeakImpl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
113};
114
115
120#define VBAHELPER_DECL_XHELPERINTERFACE \
121 virtual OUString getServiceImplName() override; \
122 virtual css::uno::Sequence< OUString > getServiceNames() override;
123
124
129#define VBAHELPER_IMPL_XHELPERINTERFACE( classname, servicename ) \
130OUString classname::getServiceImplName() \
131{ \
132 return #classname; \
133} \
134css::uno::Sequence< OUString > classname::getServiceNames() \
135{ \
136 static const css::uno::Sequence< OUString > saServiceNames { servicename }; \
137 return saServiceNames; \
138}
139
140
141#endif
142
143/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
unotools::WeakReference< AnimationNode > mxParent
virtual OUString SAL_CALL getImplementationName() override
virtual OUString getServiceImplName()=0
InheritedHelperInterfaceImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, css::uno::Reference< css::uno::XComponentContext > xContext)
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual ::sal_Int32 SAL_CALL getCreator() override
css::uno::WeakReference< ov::XHelperInterface > mxParent
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual css::uno::Any SAL_CALL Application() override
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
virtual css::uno::Sequence< OUString > getServiceNames()=0
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper< Ifc... > > Base
InheritedHelperInterfaceWeakImpl(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext)
uno::Reference< uno::XComponentContext > mxContext
css::uno::Sequence< OUString > getSupportedServiceNames()
unsigned char sal_Bool