build fix
[LibreOffice.git] / include / rtl / ref.hxx
blobb70088bcdfd3e15b4b977551875d7a7777754433
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 .
20 #ifndef INCLUDED_RTL_REF_HXX
21 #define INCLUDED_RTL_REF_HXX
23 #include <sal/config.h>
25 #include <cassert>
27 #include <sal/types.h>
29 namespace rtl
32 /** Template reference class for reference type.
34 template <class reference_type>
35 class Reference
37 /** The <b>reference_type</b> body pointer.
39 reference_type * m_pBody;
42 public:
43 /** Constructor...
45 inline Reference()
46 : m_pBody (NULL)
50 /** Constructor...
52 inline Reference (reference_type * pBody, __sal_NoAcquire)
53 : m_pBody (pBody)
57 /** Constructor...
59 inline Reference (reference_type * pBody)
60 : m_pBody (pBody)
62 if (m_pBody)
63 m_pBody->acquire();
66 /** Copy constructor...
68 inline Reference (const Reference<reference_type> & handle)
69 : m_pBody (handle.m_pBody)
71 if (m_pBody)
72 m_pBody->acquire();
75 #ifdef LIBO_INTERNAL_ONLY
76 /** Move constructor...
78 inline Reference (Reference<reference_type> && handle)
79 : m_pBody (handle.m_pBody)
81 handle.m_pBody = nullptr;
83 #endif
85 /** Destructor...
87 inline ~Reference()
89 if (m_pBody)
90 m_pBody->release();
93 /** Set...
94 Similar to assignment.
96 inline Reference<reference_type> &
97 SAL_CALL set (reference_type * pBody)
99 if (pBody)
100 pBody->acquire();
101 reference_type * const pOld = m_pBody;
102 m_pBody = pBody;
103 if (pOld)
104 pOld->release();
105 return *this;
108 /** Assignment.
109 Unbinds this instance from its body (if bound) and
110 bind it to the body represented by the handle.
112 inline Reference<reference_type> &
113 SAL_CALL operator= (const Reference<reference_type> & handle)
115 return set( handle.m_pBody );
118 #ifdef LIBO_INTERNAL_ONLY
119 /** Assignment.
120 * Unbinds this instance from its body (if bound),
121 * bind it to the body represented by the handle, and
122 * set the body represented by the handle to nullptr.
124 inline Reference<reference_type> &
125 operator= (Reference<reference_type> && handle)
127 // self-movement guts ourself
128 if (m_pBody)
129 m_pBody->release();
130 m_pBody = handle.m_pBody;
131 handle.m_pBody = nullptr;
132 return *this;
134 #endif
136 /** Assignment...
138 inline Reference<reference_type> &
139 SAL_CALL operator= (reference_type * pBody)
141 return set( pBody );
144 /** Unbind the body from this handle.
145 Note that for a handle representing a large body,
146 "handle.clear().set(new body());" _might_
147 perform a little bit better than "handle.set(new body());",
148 since in the second case two large objects exist in memory
149 (the old body and the new body).
151 inline Reference<reference_type> & SAL_CALL clear()
153 if (m_pBody)
155 reference_type * const pOld = m_pBody;
156 m_pBody = NULL;
157 pOld->release();
159 return *this;
163 /** Get the body. Can be used instead of operator->().
164 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
165 are the same.
167 inline reference_type * SAL_CALL get() const
169 return m_pBody;
173 /** Probably most common used: handle->someBodyOp().
175 inline reference_type * SAL_CALL operator->() const
177 assert(m_pBody != NULL);
178 return m_pBody;
182 /** Allows (*handle).someBodyOp().
184 inline reference_type & SAL_CALL operator*() const
186 assert(m_pBody != NULL);
187 return *m_pBody;
191 /** Returns True if the handle does point to a valid body.
193 inline bool SAL_CALL is() const
195 return (m_pBody != NULL);
199 /** Returns True if this points to pBody.
201 inline bool SAL_CALL operator== (const reference_type * pBody) const
203 return (m_pBody == pBody);
207 /** Returns True if handle points to the same body.
209 inline bool
210 SAL_CALL operator== (const Reference<reference_type> & handle) const
212 return (m_pBody == handle.m_pBody);
216 /** Needed to place References into STL collection.
218 inline bool
219 SAL_CALL operator!= (const Reference<reference_type> & handle) const
221 return (m_pBody != handle.m_pBody);
225 /** Needed to place References into STL collection.
227 inline bool
228 SAL_CALL operator< (const Reference<reference_type> & handle) const
230 return (m_pBody < handle.m_pBody);
234 /** Needed to place References into STL collection.
236 inline bool
237 SAL_CALL operator> (const Reference<reference_type> & handle) const
239 return (m_pBody > handle.m_pBody);
243 } // namespace rtl
245 #endif /* ! INCLUDED_RTL_REF_HXX */
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */