LibreOffice Module tools (master) 1
weakbase.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_TOOLS_WEAKBASE_HXX
21#define INCLUDED_TOOLS_WEAKBASE_HXX
22
23#include <tools/weakbase.h>
24
26
27namespace tools
28{
29
30template< class reference_type >
32{
33 mpWeakConnection = new WeakConnection;
34}
35
36template< class reference_type >
37inline WeakReference< reference_type >::WeakReference( reference_type* pReference )
38{
39 reset( pReference );
40}
41
42template< class reference_type >
44{
45 mpWeakConnection = rWeakRef.mpWeakConnection;
46}
47
48template< class reference_type >
50{
51 mpWeakConnection = std::move(rWeakRef.mpWeakConnection);
52 rWeakRef.reset();
53}
54
55template< class reference_type >
57{
58 return mpWeakConnection->mpReference != nullptr;
59}
60
61template< class reference_type >
62inline reference_type * WeakReference< reference_type >::get() const
63{
64 auto pWeakBase = mpWeakConnection->mpReference;
65 if (!pWeakBase)
66 return nullptr;
67 assert(dynamic_cast<reference_type *>(pWeakBase));
68 return static_cast<reference_type *>(pWeakBase);
69}
70
71template< class reference_type >
72inline void WeakReference< reference_type >::reset( reference_type* pReference )
73{
74 if( pReference )
75 mpWeakConnection = pReference->getWeakConnection();
76 else
77 reset();
78}
79
80template< class reference_type >
82{
83 mpWeakConnection = new WeakConnection;
84}
85
86template< class reference_type >
87inline reference_type * WeakReference< reference_type >::operator->() const
88{
89 return get();
90}
91
92template< class reference_type >
93inline reference_type& WeakReference< reference_type >::operator*() const
94{
95 return *get();
96}
97
98template< class reference_type >
99inline bool WeakReference< reference_type >::operator==(const reference_type * pReferenceObject) const
100{
101 return mpWeakConnection->mpReference == pReferenceObject;
102}
103
104template< class reference_type >
106{
107 return mpWeakConnection == handle.mpWeakConnection;
108}
109
110template< class reference_type >
112{
113 return mpWeakConnection != handle.mpWeakConnection;
114}
115
116template< class reference_type >
118{
119 return mpWeakConnection->mpReference < handle.mpWeakConnection->mpReference;
120}
121
122template< class reference_type >
124{
125 return mpWeakConnection->mpReference > handle.mpWeakConnection->mpReference;
126}
127
128template< class reference_type >
130 const WeakReference<reference_type>& rReference)
131{
132 if (&rReference != this)
133 mpWeakConnection = rReference.mpWeakConnection;
134 return *this;
135}
136
137template< class reference_type >
140{
141 mpWeakConnection = std::move(rReference.mpWeakConnection);
142 return *this;
143}
144
146{
147 if( mpWeakConnection.is() )
148 mpWeakConnection->mpReference = nullptr;
149}
150
152{
153 if( !mpWeakConnection.is() )
154 mpWeakConnection = new WeakConnection( this );
155 return mpWeakConnection.get();
156}
157
158}
159
160#endif
161
162/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
WeakConnection * getWeakConnection()
Definition: weakbase.hxx:151
rtl::Reference< WeakConnection > mpWeakConnection
Definition: weakbase.h:152
void clearWeak()
clears the reference pointer in all living weak references for this instance.
Definition: weakbase.hxx:145
template implementation to hold a weak reference to an instance of type reference_type
Definition: weakbase.h:74
bool operator>(const WeakReference< reference_type > &handle) const
only needed for using this class with stl containers
Definition: weakbase.hxx:123
bool operator==(const reference_type *pReferenceObject) const
returns true if this instance references pReferenceObject
Definition: weakbase.hxx:99
WeakReference< reference_type > & operator=(const WeakReference< reference_type > &handle)
the assignment operator
Definition: weakbase.hxx:129
rtl::Reference< WeakConnection > mpWeakConnection
Definition: weakbase.h:131
bool operator!=(const WeakReference< reference_type > &handle) const
only needed for using this class with stl containers
Definition: weakbase.hxx:111
reference_type * operator->() const
returns the pointer to the reference object or null
Definition: weakbase.hxx:87
bool is() const
returns true if the reference object is not null and still alive
Definition: weakbase.hxx:56
void reset()
resets this reference to null
Definition: weakbase.hxx:81
WeakReference()
constructs an empty reference
Definition: weakbase.hxx:31
reference_type & operator*() const
returns a ref to the reference object
Definition: weakbase.hxx:93
bool operator<(const WeakReference< reference_type > &handle) const
only needed for using this class with stl containers
Definition: weakbase.hxx:117
reference_type * get() const
returns the pointer to the reference object or null
Definition: weakbase.hxx:62
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
private connection helper, do not use directly
Definition: weakbase.h:61