xref: /trunk/main/formula/inc/formula/intruref.hxx (revision 5116778e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef FORMULA_INTRUREF_HXX
25 #define FORMULA_INTRUREF_HXX
26 
27 namespace formula
28 {
29 
30 /** A simple intrusive refcounting template, not thread safe, but therefore
31     also a bit faster than boost's smart_ptr or uno::Reference equivalents, and
32     the type to be refcounted has full control over its behavior.
33 
34     Mainly used in formula compiler and interpreter context, e.g. ScTokenRef,
35     ScMatrixRef.
36 
37     Type T must implement methods IncRef() and DecRef(), in case typename T is
38     const they must be const as well and the reference counter be mutable.
39   */
40 template< typename T > class SimpleIntrusiveReference
41 {
42     T* p;
43 public:
SimpleIntrusiveReference()44     inline SimpleIntrusiveReference() : p(0) {}
SimpleIntrusiveReference(const SimpleIntrusiveReference & r)45     inline SimpleIntrusiveReference( const SimpleIntrusiveReference& r )
46     {
47         p = r.p;
48         if ( p )
49             p->IncRef();
50     }
SimpleIntrusiveReference(T * t)51     inline SimpleIntrusiveReference( T *t )
52     {
53         p = t;
54         if ( p )
55             t->IncRef();
56     }
Clear()57     inline void Clear()
58     {
59         if ( p )
60         {
61             p->DecRef();
62             p = 0;
63         }
64     }
~SimpleIntrusiveReference()65     inline ~SimpleIntrusiveReference()
66     {
67         if ( p )
68             p->DecRef();
69     }
operator =(T * t)70     inline SimpleIntrusiveReference& operator=( T* t )
71     {
72         if ( t )
73             t->IncRef();
74         if ( p )
75             p->DecRef();
76         p = t;
77         return *this;
78     }
operator =(const SimpleIntrusiveReference & r)79     inline SimpleIntrusiveReference& operator=( const SimpleIntrusiveReference& r )
80     {
81         *this = r.p;
82         return *this;
83     }
Is() const84     inline bool Is() const              { return p != 0; }
operator !() const85     inline bool operator ! () const     { return p == 0; }
operator &() const86     inline T* operator&() const         { return p; }
operator ->() const87     inline T* operator->() const        { return p; }
operator *() const88     inline T& operator*() const         { return *p; }
operator T*() const89     inline operator T*() const          { return p; }
get() const90     inline T* get() const               { return p; }
91 };
92 // =============================================================================
93 } // formula
94 // =============================================================================
95 
96 #endif // SC_INTRUREF_HXX
97 
98