Bump version to 6.4-15
[LibreOffice.git] / include / tools / weakbase.h
blobfbd49d2ecf90e0ee0d97ec43bd41a3d2379772f6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #ifndef INCLUDED_TOOLS_WEAKBASE_H
20 #define INCLUDED_TOOLS_WEAKBASE_H
22 #include <sal/types.h>
23 #include <rtl/ref.hxx>
24 #include <tools/toolsdllapi.h>
26 /** the template classes in this header are helper to implement weak references
27 to implementation objects that are not refcounted.
29 THIS IS NOT THREADSAFE
31 Use this only to have 'safe' pointers to implementation objects that you
32 don't own but that you reference with a pointer.
34 Example:
36 class ImplClass : public tools::WeakBase< ImplClass >
38 ~ImplClass() { clearWeek(); } // not needed but safer, see method description
39 ...
42 class UserClass
44 tools::WeakReference< ImplClass > mxWeakRef;
46 UserClass( ImplClass* pOjbect ) : mxWeakRef( pObject ) {}
48 DoSomething()
50 if( mxWeakRef.is() )
51 mxWeakRef->DoSomethingMore();
55 namespace tools
57 class WeakBase;
59 /** private connection helper, do not use directly */
60 struct WeakConnection
62 sal_Int32 mnRefCount;
63 WeakBase* mpReference;
65 WeakConnection() : mnRefCount( 0 ), mpReference( nullptr ) {};
66 WeakConnection( WeakBase* pReference ) : mnRefCount( 0 ), mpReference( pReference ) {};
67 void acquire() { mnRefCount++; }
68 void release() { mnRefCount--; if( mnRefCount == 0 ) delete this; }
71 /** template implementation to hold a weak reference to an instance of type reference_type */
72 template <class reference_type>
73 class SAL_WARN_UNUSED WeakReference
75 public:
76 /** constructs an empty reference */
77 inline WeakReference();
79 /** constructs a reference with a pointer to a class derived from WeakBase */
80 inline WeakReference( reference_type* pReference );
82 /** constructs a reference from another reference */
83 inline WeakReference( const WeakReference< reference_type >& rWeakRef );
85 /** move a reference from another reference */
86 inline WeakReference( WeakReference< reference_type >&& rWeakRef );
88 /** returns true if the reference object is not null and still alive */
89 inline bool is() const;
91 /** returns the pointer to the reference object or null */
92 inline reference_type * get() const;
94 /** sets this reference to the given object or null */
95 inline void reset( reference_type* pReference );
97 /** resets this reference to null */
98 inline void reset();
100 /** returns the pointer to the reference object or null */
101 inline reference_type * operator->() const;
103 /** returns true if this instance references pReferenceObject */
104 inline bool operator== (const reference_type * pReferenceObject) const;
106 /** returns true if this instance and the given weakref reference the same object */
107 inline bool operator== (const WeakReference<reference_type> & handle) const;
109 /** only needed for using this class with stl containers */
110 inline bool operator!= (const WeakReference<reference_type> & handle) const;
112 /** only needed for using this class with stl containers */
113 inline bool operator< (const WeakReference<reference_type> & handle) const;
115 /** only needed for using this class with stl containers */
116 inline bool operator> (const WeakReference<reference_type> & handle) const;
118 /** the assignment operator */
119 inline WeakReference<reference_type>& operator= (const WeakReference<reference_type> & handle);
121 /** the move assignment operator */
122 inline WeakReference<reference_type>& operator= (WeakReference<reference_type> && handle);
124 private:
125 rtl::Reference<WeakConnection> mpWeakConnection;
128 /** derive your implementation classes from this class if you want them to support weak references */
129 class TOOLS_DLLPUBLIC WeakBase
131 template<typename T> friend class WeakReference;
133 public:
134 WeakBase() {}
135 virtual ~WeakBase();
136 /** clears the reference pointer in all living weak references for this instance.
137 Further created weak references will also be invalid.
138 You should call this method in the d'tor of your derived classes for an early
139 invalidate of all living weak references while your object is already inside
140 it d'tor.
142 inline void clearWeak();
144 private:
145 inline WeakConnection* getWeakConnection();
146 rtl::Reference<WeakConnection> mpWeakConnection;
151 #endif
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */