1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 _UNIVERSALL_REFERENCE_HXX
21 #define _UNIVERSALL_REFERENCE_HXX
23 #include "sal/config.h"
24 #include "xmloff/dllapi.h"
25 #include <sal/types.h>
26 #include <osl/interlck.h>
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.
35 template< class T
> class UniReference
41 /** Create an empty reference.*/
46 /** Destroy the reference and releases the element.*/
47 inline ~UniReference();
49 /** Create a new reference with the same element as in rRef and acquire this one.*/
50 inline UniReference( const UniReference
< T
> & rRef
);
53 * Create a new reference with the given element pElement and acquire this one.
55 inline UniReference( T
* pElement
);
58 * Release the reference and set the new one pObj.
60 inline UniReference
< T
> & operator = ( T
* pElement
);
63 * Release the reference and set the new one from rObj.
65 inline UniReference
< T
> & operator = ( const UniReference
< T
> & rRef
);
68 * Return the pointer to the element, may be null.
70 inline T
* operator -> () const;
73 * Returns true if the pointer to the element is valid.
75 inline sal_Bool
is() const;
78 * Return true if both elements refer to the same object.
80 inline sal_Bool
operator == ( const UniReference
& rRef
) const;
83 * Return true if both elements does not refer to the same object.
85 inline sal_Bool
operator != ( const UniReference
& rRef
) const;
87 /** Gets implementation pointer.
88 This call does <b>not</b> acquire the implementation.
90 @return <b>un</b>acquired implementation pointer
92 inline T
* get() const;
95 class XMLOFF_DLLPUBLIC UniRefBase
99 * The reference counter.
101 oslInterlockedCount m_refCount
;
104 UniRefBase() : m_refCount( 0 )
106 virtual ~UniRefBase();
108 void acquire() { osl_atomic_increment( &m_refCount
); }
113 ///////////////////////////////////////////////////////////////////////////////
115 // Inline-implementations of UniReference
118 /** Create a new reference with the same element as in rRef and acquire this one.*/
120 inline UniReference
< T
>::UniReference( const UniReference
< T
> & rRef
)
121 : mpElement( rRef
.mpElement
)
124 mpElement
->acquire();
128 inline UniReference
< T
>::~UniReference()
131 mpElement
->release();
135 * Create a new reference with the given element pElement and acquire this one.
136 * @param pInterface the interface, pointer may be null.
139 inline UniReference
< T
>::UniReference( T
* pElement
)
140 : mpElement( pElement
)
143 mpElement
->acquire();
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>
151 inline UniReference
< T
> & UniReference
< T
>::operator = ( T
* pElement
)
156 mpElement
->release();
158 mpElement
= pElement
;
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>
168 inline UniReference
< T
> & UniReference
< T
>::operator = ( const UniReference
< T
> & rRef
)
170 return operator = ( rRef
.mpElement
);
174 * Return the pointer to the interface, may be null.
177 inline T
* UniReference
< T
>::operator -> () const
183 * Return the pointer to the interface, may be null.
186 inline T
* UniReference
< T
>::get () const
188 return static_cast< T
* >( mpElement
);
192 * Returns true if the pointer to the interface is valid.
195 inline sal_Bool UniReference
< T
>::is() const
197 return (mpElement
!= 0);
200 * Return true if both interfaces refer to the same object. The operation can be
201 * much more expensive than a pointer comparision.<BR>
203 * @param rRef another interface reference
206 inline sal_Bool UniReference
< T
>::operator == ( const UniReference
& rRef
) const
208 return ( mpElement
== rRef
.mpElement
);
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>
214 * @param rRef another interface reference
217 inline sal_Bool UniReference
< T
>::operator != ( const UniReference
& rRef
) const
219 return ( ! operator == ( rRef
) );
222 #endif // _UNIVERSALL_REFERENCE_HXX
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */