Click here to Skip to main content
15,883,758 members
Articles / Desktop Programming / MFC

Win32 File Name Iteration Boost Way

Rate me:
Please Sign up or sign in to vote.
4.71/5 (10 votes)
3 Nov 2005CPOL2 min read 56.9K   397   17  
A Port of ::FindFirstFile to Boost.Range and Boost.Foreach
#pragma once

// Model of:
//   Readable and Single Pass (Input) Range of WIN32_FIND_DATA

#include <cassert>
#include <boost/mpl/bool.hpp>
#include <boost/noncopyable.hpp>
#include <boost/range/iterator_range.hpp>
#include "find_file_iterator.hpp"

namespace find_file {

	namespace detail {

		// See: Boost.BaseFromMember
		struct find_file_range_initializer
		{
		protected:
			HANDLE m_hFind;
			WIN32_FIND_DATA m_data;

			explicit find_file_range_initializer(LPCTSTR pszName)
			{
				m_hFind = ::FindFirstFile(pszName, &m_data);
			}

			~find_file_range_initializer()
			{
				if (m_hFind != INVALID_HANDLE_VALUE)
					::FindClose(m_hFind);
			}
		};

	} // namespace detail

struct find_file_range  :
	private boost::noncopyable,
	private detail::find_file_range_initializer,
	boost::iterator_range<find_file_iterator>
{
private:
	typedef boost::iterator_range<find_file_iterator> super_t;

public:
	explicit find_file_range(LPCTSTR pszName) :
		find_file_range_initializer(pszName),
		super_t(
			find_file_iterator(m_hFind, m_data),
			find_file_iterator()
		)
	{ }
};

} // namespace find_file

// Workaround:
//   As find_file_range is derived from iterator_range,
//   foreach-engine mistakes find_file_range for iterator_range
//   that is copyable. Caution!
namespace boost { namespace foreach_detail_ {

inline boost::mpl::false_ *cheap_copy(find_file::find_file_range *) { return 0; }

} } // namespace boost::foreach_detail_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Japan Japan
I am worried about my poor English...

Comments and Discussions