xref: /AOO41X/main/vbahelper/inc/vbahelper/vbahelperinterface.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #ifndef OOVBAAPI_VBA_HELPERINTERFACE_HXX
28 #define OOVBAAPI_VBA_HELPERINTERFACE_HXX
29 
30 #include <cppuhelper/implbase1.hxx>
31 #include <cppuhelper/implbase2.hxx>
32 #include <cppuhelper/implbase3.hxx>
33 #include <ooo/vba/XHelperInterface.hpp>
34 #include <vbahelper/vbahelper.hxx>
35 #include <com/sun/star/container/XNameAccess.hpp>
36 
37 // use this class when you have an a object like
38 // interface  XAnInterface which contains XHelperInterface in its inheritance hierarchy
39 // interface XAnInterface
40 // {
41 //     interface XHelperInterface;
42 //     [attribute, string] name;
43 // }
44 // or
45 // interface XAnInterface : XHelperInterface;
46 // {
47 //     [attribute, string] name;
48 // }
49 //
50 // then this class can provide a default implementation of XHelperInterface,
51 // you can use it like this
52 // typedef InheritedHelperInterfaceImpl< XAnInterface > > AnInterfaceImpl_BASE;
53 // class AnInterfaceImpl : public AnInterfaceImpl_BASE
54 // {
55 // public:
56 //     AnInterface( const Reference< HelperInterface >& xParent ) : AnInterfaceImpl_BASE( xParent ) {}
57 //     // implement XAnInterface methods only, no need to implement the XHelperInterface
58 //     // methods
59 //     virtual void setName( const OUString& );
60 //     virtual OUString getName();
61 // }
62 //
63 const ::rtl::OUString sHelperServiceName( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.HelperServiceBase" ) );
64 
65 template< typename Ifc1 >
66 class InheritedHelperInterfaceImpl : public Ifc1
67 {
68 protected:
69 	css::uno::WeakReference< ov::XHelperInterface > mxParent;
70 	css::uno::Reference< css::uno::XComponentContext > mxContext;
71 public:
72 	InheritedHelperInterfaceImpl() {}
73 	InheritedHelperInterfaceImpl( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : mxContext( xContext ) {}
74 	InheritedHelperInterfaceImpl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : mxParent( xParent ), mxContext( xContext ) {}
75 	virtual rtl::OUString& getServiceImplName() = 0;
76 	virtual css::uno::Sequence<rtl::OUString> getServiceNames() = 0;
77 
78 	// XHelperInterface Methods
79 	virtual ::sal_Int32 SAL_CALL getCreator() throw (css::script::BasicErrorException, css::uno::RuntimeException)
80 	{
81 		return 0x53756E4F;
82 	}
83 	virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent(  ) throw (css::script::BasicErrorException, css::uno::RuntimeException) { return mxParent; }
84 
85 	virtual css::uno::Any SAL_CALL Application(  ) throw (css::script::BasicErrorException, css::uno::RuntimeException) {
86             // The application could certainly be passed around in the context - seems
87             // to make sense
88             css::uno::Reference< css::container::XNameAccess > xNameAccess( mxContext, css::uno::UNO_QUERY_THROW );
89             return xNameAccess->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Application" ) ) );
90 	}
91 
92 	// XServiceInfo Methods
93 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (css::uno::RuntimeException) { return getServiceImplName(); }
94 	virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (css::uno::RuntimeException)
95 	{
96 		css::uno::Sequence< rtl::OUString > sServices = getSupportedServiceNames();
97 		const rtl::OUString* pStart = sServices.getConstArray();
98 		const rtl::OUString* pEnd = pStart + sServices.getLength();
99 		for ( ; pStart != pEnd ; ++pStart )
100 			if ( (*pStart).equals( ServiceName ) )
101 				return sal_True;
102 		return sal_False;
103 	}
104 	virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (css::uno::RuntimeException)
105 	{
106 		css::uno::Sequence< rtl::OUString > aNames = getServiceNames();
107 		return aNames;
108 	}
109  };
110 
111 template< typename Ifc1 >
112 class InheritedHelperInterfaceImpl1 : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper1< Ifc1 > >
113 {
114     typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper1< Ifc1 > > Base;
115 public:
116 	InheritedHelperInterfaceImpl1< Ifc1 >() {}
117 	InheritedHelperInterfaceImpl1< Ifc1 >( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xContext ) {}
118 	InheritedHelperInterfaceImpl1< Ifc1 >( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
119 };
120 
121 template< typename Ifc1, typename Ifc2 >
122 class InheritedHelperInterfaceImpl2 : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper2< Ifc1, Ifc2 > >
123 {
124     typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper2< Ifc1, Ifc2 > > Base;
125 public:
126 	InheritedHelperInterfaceImpl2< Ifc1, Ifc2 >() {}
127 	InheritedHelperInterfaceImpl2< Ifc1, Ifc2 >( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xContext ) {}
128 	InheritedHelperInterfaceImpl2< Ifc1, Ifc2 >( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
129 };
130 
131 template< typename Ifc1, typename Ifc2, typename Ifc3 >
132 class InheritedHelperInterfaceImpl3 : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper3< Ifc1, Ifc2, Ifc3 > >
133 {
134     typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper3< Ifc1, Ifc2, Ifc3 > > Base;
135 public:
136 	InheritedHelperInterfaceImpl3< Ifc1, Ifc2, Ifc3 >() {}
137 	InheritedHelperInterfaceImpl3< Ifc1, Ifc2, Ifc3 >( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xContext ) {}
138 	InheritedHelperInterfaceImpl3< Ifc1, Ifc2, Ifc3 >( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
139 };
140 
141 // ============================================================================
142 
143 /** Helper macro to implement the method 'getServiceImplName()' of the
144     'ooo.vba.XHelperInterface' interface. Will return the class name as service
145     implementation name.
146  */
147 #define VBAHELPER_IMPL_GETSERVICEIMPLNAME( classname ) \
148 ::rtl::OUString& classname::getServiceImplName() \
149 { \
150     static ::rtl::OUString saImplName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( #classname ) ); \
151     return saImplName; \
152 }
153 
154 // ----------------------------------------------------------------------------
155 
156 /** Helper macro to implement the method 'getServiceNames()' for a single
157     service name.
158  */
159 #define VBAHELPER_IMPL_GETSERVICENAMES( classname, servicename ) \
160 css::uno::Sequence< ::rtl::OUString > classname::getServiceNames() \
161 { \
162     static css::uno::Sequence< ::rtl::OUString > saServiceNames; \
163     if( saServiceNames.getLength() == 0 ) \
164     { \
165         saServiceNames.realloc( 1 ); \
166         saServiceNames[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( servicename ) ); \
167     } \
168     return saServiceNames; \
169 }
170 
171 // ----------------------------------------------------------------------------
172 
173 /** Helper macro to declare the methods 'getServiceImplName()' and
174     'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface in a class
175     declaration.
176  */
177 #define VBAHELPER_DECL_XHELPERINTERFACE \
178     virtual ::rtl::OUString& getServiceImplName(); \
179     virtual css::uno::Sequence< ::rtl::OUString > getServiceNames();
180 
181 // ----------------------------------------------------------------------------
182 
183 /** Helper macro to implement the methods 'getServiceImplName()' and
184     'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface. Will
185     return the class name as service implementation name.
186  */
187 #define VBAHELPER_IMPL_XHELPERINTERFACE( classname, servicename ) \
188 VBAHELPER_IMPL_GETSERVICEIMPLNAME( classname ) \
189 VBAHELPER_IMPL_GETSERVICENAMES( classname, servicename )
190 
191 // ============================================================================
192 
193 #endif
194