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