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

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

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1#ifndef _theplu_yat_utility_smart_ptr_
2#define _theplu_yat_utility_smart_ptr_
3
4// $Id: SmartPtr.h 1487 2008-09-10 08:41:36Z 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 yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "yat_assert.h"
26
27namespace theplu {
28namespace yat {
29namespace utility {
30
31  /**
32     SmartPtr is a wrapper around a pointer. Default SmartPtr is set
33     to be owner of pointer, which implies pointer will be deleted in
34     destructor. Pointer can be shared between many SmartPtr, in which
35     case a counter is kept updated in copying and assignment telling
36     how many owners there are. When the counter reaches zero, the
37     pointer is deleted.
38  */
39  template<typename T>
40  class SmartPtr
41  {
42  public:
43    /**
44       \brief Constructor
45
46       \param p underlying pointer
47       \param owner if true SmartPtr will be owner of pointer and if
48       there is no more owner delete it in destructor.
49
50       Never use this constructor to create two SmartPtr to the same
51       pointer such as
52       \code
53       MyClass* my_pointer = new MyClass;
54       SmartPtr<MyClass> sp(my_pointer);
55       SmartPtr<MyClass> sp2(my_pointer); // this is evil
56       \endcode
57       since this will cause multiple deletion. Instead use copy constructor
58       \code
59       MyClass* my_pointer = new MyClass;
60       SmartPtr<MyClass> sp(my_pointer);
61       SmartPtr<MyClass> sp2(sp);
62       \endcode
63       so the internal reference counter is updated.
64    */
65    explicit SmartPtr(T* p=NULL, bool owner=true)
66      : pointee_(p) 
67    {
68      if (owner)
69        ref_count_ = new unsigned int(1);
70      else
71        ref_count_ = NULL;
72    }
73
74    /**
75       \brief Copy constructor
76     */
77    SmartPtr(const SmartPtr& other)
78      : pointee_(other.pointee_), ref_count_(other.ref_count_)
79    {
80      if (ref_count_)
81        ++(*ref_count_);
82    }
83   
84    /**
85       \brief Destructor
86
87       If SmartPtr is owner and the only owner, underlying pointer is deleted.
88     */
89    virtual ~SmartPtr()
90    {
91      detach();
92    }
93
94    /**
95       If SmartPtr is owner and the only owner, underlying pointer is deleted.
96
97       If rhs is owner, lhs become also owner.
98     */
99    SmartPtr& operator=(const SmartPtr& rhs)
100    {
101      if (pointee_!=rhs.pointee_){
102        detach();
103        pointee_ = rhs.pointee_;
104        ref_count_= rhs.ref_count_;
105        if (ref_count_)
106          ++(*ref_count_);
107      }
108      return *this;
109    }
110
111    /**
112       \return underlying pointer
113     */
114    T* operator->(void) const
115    {
116      return pointee_;
117    }
118
119    /**
120       \return reference to underlying object
121     */
122    T& operator*(void) const
123    {
124      return *pointee_;
125    }
126
127  private:
128    T* pointee_;
129    unsigned int* ref_count_;
130
131    void detach(void)
132    {
133      if (ref_count_)
134        if (!--(*ref_count_))
135          delete pointee_;
136    }     
137  };
138
139 
140
141
142
143
144}}} // of namespace utility, yat, and theplu
145
146#endif
Note: See TracBrowser for help on using the repository browser.