Holder.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/Holder.h
22 
23  \brief An auxiliary data structure for helping to control the garbage collection of C++ objects.
24 */
25 
26 #ifndef __TERRALIB_COMMON_INTERNAL_HOLDER_H
27 #define __TERRALIB_COMMON_INTERNAL_HOLDER_H
28 
29 // Boost
30 #include <boost/noncopyable.hpp>
31 
32 namespace te
33 {
34  namespace common
35  {
36  /*!
37  \struct Holder
38 
39  \brief An auxiliary data structure for helping to control the garbage collection of C++ objects.
40 
41  \ingroup common
42  */
43  template<class T> struct Holder : public boost::noncopyable
44  {
45  T* m_handle; //!< A pointer to a C++ object.
46  bool m_isOwner; //!< If true it specifies that JsObject has the ownership of the C++ handle.
47 
48  /*!
49  \brief Initializes a new garbage collection helper object.
50 
51  \param handle A pointer to a C++ object.
52  \param isOwner If true the JsObject will have the ownership of the pointer.
53  */
54  Holder(T* handle, bool isOwner)
55  : m_handle(handle),
56  m_isOwner(isOwner)
57  {
58  }
59 
60  /*! \brief The destructor will check if it is necessary to release the C++ object handle. */
62  {
63  if(m_isOwner)
64  delete m_handle;
65  }
66 
67  T& operator*() const
68  {
69  return *m_handle;
70  }
71 
72  T* operator->() const
73  {
74  return m_handle;
75  }
76 
77  T* get() const
78  {
79  return m_handle;
80  }
81  };
82 
83  } // end namespace common
84 } // end namespace te
85 
86 #endif // __TERRALIB_COMMON_INTERNAL_HOLDER_H
87 
An auxiliary data structure for helping to control the garbage collection of C++ objects.
Definition: Holder.h:43
T & operator*() const
Definition: Holder.h:67
T * m_handle
A pointer to a C++ object.
Definition: Holder.h:45
T * operator->() const
Definition: Holder.h:72
URI C++ Library.
bool m_isOwner
If true it specifies that JsObject has the ownership of the C++ handle.
Definition: Holder.h:46
~Holder()
The destructor will check if it is necessary to release the C++ object handle.
Definition: Holder.h:61
Holder(T *handle, bool isOwner)
Initializes a new garbage collection helper object.
Definition: Holder.h:54