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

Last change on this file since 1048 was 1048, checked in by Peter, 13 years ago

well, make it English...

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.3 KB
Line 
1#ifndef _theplu_yat_utility_smart_ptr_
2#define _theplu_yat_utility_smart_ptr_
3
4// $Id: SmartPtr.h 1048 2008-02-07 13:04:26Z 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
29namespace theplu {
30namespace yat {
31namespace utility {
32
33  /**
34  */
35  template<typename T>
36  class SmartPtr
37  {
38  public:
39    /**
40       \brief Constructor
41    */
42    explicit SmartPtr(T* p=NULL, bool owner=true)
43      : pointee_(p) 
44    {
45      if (owner)
46        ref_count_ = new u_int(1);
47    }
48
49    /**
50       \brief Copy constructor
51     */
52    SmartPtr(const SmartPtr& other)
53      : pointee_(other.pointee_), ref_count_(other.ref_count_)
54    {
55      if (ref_count_)
56        ++(*ref_count_);
57    }
58   
59    /**
60       \brief Destructor
61
62       If SmartPtr is owner and the only owner, underlying pointer is deleted.
63     */
64    virtual ~SmartPtr()
65    {
66      detach();
67    }
68
69    /**
70       If SmartPtr is owner and the only owner, underlying pointer is deleted.
71
72       If rhs is owner, lhs become also owner.
73     */
74    SmartPtr& operator=(const SmartPtr& rhs)
75    {
76      if (pointee_!=rhs.pointee_){
77        detach();
78        pointee_ = rhs.pointee_;
79        ref_count_= rhs.ref_count_;
80        if (ref_count_)
81          ++(*ref_count_);
82      }
83      return *this;
84    }
85
86    /**
87       \return underlying pointer
88     */
89    T* operator->(void) const
90    {
91      return pointee_;
92    }
93
94    /**
95       \return reference to underlying object
96     */
97    T& operator*(void) const
98    {
99      return *pointee_;
100    }
101
102  private:
103    T* pointee_;
104    u_int* ref_count_;
105
106    void detach(void)
107    {
108      if (ref_count_)
109        if (!--(*ref_count_))
110          delete pointee_;
111    }     
112  };
113
114 
115
116
117
118
119}}} // of namespace utility, yat, and theplu
120
121#endif
Note: See TracBrowser for help on using the repository browser.