1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef OOVBAAPI_VBA_HELPERINTERFACE_HXX
24 #define OOVBAAPI_VBA_HELPERINTERFACE_HXX
25 
26 #include <cppuhelper/implbase1.hxx>
27 #include <cppuhelper/implbase2.hxx>
28 #include <cppuhelper/implbase3.hxx>
29 #include <ooo/vba/XHelperInterface.hpp>
30 #include <vbahelper/vbahelper.hxx>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 
33 // use this class when you have an a object like
34 // interface  XAnInterface which contains XHelperInterface in its inheritance hierarchy
35 // interface XAnInterface
36 // {
37 //     interface XHelperInterface;
38 //     [attribute, string] name;
39 // }
40 // or
41 // interface XAnInterface : XHelperInterface;
42 // {
43 //     [attribute, string] name;
44 // }
45 //
46 // then this class can provide a default implementation of XHelperInterface,
47 // you can use it like this
48 // typedef InheritedHelperInterfaceImpl< XAnInterface > > AnInterfaceImpl_BASE;
49 // class AnInterfaceImpl : public AnInterfaceImpl_BASE
50 // {
51 // public:
52 //     AnInterface( const Reference< HelperInterface >& xParent ) : AnInterfaceImpl_BASE( xParent ) {}
53 //     // implement XAnInterface methods only, no need to implement the XHelperInterface
54 //     // methods
55 //     virtual void setName( const OUString& );
56 //     virtual OUString getName();
57 // }
58 //
59 const ::rtl::OUString sHelperServiceName( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.HelperServiceBase" ) );
60 
61 template< typename Ifc1 >
62 class InheritedHelperInterfaceImpl : public Ifc1
63 {
64 protected:
65 	css::uno::WeakReference< ov::XHelperInterface > mxParent;
66 	css::uno::Reference< css::uno::XComponentContext > mxContext;
67 public:
InheritedHelperInterfaceImpl()68 	InheritedHelperInterfaceImpl() {}
InheritedHelperInterfaceImpl(const css::uno::Reference<css::uno::XComponentContext> & xContext)69 	InheritedHelperInterfaceImpl( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : mxContext( xContext ) {}
InheritedHelperInterfaceImpl(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext)70 	InheritedHelperInterfaceImpl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : mxParent( xParent ), mxContext( xContext ) {}
71 	virtual rtl::OUString& getServiceImplName() = 0;
72 	virtual css::uno::Sequence<rtl::OUString> getServiceNames() = 0;
73 
74 	// XHelperInterface Methods
getCreator()75 	virtual ::sal_Int32 SAL_CALL getCreator() throw (css::script::BasicErrorException, css::uno::RuntimeException)
76 	{
77 		return 0x53756E4F;
78 	}
getParent()79 	virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent(  ) throw (css::script::BasicErrorException, css::uno::RuntimeException) { return mxParent; }
80 
Application()81 	virtual css::uno::Any SAL_CALL Application(  ) throw (css::script::BasicErrorException, css::uno::RuntimeException) {
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( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Application" ) ) );
86 	}
87 
88 	// XServiceInfo Methods
getImplementationName()89 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (css::uno::RuntimeException) { return getServiceImplName(); }
supportsService(const::rtl::OUString & ServiceName)90 	virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (css::uno::RuntimeException)
91 	{
92 		css::uno::Sequence< rtl::OUString > sServices = getSupportedServiceNames();
93 		const rtl::OUString* pStart = sServices.getConstArray();
94 		const rtl::OUString* pEnd = pStart + sServices.getLength();
95 		for ( ; pStart != pEnd ; ++pStart )
96 			if ( (*pStart).equals( ServiceName ) )
97 				return sal_True;
98 		return sal_False;
99 	}
getSupportedServiceNames()100 	virtual css::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw (css::uno::RuntimeException)
101 	{
102 		css::uno::Sequence< rtl::OUString > aNames = getServiceNames();
103 		return aNames;
104 	}
105  };
106 
107 template< typename Ifc1 >
108 class InheritedHelperInterfaceImpl1 : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper1< Ifc1 > >
109 {
110     typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper1< Ifc1 > > Base;
111 public:
112 	InheritedHelperInterfaceImpl1< Ifc1 >() {}
113 	InheritedHelperInterfaceImpl1< Ifc1 >( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xContext ) {}
114 	InheritedHelperInterfaceImpl1< Ifc1 >( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
115 };
116 
117 template< typename Ifc1, typename Ifc2 >
118 class InheritedHelperInterfaceImpl2 : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper2< Ifc1, Ifc2 > >
119 {
120     typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper2< Ifc1, Ifc2 > > Base;
121 public:
122 	InheritedHelperInterfaceImpl2< Ifc1, Ifc2 >() {}
123 	InheritedHelperInterfaceImpl2< Ifc1, Ifc2 >( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xContext ) {}
124 	InheritedHelperInterfaceImpl2< Ifc1, Ifc2 >( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
125 };
126 
127 template< typename Ifc1, typename Ifc2, typename Ifc3 >
128 class InheritedHelperInterfaceImpl3 : public InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper3< Ifc1, Ifc2, Ifc3 > >
129 {
130     typedef InheritedHelperInterfaceImpl< ::cppu::WeakImplHelper3< Ifc1, Ifc2, Ifc3 > > Base;
131 public:
132 	InheritedHelperInterfaceImpl3< Ifc1, Ifc2, Ifc3 >() {}
133 	InheritedHelperInterfaceImpl3< Ifc1, Ifc2, Ifc3 >( const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xContext ) {}
134 	InheritedHelperInterfaceImpl3< Ifc1, Ifc2, Ifc3 >( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : Base( xParent, xContext ) {}
135 };
136 
137 // ============================================================================
138 
139 /** Helper macro to implement the method 'getServiceImplName()' of the
140     'ooo.vba.XHelperInterface' interface. Will return the class name as service
141     implementation name.
142  */
143 #define VBAHELPER_IMPL_GETSERVICEIMPLNAME( classname ) \
144 ::rtl::OUString& classname::getServiceImplName() \
145 { \
146     static ::rtl::OUString saImplName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( #classname ) ); \
147     return saImplName; \
148 }
149 
150 // ----------------------------------------------------------------------------
151 
152 /** Helper macro to implement the method 'getServiceNames()' for a single
153     service name.
154  */
155 #define VBAHELPER_IMPL_GETSERVICENAMES( classname, servicename ) \
156 css::uno::Sequence< ::rtl::OUString > classname::getServiceNames() \
157 { \
158     static css::uno::Sequence< ::rtl::OUString > saServiceNames; \
159     if( saServiceNames.getLength() == 0 ) \
160     { \
161         saServiceNames.realloc( 1 ); \
162         saServiceNames[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( servicename ) ); \
163     } \
164     return saServiceNames; \
165 }
166 
167 // ----------------------------------------------------------------------------
168 
169 /** Helper macro to declare the methods 'getServiceImplName()' and
170     'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface in a class
171     declaration.
172  */
173 #define VBAHELPER_DECL_XHELPERINTERFACE \
174     virtual ::rtl::OUString& getServiceImplName(); \
175     virtual css::uno::Sequence< ::rtl::OUString > getServiceNames();
176 
177 // ----------------------------------------------------------------------------
178 
179 /** Helper macro to implement the methods 'getServiceImplName()' and
180     'getServiceNames()' of the 'ooo.vba.XHelperInterface' interface. Will
181     return the class name as service implementation name.
182  */
183 #define VBAHELPER_IMPL_XHELPERINTERFACE( classname, servicename ) \
184 VBAHELPER_IMPL_GETSERVICEIMPLNAME( classname ) \
185 VBAHELPER_IMPL_GETSERVICENAMES( classname, servicename )
186 
187 // ============================================================================
188 
189 #endif
190