update credits
[LibreOffice.git] / include / xmloff / uniref.hxx
blob223d4bcfe2117611225f6bdec179c508671f3207
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 _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>
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.
35 template< class T > class UniReference
37 private:
38 T* mpElement;
40 public:
41 /** Create an empty reference.*/
42 UniReference()
43 : mpElement( NULL )
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 );
52 /**
53 * Create a new reference with the given element pElement and acquire this one.
55 inline UniReference( T * pElement );
57 /**
58 * Release the reference and set the new one pObj.
60 inline UniReference< T > & operator = ( T * pElement );
62 /**
63 * Release the reference and set the new one from rObj.
65 inline UniReference< T > & operator = ( const UniReference< T > & rRef );
67 /**
68 * Return the pointer to the element, may be null.
70 inline T* operator -> () const;
72 /**
73 * Returns true if the pointer to the element is valid.
75 inline sal_Bool is() const;
77 /**
78 * Return true if both elements refer to the same object.
80 inline sal_Bool operator == ( const UniReference & rRef ) const;
82 /**
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.
89 <br>
90 @return <b>un</b>acquired implementation pointer
92 inline T* get() const;
95 class XMLOFF_DLLPUBLIC UniRefBase
97 private:
98 /**
99 * The reference counter.
101 oslInterlockedCount m_refCount;
103 public:
104 UniRefBase() : m_refCount( 0 )
106 virtual ~UniRefBase();
108 void acquire() { osl_atomic_increment( &m_refCount ); }
109 void release();
113 ///////////////////////////////////////////////////////////////////////////////
115 // Inline-implementations of UniReference
118 /** Create a new reference with the same element as in rRef and acquire this one.*/
119 template< class T >
120 inline UniReference< T >::UniReference( const UniReference< T > & rRef )
121 : mpElement( rRef.mpElement )
123 if( mpElement )
124 mpElement->acquire();
127 template< class T >
128 inline UniReference< T >::~UniReference()
130 if( mpElement )
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.
138 template< class T >
139 inline UniReference< T >::UniReference( T * pElement )
140 : mpElement( pElement )
142 if( mpElement )
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>
150 template< class T >
151 inline UniReference< T > & UniReference< T >::operator = ( T * pElement )
153 if( pElement )
154 pElement->acquire();
155 if( mpElement )
156 mpElement->release();
158 mpElement = pElement;
160 return *this;
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>
167 template< class T >
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.
176 template< class T >
177 inline T* UniReference< T >::operator -> () const
179 return get();
183 * Return the pointer to the interface, may be null.
185 template< class T >
186 inline T* UniReference< T >::get () const
188 return static_cast< T * >( mpElement );
192 * Returns true if the pointer to the interface is valid.
194 template< class T >
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
205 template< class T >
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
216 template< class T >
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: */