/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #include #include #include #include #include namespace { using ::com::sun::star::uno::Type; using ::com::sun::star::uno::Any; using ::com::sun::star::uno::Reference; using ::com::sun::star::uno::RuntimeException; using ::com::sun::star::uno::UNO_SET_THROW; class Foo: public Interface1 { public: Foo() :m_refCount(0) { } Foo(const Foo&) = delete; const Foo& operator=(const Foo&) = delete; virtual Any SAL_CALL queryInterface(const Type & _type) override { if (_type == cppu::UnoType::get()) { return css::uno::makeAny>( this); } if (_type == cppu::UnoType::get()) { return css::uno::makeAny>(this); } return Any(); } virtual void SAL_CALL acquire() noexcept override { osl_atomic_increment( &m_refCount ); } virtual void SAL_CALL release() noexcept override { if ( 0 == osl_atomic_decrement( &m_refCount ) ) delete this; } protected: virtual ~Foo() { } private: oslInterlockedCount m_refCount; }; // Check that the up-casting Reference conversion constructor catches the // intended cases: struct Base1: public css::uno::XInterface { virtual ~Base1() = delete; static ::css::uno::Type const & static_type(void * = nullptr) // loplugin:refcounting { return ::cppu::UnoType::get(); } }; struct Base2: public Base1 { virtual ~Base2() override = delete; }; struct Base3: public Base1 { virtual ~Base3() override = delete; }; struct Derived: public Base2, public Base3 { virtual ~Derived() override = delete; }; // The special case using the conversion operator instead: css::uno::Reference< css::uno::XInterface > testUpcast1( css::uno::Reference< Derived > const & ref) { Base1::static_type(); // prevent loplugin:unreffun firing return ref; } // The normal up-cast case: css::uno::Reference< Base1 > testUpcast2( css::uno::Reference< Base2 > const & ref) { return ref; } // Commenting this in should cause a compiler error due to an ambiguous up-cast: /* css::uno::Reference< Base1 > testFailingUpcast3( css::uno::Reference< Derived > const & ref) { return ref; } */ // Commenting this in should cause a compiler error due to a down-cast: /* css::uno::Reference< Base2 > testFailingUpcast4( css::uno::Reference< Base1 > const & ref) { return ref; } */ // Commenting this in should cause a compiler error due to a down-cast: /* css::uno::Reference< Base1 > testFailingUpcast5( css::uno::Reference< css::uno::XInterface > const & ref) { return ref; } */ class Test: public ::CppUnit::TestFixture { public: void testUnoSetThrow(); void testUpcastCompilation(); CPPUNIT_TEST_SUITE(Test); CPPUNIT_TEST(testUnoSetThrow); CPPUNIT_TEST(testUpcastCompilation); CPPUNIT_TEST_SUITE_END(); }; void Test::testUnoSetThrow() { Reference< Interface1 > xNull; Reference< Interface1 > xFoo( new Foo ); // ctor taking Reference< interface_type > bool bCaughtException = false; try { Reference< Interface1 > x( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( true, bCaughtException ); bCaughtException = false; try { Reference< Interface1 > x( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( false, bCaughtException ); // ctor taking interface_type* bCaughtException = false; try { Reference< Interface1 > x( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( true, bCaughtException ); bCaughtException = false; try { Reference< Interface1 > x( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( false, bCaughtException ); Reference< Interface1 > x; // "set" taking Reference< interface_type > bCaughtException = false; try { x.set( xNull, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( true, bCaughtException ); bCaughtException = false; try { x.set( xFoo, UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( false, bCaughtException ); // "set" taking interface_type* bCaughtException = false; try { x.set( xNull.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( true, bCaughtException ); bCaughtException = false; try { x.set( xFoo.get(), UNO_SET_THROW ); } catch( const RuntimeException& ) { bCaughtException = true; } CPPUNIT_ASSERT_EQUAL( false, bCaughtException ); } // Include a dummy test calling those functions, to avoid warnings about those // functions being unused: void Test::testUpcastCompilation() { testUpcast1(css::uno::Reference< Derived >()); testUpcast2(css::uno::Reference< Base2 >()); } CPPUNIT_TEST_SUITE_REGISTRATION(Test); } // namespace CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ ce-4-1-6+backports LibreOffice 核心代码仓库文档基金会
summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2020-12-29loplugin:stringviewparam: operator +Stephan Bergmann
2020-12-11Adapt the remaining OUString functions to std string_viewStephan Bergmann
2020-11-30loplugin:stringviewparam include comparisons with string literalsNoel
2020-11-11loplugin:stringviewNoel
2020-11-11disable O(U)String::concat for internal codeNoel Grandin
2020-08-23Fix typo in codeAndrea Gelmini
2020-08-21Fix typosAndrea Gelmini
2020-08-18Fix typosAndrea Gelmini
2020-07-23Silence GCC 11 trunk -Werror=nonnull involving dynamic_castStephan Bergmann
2020-07-02Upcoming improved loplugin:staticanonymous -> redundantstatic: unodevtoolsStephan Bergmann
2020-05-10compact namespace in ucb..uuiNoel Grandin
2020-01-31clang-tidy modernize-concat-nested-namespaceNoel Grandin
2019-12-24sal_Char->char in unoxmlNoel Grandin
2019-10-08loplugin:redundantpointerops simplify *p.get()Noel Grandin
2019-08-12Fix typosAndrea Gelmini
2019-08-09Fix typosAndrea Gelmini
2019-05-10an uno -> a unoCaolán McNamara
2019-04-28tdf#120703 PVS: fix cleanup checksMike Kaganski
2019-04-19optimise find/insert patternNoel Grandin
2019-04-05Revert "Get rid of b2u/u2b helpers; use OUString's toUtf8/fromUtf8 instead"Mike Kaganski
2019-04-05tdf#120703: partially revert commit 85456fae54029edd26df2277a9eec5e2fe3d9739Mike Kaganski
2019-04-04Get rid of b2u/u2b helpers; use OUString's toUtf8/fromUtf8 insteadMike Kaganski
2019-04-01tdf#120703 PVS: Silence V522 warningsMike Kaganski
2019-01-27Take advantage of known index to avoid an OUString copyMatteo Casalin
2019-01-27Use optimized OUString concatenationMatteo Casalin
2018-11-29remove unnecessary "if (!empty()" checks before loopsNoel Grandin
2018-10-24remove more rtl::OUString and OString prefixesNoel Grandin
2018-10-24clang-tidy performance-unnecessary-copy-init in test..xmlscriptNoel Grandin
2018-10-17Simplify containers iterations in unodevtools, unoidlArkadiy Illarionov
2018-09-17New loplugin:externalStephan Bergmann
2018-08-02Add missing sal/log.hxx headersGabor Kelemen