GOwnPtr.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2008 Collabora Ltd.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this library; see the file COPYING.LIB. If not, write to
  17. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. * Boston, MA 02110-1301, USA.
  19. *
  20. */
  21. #ifndef GOwnPtr_h
  22. #define GOwnPtr_h
  23. #if USE(GLIB)
  24. #include <algorithm>
  25. #include <wtf/Assertions.h>
  26. #include <wtf/Noncopyable.h>
  27. extern "C" void g_free(void*);
  28. namespace WTF {
  29. template <typename T> inline void freeOwnedGPtr(T* ptr);
  30. template<> void freeOwnedGPtr<GError>(GError*);
  31. template<> void freeOwnedGPtr<GList>(GList*);
  32. template<> void freeOwnedGPtr<GSList>(GSList*);
  33. template<> void freeOwnedGPtr<GPatternSpec>(GPatternSpec*);
  34. template<> void freeOwnedGPtr<GDir>(GDir*);
  35. template<> void freeOwnedGPtr<GTimer>(GTimer*);
  36. template<> void freeOwnedGPtr<GKeyFile>(GKeyFile*);
  37. template <typename T> class GOwnPtr {
  38. WTF_MAKE_NONCOPYABLE(GOwnPtr);
  39. public:
  40. explicit GOwnPtr(T* ptr = 0) : m_ptr(ptr) { }
  41. ~GOwnPtr() { freeOwnedGPtr(m_ptr); }
  42. T* get() const { return m_ptr; }
  43. T* release()
  44. {
  45. T* ptr = m_ptr;
  46. m_ptr = 0;
  47. return ptr;
  48. }
  49. T*& outPtr()
  50. {
  51. ASSERT(!m_ptr);
  52. return m_ptr;
  53. }
  54. void set(T* ptr)
  55. {
  56. ASSERT(!ptr || m_ptr != ptr);
  57. freeOwnedGPtr(m_ptr);
  58. m_ptr = ptr;
  59. }
  60. void clear()
  61. {
  62. T* ptr = m_ptr;
  63. m_ptr = 0;
  64. freeOwnedGPtr(ptr);
  65. }
  66. T& operator*() const
  67. {
  68. ASSERT(m_ptr);
  69. return *m_ptr;
  70. }
  71. T* operator->() const
  72. {
  73. ASSERT(m_ptr);
  74. return m_ptr;
  75. }
  76. bool operator!() const { return !m_ptr; }
  77. // This conversion operator allows implicit conversion to bool but not to other integer types.
  78. typedef T* GOwnPtr::*UnspecifiedBoolType;
  79. operator UnspecifiedBoolType() const { return m_ptr ? &GOwnPtr::m_ptr : 0; }
  80. void swap(GOwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
  81. private:
  82. T* m_ptr;
  83. };
  84. template <typename T> inline void swap(GOwnPtr<T>& a, GOwnPtr<T>& b)
  85. {
  86. a.swap(b);
  87. }
  88. template <typename T, typename U> inline bool operator==(const GOwnPtr<T>& a, U* b)
  89. {
  90. return a.get() == b;
  91. }
  92. template <typename T, typename U> inline bool operator==(T* a, const GOwnPtr<U>& b)
  93. {
  94. return a == b.get();
  95. }
  96. template <typename T, typename U> inline bool operator!=(const GOwnPtr<T>& a, U* b)
  97. {
  98. return a.get() != b;
  99. }
  100. template <typename T, typename U> inline bool operator!=(T* a, const GOwnPtr<U>& b)
  101. {
  102. return a != b.get();
  103. }
  104. template <typename T> inline typename GOwnPtr<T>::PtrType getPtr(const GOwnPtr<T>& p)
  105. {
  106. return p.get();
  107. }
  108. template <typename T> inline void freeOwnedGPtr(T* ptr)
  109. {
  110. g_free(ptr);
  111. }
  112. } // namespace WTF
  113. using WTF::GOwnPtr;
  114. #endif // USE(GLIB)
  115. #endif // GOwnPtr_h