Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / cpp / counter / counter.cxx
blob86f8d87b6271c6e245eed8dca84240c27476e724
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 * service implementation: foo.Counter
38 * exported interfaces: foo.XCounter
40 * simple example component implementing a counter
43 #include <stdio.h>
44 #include <rtl/ustring.hxx>
45 #include <uno/lbnames.h>
46 #include <cppuhelper/queryinterface.hxx>
47 #include <cppuhelper/factory.hxx>
48 #include <cppuhelper/supportsservice.hxx>
49 // generated c++ interfaces
50 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
51 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52 #include <com/sun/star/lang/XServiceInfo.hpp>
53 #include <com/sun/star/registry/XRegistryKey.hpp>
54 #include <foo/XCountable.hpp>
56 #define SERVICENAME "foo.Counter"
57 #define IMPLNAME "com.sun.star.comp.example.cpp.Counter"
59 using namespace ::rtl;
60 using namespace ::osl;
61 using namespace ::cppu;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::registry;
65 using namespace ::foo;
67 class MyCounterImpl
68 : public XCountable
69 , public XServiceInfo
71 // to obtain other services if needed
72 Reference< XMultiServiceFactory > m_xServiceManager;
74 sal_Int32 m_nRefCount;
75 sal_Int32 m_nCount;
77 public:
78 MyCounterImpl( const Reference< XMultiServiceFactory > & xServiceManager )
79 : m_xServiceManager( xServiceManager ), m_nRefCount( 0 )
80 { printf( "< MyCounterImpl ctor called >\n" ); }
81 ~MyCounterImpl()
82 { printf( "< MyCounterImpl dtor called >\n" ); }
84 // XInterface implementation
85 virtual void SAL_CALL acquire() throw ()
86 { ++m_nRefCount; }
87 virtual void SAL_CALL release() throw ()
88 { if (! --m_nRefCount) delete this; }
89 virtual Any SAL_CALL queryInterface( const Type & rType ) throw (RuntimeException)
90 { return cppu::queryInterface(rType,
91 static_cast< XInterface* >( static_cast< XServiceInfo* >( this ) ),
92 static_cast< XCountable* >( this ),
93 static_cast< XServiceInfo* >( this ) ); }
95 // XServiceInfo implementation
96 virtual OUString SAL_CALL getImplementationName( ) throw(RuntimeException);
97 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) throw(RuntimeException);
98 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames( ) throw(RuntimeException);
99 static Sequence< OUString > SAL_CALL getSupportedServiceNames_Static( );
101 // XCountable implementation
102 virtual sal_Int32 SAL_CALL getCount() throw (RuntimeException)
103 { return m_nCount; }
104 virtual void SAL_CALL setCount( sal_Int32 nCount ) throw (RuntimeException)
105 { m_nCount = nCount; }
106 virtual sal_Int32 SAL_CALL increment() throw (RuntimeException)
107 { return (++m_nCount); }
108 virtual sal_Int32 SAL_CALL decrement() throw (RuntimeException)
109 { return (--m_nCount); }
112 OUString SAL_CALL MyCounterImpl::getImplementationName( )
113 throw(RuntimeException)
115 return OUString( IMPLNAME );
118 sal_Bool SAL_CALL MyCounterImpl::supportsService( const OUString& ServiceName )
119 throw(RuntimeException)
121 return cppu::supportsService(this, ServiceName);
124 Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames( )
125 throw(RuntimeException)
127 return getSupportedServiceNames_Static();
130 Sequence<OUString> SAL_CALL MyCounterImpl::getSupportedServiceNames_Static( )
132 OUString aName( SERVICENAME );
133 return Sequence< OUString >( &aName, 1 );
137 * Function to create a new component instance; is needed by factory helper implementation.
138 * @param xMgr service manager to if the components needs other component instances
140 Reference< XInterface > SAL_CALL MyCounterImpl_create(
141 const Reference< XMultiServiceFactory > & xMgr )
143 return Reference<XInterface>(static_cast<XCountable*>(new MyCounterImpl(xMgr)));
147 * This function is called to get service factories for an implementation.
149 * @param pImplName name of implementation
150 * @param pServiceManager a service manager, need for component creation
151 * @param pRegistryKey the registry key for this component, need for persistent data
152 * @return a component factory
154 extern "C" SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey)
156 void * pRet = 0;
158 if (rtl_str_compare( pImplName, IMPLNAME ) == 0)
160 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
161 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
162 OUString( IMPLNAME ),
163 MyCounterImpl_create,
164 MyCounterImpl::getSupportedServiceNames_Static() ) );
166 if (xFactory.is())
168 xFactory->acquire();
169 pRet = xFactory.get();
173 return pRet;
176 extern "C" SAL_DLLPUBLIC_EXPORT void SAL_CALL
177 component_getImplementationEnvironment(
178 char const ** ppEnvTypeName, uno_Environment **)
180 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
183 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */