1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppuhelper.hxx"
30 
31 #include "sal/config.h"
32 
33 #include "test/cppuhelper/propertysetmixin/XSupplier.hpp"
34 #include "test/cppuhelper/propertysetmixin/XTest3.hpp"
35 
36 #include "com/sun/star/beans/Ambiguous.hpp"
37 #include "com/sun/star/beans/Defaulted.hpp"
38 #include "com/sun/star/beans/Optional.hpp"
39 #include "com/sun/star/beans/PropertyVetoException.hpp"
40 #include "com/sun/star/beans/UnknownPropertyException.hpp"
41 #include "com/sun/star/lang/XComponent.hpp"
42 #include "cppuhelper/propertysetmixin.hxx"
43 #include "cppuhelper/factory.hxx"
44 #include "cppuhelper/implbase1.hxx"
45 #include "cppuhelper/implementationentry.hxx"
46 #include "cppuhelper/queryinterface.hxx"
47 #include "cppuhelper/weak.hxx"
48 #include "com/sun/star/uno/Any.hxx"
49 #include "com/sun/star/uno/Exception.hpp"
50 #include "com/sun/star/uno/Reference.hxx"
51 #include "com/sun/star/uno/RuntimeException.hpp"
52 #include "com/sun/star/uno/Sequence.hxx"
53 #include "com/sun/star/uno/Type.hxx"
54 #include "com/sun/star/uno/XComponentContext.hpp"
55 #include "com/sun/star/uno/XInterface.hpp"
56 #include "osl/mutex.hxx"
57 #include "rtl/ustring.h"
58 #include "rtl/ustring.hxx"
59 #include "sal/types.h"
60 #include "uno/lbnames.h"
61 
62 namespace com { namespace sun { namespace star {
63     class XEventListener;
64 } } }
65 
66 namespace css = com::sun::star;
67 
68 namespace {
69 
70 class Empty1:
71     public cppu::OWeakObject, public css::lang::XComponent,
72     public cppu::PropertySetMixin< css::lang::XComponent >
73 {
74 public:
75     explicit Empty1(
76         css::uno::Reference< css::uno::XComponentContext > const & context):
77         cppu::PropertySetMixin< css::lang::XComponent >(
78             context, static_cast< Implements >(0),
79             css::uno::Sequence< rtl::OUString >())
80     {}
81 
82     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
83         throw (css::uno::RuntimeException);
84 
85     virtual void SAL_CALL acquire() throw () { OWeakObject::acquire(); }
86 
87     virtual void SAL_CALL release() throw () { OWeakObject::release(); }
88 
89     virtual void SAL_CALL dispose() throw (css::uno::RuntimeException) {
90         cppu::PropertySetMixin< css::lang::XComponent >::dispose();
91     }
92 
93     virtual void SAL_CALL addEventListener(
94         css::uno::Reference< css::lang::XEventListener > const &)
95         throw (css::uno::RuntimeException)
96     {}
97 
98     virtual void SAL_CALL removeEventListener(
99         css::uno::Reference< css::lang::XEventListener > const &)
100         throw (css::uno::RuntimeException)
101     {}
102 
103 private:
104     Empty1(Empty1 &); // not defined
105     void operator =(Empty1 &); // not defined
106 
107     virtual ~Empty1() {}
108 };
109 
110 css::uno::Any Empty1::queryInterface(css::uno::Type const & type)
111     throw (css::uno::RuntimeException)
112 {
113     css::uno::Any a(OWeakObject::queryInterface(type));
114     if (a.hasValue()) {
115         return a;
116     }
117     a = cppu::queryInterface(
118         type, static_cast< css::lang::XComponent * >(this));
119     return a.hasValue()
120         ? a
121         : cppu::PropertySetMixin< css::lang::XComponent >::queryInterface(
122             type);
123 }
124 
125 class Empty2:
126     public cppu::OWeakObject, public css::lang::XComponent,
127     public cppu::PropertySetMixin< css::lang::XComponent >
128 {
129 public:
130     explicit Empty2(
131         css::uno::Reference< css::uno::XComponentContext > const & context):
132         cppu::PropertySetMixin< css::lang::XComponent >(
133             context,
134             static_cast< Implements >(
135                 IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_FAST_PROPERTY_SET
136                 | IMPLEMENTS_PROPERTY_ACCESS),
137             css::uno::Sequence< rtl::OUString >())
138     {}
139 
140     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
141         throw (css::uno::RuntimeException);
142 
143     virtual void SAL_CALL acquire() throw () { OWeakObject::acquire(); }
144 
145     virtual void SAL_CALL release() throw () { OWeakObject::release(); }
146 
147     virtual void SAL_CALL dispose() throw (css::uno::RuntimeException) {
148         cppu::PropertySetMixin< css::lang::XComponent >::dispose();
149     }
150 
151     virtual void SAL_CALL addEventListener(
152         css::uno::Reference< css::lang::XEventListener > const &)
153         throw (css::uno::RuntimeException)
154     {}
155 
156     virtual void SAL_CALL removeEventListener(
157         css::uno::Reference< css::lang::XEventListener > const &)
158         throw (css::uno::RuntimeException)
159     {}
160 
161 private:
162     Empty2(Empty2 &); // not defined
163     void operator =(Empty2 &); // not defined
164 
165     virtual ~Empty2() {}
166 };
167 
168 css::uno::Any Empty2::queryInterface(css::uno::Type const & type)
169     throw (css::uno::RuntimeException)
170 {
171     css::uno::Any a(OWeakObject::queryInterface(type));
172     if (a.hasValue()) {
173         return a;
174     }
175     a = cppu::queryInterface(
176         type, static_cast< css::lang::XComponent * >(this));
177     return a.hasValue()
178         ? a
179         : cppu::PropertySetMixin< css::lang::XComponent >::queryInterface(
180             type);
181 }
182 
183 css::uno::Sequence< rtl::OUString > sequenceThird() {
184     css::uno::Sequence< rtl::OUString > s(1);
185     s[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Third"));
186     return s;
187 }
188 
189 class Full:
190     public cppu::OWeakObject, public test::cppuhelper::propertysetmixin::XTest3,
191     public cppu::PropertySetMixin<
192     test::cppuhelper::propertysetmixin::XTest3 >
193 {
194 public:
195     explicit Full(
196         css::uno::Reference< css::uno::XComponentContext > const & context):
197         cppu::PropertySetMixin<
198         test::cppuhelper::propertysetmixin::XTest3 >(
199             context,
200             static_cast< Implements >(
201                 IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_FAST_PROPERTY_SET
202                 | IMPLEMENTS_PROPERTY_ACCESS),
203             sequenceThird()),
204         m_a1(0),
205         m_a2(
206             css::beans::Defaulted< css::beans::Optional< sal_Int32 > >(
207                 css::beans::Optional< sal_Int32 >(), true),
208             false)
209     {}
210 
211     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type)
212         throw (css::uno::RuntimeException);
213 
214     virtual void SAL_CALL acquire() throw () { OWeakObject::acquire(); }
215 
216     virtual void SAL_CALL release() throw () { OWeakObject::release(); }
217 
218     virtual sal_Int32 SAL_CALL getFirst() throw (css::uno::RuntimeException);
219 
220     virtual void SAL_CALL setFirst(sal_Int32 value)
221         throw (css::uno::RuntimeException);
222 
223     virtual
224     css::beans::Ambiguous<
225         css::beans::Defaulted< css::beans::Optional< sal_Int32 > > >
226     SAL_CALL getSecond()
227         throw (
228             css::beans::UnknownPropertyException, css::uno::RuntimeException);
229 
230     virtual void SAL_CALL setSecond(
231         css::beans::Ambiguous<
232         css::beans::Defaulted< css::beans::Optional< ::sal_Int32 > > > const &
233         value)
234         throw (
235             css::beans::PropertyVetoException,
236             css::beans::UnknownPropertyException, css::uno::RuntimeException);
237 
238     virtual sal_Int32 SAL_CALL getThird()
239         throw (
240             css::beans::UnknownPropertyException, css::uno::RuntimeException);
241 
242     virtual void SAL_CALL setThird(sal_Int32 value)
243         throw (
244             css::beans::UnknownPropertyException, css::uno::RuntimeException);
245 
246     virtual sal_Int32 SAL_CALL getFourth()
247         throw (
248             css::beans::UnknownPropertyException, css::uno::RuntimeException);
249 
250     virtual void SAL_CALL setFourth(sal_Int32 value)
251         throw (
252             css::beans::UnknownPropertyException, css::uno::RuntimeException);
253 
254 private:
255     Full(Full &); // not defined
256     void operator =(Full &); // not defined
257 
258     virtual ~Full() {}
259 
260     osl::Mutex m_mutex;
261     sal_Int32 m_a1;
262     css::beans::Ambiguous<
263         css::beans::Defaulted< css::beans::Optional< sal_Int32 > > > m_a2;
264 };
265 
266 css::uno::Any Full::queryInterface(css::uno::Type const & type)
267     throw (css::uno::RuntimeException)
268 {
269     css::uno::Any a(OWeakObject::queryInterface(type));
270     if (a.hasValue()) {
271         return a;
272     }
273     a = cppu::queryInterface(
274         type,
275         static_cast< test::cppuhelper::propertysetmixin::XTest3 * >(this));
276     return a.hasValue()
277         ? a
278         : (cppu::PropertySetMixin<
279            test::cppuhelper::propertysetmixin::XTest3 >::queryInterface(
280                type));
281 }
282 
283 sal_Int32 Full::getFirst() throw (css::uno::RuntimeException) {
284     osl::MutexGuard g(m_mutex);
285     return m_a1;
286 }
287 
288 void Full::setFirst(sal_Int32 value) throw (css::uno::RuntimeException) {
289     prepareSet(
290         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("First")), css::uno::Any(),
291         css::uno::Any(), 0);
292     osl::MutexGuard g(m_mutex);
293     m_a1 = value;
294 }
295 
296 css::beans::Ambiguous<
297     css::beans::Defaulted< css::beans::Optional< sal_Int32 > > >
298 Full::getSecond()
299     throw (css::beans::UnknownPropertyException, css::uno::RuntimeException)
300 {
301     osl::MutexGuard g(m_mutex);
302     return m_a2;
303 }
304 
305 void Full::setSecond(
306     css::beans::Ambiguous<
307     css::beans::Defaulted< css::beans::Optional< ::sal_Int32 > > > const &
308     value)
309     throw (
310         css::beans::PropertyVetoException, css::beans::UnknownPropertyException,
311         css::uno::RuntimeException)
312 {
313     css::uno::Any v;
314     if (value.Value.Value.IsPresent) {
315         v <<= value.Value.Value.Value;
316     }
317     BoundListeners l;
318     prepareSet(
319         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Second")), css::uno::Any(),
320         v, &l);
321     {
322         osl::MutexGuard g(m_mutex);
323         m_a2 = value;
324     }
325     l.notify();
326 }
327 
328 sal_Int32 Full::getThird()
329     throw (css::beans::UnknownPropertyException, css::uno::RuntimeException)
330 {
331     throw css::beans::UnknownPropertyException(
332         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Third")),
333         static_cast< cppu::OWeakObject * >(this));
334 }
335 
336 void Full::setThird(sal_Int32)
337     throw (css::beans::UnknownPropertyException, css::uno::RuntimeException)
338 {
339     throw css::beans::UnknownPropertyException(
340         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Third")),
341         static_cast< cppu::OWeakObject * >(this));
342 }
343 
344 sal_Int32 Full::getFourth()
345     throw (css::beans::UnknownPropertyException, css::uno::RuntimeException)
346 {
347     throw css::beans::UnknownPropertyException(
348         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Fourth")),
349         static_cast< cppu::OWeakObject * >(this));
350 }
351 
352 void Full::setFourth(sal_Int32)
353     throw (css::beans::UnknownPropertyException, css::uno::RuntimeException)
354 {
355     throw css::beans::UnknownPropertyException(
356         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Fourth")),
357         static_cast< cppu::OWeakObject * >(this));
358 }
359 
360 class Supplier:
361     public cppu::WeakImplHelper1<
362     test::cppuhelper::propertysetmixin::XSupplier >
363 {
364 public:
365     explicit Supplier(
366         css::uno::Reference< css::uno::XComponentContext > const & context):
367         m_context(context) {}
368 
369     virtual css::uno::Reference< css::lang::XComponent > SAL_CALL getEmpty1()
370         throw (css::uno::RuntimeException)
371     { return new Empty1(m_context); }
372 
373     virtual css::uno::Reference< css::lang::XComponent > SAL_CALL getEmpty2()
374         throw (css::uno::RuntimeException)
375     { return new Empty2(m_context); }
376 
377     virtual css::uno::Reference< test::cppuhelper::propertysetmixin::XTest3 >
378     SAL_CALL getFull() throw (css::uno::RuntimeException)
379     { return new Full(m_context); }
380 
381 private:
382     Supplier(Supplier &); // not defined
383     void operator =(Supplier &); // not defined
384 
385     virtual ~Supplier() {}
386 
387     css::uno::Reference< css::uno::XComponentContext > m_context;
388 };
389 
390 css::uno::Reference< css::uno::XInterface > SAL_CALL create(
391     css::uno::Reference< css::uno::XComponentContext > const & context)
392     SAL_THROW((css::uno::Exception))
393 {
394     return static_cast< cppu::OWeakObject * >(new Supplier(context));
395 }
396 
397 rtl::OUString SAL_CALL getImplementationName() {
398     return rtl::OUString(
399         RTL_CONSTASCII_USTRINGPARAM(
400             "test.cppuhelper.propertysetmixin.comp.CppSupplier"));
401 }
402 
403 css::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() {
404     css::uno::Sequence< rtl::OUString > s(1);
405     s[0] = rtl::OUString(
406         RTL_CONSTASCII_USTRINGPARAM(
407             "test.cppuhelper.propertysetmixin.CppSupplier"));
408     return s;
409 }
410 
411 cppu::ImplementationEntry entries[] = {
412     { &create, &getImplementationName, &getSupportedServiceNames,
413       &cppu::createSingleComponentFactory, 0, 0 },
414     { 0, 0, 0, 0, 0, 0 } };
415 
416 }
417 
418 extern "C" void * SAL_CALL component_getFactory(
419     char const * implName, void * serviceManager, void * registryKey)
420 {
421     return cppu::component_getFactoryHelper(
422         implName, serviceManager, registryKey, entries);
423 }
424 
425 extern "C" void SAL_CALL component_getImplementationEnvironment(
426     char const ** envTypeName, uno_Environment **)
427 {
428     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
429 }
430