source: trunk/yat/utility/SmartPtr.h @ 1486

Last change on this file since 1486 was 1486, checked in by Jari Häkkinen, 13 years ago

Addresses #436.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1#ifndef _theplu_yat_utility_smart_ptr_
2#define _theplu_yat_utility_smart_ptr_
3
4// $Id: SmartPtr.h 1486 2008-09-09 21:17:19Z jari $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "yat_assert.h"
28
29namespace theplu {
30namespace yat {
31namespace utility {
32
33  /**
34     SmartPtr is a wrapper around a pointer. Default SmartPtr is set
35     to be owner of pointer, which implies pointer will be deleted in
36     destructor. Pointer can be shared between many SmartPtr, in which
37     case a counter is kept updated in copying and assignment telling
38     how many owners there are. When the counter reaches zero, the
39     pointer is deleted.
40  */
41  template<typename T>
42  class SmartPtr
43  {
44  public:
45    /**
46       \brief Constructor
47
48       \param p underlying pointer
49       \param owner if true SmartPtr will be owner of pointer and if
50       there is no more owner delete it in destructor.
51
52       Never use this constructor to create two SmartPtr to the same
53       pointer such as
54       \code
55       MyClass* my_pointer = new MyClass;
56       SmartPtr<MyClass> sp(my_pointer);
57       SmartPtr<MyClass> sp2(my_pointer); // this is evil
58       \endcode
59       since this will cause multiple deletion. Instead use copy constructor
60       \code
61       MyClass* my_pointer = new MyClass;
62       SmartPtr<MyClass> sp(my_pointer);
63       SmartPtr<MyClass> sp2(sp);
64       \endcode
65       so the internal reference counter is updated.
66    */
67    explicit SmartPtr(T* p=NULL, bool owner=true)
68      : pointee_(p) 
69    {
70      if (owner)
71        ref_count_ = new unsigned int(1);
72      else
73        ref_count_ = NULL;
74    }
75
76    /**
77       \brief Copy constructor
78     */
79    SmartPtr(const SmartPtr& other)
80      : pointee_(other.pointee_), ref_count_(other.ref_count_)
81    {
82      if (ref_count_)
83        ++(*ref_count_);
84    }
85   
86    /**
87       \brief Destructor
88
89       If SmartPtr is owner and the only owner, underlying pointer is deleted.
90     */
91    virtual ~SmartPtr()
92    {
93      detach();
94    }
95
96    /**
97       If SmartPtr is owner and the only owner, underlying pointer is deleted.
98
99       If rhs is owner, lhs become also owner.
100     */
101    SmartPtr& operator=(const SmartPtr& rhs)
102    {
103      if (pointee_!=rhs.pointee_){
104        detach();
105        pointee_ = rhs.pointee_;
106        ref_count_= rhs.ref_count_;
107        if (ref_count_)
108          ++(*ref_count_);
109      }
110      return *this;
111    }
112
113    /**
114       \return underlying pointer
115     */
116    T* operator->(void) const
117    {
118      return pointee_;
119    }
120
121    /**
122       \return reference to underlying object
123     */
124    T& operator*(void) const
125    {
126      return *pointee_;
127    }
128
129  private:
130    T* pointee_;
131    unsigned int* ref_count_;
132
133    void detach(void)
134    {
135      if (ref_count_)
136        if (!--(*ref_count_))
137          delete pointee_;
138    }     
139  };
140
141 
142
143
144
145
146}}} // of namespace utility, yat, and theplu
147
148#endif
Note: See TracBrowser for help on using the repository browser.