Handle pasting disallowed clipboard contents on iOS
[LibreOffice.git] / include / rtl / ref.hxx
blob38dfe3769e8125f8a4a221e1429e0911d0cb2b1c
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 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_RTL_REF_HXX
25 #define INCLUDED_RTL_REF_HXX
27 #include "sal/config.h"
29 #include <cassert>
30 #include <cstddef>
31 #include <functional>
32 #ifdef LIBO_INTERNAL_ONLY
33 #include <type_traits>
34 #include "com/sun/star/uno/Reference.h"
35 #endif
37 #include "sal/types.h"
39 namespace rtl
42 /** Template reference class for reference type.
44 template <class reference_type>
45 class Reference
47 /** The <b>reference_type</b> body pointer.
49 reference_type * m_pBody;
52 public:
53 /** Constructor...
55 Reference()
56 : m_pBody (NULL)
60 /** Constructor...
62 Reference (reference_type * pBody, __sal_NoAcquire)
63 : m_pBody (pBody)
67 /** Constructor...
69 Reference (reference_type * pBody)
70 : m_pBody (pBody)
72 if (m_pBody)
73 m_pBody->acquire();
76 /** Copy constructor...
78 Reference (const Reference<reference_type> & handle)
79 : m_pBody (handle.m_pBody)
81 if (m_pBody)
82 m_pBody->acquire();
85 #ifdef LIBO_INTERNAL_ONLY
86 /** Move constructor...
88 Reference (Reference<reference_type> && handle) noexcept
89 : m_pBody (handle.m_pBody)
91 handle.m_pBody = nullptr;
93 #endif
95 #if defined LIBO_INTERNAL_ONLY
96 /** Up-casting conversion constructor: Copies interface reference.
98 Does not work for up-casts to ambiguous bases.
100 @param rRef another reference
102 template< class derived_type >
103 inline Reference(
104 const Reference< derived_type > & rRef,
105 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
106 : m_pBody (rRef.get())
108 if (m_pBody)
109 m_pBody->acquire();
112 /** Up-casting conversion operator to convert to css::uno::Interface
114 Does not work for up-casts to ambiguous bases.
116 template< class super_type,
117 std::enable_if_t<std::is_base_of_v<super_type, reference_type>, int> = 0 >
118 inline operator css::uno::Reference<super_type>() const
120 return css::uno::Reference<super_type>(m_pBody);
122 #endif
124 /** Destructor...
126 ~Reference() COVERITY_NOEXCEPT_FALSE
128 if (m_pBody)
129 m_pBody->release();
132 /** Set...
133 Similar to assignment.
135 Reference<reference_type> &
136 SAL_CALL set (reference_type * pBody)
138 if (pBody)
139 pBody->acquire();
140 reference_type * const pOld = m_pBody;
141 m_pBody = pBody;
142 if (pOld)
143 pOld->release();
144 return *this;
147 /** Assignment.
148 Unbinds this instance from its body (if bound) and
149 bind it to the body represented by the handle.
151 Reference<reference_type> &
152 SAL_CALL operator= (const Reference<reference_type> & handle)
154 return set( handle.m_pBody );
157 #ifdef LIBO_INTERNAL_ONLY
158 /** Assignment.
159 * Unbinds this instance from its body (if bound),
160 * bind it to the body represented by the handle, and
161 * set the body represented by the handle to nullptr.
163 Reference<reference_type> &
164 operator= (Reference<reference_type> && handle)
166 // self-movement guts ourself
167 if (m_pBody)
168 m_pBody->release();
169 m_pBody = handle.m_pBody;
170 handle.m_pBody = nullptr;
171 return *this;
173 #endif
175 /** Assignment...
177 Reference<reference_type> &
178 SAL_CALL operator= (reference_type * pBody)
180 return set( pBody );
183 /** Unbind the body from this handle.
184 Note that for a handle representing a large body,
185 "handle.clear().set(new body());" _might_
186 perform a little bit better than "handle.set(new body());",
187 since in the second case two large objects exist in memory
188 (the old body and the new body).
190 Reference<reference_type> & SAL_CALL clear()
192 if (m_pBody)
194 reference_type * const pOld = m_pBody;
195 m_pBody = NULL;
196 pOld->release();
198 return *this;
202 /** Get the body. Can be used instead of operator->().
203 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
204 are the same.
206 reference_type * SAL_CALL get() const
208 return m_pBody;
212 /** Probably most common used: handle->someBodyOp().
214 reference_type * SAL_CALL operator->() const
216 assert(m_pBody != NULL);
217 return m_pBody;
221 /** Allows (*handle).someBodyOp().
223 reference_type & SAL_CALL operator*() const
225 assert(m_pBody != NULL);
226 return *m_pBody;
230 /** Returns True if the handle does point to a valid body.
232 bool SAL_CALL is() const
234 return (m_pBody != NULL);
237 #if defined LIBO_INTERNAL_ONLY
238 /** Returns True if the handle does point to a valid body.
240 explicit operator bool() const
242 return is();
244 #endif
246 /** Returns True if this points to pBody.
248 bool SAL_CALL operator== (const reference_type * pBody) const
250 return (m_pBody == pBody);
254 /** Returns True if handle points to the same body.
256 bool
257 SAL_CALL operator== (const Reference<reference_type> & handle) const
259 return (m_pBody == handle.m_pBody);
263 /** Needed to place References into STL collection.
265 bool
266 SAL_CALL operator!= (const Reference<reference_type> & handle) const
268 return (m_pBody != handle.m_pBody);
272 /** Needed to place References into STL collection.
274 bool
275 SAL_CALL operator< (const Reference<reference_type> & handle) const
277 return (m_pBody < handle.m_pBody);
281 /** Needed to place References into STL collection.
283 bool
284 SAL_CALL operator> (const Reference<reference_type> & handle) const
286 return (m_pBody > handle.m_pBody);
290 } // namespace rtl
292 #if defined LIBO_INTERNAL_ONLY
293 namespace std
296 /// @cond INTERNAL
298 Make rtl::Reference hashable by default for use in STL containers.
300 @since LibreOffice 6.3
302 template<typename T>
303 struct hash<::rtl::Reference<T>>
305 std::size_t operator()(::rtl::Reference<T> const & s) const
306 { return std::size_t(s.get()); }
308 /// @endcond
312 #endif
314 #endif /* ! INCLUDED_RTL_REF_HXX */
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */