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 .
19 #ifndef INCLUDED_VBAHELPER_VBAHELPERINTERFACE_HXX
20 #define INCLUDED_VBAHELPER_VBAHELPERINTERFACE_HXX
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>
28 #include <cppuhelper/implbase.hxx>
29 #include <cppuhelper/weakref.hxx>
30 #include <ooo/vba/XHelperInterface.hpp>
31 #include <rtl/ustring.hxx>
32 #include <sal/types.h>
33 #include <vbahelper/vbahelper.hxx>
35 // use this class when you have an object like
36 // interface XAnInterface which contains XHelperInterface in its inheritance hierarchy
37 // interface XAnInterface
39 // interface XHelperInterface;
40 // [attribute, string] name;
43 // interface XAnInterface : XHelperInterface;
45 // [attribute, string] name;
48 // then this class can provide a default implementation of XHelperInterface,
49 // you can use it like this
50 // typedef InheritedHelperInterfaceImpl< XAnInterface > > AnInterfaceImpl_BASE;
51 // class AnInterfaceImpl : public AnInterfaceImpl_BASE
54 // AnInterface( const Reference< HelperInterface >& xParent ) : AnInterfaceImpl_BASE( xParent ) {}
55 // // implement XAnInterface methods only, no need to implement the XHelperInterface
57 // virtual void setName( const OUString& );
58 // virtual OUString getName();
62 template< typename
... Ifc
>
63 class SAL_DLLPUBLIC_TEMPLATE InheritedHelperInterfaceImpl
: public Ifc
...
66 css::uno::WeakReference
< ov::XHelperInterface
> mxParent
;
67 css::uno::Reference
< css::uno::XComponentContext
> mxContext
;
69 InheritedHelperInterfaceImpl( const css::uno::Reference
< ov::XHelperInterface
>& xParent
, const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) : mxParent( xParent
), mxContext( xContext
) {}
70 virtual OUString
getServiceImplName() = 0;
71 virtual css::uno::Sequence
<OUString
> getServiceNames() = 0;
73 // XHelperInterface Methods
74 virtual ::sal_Int32 SAL_CALL
getCreator() override
78 virtual css::uno::Reference
< ov::XHelperInterface
> SAL_CALL
getParent( ) override
{ return mxParent
; }
80 virtual css::uno::Any SAL_CALL
Application( ) override
{
81 // The application could certainly be passed around in the context - seems
83 css::uno::Reference
< css::container::XNameAccess
> xNameAccess( mxContext
, css::uno::UNO_QUERY_THROW
);
84 return xNameAccess
->getByName( "Application" );
87 // XServiceInfo Methods
88 virtual OUString SAL_CALL
getImplementationName( ) override
{ return getServiceImplName(); }
89 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) override
91 css::uno::Sequence
< OUString
> sServices
= getSupportedServiceNames();
92 const OUString
* pStart
= sServices
.getConstArray();
93 const OUString
* pEnd
= pStart
+ sServices
.getLength();
94 for ( ; pStart
!= pEnd
; ++pStart
)
95 if ( *pStart
== ServiceName
)
99 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) override
101 css::uno::Sequence
< OUString
> aNames
= getServiceNames();
106 template <typename
... Ifc
>
107 class SAL_DLLPUBLIC_TEMPLATE InheritedHelperInterfaceWeakImpl
: public InheritedHelperInterfaceImpl
< ::cppu::WeakImplHelper
< Ifc
... > >
109 typedef InheritedHelperInterfaceImpl
< ::cppu::WeakImplHelper
< Ifc
... > > Base
;
111 InheritedHelperInterfaceWeakImpl( const css::uno::Reference
< ov::XHelperInterface
>& xParent
, const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) : Base( xParent
, xContext
) {}
115 /** Helper macro to declare the methods 'getServiceImplName()' and
116 'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface in a class
119 #define VBAHELPER_DECL_XHELPERINTERFACE \
120 virtual OUString getServiceImplName() override; \
121 virtual css::uno::Sequence< OUString > getServiceNames() override;
124 /** Helper macro to implement the methods 'getServiceImplName()' and
125 'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface. Will
126 return the class name as service implementation name.
128 #define VBAHELPER_IMPL_XHELPERINTERFACE( classname, servicename ) \
129 OUString classname::getServiceImplName() \
133 css::uno::Sequence< OUString > classname::getServiceNames() \
135 static css::uno::Sequence< OUString > saServiceNames; \
136 if( saServiceNames.getLength() == 0 ) \
138 saServiceNames.realloc( 1 ); \
139 saServiceNames[ 0 ] = servicename; \
141 return saServiceNames; \
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */