ScopedHandle.h
Go to the documentation of this file.
1 //===========================================================================
2 /*!
3  *
4  *
5  * \brief A scoped_ptr like container for C type handles
6  *
7  *
8  * \par
9  * This class provides RAII handle management to Shark
10  *
11  *
12  *
13  *
14  * \author B. Li
15  * \date 2012
16  *
17  *
18  * \par Copyright 1995-2017 Shark Development Team
19  *
20  * <BR><HR>
21  * This file is part of Shark.
22  * <http://shark-ml.org/>
23  *
24  * Shark is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU Lesser General Public License as published
26  * by the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * Shark is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  * GNU Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public License
35  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
36  *
37  */
38 //===========================================================================
39 
40 #ifndef SHARK_CORE_UTILITY_SCOPED_HANDLE_H
41 #define SHARK_CORE_UTILITY_SCOPED_HANDLE_H
42 
43 #include "shark/Core/Exception.h"
44 
45 #include <boost/assert.hpp>
46 #include <boost/bind/arg.hpp>
47 #include <boost/format.hpp>
48 #include <boost/function.hpp>
49 #include <boost/lambda/lambda.hpp>
50 #include <boost/noncopyable.hpp>
51 
52 namespace shark {
53 
54 /// A handle container which usually taking C style handle such as file Id
55 /// @tparam T The type of handle the container holds
56 template <typename T>
57 class ScopedHandle : private boost::noncopyable
58 {
59 public:
60 
61  /// The typedef of deleter for type a T
62  typedef boost::function<void (const T&) > DeleterType;
63 
64  /// Type of verifier which should return true for valid handle, and false otherwise
65  typedef boost::function<bool (const T&) > VerifierType;
66 
67  /// Constructor
68  /// @param handle
69  /// The handle container will hold
70  /// @param deleter
71  /// The deleter used for freeing a handle which should return true for valid handles, false otherwise
72  /// @param handleDescription
73  /// A description of handle for easy debugging in case of validation failure
75  const T& handle,
76  const DeleterType& deleter,
77  const std::string& handleDescription = "")
78  :
79  m_handle(handle),
80  m_isValidHandle(true), // null verifier means valid handle
81  m_deleter(deleter)
82  {
83  BOOST_ASSERT(deleter);
84  if (!m_isValidHandle)
85  throw SHARKEXCEPTION((boost::format("%s (FAILED)") % handleDescription).str());
86  }
87 
89  {
90  if (m_isValidHandle)
91  m_deleter(m_handle);
92  }
93 
94  /// The only way to access handle externally
95  const T& operator*() const { return m_handle; }
96 
97 private:
98 
99  const T m_handle;
100  const bool m_isValidHandle;
101  const DeleterType m_deleter;
102 };
103 
104 } // namespace shark {
105 
106 #endif // SHARK_CORE_SCOPED_HANDLE_H