Counted.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/common/Counted.h
22 
23  \brief A counted class keeps track of how many abstract instances are pointing to it.
24 */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_COUNTED_H
27 #define __TERRALIB_COMMON_INTERNAL_COUNTED_H
28 
29 // Boost
30 #include <boost/noncopyable.hpp>
31 
32 namespace te
33 {
34  namespace common
35  {
36  /*!
37  \class Counted
38 
39  \brief A counted class keeps track of how many abstract instances are pointing to it.
40 
41  \ingroup common
42  */
43  class Counted : public boost::noncopyable
44  {
45  public:
46 
47  /*! \brief Constructor. */
48  Counted();
49 
50  /*! \brief Increases the number of references to this object. */
51  void attach();
52 
53  /*! \brief Decreases the number of references to this object. Destroy it if there are no more references to it. */
54  void detach();
55 
56  /*! \brief Returns the number of references to this object. */
57  int refCount();
58 
59  protected:
60 
61  /*! \brief Destructor. */
62  virtual ~Counted();
63 
64  private:
65 
66  int m_refCount; //!< The number of references to this object.
67  };
68 
70  {
71  p->attach();
72  }
73 
75  {
76  p->detach();
77  }
78 
80  : m_refCount(0)
81  {
82  }
83 
85  {
86  }
87 
88  inline void Counted::attach()
89  {
90  ++m_refCount;
91  }
92 
93  inline void Counted::detach()
94  {
95  if(--m_refCount == 0)
96  delete this;
97  }
98 
99  inline int Counted::refCount()
100  {
101  return m_refCount;
102  }
103 
104  } // end namespace common
105 } // end namespace te
106 
107 #endif // __TERRALIB_COMMON_INTERNAL_COUNTED_H
108 
void detach()
Decreases the number of references to this object. Destroy it if there are no more references to it...
Definition: Counted.h:93
Counted()
Constructor.
Definition: Counted.h:79
URI C++ Library.
void attach()
Increases the number of references to this object.
Definition: Counted.h:88
int refCount()
Returns the number of references to this object.
Definition: Counted.h:99
void intrusive_ptr_release(Counted *p)
Definition: Counted.h:74
void intrusive_ptr_add_ref(Counted *p)
Definition: Counted.h:69
int m_refCount
The number of references to this object.
Definition: Counted.h:66
virtual ~Counted()
Destructor.
Definition: Counted.h:84
A counted class keeps track of how many abstract instances are pointing to it.
Definition: Counted.h:43