xref: /trunk/main/cppuhelper/qa/propertysetmixin/comp_propertysetmixin.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cppuhelper.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "test/cppuhelper/propertysetmixin/XSupplier.hpp"
30 #include "test/cppuhelper/propertysetmixin/XTest3.hpp"
31 
32 #include "com/sun/star/beans/Ambiguous.hpp"
33 #include "com/sun/star/beans/Defaulted.hpp"
34 #include "com/sun/star/beans/Optional.hpp"
35 #include "com/sun/star/beans/PropertyVetoException.hpp"
36 #include "com/sun/star/beans/UnknownPropertyException.hpp"
37 #include "com/sun/star/lang/XComponent.hpp"
38 #include "cppuhelper/propertysetmixin.hxx"
39 #include "cppuhelper/factory.hxx"
40 #include "cppuhelper/implbase1.hxx"
41 #include "cppuhelper/implementationentry.hxx"
42 #include "cppuhelper/queryinterface.hxx"
43 #include "cppuhelper/weak.hxx"
44 #include "com/sun/star/uno/Any.hxx"
45 #include "com/sun/star/uno/Exception.hpp"
46 #include "com/sun/star/uno/Reference.hxx"
47 #include "com/sun/star/uno/RuntimeException.hpp"
48 #include "com/sun/star/uno/Sequence.hxx"
49 #include "com/sun/star/uno/Type.hxx"
50 #include "com/sun/star/uno/XComponentContext.hpp"
51 #include "com/sun/star/uno/XInterface.hpp"
52 #include "osl/mutex.hxx"
53 #include "rtl/ustring.h"
54 #include "rtl/ustring.hxx"
55 #include "sal/types.h"
56 #include "uno/lbnames.h"
57 
58 namespace com { namespace sun { namespace star {
59     class XEventListener;
60 } } }
61 
62 namespace css = com::sun::star;
63 
64 namespace {
65 
66 class Empty1:
67     public cppu::OWeakObject, public css::lang::XComponent,
68     public cppu::PropertySetMixin< css::lang::XComponent >
69 {
70 public:
Empty1(css::uno::Reference<css::uno::XComponentContext> const & context)71     explicit Empty1(
72         css::uno::Reference< css::uno::XComponentContext > const & context):
73         cppu::PropertySetMixin< css::lang::XComponent >(
74             context, static_cast< Implements >(0),
75             css::uno::Sequence< rtl::OUString >())
76     {}
77 
78     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type);
79 
acquire()80     virtual void SAL_CALL acquire() throw () { OWeakObject::acquire(); }
81 
release()82     virtual void SAL_CALL release() throw () { OWeakObject::release(); }
83 
dispose()84     virtual void SAL_CALL dispose() {
85         cppu::PropertySetMixin< css::lang::XComponent >::dispose();
86     }
87 
addEventListener(css::uno::Reference<css::lang::XEventListener> const &)88     virtual void SAL_CALL addEventListener(
89         css::uno::Reference< css::lang::XEventListener > const &)
90     {}
91 
removeEventListener(css::uno::Reference<css::lang::XEventListener> const &)92     virtual void SAL_CALL removeEventListener(
93         css::uno::Reference< css::lang::XEventListener > const &)
94     {}
95 
96 private:
97     Empty1(Empty1 &); // not defined
98     void operator =(Empty1 &); // not defined
99 
~Empty1()100     virtual ~Empty1() {}
101 };
102 
queryInterface(css::uno::Type const & type)103 css::uno::Any Empty1::queryInterface(css::uno::Type const & type)
104 {
105     css::uno::Any a(OWeakObject::queryInterface(type));
106     if (a.hasValue()) {
107         return a;
108     }
109     a = cppu::queryInterface(
110         type, static_cast< css::lang::XComponent * >(this));
111     return a.hasValue()
112         ? a
113         : cppu::PropertySetMixin< css::lang::XComponent >::queryInterface(
114             type);
115 }
116 
117 class Empty2:
118     public cppu::OWeakObject, public css::lang::XComponent,
119     public cppu::PropertySetMixin< css::lang::XComponent >
120 {
121 public:
Empty2(css::uno::Reference<css::uno::XComponentContext> const & context)122     explicit Empty2(
123         css::uno::Reference< css::uno::XComponentContext > const & context):
124         cppu::PropertySetMixin< css::lang::XComponent >(
125             context,
126             static_cast< Implements >(
127                 IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_FAST_PROPERTY_SET
128                 | IMPLEMENTS_PROPERTY_ACCESS),
129             css::uno::Sequence< rtl::OUString >())
130     {}
131 
132     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type);
133 
acquire()134     virtual void SAL_CALL acquire() throw () { OWeakObject::acquire(); }
135 
release()136     virtual void SAL_CALL release() throw () { OWeakObject::release(); }
137 
dispose()138     virtual void SAL_CALL dispose() {
139         cppu::PropertySetMixin< css::lang::XComponent >::dispose();
140     }
141 
addEventListener(css::uno::Reference<css::lang::XEventListener> const &)142     virtual void SAL_CALL addEventListener(
143         css::uno::Reference< css::lang::XEventListener > const &)
144     {}
145 
removeEventListener(css::uno::Reference<css::lang::XEventListener> const &)146     virtual void SAL_CALL removeEventListener(
147         css::uno::Reference< css::lang::XEventListener > const &)
148     {}
149 
150 private:
151     Empty2(Empty2 &); // not defined
152     void operator =(Empty2 &); // not defined
153 
~Empty2()154     virtual ~Empty2() {}
155 };
156 
queryInterface(css::uno::Type const & type)157 css::uno::Any Empty2::queryInterface(css::uno::Type const & type)
158 {
159     css::uno::Any a(OWeakObject::queryInterface(type));
160     if (a.hasValue()) {
161         return a;
162     }
163     a = cppu::queryInterface(
164         type, static_cast< css::lang::XComponent * >(this));
165     return a.hasValue()
166         ? a
167         : cppu::PropertySetMixin< css::lang::XComponent >::queryInterface(
168             type);
169 }
170 
sequenceThird()171 css::uno::Sequence< rtl::OUString > sequenceThird() {
172     css::uno::Sequence< rtl::OUString > s(1);
173     s[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Third"));
174     return s;
175 }
176 
177 class Full:
178     public cppu::OWeakObject, public test::cppuhelper::propertysetmixin::XTest3,
179     public cppu::PropertySetMixin<
180     test::cppuhelper::propertysetmixin::XTest3 >
181 {
182 public:
Full(css::uno::Reference<css::uno::XComponentContext> const & context)183     explicit Full(
184         css::uno::Reference< css::uno::XComponentContext > const & context):
185         cppu::PropertySetMixin<
186         test::cppuhelper::propertysetmixin::XTest3 >(
187             context,
188             static_cast< Implements >(
189                 IMPLEMENTS_PROPERTY_SET | IMPLEMENTS_FAST_PROPERTY_SET
190                 | IMPLEMENTS_PROPERTY_ACCESS),
191             sequenceThird()),
192         m_a1(0),
193         m_a2(
194             css::beans::Defaulted< css::beans::Optional< sal_Int32 > >(
195                 css::beans::Optional< sal_Int32 >(), true),
196             false)
197     {}
198 
199     virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const & type);
200 
acquire()201     virtual void SAL_CALL acquire() throw () { OWeakObject::acquire(); }
202 
release()203     virtual void SAL_CALL release() throw () { OWeakObject::release(); }
204 
205     virtual sal_Int32 SAL_CALL getFirst();
206 
207     virtual void SAL_CALL setFirst(sal_Int32 value);
208 
209     virtual
210     css::beans::Ambiguous<
211         css::beans::Defaulted< css::beans::Optional< sal_Int32 > > >
212     SAL_CALL getSecond();
213 
214     virtual void SAL_CALL setSecond(
215         css::beans::Ambiguous<
216         css::beans::Defaulted< css::beans::Optional< ::sal_Int32 > > > const &
217         value);
218 
219     virtual sal_Int32 SAL_CALL getThird();
220 
221     virtual void SAL_CALL setThird(sal_Int32 value);
222 
223     virtual sal_Int32 SAL_CALL getFourth();
224 
225     virtual void SAL_CALL setFourth(sal_Int32 value);
226 
227 private:
228     Full(Full &); // not defined
229     void operator =(Full &); // not defined
230 
~Full()231     virtual ~Full() {}
232 
233     osl::Mutex m_mutex;
234     sal_Int32 m_a1;
235     css::beans::Ambiguous<
236         css::beans::Defaulted< css::beans::Optional< sal_Int32 > > > m_a2;
237 };
238 
queryInterface(css::uno::Type const & type)239 css::uno::Any Full::queryInterface(css::uno::Type const & type)
240 {
241     css::uno::Any a(OWeakObject::queryInterface(type));
242     if (a.hasValue()) {
243         return a;
244     }
245     a = cppu::queryInterface(
246         type,
247         static_cast< test::cppuhelper::propertysetmixin::XTest3 * >(this));
248     return a.hasValue()
249         ? a
250         : (cppu::PropertySetMixin<
251            test::cppuhelper::propertysetmixin::XTest3 >::queryInterface(
252                type));
253 }
254 
getFirst()255 sal_Int32 Full::getFirst() {
256     osl::MutexGuard g(m_mutex);
257     return m_a1;
258 }
259 
setFirst(sal_Int32 value)260 void Full::setFirst(sal_Int32 value) {
261     prepareSet(
262         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("First")), css::uno::Any(),
263         css::uno::Any(), 0);
264     osl::MutexGuard g(m_mutex);
265     m_a1 = value;
266 }
267 
268 css::beans::Ambiguous<
269     css::beans::Defaulted< css::beans::Optional< sal_Int32 > > >
getSecond()270 Full::getSecond()
271 {
272     osl::MutexGuard g(m_mutex);
273     return m_a2;
274 }
275 
setSecond(css::beans::Ambiguous<css::beans::Defaulted<css::beans::Optional<::sal_Int32>>> const & value)276 void Full::setSecond(
277     css::beans::Ambiguous<
278     css::beans::Defaulted< css::beans::Optional< ::sal_Int32 > > > const &
279     value)
280 {
281     css::uno::Any v;
282     if (value.Value.Value.IsPresent) {
283         v <<= value.Value.Value.Value;
284     }
285     BoundListeners l;
286     prepareSet(
287         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Second")), css::uno::Any(),
288         v, &l);
289     {
290         osl::MutexGuard g(m_mutex);
291         m_a2 = value;
292     }
293     l.notify();
294 }
295 
getThird()296 sal_Int32 Full::getThird()
297 {
298     throw css::beans::UnknownPropertyException(
299         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Third")),
300         static_cast< cppu::OWeakObject * >(this));
301 }
302 
setThird(sal_Int32)303 void Full::setThird(sal_Int32)
304 {
305     throw css::beans::UnknownPropertyException(
306         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Third")),
307         static_cast< cppu::OWeakObject * >(this));
308 }
309 
getFourth()310 sal_Int32 Full::getFourth()
311 {
312     throw css::beans::UnknownPropertyException(
313         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Fourth")),
314         static_cast< cppu::OWeakObject * >(this));
315 }
316 
setFourth(sal_Int32)317 void Full::setFourth(sal_Int32)
318 {
319     throw css::beans::UnknownPropertyException(
320         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Fourth")),
321         static_cast< cppu::OWeakObject * >(this));
322 }
323 
324 class Supplier:
325     public cppu::WeakImplHelper1<
326     test::cppuhelper::propertysetmixin::XSupplier >
327 {
328 public:
Supplier(css::uno::Reference<css::uno::XComponentContext> const & context)329     explicit Supplier(
330         css::uno::Reference< css::uno::XComponentContext > const & context):
331         m_context(context) {}
332 
getEmpty1()333     virtual css::uno::Reference< css::lang::XComponent > SAL_CALL getEmpty1()
334     { return new Empty1(m_context); }
335 
getEmpty2()336     virtual css::uno::Reference< css::lang::XComponent > SAL_CALL getEmpty2()
337     { return new Empty2(m_context); }
338 
339     virtual css::uno::Reference< test::cppuhelper::propertysetmixin::XTest3 >
getFull()340     SAL_CALL getFull()
341     { return new Full(m_context); }
342 
343 private:
344     Supplier(Supplier &); // not defined
345     void operator =(Supplier &); // not defined
346 
~Supplier()347     virtual ~Supplier() {}
348 
349     css::uno::Reference< css::uno::XComponentContext > m_context;
350 };
351 
create(css::uno::Reference<css::uno::XComponentContext> const & context)352 css::uno::Reference< css::uno::XInterface > SAL_CALL create(
353     css::uno::Reference< css::uno::XComponentContext > const & context)
354 {
355     return static_cast< cppu::OWeakObject * >(new Supplier(context));
356 }
357 
getImplementationName()358 rtl::OUString SAL_CALL getImplementationName() {
359     return rtl::OUString(
360         RTL_CONSTASCII_USTRINGPARAM(
361             "test.cppuhelper.propertysetmixin.comp.CppSupplier"));
362 }
363 
getSupportedServiceNames()364 css::uno::Sequence< rtl::OUString > SAL_CALL getSupportedServiceNames() {
365     css::uno::Sequence< rtl::OUString > s(1);
366     s[0] = rtl::OUString(
367         RTL_CONSTASCII_USTRINGPARAM(
368             "test.cppuhelper.propertysetmixin.CppSupplier"));
369     return s;
370 }
371 
372 cppu::ImplementationEntry entries[] = {
373     { &create, &getImplementationName, &getSupportedServiceNames,
374       &cppu::createSingleComponentFactory, 0, 0 },
375     { 0, 0, 0, 0, 0, 0 } };
376 
377 }
378 
component_getFactory(char const * implName,void * serviceManager,void * registryKey)379 extern "C" void * SAL_CALL component_getFactory(
380     char const * implName, void * serviceManager, void * registryKey)
381 {
382     return cppu::component_getFactoryHelper(
383         implName, serviceManager, registryKey, entries);
384 }
385 
component_getImplementationEnvironment(char const ** envTypeName,uno_Environment **)386 extern "C" void SAL_CALL component_getImplementationEnvironment(
387     char const ** envTypeName, uno_Environment **)
388 {
389     *envTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
390 }
391