1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist *
3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*34dd1e25SAndrew Rist * distributed with this work for additional information
6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at
10*34dd1e25SAndrew Rist *
11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist *
13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the
17*34dd1e25SAndrew Rist * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist * under the License.
19*34dd1e25SAndrew Rist *
20*34dd1e25SAndrew Rist *************************************************************/
21*34dd1e25SAndrew Rist
22*34dd1e25SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir /*************************************************************************
25cdf0e10cSrcweir *************************************************************************
26cdf0e10cSrcweir *
27cdf0e10cSrcweir * service implementation: foo.Counter
28cdf0e10cSrcweir * exported interfaces: foo.XCounter
29cdf0e10cSrcweir *
30cdf0e10cSrcweir * simple example component implementing a counter
31cdf0e10cSrcweir *
32cdf0e10cSrcweir *************************************************************************
33cdf0e10cSrcweir *************************************************************************/
34cdf0e10cSrcweir
35cdf0e10cSrcweir #include <stdio.h>
36cdf0e10cSrcweir #include <rtl/ustring.hxx>
37cdf0e10cSrcweir #include <cppuhelper/queryinterface.hxx> // helper for queryInterface() impl
38cdf0e10cSrcweir #include <cppuhelper/factory.hxx> // helper for component factory
39cdf0e10cSrcweir // generated c++ interfaces
40cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
41cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
43cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
44cdf0e10cSrcweir #include <foo/XCountable.hpp>
45cdf0e10cSrcweir
46cdf0e10cSrcweir #define SERVICENAME "foo.Counter"
47cdf0e10cSrcweir #define IMPLNAME "com.sun.star.comp.example.cpp.Counter"
48cdf0e10cSrcweir
49cdf0e10cSrcweir using namespace ::rtl;
50cdf0e10cSrcweir using namespace ::osl;
51cdf0e10cSrcweir using namespace ::cppu;
52cdf0e10cSrcweir using namespace ::com::sun::star::uno;
53cdf0e10cSrcweir using namespace ::com::sun::star::lang;
54cdf0e10cSrcweir using namespace ::com::sun::star::registry;
55cdf0e10cSrcweir using namespace ::foo;
56cdf0e10cSrcweir
57cdf0e10cSrcweir
58cdf0e10cSrcweir //========================================================================
59cdf0e10cSrcweir class MyCounterImpl
60cdf0e10cSrcweir : public XCountable
61cdf0e10cSrcweir , public XServiceInfo
62cdf0e10cSrcweir {
63cdf0e10cSrcweir // to obtain other services if needed
64cdf0e10cSrcweir Reference< XMultiServiceFactory > m_xServiceManager;
65cdf0e10cSrcweir
66cdf0e10cSrcweir sal_Int32 m_nRefCount;
67cdf0e10cSrcweir sal_Int32 m_nCount;
68cdf0e10cSrcweir
69cdf0e10cSrcweir public:
MyCounterImpl(const Reference<XMultiServiceFactory> & xServiceManager)70cdf0e10cSrcweir MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager )
71cdf0e10cSrcweir : m_xServiceManager( xServiceManager ), m_nRefCount( 0 )
72cdf0e10cSrcweir { printf( "< MyCounterImpl ctor called >\n" ); }
~MyCounterImpl()73cdf0e10cSrcweir ~MyCounterImpl()
74cdf0e10cSrcweir { printf( "< MyCounterImpl dtor called >\n" ); }
75cdf0e10cSrcweir
76cdf0e10cSrcweir // XInterface implementation
acquire()77cdf0e10cSrcweir virtual void SAL_CALL acquire() throw ()
78cdf0e10cSrcweir { ++m_nRefCount; }
release()79cdf0e10cSrcweir virtual void SAL_CALL release() throw ()
80cdf0e10cSrcweir { if (! --m_nRefCount) delete this; }
queryInterface(const Type & rType)81cdf0e10cSrcweir virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException)
82cdf0e10cSrcweir { return cppu::queryInterface(rType,
83cdf0e10cSrcweir static_cast< XInterface* >( static_cast< XServiceInfo* >( this ) ),
84cdf0e10cSrcweir static_cast< XCountable* >( this ),
85cdf0e10cSrcweir static_cast< XServiceInfo* >( this ) ); }
86cdf0e10cSrcweir
87cdf0e10cSrcweir // XServiceInfo implementation
88cdf0e10cSrcweir virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
89cdf0e10cSrcweir virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
90cdf0e10cSrcweir virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
91cdf0e10cSrcweir static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static( );
92cdf0e10cSrcweir
93cdf0e10cSrcweir // XCountable implementation
getCount()94cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException)
95cdf0e10cSrcweir { return m_nCount; }
setCount(sal_Int32 nCount)96cdf0e10cSrcweir virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException)
97cdf0e10cSrcweir { m_nCount = nCount; }
increment()98cdf0e10cSrcweir virtual sal_Int32 SAL_CALL increment() throw (RuntimeException)
99cdf0e10cSrcweir { return (++m_nCount); }
decrement()100cdf0e10cSrcweir virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException)
101cdf0e10cSrcweir { return (--m_nCount); }
102cdf0e10cSrcweir };
103cdf0e10cSrcweir
104cdf0e10cSrcweir //*************************************************************************
getImplementationName()105cdf0e10cSrcweir OUString SAL_CALL MyCounterImpl::getImplementationName( )
106cdf0e10cSrcweir throw(RuntimeException)
107cdf0e10cSrcweir {
108cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) );
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
111cdf0e10cSrcweir //*************************************************************************
supportsService(const OUString & ServiceName)112cdf0e10cSrcweir sal_Bool SAL_CALL MyCounterImpl::supportsService( const OUString& ServiceName )
113cdf0e10cSrcweir throw(RuntimeException)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir Sequence< OUString > aSNL = getSupportedServiceNames();
116cdf0e10cSrcweir const OUString * pArray = aSNL.getArray();
117cdf0e10cSrcweir for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
118cdf0e10cSrcweir if( pArray[i] == ServiceName )
119cdf0e10cSrcweir return sal_True;
120cdf0e10cSrcweir return sal_False;
121cdf0e10cSrcweir }
122cdf0e10cSrcweir
123cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames()124cdf0e10cSrcweir Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames( )
125cdf0e10cSrcweir throw(RuntimeException)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir return getSupportedServiceNames_Static();
128cdf0e10cSrcweir }
129cdf0e10cSrcweir
130cdf0e10cSrcweir //*************************************************************************
getSupportedServiceNames_Static()131cdf0e10cSrcweir Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames_Static( )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir OUString aName( RTL_CONSTASCII_USTRINGPARAM(SERVICENAME) );
134cdf0e10cSrcweir return Sequence< OUString >( &aName, 1 );
135cdf0e10cSrcweir }
136cdf0e10cSrcweir
137cdf0e10cSrcweir
138cdf0e10cSrcweir
139cdf0e10cSrcweir
140cdf0e10cSrcweir /**
141cdf0e10cSrcweir * Function to create a new component instance; is needed by factory helper implementation.
142cdf0e10cSrcweir * @param xMgr service manager to if the components needs other component instances
143cdf0e10cSrcweir */
MyCounterImpl_create(const Reference<XMultiServiceFactory> & xMgr)144cdf0e10cSrcweir Reference< XInterface > SAL_CALL MyCounterImpl_create(
145cdf0e10cSrcweir const Reference< XMultiServiceFactory > & xMgr )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir return Reference<XInterface>(static_cast<XCountable*>(new MyCounterImpl(xMgr)));
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
150cdf0e10cSrcweir
151cdf0e10cSrcweir //#########################################################################
152cdf0e10cSrcweir //#### EXPORTED ###########################################################
153cdf0e10cSrcweir //#########################################################################
154cdf0e10cSrcweir
155cdf0e10cSrcweir
156cdf0e10cSrcweir /**
157cdf0e10cSrcweir * Gives the environment this component belongs to.
158cdf0e10cSrcweir */
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment ** ppEnv)159cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv)
160cdf0e10cSrcweir {
161cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
162cdf0e10cSrcweir }
163cdf0e10cSrcweir
164cdf0e10cSrcweir /**
165cdf0e10cSrcweir * This function creates an implementation section in the registry and another subkey
166cdf0e10cSrcweir *
167cdf0e10cSrcweir * for each supported service.
168cdf0e10cSrcweir * @param pServiceManager the service manager
169cdf0e10cSrcweir * @param pRegistryKey the registry key
170cdf0e10cSrcweir */
171cdf0e10cSrcweir // extern "C" SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(void * pServiceManager, void * pRegistryKey)
172cdf0e10cSrcweir // {
173cdf0e10cSrcweir // sal_Bool result = sal_False;
174cdf0e10cSrcweir
175cdf0e10cSrcweir // if (pRegistryKey)
176cdf0e10cSrcweir // {
177cdf0e10cSrcweir // try
178cdf0e10cSrcweir // {
179cdf0e10cSrcweir // Reference< XRegistryKey > xNewKey(
180cdf0e10cSrcweir // reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
181cdf0e10cSrcweir // OUString( RTL_CONSTASCII_USTRINGPARAM("/" IMPLNAME "/UNO/SERVICES") ) ) );
182cdf0e10cSrcweir
183cdf0e10cSrcweir // const Sequence< OUString > & rSNL =
184cdf0e10cSrcweir // MyCounterImpl::getSupportedServiceNames_Static();
185cdf0e10cSrcweir // const OUString * pArray = rSNL.getConstArray();
186cdf0e10cSrcweir // for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
187cdf0e10cSrcweir // xNewKey->createKey( pArray[nPos] );
188cdf0e10cSrcweir
189cdf0e10cSrcweir // return sal_True;
190cdf0e10cSrcweir // }
191cdf0e10cSrcweir // catch (InvalidRegistryException &)
192cdf0e10cSrcweir // {
193cdf0e10cSrcweir // // we should not ignore exceptions
194cdf0e10cSrcweir // }
195cdf0e10cSrcweir // }
196cdf0e10cSrcweir // return result;
197cdf0e10cSrcweir // }
198cdf0e10cSrcweir
199cdf0e10cSrcweir /**
200cdf0e10cSrcweir * This function is called to get service factories for an implementation.
201cdf0e10cSrcweir *
202cdf0e10cSrcweir * @param pImplName name of implementation
203cdf0e10cSrcweir * @param pServiceManager a service manager, need for component creation
204cdf0e10cSrcweir * @param pRegistryKey the registry key for this component, need for persistent data
205cdf0e10cSrcweir * @return a component factory
206cdf0e10cSrcweir */
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)207cdf0e10cSrcweir extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey)
208cdf0e10cSrcweir {
209cdf0e10cSrcweir void * pRet = 0;
210cdf0e10cSrcweir
211cdf0e10cSrcweir if (rtl_str_compare( pImplName, IMPLNAME ) == 0)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir Reference< XSingleServiceFactory > xFactory( createSingleFactory(
214cdf0e10cSrcweir reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
215cdf0e10cSrcweir OUString( RTL_CONSTASCII_USTRINGPARAM(IMPLNAME) ),
216cdf0e10cSrcweir MyCounterImpl_create,
217cdf0e10cSrcweir MyCounterImpl::getSupportedServiceNames_Static() ) );
218cdf0e10cSrcweir
219cdf0e10cSrcweir if (xFactory.is())
220cdf0e10cSrcweir {
221cdf0e10cSrcweir xFactory->acquire();
222cdf0e10cSrcweir pRet = xFactory.get();
223cdf0e10cSrcweir }
224cdf0e10cSrcweir }
225cdf0e10cSrcweir
226cdf0e10cSrcweir return pRet;
227cdf0e10cSrcweir }
228