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/script/BasicErrorException.hpp>
24 #include <com/sun/star/uno/Any.hxx>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/uno/RuntimeException.hpp>
27 #include <com/sun/star/uno/Sequence.hxx>
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 namespace com
{ namespace sun
{ namespace star
{
36 namespace uno
{ class XComponentContext
; } }
39 // use this class when you have an a object like
40 // interface XAnInterface which contains XHelperInterface in its inheritance hierarchy
41 // interface XAnInterface
43 // interface XHelperInterface;
44 // [attribute, string] name;
47 // interface XAnInterface : XHelperInterface;
49 // [attribute, string] name;
52 // then this class can provide a default implementation of XHelperInterface,
53 // you can use it like this
54 // typedef InheritedHelperInterfaceImpl< XAnInterface > > AnInterfaceImpl_BASE;
55 // class AnInterfaceImpl : public AnInterfaceImpl_BASE
58 // AnInterface( const Reference< HelperInterface >& xParent ) : AnInterfaceImpl_BASE( xParent ) {}
59 // // implement XAnInterface methods only, no need to implement the XHelperInterface
61 // virtual void setName( const OUString& );
62 // virtual OUString getName();
66 template< typename
... Ifc
>
67 class SAL_DLLPUBLIC_TEMPLATE InheritedHelperInterfaceImpl
: public Ifc
...
70 css::uno::WeakReference
< ov::XHelperInterface
> mxParent
;
71 css::uno::Reference
< css::uno::XComponentContext
> mxContext
;
73 InheritedHelperInterfaceImpl( const css::uno::Reference
< ov::XHelperInterface
>& xParent
, const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) : mxParent( xParent
), mxContext( xContext
) {}
74 virtual OUString
getServiceImplName() = 0;
75 virtual css::uno::Sequence
<OUString
> getServiceNames() = 0;
77 // XHelperInterface Methods
78 virtual ::sal_Int32 SAL_CALL
getCreator() throw (css::script::BasicErrorException
, css::uno::RuntimeException
) override
82 virtual css::uno::Reference
< ov::XHelperInterface
> SAL_CALL
getParent( ) throw (css::script::BasicErrorException
, css::uno::RuntimeException
) override
{ return mxParent
; }
84 virtual css::uno::Any SAL_CALL
Application( ) throw (css::script::BasicErrorException
, css::uno::RuntimeException
) override
{
85 // The application could certainly be passed around in the context - seems
87 css::uno::Reference
< css::container::XNameAccess
> xNameAccess( mxContext
, css::uno::UNO_QUERY_THROW
);
88 return xNameAccess
->getByName( "Application" );
91 // XServiceInfo Methods
92 virtual OUString SAL_CALL
getImplementationName( ) throw (css::uno::RuntimeException
) override
{ return getServiceImplName(); }
93 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
) throw (css::uno::RuntimeException
) override
95 css::uno::Sequence
< OUString
> sServices
= getSupportedServiceNames();
96 const OUString
* pStart
= sServices
.getConstArray();
97 const OUString
* pEnd
= pStart
+ sServices
.getLength();
98 for ( ; pStart
!= pEnd
; ++pStart
)
99 if ( (*pStart
).equals( ServiceName
) )
103 virtual css::uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( ) throw (css::uno::RuntimeException
) override
105 css::uno::Sequence
< OUString
> aNames
= getServiceNames();
110 template <typename
... Ifc
>
111 class SAL_DLLPUBLIC_TEMPLATE InheritedHelperInterfaceWeakImpl
: public InheritedHelperInterfaceImpl
< ::cppu::WeakImplHelper
< Ifc
... > >
113 typedef InheritedHelperInterfaceImpl
< ::cppu::WeakImplHelper
< Ifc
... > > Base
;
115 InheritedHelperInterfaceWeakImpl
< Ifc
... >( const css::uno::Reference
< ov::XHelperInterface
>& xParent
, const css::uno::Reference
< css::uno::XComponentContext
>& xContext
) : Base( xParent
, xContext
) {}
119 /** Helper macro to declare the methods 'getServiceImplName()' and
120 'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface in a class
123 #define VBAHELPER_DECL_XHELPERINTERFACE \
124 virtual OUString getServiceImplName() override; \
125 virtual css::uno::Sequence< OUString > getServiceNames() override;
128 /** Helper macro to implement the methods 'getServiceImplName()' and
129 'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface. Will
130 return the class name as service implementation name.
132 #define VBAHELPER_IMPL_XHELPERINTERFACE( classname, servicename ) \
133 OUString classname::getServiceImplName() \
135 return OUString( #classname ); \
137 css::uno::Sequence< OUString > classname::getServiceNames() \
139 static css::uno::Sequence< OUString > saServiceNames; \
140 if( saServiceNames.getLength() == 0 ) \
142 saServiceNames.realloc( 1 ); \
143 saServiceNames[ 0 ] = servicename; \
145 return saServiceNames; \
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */