Line data Source code
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_XMLOFF_UNIREF_HXX
21 : #define INCLUDED_XMLOFF_UNIREF_HXX
22 :
23 : #include <sal/config.h>
24 : #include <xmloff/dllapi.h>
25 : #include <sal/types.h>
26 : #include <osl/interlck.h>
27 :
28 : /**
29 : * An instance of this class holds a pointer to an object. The lifetime of
30 : * the object is controled by the instance. The constructor calls
31 : * acquire() and the destructor calls release().
32 : * You could derive your class from the baseclass UniRefBase which implements
33 : * the methods acquire and release, yet.
34 : */
35 : template< class T > class UniReference
36 : {
37 : private:
38 : T* mpElement;
39 :
40 : public:
41 : /** Create an empty reference.*/
42 0 : UniReference()
43 0 : : mpElement( NULL )
44 0 : {}
45 :
46 : /** Destroy the reference and releases the element.*/
47 : inline ~UniReference();
48 :
49 : /** Create a new reference with the same element as in rRef and acquire this one.*/
50 : inline UniReference( const UniReference< T > & rRef );
51 :
52 : /**
53 : * Create a new reference with the given element pElement and acquire this one.
54 : */
55 : inline UniReference( T * pElement );
56 :
57 : /**
58 : * Release the reference and set the new one pObj.
59 : */
60 : inline UniReference< T > & operator = ( T * pElement );
61 :
62 : /**
63 : * Release the reference and set the new one from rObj.
64 : */
65 : inline UniReference< T > & operator = ( const UniReference< T > & rRef );
66 :
67 : /**
68 : * Return the pointer to the element, may be null.
69 : */
70 : inline T* operator -> () const;
71 :
72 : /**
73 : * Returns true if the pointer to the element is valid.
74 : */
75 : inline bool is() const;
76 :
77 : /**
78 : * Return true if both elements refer to the same object.
79 : */
80 : inline bool operator == ( const UniReference & rRef ) const;
81 :
82 : /**
83 : * Return true if both elements does not refer to the same object.
84 : */
85 : inline bool operator != ( const UniReference & rRef ) const;
86 :
87 : /** Gets implementation pointer.
88 : This call does <b>not</b> acquire the implementation.
89 : <br>
90 : @return <b>un</b>acquired implementation pointer
91 : */
92 : inline T* get() const;
93 : };
94 :
95 : class XMLOFF_DLLPUBLIC UniRefBase
96 : {
97 : private:
98 : /**
99 : * The reference counter.
100 : */
101 : oslInterlockedCount m_refCount;
102 :
103 : public:
104 0 : UniRefBase() : m_refCount( 0 )
105 0 : {}
106 : virtual ~UniRefBase();
107 :
108 0 : void acquire() { osl_atomic_increment( &m_refCount ); }
109 : void release();
110 :
111 : };
112 :
113 :
114 :
115 : // Inline-implementations of UniReference
116 :
117 :
118 : /** Create a new reference with the same element as in rRef and acquire this one.*/
119 : template< class T >
120 0 : inline UniReference< T >::UniReference( const UniReference< T > & rRef )
121 0 : : mpElement( rRef.mpElement )
122 : {
123 0 : if( mpElement )
124 0 : mpElement->acquire();
125 0 : }
126 :
127 : template< class T >
128 0 : inline UniReference< T >::~UniReference()
129 : {
130 0 : if( mpElement )
131 0 : mpElement->release();
132 0 : }
133 :
134 : /**
135 : * Create a new reference with the given element pElement and acquire this one.
136 : * @param pInterface the interface, pointer may be null.
137 : */
138 : template< class T >
139 0 : inline UniReference< T >::UniReference( T * pElement )
140 0 : : mpElement( pElement )
141 : {
142 0 : if( mpElement )
143 0 : mpElement->acquire();
144 0 : }
145 :
146 : /**
147 : * Release the reference and set the new one pObj.<BR>
148 : * <B>The operation is not thread save. You must protect all assigns to a reference class.</B>
149 : */
150 : template< class T >
151 0 : inline UniReference< T > & UniReference< T >::operator = ( T * pElement )
152 : {
153 0 : if( pElement )
154 0 : pElement->acquire();
155 0 : if( mpElement )
156 0 : mpElement->release();
157 :
158 0 : mpElement = pElement;
159 :
160 0 : return *this;
161 : }
162 :
163 : /**
164 : * Release the reference and set the new one from rObj.<BR>
165 : * <B>The operation is not thread save. You must protect all assigns to a reference class.</B>
166 : */
167 : template< class T >
168 0 : inline UniReference< T > & UniReference< T >::operator = ( const UniReference< T > & rRef )
169 : {
170 0 : return operator = ( rRef.mpElement );
171 : }
172 :
173 : /**
174 : * Return the pointer to the interface, may be null.
175 : */
176 : template< class T >
177 0 : inline T* UniReference< T >::operator -> () const
178 : {
179 0 : return get();
180 : }
181 :
182 : /**
183 : * Return the pointer to the interface, may be null.
184 : */
185 : template< class T >
186 0 : inline T* UniReference< T >::get () const
187 : {
188 0 : return static_cast< T * >( mpElement );
189 : }
190 :
191 : /**
192 : * Returns true if the pointer to the interface is valid.
193 : */
194 : template< class T >
195 0 : inline bool UniReference< T >::is() const
196 : {
197 0 : return (mpElement != 0);
198 : }
199 : /**
200 : * Return true if both interfaces refer to the same object. The operation can be
201 : * much more expensive than a pointer comparision.<BR>
202 : *
203 : * @param rRef another interface reference
204 : */
205 : template< class T >
206 : inline bool UniReference< T >::operator == ( const UniReference & rRef ) const
207 : {
208 : return ( mpElement == rRef.mpElement );
209 : }
210 : /**
211 : * Return true if both interfaces does not refer to the same object. The operation can be
212 : * much more expensive than a pointer comparision.<BR>
213 : *
214 : * @param rRef another interface reference
215 : */
216 : template< class T >
217 : inline bool UniReference< T >::operator != ( const UniReference & rRef ) const
218 : {
219 : return ( ! operator == ( rRef ) );
220 : }
221 :
222 : #endif // INCLUDED_XMLOFF_UNIREF_HXX
223 :
224 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|