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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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
;
71 // to obtain other services if needed
72 Reference
< XMultiServiceFactory
> m_xServiceManager
;
74 sal_Int32 m_nRefCount
;
78 MyCounterImpl( const Reference
< XMultiServiceFactory
> & xServiceManager
)
79 : m_xServiceManager( xServiceManager
), m_nRefCount( 0 )
80 { std::cout
<< "< MyCounterImpl ctor called >" << std::endl
; }
82 { std::cout
<< "< MyCounterImpl dtor called >" << std::endl
; }
84 // XInterface implementation
85 virtual void SAL_CALL
acquire() SAL_NOEXCEPT
87 virtual void SAL_CALL
release() SAL_NOEXCEPT
88 { if (! --m_nRefCount
) delete this; }
89 virtual Any SAL_CALL
queryInterface( const Type
& rType
)
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( );
97 virtual sal_Bool SAL_CALL
supportsService( const OUString
& ServiceName
);
98 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames( );
99 static Sequence
< OUString
> SAL_CALL
getSupportedServiceNames_Static( );
101 // XCountable implementation
102 virtual sal_Int32 SAL_CALL
getCount()
104 virtual void SAL_CALL
setCount( sal_Int32 nCount
)
105 { m_nCount
= nCount
; }
106 virtual sal_Int32 SAL_CALL
increment()
107 { return (++m_nCount
); }
108 virtual sal_Int32 SAL_CALL
decrement()
109 { return (--m_nCount
); }
112 OUString SAL_CALL
MyCounterImpl::getImplementationName( )
114 return OUString( IMPLNAME
);
117 sal_Bool SAL_CALL
MyCounterImpl::supportsService( const OUString
& ServiceName
)
119 return cppu::supportsService(this, ServiceName
);
122 Sequence
<OUString
> SAL_CALL
MyCounterImpl::getSupportedServiceNames( )
124 return getSupportedServiceNames_Static();
127 Sequence
<OUString
> SAL_CALL
MyCounterImpl::getSupportedServiceNames_Static( )
129 OUString
aName( SERVICENAME
);
130 return Sequence
< OUString
>( &aName
, 1 );
134 * Function to create a new component instance; is needed by factory helper implementation.
135 * @param xMgr service manager to if the components needs other component instances
137 Reference
< XInterface
> SAL_CALL
MyCounterImpl_create(
138 const Reference
< XMultiServiceFactory
> & xMgr
)
140 return Reference
<XInterface
>(static_cast<XCountable
*>(new MyCounterImpl(xMgr
)));
144 * This function is called to get service factories for an implementation.
146 * @param pImplName name of implementation
147 * @param pServiceManager a service manager, need for component creation
148 * @param pRegistryKey the registry key for this component, need for persistent data
149 * @return a component factory
151 extern "C" SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(const char * pImplName
, void * pServiceManager
, void * pRegistryKey
)
155 if (rtl_str_compare( pImplName
, IMPLNAME
) == 0)
157 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
158 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
159 OUString( IMPLNAME
),
160 MyCounterImpl_create
,
161 MyCounterImpl::getSupportedServiceNames_Static() ) );
166 pRet
= xFactory
.get();
173 extern "C" SAL_DLLPUBLIC_EXPORT
void SAL_CALL
174 component_getImplementationEnvironment(
175 char const ** ppEnvTypeName
, uno_Environment
**)
177 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */