This is my first attempt at creating an STL-style iterator. Is this code ok?
std::set<std::wstring> getVolumeNames()
{
class FindVolumeWrapper : public std::iterator<std::input_iterator_tag, std::wstring>
{
WCHAR m_curVolName[MAX_PATH];
HANDLE m_hFindVol;
DWORD m_dwError;
public:
// start iterator
FindVolumeWrapper(bool)
: m_hFindVol(::FindFirstVolumeW(m_curVolName, ARRAYSIZE(m_curVolName)))
, m_dwError(::GetLastError())
{
if (m_hFindVol == INVALID_HANDLE_VALUE && m_dwError != ERROR_NO_MORE_FILES)
{
throw AutoWinError(m_dwError);
}
if (m_dwError == ERROR_MORE_DATA)
{
m_dwError = ERROR_SUCCESS;
}
}
// end iterator
FindVolumeWrapper()
: m_hFindVol(INVALID_HANDLE_VALUE)
, m_dwError(ERROR_NO_MORE_FILES)
{
m_curVolName[0] = L'\0';
}
FindVolumeWrapper& GetNextVolume()
{
BOOL bSuccess = ::FindNextVolumeW(m_hFindVol,
m_curVolName,
ARRAYSIZE(m_curVolName));
if (!bSuccess)
{
m_dwError = ::GetLastError();
::FindVolumeClose(m_hFindVol);
if (m_dwError != ERROR_NO_MORE_FILES)
{
throw AutoWinError(m_dwError);
}
}
return *this;
}
FindVolumeWrapper& operator ++ ()
{
return GetNextVolume();
}
std::wstring operator * ()
{
return m_curVolName;
}
// equal if either both good and point to same element, or both faulty
bool operator == (FindVolumeWrapper const& other)
{
return (this->m_dwError == ERROR_SUCCESS
&& other.m_dwError == ERROR_SUCCESS
&& wcscmp(m_curVolName, other.m_curVolName) == 0)
|| (this->m_dwError != ERROR_SUCCESS && other.m_dwError != ERROR_SUCCESS);
}
bool operator != (FindVolumeWrapper const& other)
{
return !operator == (other);
}
};
std::set<std::wstring> volumeNames(FindVolumeWrapper(true), FindVolumeWrapper());
return volumeNames;
}