/xbmc/utils/AutoPtrHandle.cpp

http://github.com/xbmc/xbmc · C++ · 136 lines · 97 code · 18 blank · 21 comment · 7 complexity · a8d51253dc68048f6341d79f98321568 MD5 · raw file

  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program 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 General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with XBMC; see the file COPYING. If not, see
  17. * <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "AutoPtrHandle.h"
  21. using namespace AUTOPTR;
  22. CAutoPtrHandle::CAutoPtrHandle(HANDLE hHandle)
  23. : m_hHandle(hHandle)
  24. {}
  25. CAutoPtrHandle::~CAutoPtrHandle(void)
  26. {
  27. Cleanup();
  28. }
  29. CAutoPtrHandle::operator HANDLE()
  30. {
  31. return m_hHandle;
  32. }
  33. void CAutoPtrHandle::attach(HANDLE hHandle)
  34. {
  35. Cleanup();
  36. m_hHandle = hHandle;
  37. }
  38. HANDLE CAutoPtrHandle::release()
  39. {
  40. HANDLE hTmp = m_hHandle;
  41. m_hHandle = INVALID_HANDLE_VALUE;
  42. return hTmp;
  43. }
  44. void CAutoPtrHandle::Cleanup()
  45. {
  46. if ( isValid() )
  47. {
  48. CloseHandle(m_hHandle);
  49. m_hHandle = INVALID_HANDLE_VALUE;
  50. }
  51. }
  52. bool CAutoPtrHandle::isValid() const
  53. {
  54. if ( INVALID_HANDLE_VALUE != m_hHandle)
  55. return true;
  56. return false;
  57. }
  58. void CAutoPtrHandle::reset()
  59. {
  60. Cleanup();
  61. }
  62. //-------------------------------------------------------------------------------
  63. CAutoPtrFind ::CAutoPtrFind(HANDLE hHandle)
  64. : CAutoPtrHandle(hHandle)
  65. {}
  66. CAutoPtrFind::~CAutoPtrFind(void)
  67. {
  68. Cleanup();
  69. }
  70. void CAutoPtrFind::Cleanup()
  71. {
  72. if ( isValid() )
  73. {
  74. FindClose(m_hHandle);
  75. m_hHandle = INVALID_HANDLE_VALUE;
  76. }
  77. }
  78. //-------------------------------------------------------------------------------
  79. CAutoPtrSocket::CAutoPtrSocket(SOCKET hSocket)
  80. : m_hSocket(hSocket)
  81. {}
  82. CAutoPtrSocket::~CAutoPtrSocket(void)
  83. {
  84. Cleanup();
  85. }
  86. CAutoPtrSocket::operator SOCKET()
  87. {
  88. return m_hSocket;
  89. }
  90. void CAutoPtrSocket::attach(SOCKET hSocket)
  91. {
  92. Cleanup();
  93. m_hSocket = hSocket;
  94. }
  95. SOCKET CAutoPtrSocket::release()
  96. {
  97. SOCKET hTmp = m_hSocket;
  98. m_hSocket = INVALID_SOCKET;
  99. return hTmp;
  100. }
  101. void CAutoPtrSocket::Cleanup()
  102. {
  103. if ( isValid() )
  104. {
  105. closesocket(m_hSocket);
  106. m_hSocket = INVALID_SOCKET;
  107. }
  108. }
  109. bool CAutoPtrSocket::isValid() const
  110. {
  111. if ( INVALID_SOCKET != m_hSocket)
  112. return true;
  113. return false;
  114. }
  115. void CAutoPtrSocket::reset()
  116. {
  117. Cleanup();
  118. }