|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI was inspired by the excellent article, Win32 File Name Iteration STL Way. I think the problem of the article is that the handle of Requirements
find_file_range
find_file_range ffrng("*.*");
typedef boost::range_result_iterator<find_file_range>::type iter_t;
for (iter_t it = boost::begin(ffrng), last = boost::end(ffrng); it != last; ++it) {
std::cout << it->cFileName << std::endl;
}
If you iterate Foreach will ComeBoost.Foreach (accepted into Boost, but not yet part of the release) provides find_file_range ffrng("*.*");
BOOST_FOREACH (WIN32_FIND_DATA& data, ffrng) {
std::cout << data.cFileName << std::endl;
}
Is this a new language? No, It's C++. The implementation is magical. If you are interested in it, check Conditional Love: FOREACH Redux written by Eric Niebler. FilteringOne class, one responsibility. find_file_range ffrng("*.*");
BOOST_FOREACH (
WIN32_FIND_DATA& data,
boost::make_filter_range(ffrng, boost::not1(find_file_is_dots()))
)
{
std::cout << data.cFileName << std::endl;
}
TransformingAs a mark of respect for the original article, find_file_range ffrng("*.*");
BOOST_FOREACH (
std::string const& filename,
boost::make_transform_range(ffrng, find_file_construct<std::string>())
)
{
std::cout << filename << std::endl;
}
Chain of Range AdaptorsThe chain of filtering and transforming is also available: find_file_range ffrng("*.*");
BOOST_FOREACH (
std::string const& filename,
boost::make_transform_range(
boost::make_filter_range(
boost::make_filter_range(
ffrng,
boost::not1(find_file_is_hidden())
),
boost::not1(find_file_is_directory())
),
find_file_stringize<std::string>()
)
)
{
std::cout << filename << std::endl;
}
This chain is lazy and functional, but unreadable. And so Boost.RangeEx provides Range Adaptors: find_file_range ffrng("*.*");
BOOST_FOREACH (
std::string const& filename,
ffrng |
boost::adaptor::filter(boost::not1(find_file_is_hidden())) |
boost::adaptor::filter(boost::not1(find_file_is_directory())) |
boost::adaptor::transform(find_file_construct<std::string>())
)
{
std::cout << filename << std::endl;
}
This is rather "procedural" and shows the future of C++. Points of Interest
std::string inputs; {
boost::copy(make_istream_range<char>(std::cin), std::back_inserter(inputs));
}
std::cout << inputs << std::endl;
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||