1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
13 #include <rtl/ref.hxx>
14 #include <boost/intrusive_ptr.hpp>
15 #include <com/sun/star/uno/XInterface.hpp>
16 #include <cppuhelper/weak.hxx>
17 #include <unotools/weakref.hxx>
19 struct UnoObject
: public cppu::OWeakObject
22 struct UnoSubObject
: public UnoObject
27 // Note, getting duplicate warnings for some reason I cannot fathom
32 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
33 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
34 std::unique_ptr
<UnoObject
> m_foo1
;
35 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
36 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
37 std::shared_ptr
<UnoObject
> m_foo2
;
38 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
39 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
40 boost::intrusive_ptr
<UnoObject
> m_foo3
;
41 rtl::Reference
<UnoObject
> m_foo4
; // no warning expected
44 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
45 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
46 std::unique_ptr
<UnoObject
> foo1();
47 rtl::Reference
<UnoObject
> foo2(); // no warning expected
48 // expected-error@+2 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
49 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via smart pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
50 void foo3(std::unique_ptr
<UnoObject
> p
);
52 void test2(UnoObject
* pUnoObject
)
54 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being deleted via delete, should be managed via rtl::Reference [loplugin:refcounting]}}
58 template <typename T
> struct Dependent
: T
60 void f() { delete this; }
61 //TODO: missing expected error@+1 {{cppu::OWeakObject subclass 'Dependent<UnoObject>' being deleted via delete, should be managed via rtl::Reference [loplugin:refcounting]}}
62 void g() { delete this; }
67 void dummy(Dependent
<Dummy
>* p1
, Dependent
<UnoObject
>* p2
)
75 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
76 UnoObject
* p
= new UnoObject
;
78 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
84 // expected-error@+1 {{new object of cppu::OWeakObject subclass 'UnoObject' being returned via raw pointer, should be returned by via rtl::Reference [loplugin:refcounting]}}
87 rtl::Reference
<UnoObject
> foo6()
89 // no warning expected
92 const rtl::Reference
<UnoObject
>& getConstRef();
95 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoSubObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
96 UnoSubObject
* p1
= static_cast<UnoSubObject
*>(foo6().get());
98 // expected-error@+1 {{cppu::OWeakObject subclass 'UnoSubObject' being managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
99 p1
= static_cast<UnoSubObject
*>(foo6().get());
101 rtl::Reference
<UnoObject
> u2
;
102 // no warning expected
103 UnoSubObject
* p2
= static_cast<UnoSubObject
*>(u2
.get());
105 p2
= static_cast<UnoSubObject
*>(u2
.get());
106 // no warning expected
107 UnoSubObject
* p3
= static_cast<UnoSubObject
*>(getConstRef().get());
109 p3
= static_cast<UnoSubObject
*>(getConstRef().get());
112 const unotools::WeakReference
<UnoObject
>& getWeakRef();
115 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
116 UnoSubObject
* p1
= static_cast<UnoSubObject
*>(getWeakRef().get().get());
119 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
120 UnoObject
* p2
= getWeakRef().get().get();
123 unotools::WeakReference
<UnoObject
> weak1
;
124 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
125 UnoSubObject
* p3
= dynamic_cast<UnoSubObject
*>(weak1
.get().get());
128 // expected-error@+1 {{weak object being converted to strong, and then the reference dropped, and managed via raw pointer, should be managed via rtl::Reference [loplugin:refcounting]}}
129 UnoObject
* p4
= weak1
.get().get();
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */