handlemanager.cpp
来自「IO函数调用测试」· C++ 代码 · 共 268 行
CPP
268 行
#include "stdafx.h"
#include "afxtempl.h"
#include "TraceEvent.h"
#include "handle.h"
#include "HandleManager.h"
HandleList HandleManager::hList;
/****************************************************************************
* HandleManager::Find
* Inputs:
* HANDLE h: Handle to locate
* Result: CHandle *
* CHandle object of entry matching h, or NULL
****************************************************************************/
CHandle * HandleManager::Find(HANDLE h)
{
if(hList.IsEmpty())
return NULL;
POSITION p = hList.GetHeadPosition();
while(p != NULL)
{ /* loop */
CHandle * e = hList.GetNext(p);
if(e->h == h)
return e;
} /* loop */
return NULL; // not found
}
/****************************************************************************
* HandleManager:Find
* Inputs:
* CString & s: Name of handle to find
* Result: CHandle *
* Reference to handle, or NULL if not found
****************************************************************************/
CHandle * HandleManager::Find(CString & s)
{
if(hList.IsEmpty())
return NULL;
POSITION p = hList.GetHeadPosition();
while(p != NULL)
{ /* loop */
CHandle * h = hList.GetNext(p);
if(h->Name == s)
return h;
} /* loop */
return NULL; // not found
}
/****************************************************************************
* HandleManager::Append
* Inputs:
* CHandle * ho: Handle object to append
* Result: POSITION
* Position in which handle was added to list
* Effect:
* Addes the handle to the list.
****************************************************************************/
POSITION HandleManager::Append(CHandle * ho)
{
return hList.AddTail(ho);
}
/****************************************************************************
* HandleManager::Append
* Inputs:
* TraceEvent * e: Trace event to be added
* Result: POSITION
*
* Effect:
* Creates a CHandle object for the event
****************************************************************************/
POSITION HandleManager::Append(TraceEvent * e, HANDLE h)
{
if(e == NULL)
return (POSITION)NULL;
CString creator = e->display();
CString rawname = e->name();
CString name = makeUnique(rawname);
CHandle * nh = new CHandle(h, name, creator, e);
return Append(nh);
}
/****************************************************************************
* HandleManager::Remove
* Inputs:
* HANDLE h: Handle to be removed
* Result: BOOL
* TRUE if success
* FALSE if close failed
* Effect:
* Removes the handle from the handle descriptor table. Destroys the
* CHandle object, but leaves the TraceEvent object alone. Destroying
* the CHandle object closes the handle
****************************************************************************/
BOOL HandleManager::Remove(HANDLE h)
{
POSITION p = hList.GetHeadPosition();
while(p != NULL)
{ /* loop */
POSITION lastp = p;
CHandle * e = hList.GetNext(p);
if(e->h == h)
{ /* delete it */
return Remove(lastp);
} /* delete it */
} /* loop */
ASSERT(FALSE); // should never be called if handle not in list
return FALSE;
}
/****************************************************************************
* HandleManager::Remove
* Inputs:
* POSITION p: Position of Handle Object to remove
* Result: BOOL
* TRUE if successful, FALSE if error
* Effect:
* Closes the handle associated with the handle object, deletes the
* handle object.
****************************************************************************/
BOOL HandleManager::Remove(POSITION p)
{
CHandle * e = hList.GetAt(p);
if(e->h != NULL)
{ /* has handle */
BOOL closeok;
DWORD err;
// THis handles the exception that is raised if a handle
// is closed already
// STATUS_INVALID_HANDLE is the assumed exception
__try {
closeok = ::CloseHandle(e->h);
err = ::GetLastError();
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
closeok = FALSE;
err = STATUS_INVALID_HANDLE; // ::GetLastError not set on exception
}
if(closeok || err == STATUS_INVALID_HANDLE)
{ /* closed */
e->h = NULL; // avoid duplicate CloseHandle in destructor
if(e == current_handle)
current_handle = NULL;
hList.RemoveAt(p);
delete e;
return TRUE;
} /* closed */
else
return FALSE;
} /* has handle */
else
{ /* just remove */
hList.RemoveAt(p);
delete e;
return TRUE; // no handle, so succeeded
} /* just remove */
}
/****************************************************************************
* HandleManager::~HandleManager
* Effect:
* Deletes the object
****************************************************************************/
HandleManager::~HandleManager()
{
Clear();
}
/****************************************************************************
* HandleManager::makeUnique
* Inputs:
* CString & s: String to make unique version of
* Result: CString
* Unique string
* Effect:
* May create a string of the form s(i) where i is a unique integer
****************************************************************************/
CString HandleManager::makeUnique(CString & s)
{
if(isUnique(s))
return s;
for(int i = 2; ; i++)
{ /* make unique */
CString t;
t.Format(_T("%s(%d)"), (LPCTSTR)s, i);
if(isUnique(t))
return t;
} /* make unique */
return s; // should never get here...
}
/****************************************************************************
* HandleManager::isUnique
* Inputs:
* CString * s: Reference to a string
* Result: BOOL
* TRUE if there is no instance of the string as a short name in the
* handle list, FALSE otherwise
****************************************************************************/
BOOL HandleManager::isUnique(CString & s)
{
for(POSITION p = hList.GetHeadPosition(); p != NULL; )
{ /* loop s */
CHandle * ho = hList.GetNext(p);
if(ho != NULL)
{ /* check it */
if(ho->Name == s)
return FALSE;
} /* check it */
} /* loop s */
return TRUE;
}
/****************************************************************************
* HandleManager::getCurrentHandle
* Result: CHandle *
* Current handle, or NULL
****************************************************************************/
CHandle * HandleManager::getCurrentHandle()
{
return current_handle;
}
/****************************************************************************
* HandleManager::setCurrentHandle
* Inputs:
* CHandle * ho: New handle to set as current handle
* Result: CHandle *
* Previous value of current_handle
* Effect:
* Sets the new handle as the current handle
****************************************************************************/
CHandle * HandleManager::setCurrentHandle(CHandle * ho)
{
CHandle * result = current_handle;
current_handle = ho;
return result;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?