merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / cpp / counter / counter.cxx
blob173bf325cd72379e562528042f318e46ca16c55a
1 /*************************************************************************
3 * $RCSfile: counter.cxx,v $
5 * $Revision: 1.7 $
7 * last change: $Author: rt $ $Date: 2008-04-10 16:54:51 $
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 /*************************************************************************
42 *************************************************************************
44 * service implementation: foo.Counter
45 * exported interfaces: foo.XCounter
47 * simple example component implementing a counter
49 *************************************************************************
50 *************************************************************************/
52 #include <stdio.h>
53 #include <rtl/ustring.hxx>
54 #include <cppuhelper/queryinterface.hxx> // helper for queryInterface() impl
55 #include <cppuhelper/factory.hxx> // helper for component factory
56 // generated c++ interfaces
57 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
58 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
59 #include <com/sun/star/lang/XServiceInfo.hpp>
60 #include <com/sun/star/registry/XRegistryKey.hpp>
61 #include <foo/XCountable.hpp>
63 #define SERVICENAME "foo.Counter"
64 #define IMPLNAME "com.sun.star.comp.example.cpp.Counter"
66 using namespace ::rtl;
67 using namespace ::osl;
68 using namespace ::cppu;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::lang;
71 using namespace ::com::sun::star::registry;
72 using namespace ::foo;
75 //========================================================================
76 class MyCounterImpl
77 : public XCountable
78 , public XServiceInfo
80 // to obtain other services if needed
81 Reference< XMultiServiceFactory > m_xServiceManager;
83 sal_Int32 m_nRefCount;
84 sal_Int32 m_nCount;
86 public:
87 MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager )
88 : m_xServiceManager( xServiceManager ), m_nRefCount( 0 )
89 { printf( "< MyCounterImpl ctor called >\n" ); }
90 ~MyCounterImpl()
91 { printf( "< MyCounterImpl dtor called >\n" ); }
93 // XInterface implementation
94 virtual void SAL_CALL acquire() throw ()
95 { ++m_nRefCount; }
96 virtual void SAL_CALL release() throw ()
97 { if (! --m_nRefCount) delete this; }
98 virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException)
99 { return cppu::queryInterface(rType,
100 static_cast< XInterface* >( static_cast< XServiceInfo* >( this ) ),
101 static_cast< XCountable* >( this ),
102 static_cast< XServiceInfo* >( this ) ); }
104 // XServiceInfo implementation
105 virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
106 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
107 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
108 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static( );
110 // XCountable implementation
111 virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException)
112 { return m_nCount; }
113 virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException)
114 { m_nCount = nCount; }
115 virtual sal_Int32 SAL_CALL increment() throw (RuntimeException)
116 { return (++m_nCount); }
117 virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException)
118 { return (--m_nCount); }
121 //*************************************************************************
122 OUString SAL_CALL MyCounterImpl::getImplementationName( )
123 throw(RuntimeException)
125 return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) );
128 //*************************************************************************
129 sal_Bool SAL_CALL MyCounterImpl::supportsService( const OUString& ServiceName )
130 throw(RuntimeException)
132 Sequence< OUString > aSNL = getSupportedServiceNames();
133 const OUString * pArray = aSNL.getArray();
134 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
135 if( pArray[i] == ServiceName )
136 return sal_True;
137 return sal_False;
140 //*************************************************************************
141 Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames( )
142 throw(RuntimeException)
144 return getSupportedServiceNames_Static();
147 //*************************************************************************
148 Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames_Static( )
150 OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME) );
151 return Sequence< OUString >( &aName, 1 );
158 * Function to create a new component instance; is needed by factory helper implementation.
159 * @param xMgr service manager to if the components needs other component instances
161 Reference< XInterface > SAL_CALL MyCounterImpl_create(
162 const Reference< XMultiServiceFactory > & xMgr )
164 return Reference<XInterface>(static_cast<XCountable*>(new MyCounterImpl(xMgr)));
168 //#########################################################################
169 //#### EXPORTED ###########################################################
170 //#########################################################################
174 * Gives the environment this component belongs to.
176 extern "C" void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv)
178 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
182 * This function creates an implementation section in the registry and another subkey
184 * for each supported service.
185 * @param pServiceManager the service manager
186 * @param pRegistryKey the registry key
188 extern "C" sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey)
190 sal_Bool result = sal_False;
192 if (pRegistryKey)
196 Reference< XRegistryKey > xNewKey(
197 reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
198 OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME "/UNO/SERVICES") ) ) );
200 const Sequence< OUString > & rSNL =
201 MyCounterImpl::getSupportedServiceNames_Static();
202 const OUString * pArray = rSNL.getConstArray();
203 for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
204 xNewKey->createKey( pArray[nPos] );
206 return sal_True;
208 catch (InvalidRegistryException &)
210 // we should not ignore exceptions
213 return result;
217 * This function is called to get service factories for an implementation.
219 * @param pImplName name of implementation
220 * @param pServiceManager a service manager, need for component creation
221 * @param pRegistryKey the registry key for this component, need for persistent data
222 * @return a component factory
224 extern "C" void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey)
226 void * pRet = 0;
228 if (rtl_str_compare( pImplName, IMPLNAME ) == 0)
230 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
231 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
232 OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) ),
233 MyCounterImpl_create,
234 MyCounterImpl::getSupportedServiceNames_Static() ) );
236 if (xFactory.is())
238 xFactory->acquire();
239 pRet = xFactory.get();
243 return pRet;