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

Last change on this file since 1133 was 1133, checked in by Peter, 14 years ago

using SmartPtr? in KernelLookup? rather than doing the ref count myself

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