merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / Components / CppComponent / service1_impl.cxx
blob73b858bf5bd95e63ae05ed97cc11936172d2ec31
1 /*************************************************************************
3 * $RCSfile: service1_impl.cxx,v $
5 * $Revision: 1.8 $
7 * last change: $Author: kz $ $Date: 2006-11-06 15:00:06 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 #include <osl/interlck.h>
42 #include <osl/mutex.hxx>
43 #include <rtl/uuid.h>
44 #include <cppuhelper/factory.hxx>
46 #include <com/sun/star/lang/XServiceInfo.hpp>
47 #include <com/sun/star/lang/XTypeProvider.hpp>
48 #include <my_module/XSomething.hpp>
51 using namespace ::rtl; // for OUString
52 using namespace ::com::sun::star; // for odk interfaces
53 using namespace ::com::sun::star::uno; // for basic types
55 namespace my_sc_impl
58 Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl()
60 Sequence< OUString > names(1);
61 names[0] = OUString(RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1"));
62 return names;
65 OUString SAL_CALL getImplementationName_MyService1Impl()
67 return OUString( RTL_CONSTASCII_USTRINGPARAM(
68 "my_module.my_sc_implementation.MyService1") );
72 class MyService1Impl
73 : public ::my_module::XSomething
74 , public lang::XServiceInfo
75 , public lang::XTypeProvider
77 oslInterlockedCount m_refcount;
78 OUString m_sData;
79 // it's good practise to store the context for further use when you use
80 // other UNO API's in your implementation
81 Reference< XComponentContext > m_xContext;
82 public:
83 inline MyService1Impl(Reference< XComponentContext > const & xContext) throw ()
84 : m_refcount( 0 ),
85 m_xContext(xContext)
88 virtual ~MyService1Impl() {}
90 // XInterface
91 virtual Any SAL_CALL queryInterface( Type const & type )
92 throw (RuntimeException);
93 virtual void SAL_CALL acquire()
94 throw ();
95 virtual void SAL_CALL release()
96 throw ();
97 // XTypeProvider
98 virtual Sequence< Type > SAL_CALL getTypes()
99 throw (RuntimeException);
100 virtual Sequence< sal_Int8 > SAL_CALL getImplementationId()
101 throw (RuntimeException);
102 // XSomething
103 virtual OUString SAL_CALL methodOne( OUString const & str )
104 throw (RuntimeException);
105 virtual OUString SAL_CALL methodTwo( )
106 throw (RuntimeException);
107 // XServiceInfo
108 virtual OUString SAL_CALL getImplementationName()
109 throw (RuntimeException);
110 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
111 throw (RuntimeException);
112 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
113 throw (RuntimeException);
116 // XInterface implementation
117 Any MyService1Impl::queryInterface( Type const & type )
118 throw (RuntimeException)
120 if (type.equals(::cppu::UnoType< Reference< XInterface > >::get()))
122 // return XInterface interface
123 // (resolve ambiguity by casting to lang::XTypeProvider)
124 Reference< XInterface > x(
125 static_cast< lang::XTypeProvider * >( this ) );
126 return makeAny( x );
128 if (type.equals(::cppu::UnoType< Reference< lang::XTypeProvider > >::get()))
130 // return XInterface interface
131 Reference< XInterface > x(
132 static_cast< lang::XTypeProvider * >( this ) );
133 return makeAny( x );
135 if (type.equals(::cppu::UnoType< Reference< lang::XServiceInfo > >::get()))
137 // return XServiceInfo interface
138 Reference< lang::XServiceInfo > x(
139 static_cast< lang::XServiceInfo * >( this ) );
140 return makeAny( x );
142 if (type.equals(::cppu::UnoType< Reference< ::my_module::XSomething > >::get()))
144 // return sample interface
145 Reference< ::my_module::XSomething > x(
146 static_cast< ::my_module::XSomething * >( this ) );
147 return makeAny( x );
149 // querying for unsupported type
150 return Any();
153 void MyService1Impl::acquire()
154 throw ()
156 // thread-safe incrementation of reference count
157 ::osl_incrementInterlockedCount( &m_refcount );
160 void MyService1Impl::release()
161 throw ()
163 // thread-safe decrementation of reference count
164 if (0 == ::osl_decrementInterlockedCount( &m_refcount ))
166 delete this; // shutdown this object
170 // XTypeProvider implementation
171 Sequence< Type > MyService1Impl::getTypes()
172 throw (RuntimeException)
174 Sequence< Type > seq( 3 );
175 seq[ 0 ] = ::cppu::UnoType< Reference< lang::XTypeProvider > >::get();
176 seq[ 1 ] = ::cppu::UnoType< Reference< lang::XServiceInfo > >::get();
177 seq[ 2 ] = ::cppu::UnoType< Reference< ::my_module::XSomething > >::get();
178 return seq;
180 Sequence< sal_Int8 > MyService1Impl::getImplementationId()
181 throw (RuntimeException)
183 static Sequence< sal_Int8 > * s_pId = 0;
184 if (! s_pId)
186 // create unique id
187 Sequence< sal_Int8 > id( 16 );
188 ::rtl_createUuid( (sal_uInt8 *)id.getArray(), 0, sal_True );
189 // guard initialization with some mutex
190 ::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
191 if (! s_pId)
193 static Sequence< sal_Int8 > s_id( id );
194 s_pId = &s_id;
197 return *s_pId;
200 // XSomething implementation
201 OUString MyService1Impl::methodOne( OUString const & str )
202 throw (RuntimeException)
204 m_sData = str;
205 return OUString( RTL_CONSTASCII_USTRINGPARAM(
206 "called methodOne() of MyService1 implementation: ") ) + m_sData;
209 OUString MyService1Impl::methodTwo( )
210 throw (RuntimeException)
212 return OUString( RTL_CONSTASCII_USTRINGPARAM(
213 "called methodTwo() of MyService1 implementation: ") ) + m_sData;
216 // XServiceInfo implementation
217 OUString MyService1Impl::getImplementationName()
218 throw (RuntimeException)
220 // unique implementation name
221 return OUString( RTL_CONSTASCII_USTRINGPARAM(
222 "my_module.my_sc_implementation.MyService1") );
224 sal_Bool MyService1Impl::supportsService( OUString const & serviceName )
225 throw (RuntimeException)
227 // this object only supports one service, so the test is simple
228 return serviceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM(
229 "my_module.MyService1") );
231 Sequence< OUString > MyService1Impl::getSupportedServiceNames()
232 throw (RuntimeException)
234 // this object only supports one service
235 OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") );
236 return Sequence< OUString >( &serviceName, 1 );
239 Reference< XInterface > SAL_CALL create_MyService1Impl(
240 Reference< XComponentContext > const & xContext )
241 SAL_THROW( () )
243 return static_cast< lang::XTypeProvider * >( new MyService1Impl( xContext) );
246 // forward decl: implemented in service2_impl.cxx
247 Reference< XInterface > SAL_CALL create_MyService2Impl(
248 Reference< XComponentContext > const & ) SAL_THROW( () );
253 extern "C" void SAL_CALL component_getImplementationEnvironment(
254 sal_Char const ** ppEnvTypeName, uno_Environment ** )
256 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
259 extern "C" sal_Bool SAL_CALL component_writeInfo(
260 lang::XMultiServiceFactory * xMgr, registry::XRegistryKey * xRegistry )
262 if (xRegistry)
266 // implementation of MyService1A
267 Reference< registry::XRegistryKey > xKey(
268 xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
269 "my_module.my_sc_implementation.MyService1/UNO/SERVICES") ) ) );
270 // subkeys denote implemented services of implementation
271 xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
272 "my_module.MyService1") ) );
273 // implementation of MyService1B
274 xKey = xRegistry->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
275 "my_module.my_sc_implementation.MyService2/UNO/SERVICES") ) );
276 // subkeys denote implemented services of implementation
277 xKey->createKey( OUString( RTL_CONSTASCII_USTRINGPARAM(
278 "my_module.MyService2") ) );
279 return sal_True; // success
281 catch (registry::InvalidRegistryException &)
283 // function fails if exception caught
286 return sal_False;
288 extern "C" void * SAL_CALL component_getFactory(
289 sal_Char const * implName, lang::XMultiServiceFactory * xMgr, void * )
291 Reference< lang::XSingleComponentFactory > xFactory;
292 if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService1" ))
294 // create component factory for MyService1 implementation
295 OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService1") );
296 xFactory = ::cppu::createSingleComponentFactory(
297 ::my_sc_impl::create_MyService1Impl,
298 OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService1") ),
299 Sequence< OUString >( &serviceName, 1 ) );
301 else if (0 == ::rtl_str_compare( implName, "my_module.my_sc_implementation.MyService2" ))
303 // create component factory for MyService12 implementation
304 OUString serviceName( RTL_CONSTASCII_USTRINGPARAM("my_module.MyService2") );
305 xFactory = ::cppu::createSingleComponentFactory(
306 ::my_sc_impl::create_MyService2Impl,
307 OUString( RTL_CONSTASCII_USTRINGPARAM("my_module.my_sc_implementation.MyService2") ),
308 Sequence< OUString >( &serviceName, 1 ) );
310 if (xFactory.is())
311 xFactory->acquire();
312 return xFactory.get(); // return acquired interface pointer or null