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