xref: /aoo42x/main/odk/examples/cpp/counter/counter.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir /*************************************************************************
36*cdf0e10cSrcweir  *************************************************************************
37*cdf0e10cSrcweir  *
38*cdf0e10cSrcweir  * service implementation:	 foo.Counter
39*cdf0e10cSrcweir  * exported interfaces:		 foo.XCounter
40*cdf0e10cSrcweir  *
41*cdf0e10cSrcweir  * simple example component implementing a counter
42*cdf0e10cSrcweir  *
43*cdf0e10cSrcweir  *************************************************************************
44*cdf0e10cSrcweir  *************************************************************************/
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <stdio.h>
47*cdf0e10cSrcweir #include <rtl/ustring.hxx>
48*cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx> // helper for queryInterface() impl
49*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> // helper for component factory
50*cdf0e10cSrcweir // generated c++ interfaces
51*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
55*cdf0e10cSrcweir #include <foo/XCountable.hpp>
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #define SERVICENAME "foo.Counter"
58*cdf0e10cSrcweir #define IMPLNAME	"com.sun.star.comp.example.cpp.Counter"
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir using namespace ::rtl;
61*cdf0e10cSrcweir using namespace ::osl;
62*cdf0e10cSrcweir using namespace ::cppu;
63*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
64*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
65*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
66*cdf0e10cSrcweir using namespace ::foo;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir //========================================================================
70*cdf0e10cSrcweir class MyCounterImpl
71*cdf0e10cSrcweir 	: public XCountable
72*cdf0e10cSrcweir 	, public XServiceInfo
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir 	// to obtain other services if needed
75*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > m_xServiceManager;
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	sal_Int32 m_nRefCount;
78*cdf0e10cSrcweir 	sal_Int32 m_nCount;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir public:
81*cdf0e10cSrcweir 	MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager )
82*cdf0e10cSrcweir 		: m_xServiceManager( xServiceManager ), m_nRefCount( 0 )
83*cdf0e10cSrcweir 		{ printf( "< MyCounterImpl ctor called >\n" ); }
84*cdf0e10cSrcweir 	~MyCounterImpl()
85*cdf0e10cSrcweir 		{ printf( "< MyCounterImpl dtor called >\n" ); }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 	// XInterface implementation
88*cdf0e10cSrcweir 	virtual void SAL_CALL acquire() throw ()
89*cdf0e10cSrcweir 		{ ++m_nRefCount; }
90*cdf0e10cSrcweir 	virtual void SAL_CALL release() throw ()
91*cdf0e10cSrcweir 		{ if (! --m_nRefCount) delete this; }
92*cdf0e10cSrcweir 	virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException)
93*cdf0e10cSrcweir 		{ return cppu::queryInterface(rType,
94*cdf0e10cSrcweir         							  static_cast< XInterface* >( static_cast< XServiceInfo* >( this ) ),
95*cdf0e10cSrcweir         							  static_cast< XCountable* >( this ),
96*cdf0e10cSrcweir                                       static_cast< XServiceInfo* >( this ) ); }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     // XServiceInfo	implementation
99*cdf0e10cSrcweir     virtual OUString SAL_CALL getImplementationName(  ) throw(RuntimeException);
100*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
101*cdf0e10cSrcweir     virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(  ) throw(RuntimeException);
102*cdf0e10cSrcweir     static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static(  );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	// XCountable implementation
105*cdf0e10cSrcweir 	virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException)
106*cdf0e10cSrcweir 		{ return m_nCount; }
107*cdf0e10cSrcweir 	virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException)
108*cdf0e10cSrcweir 		{ m_nCount = nCount; }
109*cdf0e10cSrcweir 	virtual sal_Int32 SAL_CALL increment() throw (RuntimeException)
110*cdf0e10cSrcweir 		{ return (++m_nCount); }
111*cdf0e10cSrcweir 	virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException)
112*cdf0e10cSrcweir 		{ return (--m_nCount); }
113*cdf0e10cSrcweir };
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir //*************************************************************************
116*cdf0e10cSrcweir OUString SAL_CALL MyCounterImpl::getImplementationName(  )
117*cdf0e10cSrcweir 	throw(RuntimeException)
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir 	return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) );
120*cdf0e10cSrcweir }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir //*************************************************************************
123*cdf0e10cSrcweir sal_Bool SAL_CALL MyCounterImpl::supportsService( const OUString& ServiceName )
124*cdf0e10cSrcweir 	throw(RuntimeException)
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	Sequence< OUString > aSNL = getSupportedServiceNames();
127*cdf0e10cSrcweir 	const OUString * pArray = aSNL.getArray();
128*cdf0e10cSrcweir 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
129*cdf0e10cSrcweir 		if( pArray[i] == ServiceName )
130*cdf0e10cSrcweir 			return sal_True;
131*cdf0e10cSrcweir 	return sal_False;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir //*************************************************************************
135*cdf0e10cSrcweir Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames(  )
136*cdf0e10cSrcweir 	throw(RuntimeException)
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir 	return getSupportedServiceNames_Static();
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir //*************************************************************************
142*cdf0e10cSrcweir Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames_Static(  )
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir 	OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME) );
145*cdf0e10cSrcweir 	return Sequence< OUString >( &aName, 1 );
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir /**
152*cdf0e10cSrcweir  * Function to create a new component instance; is needed by factory helper implementation.
153*cdf0e10cSrcweir  * @param xMgr service manager to if the components needs other component instances
154*cdf0e10cSrcweir  */
155*cdf0e10cSrcweir Reference< XInterface > SAL_CALL MyCounterImpl_create(
156*cdf0e10cSrcweir 	const Reference< XMultiServiceFactory > & xMgr )
157*cdf0e10cSrcweir {
158*cdf0e10cSrcweir 	return Reference<XInterface>(static_cast<XCountable*>(new MyCounterImpl(xMgr)));
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir //#########################################################################
163*cdf0e10cSrcweir //#### EXPORTED ###########################################################
164*cdf0e10cSrcweir //#########################################################################
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir /**
168*cdf0e10cSrcweir  * Gives the environment this component belongs to.
169*cdf0e10cSrcweir  */
170*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
173*cdf0e10cSrcweir }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir /**
176*cdf0e10cSrcweir  * This function creates an implementation section in the registry and another subkey
177*cdf0e10cSrcweir  *
178*cdf0e10cSrcweir  * for each supported service.
179*cdf0e10cSrcweir  * @param pServiceManager   the service manager
180*cdf0e10cSrcweir  * @param pRegistryKey      the registry key
181*cdf0e10cSrcweir  */
182*cdf0e10cSrcweir // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey)
183*cdf0e10cSrcweir // {
184*cdf0e10cSrcweir // 	sal_Bool result = sal_False;
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir // 	if (pRegistryKey)
187*cdf0e10cSrcweir // 	{
188*cdf0e10cSrcweir // 		try
189*cdf0e10cSrcweir // 		{
190*cdf0e10cSrcweir // 			Reference< XRegistryKey > xNewKey(
191*cdf0e10cSrcweir // 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
192*cdf0e10cSrcweir // 					OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME "/UNO/SERVICES") ) ) );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir // 			const Sequence< OUString > & rSNL =
195*cdf0e10cSrcweir // 				MyCounterImpl::getSupportedServiceNames_Static();
196*cdf0e10cSrcweir // 			const OUString * pArray = rSNL.getConstArray();
197*cdf0e10cSrcweir // 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
198*cdf0e10cSrcweir // 				xNewKey->createKey( pArray[nPos] );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir // 			return sal_True;
201*cdf0e10cSrcweir // 		}
202*cdf0e10cSrcweir // 		catch (InvalidRegistryException &)
203*cdf0e10cSrcweir // 		{
204*cdf0e10cSrcweir // 			// we should not ignore exceptions
205*cdf0e10cSrcweir // 		}
206*cdf0e10cSrcweir // 	}
207*cdf0e10cSrcweir // 	return result;
208*cdf0e10cSrcweir // }
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir /**
211*cdf0e10cSrcweir  * This function is called to get service factories for an implementation.
212*cdf0e10cSrcweir  *
213*cdf0e10cSrcweir  * @param pImplName       name of implementation
214*cdf0e10cSrcweir  * @param pServiceManager a service manager, need for component creation
215*cdf0e10cSrcweir  * @param pRegistryKey    the registry key for this component, need for persistent data
216*cdf0e10cSrcweir  * @return a component factory
217*cdf0e10cSrcweir  */
218*cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey)
219*cdf0e10cSrcweir {
220*cdf0e10cSrcweir 	void * pRet = 0;
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 	if (rtl_str_compare( pImplName, IMPLNAME ) == 0)
223*cdf0e10cSrcweir 	{
224*cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
225*cdf0e10cSrcweir 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
226*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) ),
227*cdf0e10cSrcweir 			MyCounterImpl_create,
228*cdf0e10cSrcweir 			MyCounterImpl::getSupportedServiceNames_Static() ) );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 		if (xFactory.is())
231*cdf0e10cSrcweir 		{
232*cdf0e10cSrcweir 			xFactory->acquire();
233*cdf0e10cSrcweir 			pRet = xFactory.get();
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 	return pRet;
238*cdf0e10cSrcweir }
239