My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ref.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _RTL_REF_HXX_
30 #define _RTL_REF_HXX_
31 
32 #include <sal/types.h>
33 #include <osl/diagnose.h>
34 #include <osl/interlck.h>
35 
36 namespace rtl
37 {
38 
42 {
43 public:
46  virtual oslInterlockedCount SAL_CALL acquire() = 0;
47 
50  virtual oslInterlockedCount SAL_CALL release() = 0;
51 
52 #if !defined _MSC_VER // public -> protected changes mangled names there
53 protected:
54 #endif
56  // avoid warnings about virtual members and non-virtual dtor
57 };
58 
59 
62 template <class reference_type>
63 class Reference
64 {
67  reference_type * m_pBody;
68 
69 
70 public:
73  inline Reference()
74  : m_pBody (0)
75  {}
76 
77 
80  inline Reference (reference_type * pBody)
81  : m_pBody (pBody)
82  {
83  if (m_pBody)
84  m_pBody->acquire();
85  }
86 
87 
90  inline Reference (const Reference<reference_type> & handle)
91  : m_pBody (handle.m_pBody)
92  {
93  if (m_pBody)
94  m_pBody->acquire();
95  }
96 
97 
100  inline ~Reference()
101  {
102  if (m_pBody)
103  m_pBody->release();
104  }
105 
110  SAL_CALL set (reference_type * pBody)
111  {
112  if (pBody)
113  pBody->acquire();
114  reference_type * const pOld = m_pBody;
115  m_pBody = pBody;
116  if (pOld)
117  pOld->release();
118  return *this;
119  }
120 
126  SAL_CALL operator= (const Reference<reference_type> & handle)
127  {
128  return set( handle.m_pBody );
129  }
130 
134  SAL_CALL operator= (reference_type * pBody)
135  {
136  return set( pBody );
137  }
138 
146  inline Reference<reference_type> & SAL_CALL clear()
147  {
148  if (m_pBody)
149  {
150  reference_type * const pOld = m_pBody;
151  m_pBody = 0;
152  pOld->release();
153  }
154  return *this;
155  }
156 
157 
162  inline reference_type * SAL_CALL get() const
163  {
164  return m_pBody;
165  }
166 
167 
170  inline reference_type * SAL_CALL operator->() const
171  {
172  OSL_PRECOND(m_pBody, "Reference::operator->() : null body");
173  return m_pBody;
174  }
175 
176 
179  inline reference_type & SAL_CALL operator*() const
180  {
181  OSL_PRECOND(m_pBody, "Reference::operator*() : null body");
182  return *m_pBody;
183  }
184 
185 
188  inline sal_Bool SAL_CALL is() const
189  {
190  return (m_pBody != 0);
191  }
192 
193 
196  inline sal_Bool SAL_CALL operator== (const reference_type * pBody) const
197  {
198  return (m_pBody == pBody);
199  }
200 
201 
204  inline sal_Bool
205  SAL_CALL operator== (const Reference<reference_type> & handle) const
206  {
207  return (m_pBody == handle.m_pBody);
208  }
209 
210 
213  inline sal_Bool
214  SAL_CALL operator!= (const Reference<reference_type> & handle) const
215  {
216  return (m_pBody != handle.m_pBody);
217  }
218 
219 
222  inline sal_Bool
223  SAL_CALL operator< (const Reference<reference_type> & handle) const
224  {
225  return (m_pBody < handle.m_pBody);
226  }
227 
228 
231  inline sal_Bool
232  SAL_CALL operator> (const Reference<reference_type> & handle) const
233  {
234  return (m_pBody > handle.m_pBody);
235  }
236 };
237 
239 
241 template <typename T>
242 inline T * get_pointer( Reference<T> const& r )
243 {
244  return r.get();
245 }
247 
248 } // namespace rtl
249 
250 #endif /* !_RTL_REF_HXX_ */
251 
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */