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