Click here to Skip to main content
11,631,906 members (69,057 online)
Click here to Skip to main content
Add your own
alternative version

Win32 File Name Iteration Boost Way

, 3 Nov 2005 CPOL 39.8K 362 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)

Share

About the Author

mb2sync
Japan Japan
I am worried about my poor English...

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150728.1 | Last Updated 4 Nov 2005
Article Copyright 2005 by mb2sync
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid