ref_ptr.h 6.38 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2015, Lawrence Livermore National Security, LLC
hrchilds's avatar
hrchilds committed
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
hrchilds's avatar
hrchilds committed
6 7
* All rights reserved.
*
brugger's avatar
 
brugger committed
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
hrchilds's avatar
hrchilds committed
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
brugger's avatar
 
brugger committed
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
hrchilds's avatar
hrchilds committed
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
brugger's avatar
 
brugger committed
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
hrchilds's avatar
hrchilds committed
30 31 32 33 34 35 36 37 38
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

hrchilds's avatar
hrchilds committed
39 40 41
#ifndef REF_PTR_H
#define REF_PTR_H

hrchilds's avatar
hrchilds committed
42
#include <visitstream.h>
camp's avatar
camp committed
43
#include "VisItAtomic.h"
hrchilds's avatar
hrchilds committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

// ****************************************************************************
//  Class: ref_ptr
//
//  Purpose:
//      Reference counted pointer.  
//      This is similar to the STL auto_ptr, but it is actually useful.
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//
//    Hank Childs, Tue Oct 17 13:52:17 PDT 2000
//    Fixed bug with assigning new pointer and not decreasing reference count
//    for previous pointer and added routines Add- and RemoveReference.  Also
//    added a conversion function (Copy) for downcasting and changed the
//    ostream operator to Print to avoid warnings.
//
//    Jeremy Meredith, Tue Jun  5 13:04:16 PDT 2001
//    Added a check for equality so that self-assignment is a no-op.
//
//    Brad Whitlock, Thu Apr 4 14:17:44 PST 2002
//    Added GetN method and removed CopyTo method to get around the Windows
//    compiler.
//
//    Hank Childs, Tue Sep 24 09:29:57 PDT 2002
//    Remove references to VoidRef, since it is getting its own class.
//
hrchilds's avatar
hrchilds committed
73 74 75
//    Brad Whitlock, Tue Aug 10 17:06:50 PST 2004
//    Removed some Windows API stuff that was not necessary.
//
76 77 78
//    Tom Fogal, Tue Jun 23 20:05:12 MDT 2009
//    Made `source' argument in CopyTo const.
//
camp's avatar
camp committed
79 80 81
//    David Camp, Tue Mar 12 13:19:37 PDT 2013
//    Made the code thread safe. Changed the int to be an atomic type.
//
hrchilds's avatar
hrchilds committed
82 83 84
// ****************************************************************************

template <class T>
hrchilds's avatar
hrchilds committed
85
class ref_ptr
hrchilds's avatar
hrchilds committed
86 87 88
{
 public:
    ref_ptr();
camp's avatar
camp committed
89
    ref_ptr(T *p_, VISIT_ATOMIC_TYPE * = NULL);
hrchilds's avatar
hrchilds committed
90 91 92 93 94 95 96 97
    ref_ptr(const ref_ptr<T> &rhs);
    ~ref_ptr();
    void operator=(T *rhs);
    void operator=(const ref_ptr<T> &rhs);
    T *operator*();
    const T *operator*() const;
    T *operator->();
    const T *operator->() const;
camp's avatar
camp committed
98
    VISIT_ATOMIC_TYPE *GetN() const;
hrchilds's avatar
hrchilds committed
99
    void Print(ostream&);
camp's avatar
camp committed
100 101 102 103 104
    void   SetReference(T *p_)
    {
        RemoveReference();
        AddReference(p_, NULL);
    }
hrchilds's avatar
hrchilds committed
105
 private:
camp's avatar
camp committed
106 107
    T                 *p;
    VISIT_ATOMIC_TYPE *n;
hrchilds's avatar
hrchilds committed
108

camp's avatar
camp committed
109
    void   AddReference(T *, VISIT_ATOMIC_TYPE *);
hrchilds's avatar
hrchilds committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    void   RemoveReference(void);
};

// ****************************************************************************
//  Inline Methods
// ****************************************************************************
template <class T>
inline
ref_ptr<T>::ref_ptr()
{
    AddReference(NULL, NULL);
}

template <class T>
inline
camp's avatar
camp committed
125
ref_ptr<T>::ref_ptr(T *p_, VISIT_ATOMIC_TYPE *n_)
hrchilds's avatar
hrchilds committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
{
    AddReference(p_, n_);
}

template <class T>
inline
ref_ptr<T>::ref_ptr(const ref_ptr<T> &rhs)
{
    AddReference(rhs.p, rhs.n);
}

template <class T>
inline
ref_ptr<T>::~ref_ptr()
{
    RemoveReference();
}

template <class T>
inline void
ref_ptr<T>::operator=(T *rhs)
{
    RemoveReference();
    AddReference(rhs, NULL);
}

template <class T>
inline void
ref_ptr<T>::operator=(const ref_ptr<T> &rhs)
{
    if (p == rhs.p && n == rhs.n)
        return;
    RemoveReference();
    AddReference(rhs.p, rhs.n);
}

template <class T>
inline T *
ref_ptr<T>::operator*()
{
    return p;
}

template <class T>
inline const T *
ref_ptr<T>::operator*() const
{
    return p;
}

template <class T>
inline T *
ref_ptr<T>::operator->()
{
    return p;
}

template <class T>
inline const T *
ref_ptr<T>::operator->() const
{
    return p;
}

template <class T>
camp's avatar
camp committed
191
inline VISIT_ATOMIC_TYPE *
hrchilds's avatar
hrchilds committed
192 193 194 195 196 197 198
ref_ptr<T>::GetN() const
{
    return n;
}

template <class T>
void
camp's avatar
camp committed
199
ref_ptr<T>::AddReference(T *p_, VISIT_ATOMIC_TYPE *n_)
hrchilds's avatar
hrchilds committed
200 201 202 203 204 205
{
    p = p_;
    if (p != NULL)
    {
        if (n_ == NULL)
        {
camp's avatar
camp committed
206
            n = new VISIT_ATOMIC_TYPE(1); 
hrchilds's avatar
hrchilds committed
207 208 209 210
        }
        else
        {
            n = n_;
camp's avatar
camp committed
211
            AtomicInc(*n);
hrchilds's avatar
hrchilds committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225
        }
    }
    else
    {
        n = NULL;
    }
}

template <class T>
void
ref_ptr<T>::RemoveReference(void)
{
    if (p)
    {
camp's avatar
camp committed
226 227
        VISIT_ATOMIC_TYPE a = AtomicDec(*n);
        if (a == 0)
hrchilds's avatar
hrchilds committed
228 229 230 231 232 233 234 235 236 237 238
        {
            delete p;
            delete n;
        }
    }
}

template <class T>
inline void
ref_ptr<T>::Print(ostream &out)
{
hrchilds's avatar
hrchilds committed
239
    if ((n!=NULL) && (p!=NULL))
hrchilds's avatar
hrchilds committed
240
        out << "<" << *n << ", " << (void*) p << ">";
hrchilds's avatar
hrchilds committed
241 242
    else
        out << "<NULL>";
hrchilds's avatar
hrchilds committed
243 244 245 246 247
}


template <class S, class T>
inline void
248
CopyTo(ref_ptr<S> &lhs, const ref_ptr<T> &rhs)
hrchilds's avatar
hrchilds committed
249 250 251 252 253 254
{
    ref_ptr<S> newone((S *)*rhs, rhs.GetN());
    lhs = newone;
}

#endif