PageRenderTime 29ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 21#include "AutoPtrHandle.h"
 22
 23using namespace AUTOPTR;
 24
 25CAutoPtrHandle::CAutoPtrHandle(HANDLE hHandle)
 26    : m_hHandle(hHandle)
 27{}
 28
 29CAutoPtrHandle::~CAutoPtrHandle(void)
 30{
 31  Cleanup();
 32}
 33
 34CAutoPtrHandle::operator HANDLE()
 35{
 36  return m_hHandle;
 37}
 38
 39void CAutoPtrHandle::attach(HANDLE hHandle)
 40{
 41  Cleanup();
 42  m_hHandle = hHandle;
 43}
 44
 45HANDLE CAutoPtrHandle::release()
 46{
 47  HANDLE hTmp = m_hHandle;
 48  m_hHandle = INVALID_HANDLE_VALUE;
 49  return hTmp;
 50}
 51
 52void CAutoPtrHandle::Cleanup()
 53{
 54  if ( isValid() )
 55  {
 56    CloseHandle(m_hHandle);
 57    m_hHandle = INVALID_HANDLE_VALUE;
 58  }
 59}
 60
 61bool CAutoPtrHandle::isValid() const
 62{
 63  if ( INVALID_HANDLE_VALUE != m_hHandle)
 64    return true;
 65  return false;
 66}
 67void CAutoPtrHandle::reset()
 68{
 69  Cleanup();
 70}
 71
 72//-------------------------------------------------------------------------------
 73CAutoPtrFind ::CAutoPtrFind(HANDLE hHandle)
 74    : CAutoPtrHandle(hHandle)
 75{}
 76CAutoPtrFind::~CAutoPtrFind(void)
 77{
 78  Cleanup();
 79}
 80
 81void CAutoPtrFind::Cleanup()
 82{
 83  if ( isValid() )
 84  {
 85    FindClose(m_hHandle);
 86    m_hHandle = INVALID_HANDLE_VALUE;
 87  }
 88}
 89
 90//-------------------------------------------------------------------------------
 91CAutoPtrSocket::CAutoPtrSocket(SOCKET hSocket)
 92    : m_hSocket(hSocket)
 93{}
 94
 95CAutoPtrSocket::~CAutoPtrSocket(void)
 96{
 97  Cleanup();
 98}
 99
100CAutoPtrSocket::operator SOCKET()
101{
102  return m_hSocket;
103}
104
105void CAutoPtrSocket::attach(SOCKET hSocket)
106{
107  Cleanup();
108  m_hSocket = hSocket;
109}
110
111SOCKET CAutoPtrSocket::release()
112{
113  SOCKET hTmp = m_hSocket;
114  m_hSocket = INVALID_SOCKET;
115  return hTmp;
116}
117
118void CAutoPtrSocket::Cleanup()
119{
120  if ( isValid() )
121  {
122    closesocket(m_hSocket);
123    m_hSocket = INVALID_SOCKET;
124  }
125}
126
127bool CAutoPtrSocket::isValid() const
128{
129  if ( INVALID_SOCKET != m_hSocket)
130    return true;
131  return false;
132}
133void CAutoPtrSocket::reset()
134{
135  Cleanup();
136}