LibreOffice
LibreOffice 4.2 SDK 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  * 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_RTL_REF_HXX
21 #define INCLUDED_RTL_REF_HXX
22 
23 #include <sal/types.h>
24 #include <osl/diagnose.h>
25 #include <osl/interlck.h>
26 
27 namespace rtl
28 {
29 
33 {
34 public:
37  virtual oslInterlockedCount SAL_CALL acquire() = 0;
38 
41  virtual oslInterlockedCount SAL_CALL release() = 0;
42 
43 #if !defined _MSC_VER // public -> protected changes mangled names there
44 protected:
45 #endif
47  // avoid warnings about virtual members and non-virtual dtor
48 };
49 
50 
53 template <class reference_type>
54 class Reference
55 {
58  reference_type * m_pBody;
59 
60 
61 public:
64  inline Reference()
65  : m_pBody (0)
66  {}
67 
68 
71  inline Reference (reference_type * pBody)
72  : m_pBody (pBody)
73  {
74  if (m_pBody)
75  m_pBody->acquire();
76  }
77 
78 
81  inline Reference (const Reference<reference_type> & handle)
82  : m_pBody (handle.m_pBody)
83  {
84  if (m_pBody)
85  m_pBody->acquire();
86  }
87 
88 
91  inline ~Reference()
92  {
93  if (m_pBody)
94  m_pBody->release();
95  }
96 
101  SAL_CALL set (reference_type * pBody)
102  {
103  if (pBody)
104  pBody->acquire();
105  reference_type * const pOld = m_pBody;
106  m_pBody = pBody;
107  if (pOld)
108  pOld->release();
109  return *this;
110  }
111 
117  SAL_CALL operator= (const Reference<reference_type> & handle)
118  {
119  return set( handle.m_pBody );
120  }
121 
125  SAL_CALL operator= (reference_type * pBody)
126  {
127  return set( pBody );
128  }
129 
137  inline Reference<reference_type> & SAL_CALL clear()
138  {
139  if (m_pBody)
140  {
141  reference_type * const pOld = m_pBody;
142  m_pBody = 0;
143  pOld->release();
144  }
145  return *this;
146  }
147 
148 
153  inline reference_type * SAL_CALL get() const
154  {
155  return m_pBody;
156  }
157 
158 
161  inline reference_type * SAL_CALL operator->() const
162  {
163  OSL_PRECOND(m_pBody, "Reference::operator->() : null body");
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 sal_Bool SAL_CALL is() const
180  {
181  return (m_pBody != 0);
182  }
183 
184 
187  inline sal_Bool SAL_CALL operator== (const reference_type * pBody) const
188  {
189  return (m_pBody == pBody);
190  }
191 
192 
195  inline sal_Bool
196  SAL_CALL operator== (const Reference<reference_type> & handle) const
197  {
198  return (m_pBody == handle.m_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 
230 
232 template <typename T>
233 inline T * get_pointer( Reference<T> const& r )
234 {
235  return r.get();
236 }
238 
239 } // namespace rtl
240 
241 #endif /* ! INCLUDED_RTL_REF_HXX */
242 
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< reference_type > & operator=(const Reference< reference_type > &handle)
Assignment.
Definition: ref.hxx:117
Reference(const Reference< reference_type > &handle)
Copy constructor...
Definition: ref.hxx:81
~IReference()
Definition: ref.hxx:46
virtual oslInterlockedCount release()=0
Reference< reference_type > & clear()
Unbind the body from this handle.
Definition: ref.hxx:137
#define OSL_PRECOND(c, m)
Definition: diagnose.h:159
reference_type & operator*() const
Allows (*handle).someBodyOp().
Definition: ref.hxx:170
sal_Int32 oslInterlockedCount
Definition: interlck.h:32
~Reference()
Destructor...
Definition: ref.hxx:91
virtual oslInterlockedCount acquire()=0
Reference()
Constructor...
Definition: ref.hxx:64
Reference< reference_type > & set(reference_type *pBody)
Set...
Definition: ref.hxx:101
reference_type * operator->() const
Probably most common used: handle->someBodyOp().
Definition: ref.hxx:161
sal_Bool operator==(const reference_type *pBody) const
Returns True if this points to pBody.
Definition: ref.hxx:187
unsigned char sal_Bool
Definition: types.h:46
Template reference class for reference type derived from IReference.
Definition: ref.hxx:54
sal_Bool operator>(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:223
sal_Bool is() const
Returns True if the handle does point to a valid body.
Definition: ref.hxx:179
Interface for a reference type.
Definition: ref.hxx:32
Reference(reference_type *pBody)
Constructor...
Definition: ref.hxx:71
sal_Bool operator!=(const Reference< reference_type > &handle) const
Needed to place References into STL collection.
Definition: ref.hxx:205