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

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

Made the project to compile on my Intel Mac running Leopard.

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