Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / odk / examples / DevelopersGuide / Components / CppComponent / service2_impl.cxx
blob5fa2e5af13315e6160cf7729167eba47a4a584ec
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 *************************************************************************/
36 #include <cppuhelper/implbase3.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/implementationentry.hxx>
39 #include <cppuhelper/supportsservice.hxx>
40 #include <uno/lbnames.h>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/lang/XInitialization.hpp>
44 #include <com/sun/star/lang/IllegalArgumentException.hpp>
45 #include <my_module/XSomething.hpp>
48 using namespace ::rtl; // for OUString
49 using namespace ::com::sun::star; // for odk interfaces
50 using namespace ::com::sun::star::uno; // for basic types
53 namespace my_sc_impl
56 extern Sequence< OUString > SAL_CALL getSupportedServiceNames_MyService1Impl();
57 extern OUString SAL_CALL getImplementationName_MyService1Impl();
58 extern Reference< XInterface > SAL_CALL create_MyService1Impl(
59 Reference< XComponentContext > const & xContext );
61 static Sequence< OUString > getSupportedServiceNames_MyService2Impl()
63 Sequence<OUString> names(1);
64 names[0] = "my_module.MyService2";
65 return names;
68 static OUString getImplementationName_MyService2Impl()
70 return OUString("my_module.my_sc_implementation.MyService2");
73 class MyService2Impl : public ::cppu::WeakImplHelper3<
74 ::my_module::XSomething, lang::XServiceInfo, lang::XInitialization >
76 OUString m_sData;
77 // it's good practise to store the context for further use when you use
78 // other UNO API's in your implementation
79 Reference< XComponentContext > m_xContext;
80 public:
81 inline MyService2Impl(Reference< XComponentContext > const & xContext) throw ()
82 : m_xContext(xContext)
85 virtual ~MyService2Impl() {}
87 // focus on three given interfaces,
88 // no need to implement XInterface, XTypeProvider, XWeak
90 // XInitialization will be called upon
91 // createInstanceWithArguments[AndContext]()
92 virtual void SAL_CALL initialize( Sequence< Any > const & args )
93 throw (Exception);
94 // XSomething
95 virtual OUString SAL_CALL methodOne( OUString const & str )
96 throw (RuntimeException);
97 virtual OUString SAL_CALL methodTwo( )
98 throw (RuntimeException);
99 // XServiceInfo
100 virtual OUString SAL_CALL getImplementationName()
101 throw (RuntimeException);
102 virtual sal_Bool SAL_CALL supportsService( OUString const & serviceName )
103 throw (RuntimeException);
104 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames()
105 throw (RuntimeException);
108 // XInitialization implementation
109 void MyService2Impl::initialize( Sequence< Any > const & args )
110 throw (Exception)
112 if (args.getLength() != 1)
114 throw lang::IllegalArgumentException(
115 OUString(
116 "give a string instantiating this component!"),
117 // resolve to XInterface reference:
118 static_cast< ::cppu::OWeakObject * >(this),
119 0 ); // argument pos
121 if (! (args[ 0 ] >>= m_sData))
123 throw lang::IllegalArgumentException(
124 OUString(
125 "no string given as argument!"),
126 // resolve to XInterface reference:
127 static_cast< ::cppu::OWeakObject * >(this),
128 0 ); // argument pos
132 // XSomething implementation
133 OUString MyService2Impl::methodOne( OUString const & str )
134 throw (RuntimeException)
136 m_sData = str;
137 return OUString( "called methodOne() of MyService2 implementation: " ) + m_sData;
140 OUString MyService2Impl::methodTwo( )
141 throw (RuntimeException)
143 return OUString( "called methodTwo() of MyService2 implementation: " ) + m_sData;
146 // XServiceInfo implementation
147 OUString MyService2Impl::getImplementationName()
148 throw (RuntimeException)
150 // unique implementation name
151 return OUString("my_module.my_sc_implementation.MyService2");
154 sal_Bool MyService2Impl::supportsService( OUString const & serviceName )
155 throw (RuntimeException)
157 return cppu::supportsService(this, serviceName);
160 Sequence< OUString > MyService2Impl::getSupportedServiceNames()
161 throw (RuntimeException)
163 return getSupportedServiceNames_MyService2Impl();
166 Reference< XInterface > SAL_CALL create_MyService2Impl(
167 Reference< XComponentContext > const & xContext )
169 return static_cast< ::cppu::OWeakObject * >( new MyService2Impl( xContext ) );
174 /* shared lib exports implemented without helpers in service_impl1.cxx */
175 namespace my_sc_impl
177 static const struct ::cppu::ImplementationEntry s_component_entries [] =
180 create_MyService1Impl, getImplementationName_MyService1Impl,
181 getSupportedServiceNames_MyService1Impl,
182 ::cppu::createSingleComponentFactory,
183 0, 0
186 create_MyService2Impl, getImplementationName_MyService2Impl,
187 getSupportedServiceNames_MyService2Impl,
188 ::cppu::createSingleComponentFactory,
189 0, 0
191 { 0, 0, 0, 0, 0, 0 }
195 extern "C"
198 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
199 sal_Char const * implName, lang::XMultiServiceFactory * xMgr,
200 registry::XRegistryKey * xRegistry )
202 return ::cppu::component_getFactoryHelper(
203 implName, xMgr, xRegistry, ::my_sc_impl::s_component_entries );
206 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
207 char const ** ppEnvTypeName, uno_Environment **)
209 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */