xref: /aoo41x/main/formula/inc/formula/intruref.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef FORMULA_INTRUREF_HXX
29 #define FORMULA_INTRUREF_HXX
30 
31 namespace formula
32 {
33 
34 /** A simple intrusive refcounting template, not thread safe, but therefore
35     also a bit faster than boost's smart_ptr or uno::Reference equivalents, and
36     the type to be refcounted has full control over its behavior.
37 
38     Mainly used in formula compiler and interpreter context, e.g. ScTokenRef,
39     ScMatrixRef.
40 
41     Type T must implement methods IncRef() and DecRef(), in case typename T is
42     const they must be const as well and the reference counter be mutable.
43   */
44 template< typename T > class SimpleIntrusiveReference
45 {
46     T* p;
47 public:
48     inline SimpleIntrusiveReference() : p(0) {}
49     inline SimpleIntrusiveReference( const SimpleIntrusiveReference& r )
50     {
51         p = r.p;
52         if ( p )
53             p->IncRef();
54     }
55     inline SimpleIntrusiveReference( T *t )
56     {
57         p = t;
58         if ( p )
59             t->IncRef();
60     }
61     inline void Clear()
62     {
63         if ( p )
64         {
65             p->DecRef();
66             p = 0;
67         }
68     }
69     inline ~SimpleIntrusiveReference()
70     {
71         if ( p )
72             p->DecRef();
73     }
74     inline SimpleIntrusiveReference& operator=( T* t )
75     {
76         if ( t )
77             t->IncRef();
78         if ( p )
79             p->DecRef();
80         p = t;
81         return *this;
82     }
83     inline SimpleIntrusiveReference& operator=( const SimpleIntrusiveReference& r )
84     {
85         *this = r.p;
86         return *this;
87     }
88     inline bool Is() const              { return p != 0; }
89     inline bool operator ! () const     { return p == 0; }
90     inline T* operator&() const         { return p; }
91     inline T* operator->() const        { return p; }
92     inline T& operator*() const         { return *p; }
93     inline operator T*() const          { return p; }
94     inline T* get() const               { return p; }
95 };
96 // =============================================================================
97 } // formula
98 // =============================================================================
99 
100 #endif // SC_INTRUREF_HXX
101 
102