Macro for C++11 range based for loop

This commit is contained in:
Daniel James
2017-12-28 17:04:34 +00:00
parent 6c836e24dd
commit 9085d2e95a
14 changed files with 77 additions and 52 deletions

View File

@@ -54,4 +54,4 @@ IncludeCategories:
Priority: 40
# Boost specific stuff
ForEachMacros: [ BOOST_FOREACH ]
ForEachMacros: [ BOOST_FOREACH, QUICKBOOK_FOR ]

View File

@@ -17,7 +17,6 @@
#include <boost/algorithm/string/replace.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/next_prior.hpp>
#include <boost/range/algorithm/replace.hpp>
@@ -25,6 +24,7 @@
#include "block_tags.hpp"
#include "document_state.hpp"
#include "files.hpp"
#include "for.hpp"
#include "grammar.hpp"
#include "markups.hpp"
#include "path.hpp"
@@ -103,7 +103,7 @@ namespace quickbook
if (qbk_version_n >= 107u) {
char const* allowed_punctuation = "_.-";
BOOST_FOREACH (char c, id) {
QUICKBOOK_FOR (char c, id) {
if (!std::isalnum(c) &&
!std::strchr(allowed_punctuation, c))
valid = false;
@@ -692,7 +692,7 @@ namespace quickbook
state.out << markup.pre;
BOOST_FOREACH (value item, list) {
QUICKBOOK_FOR (value item, list) {
state.out << "<listitem>";
state.out << item.get_encoded();
state.out << "</listitem>";
@@ -892,7 +892,7 @@ namespace quickbook
value_consumer values = image;
attributes["fileref"] = values.consume();
BOOST_FOREACH (value pair_, values) {
QUICKBOOK_FOR (value pair_, values) {
value_consumer pair = pair_;
value name = pair.consume();
value value = pair.consume();
@@ -1035,7 +1035,7 @@ namespace quickbook
state.phrase << "<imageobject><imagedata";
BOOST_FOREACH (attribute_map::value_type const& attr, attributes) {
QUICKBOOK_FOR (attribute_map::value_type const& attr, attributes) {
state.phrase << " " << attr.first << "=\"";
write_plain_text(state.phrase.get(), attr.second);
state.phrase << "\"";
@@ -1087,7 +1087,7 @@ namespace quickbook
std::string identifier = values.consume().get_quickbook().to_s();
std::vector<std::string> template_values;
BOOST_FOREACH (value const& p, values.consume()) {
QUICKBOOK_FOR (value const& p, values.consume()) {
template_values.push_back(p.get_quickbook().to_s());
}
@@ -1413,7 +1413,7 @@ namespace quickbook
std::vector<value> args;
BOOST_FOREACH (value arg, values) {
QUICKBOOK_FOR (value arg, values) {
args.push_back(arg);
}
@@ -1563,7 +1563,7 @@ namespace quickbook
detail::print_string(title, state.out.get());
state.out << "</title>\n";
BOOST_FOREACH (value_consumer entry, values) {
QUICKBOOK_FOR (value_consumer entry, values) {
state.out << "<varlistentry>";
if (entry.check()) {
@@ -1574,7 +1574,7 @@ namespace quickbook
if (entry.check()) {
state.out << "<listitem>";
BOOST_FOREACH (value phrase, entry)
QUICKBOOK_FOR (value phrase, entry)
state.out << phrase.get_encoded();
state.out << "</listitem>";
}
@@ -1624,7 +1624,7 @@ namespace quickbook
int span_count = 0;
value_consumer lookahead = values;
BOOST_FOREACH (value row, lookahead) {
QUICKBOOK_FOR (value row, lookahead) {
++row_count;
span_count = boost::distance(row);
}
@@ -1654,7 +1654,7 @@ namespace quickbook
if (row_count > 1) {
state.out << "<thead>"
<< "<row>";
BOOST_FOREACH (value cell, values.consume()) {
QUICKBOOK_FOR (value cell, values.consume()) {
state.out << "<entry>" << cell.get_encoded() << "</entry>";
}
state.out << "</row>\n"
@@ -1663,9 +1663,9 @@ namespace quickbook
state.out << "<tbody>\n";
BOOST_FOREACH (value row, values) {
QUICKBOOK_FOR (value row, values) {
state.out << "<row>";
BOOST_FOREACH (value cell, row) {
QUICKBOOK_FOR (value cell, row) {
state.out << "<entry>" << cell.get_encoded() << "</entry>";
}
state.out << "</row>\n";
@@ -1858,7 +1858,7 @@ namespace quickbook
state.templates.push();
}
BOOST_FOREACH (template_symbol& ts, storage) {
QUICKBOOK_FOR (template_symbol& ts, storage) {
std::string tname = ts.identifier;
if (tname != "!") {
ts.lexical_parent = &state.templates.top_scope();
@@ -1872,7 +1872,7 @@ namespace quickbook
}
if (load_type == block_tags::include) {
BOOST_FOREACH (template_symbol& ts, storage) {
QUICKBOOK_FOR (template_symbol& ts, storage) {
std::string tname = ts.identifier;
if (tname == "!") {
@@ -1898,7 +1898,7 @@ namespace quickbook
std::set<quickbook_path> search =
include_search(parameter, state, first);
BOOST_FOREACH (quickbook_path const& path, search) {
QUICKBOOK_FOR (quickbook_path const& path, search) {
try {
if (qbk_version_n >= 106) {
if (state.imported &&

View File

@@ -9,7 +9,7 @@
#include "dependency_tracker.hpp"
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>
#include "for.hpp"
#include "path.hpp"
namespace quickbook
@@ -23,7 +23,7 @@ namespace quickbook
std::string result;
result.reserve(generic.size());
BOOST_FOREACH (char c, generic) {
QUICKBOOK_FOR (char c, generic) {
if (c >= 0 && c < 16) {
result += control_escapes[(unsigned int)c];
}
@@ -98,15 +98,15 @@ namespace quickbook
void dependency_tracker::write_dependencies(std::ostream& out, flags f)
{
if (f & checked) {
BOOST_FOREACH (dependency_list::value_type const& d, dependencies) {
QUICKBOOK_FOR (dependency_list::value_type const& d, dependencies) {
out << (d.second ? "+ " : "- ") << get_path(d.first, f)
<< std::endl;
}
BOOST_FOREACH (glob_list::value_type const& g, glob_dependencies) {
QUICKBOOK_FOR (glob_list::value_type const& g, glob_dependencies) {
out << "g " << get_path(g.first, f) << std::endl;
BOOST_FOREACH (fs::path const& p, g.second) {
QUICKBOOK_FOR (fs::path const& p, g.second) {
out << "+ " << get_path(p, f) << std::endl;
}
}
@@ -114,19 +114,19 @@ namespace quickbook
else {
std::set<std::string> paths;
BOOST_FOREACH (dependency_list::value_type const& d, dependencies) {
QUICKBOOK_FOR (dependency_list::value_type const& d, dependencies) {
if (d.second) {
paths.insert(get_path(d.first, f));
}
}
BOOST_FOREACH (glob_list::value_type const& g, glob_dependencies) {
BOOST_FOREACH (fs::path const& p, g.second) {
QUICKBOOK_FOR (glob_list::value_type const& g, glob_dependencies) {
QUICKBOOK_FOR (fs::path const& p, g.second) {
paths.insert(get_path(p, f));
}
}
BOOST_FOREACH (std::string const& p, paths) {
QUICKBOOK_FOR (std::string const& p, paths) {
out << p << std::endl;
}
}

View File

@@ -12,10 +12,10 @@
#include <boost/algorithm/string/join.hpp>
#include <boost/bind.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>
#include "doc_info_tags.hpp"
#include "document_state.hpp"
#include "files.hpp"
#include "for.hpp"
#include "path.hpp"
#include "quickbook.hpp"
#include "state.hpp"
@@ -414,7 +414,7 @@ namespace quickbook
if (!authors.empty()) {
tmp << " <authorgroup>\n";
BOOST_FOREACH (value_consumer author_values, authors) {
QUICKBOOK_FOR (value_consumer author_values, authors) {
while (author_values.check()) {
value surname =
author_values.consume(doc_info_tags::author_surname);
@@ -432,7 +432,7 @@ namespace quickbook
tmp << " </authorgroup>\n";
}
BOOST_FOREACH (value_consumer copyright, copyrights) {
QUICKBOOK_FOR (value_consumer copyright, copyrights) {
while (copyright.check()) {
tmp << "\n"
<< " <copyright>\n";
@@ -487,7 +487,7 @@ namespace quickbook
<< "\n";
}
BOOST_FOREACH (value_consumer category_values, categories) {
QUICKBOOK_FOR (value_consumer category_values, categories) {
value category = category_values.optional_consume();
if (!category.empty()) {
tmp << " <" << doc_type << "category name=\"category:"
@@ -498,7 +498,7 @@ namespace quickbook
category_values.finish();
}
BOOST_FOREACH (value_consumer biblioid, biblioids) {
QUICKBOOK_FOR (value_consumer biblioid, biblioids) {
value class_ = biblioid.consume(doc_info_tags::biblioid_class);
value value_ = biblioid.consume(doc_info_tags::biblioid_value);
@@ -508,7 +508,7 @@ namespace quickbook
biblioid.finish();
}
BOOST_FOREACH (value escaped, escaped_attributes) {
QUICKBOOK_FOR (value escaped, escaped_attributes) {
tmp << "<!--quickbook-escape-prefix-->" << escaped.get_quickbook()
<< "<!--quickbook-escape-postfix-->";
}

View File

@@ -9,7 +9,6 @@
=============================================================================*/
#include <map>
#include <boost/foreach.hpp>
#include <boost/spirit/include/classic_chset.hpp>
#include <boost/spirit/include/classic_core.hpp>
#include <boost/spirit/include/classic_loops.hpp>
@@ -19,6 +18,7 @@
#include <boost/spirit/include/phoenix1_primitives.hpp>
#include "actions.hpp"
#include "doc_info_tags.hpp"
#include "for.hpp"
#include "grammar_impl.hpp"
#include "phrase_tags.hpp"
#include "state.hpp"
@@ -99,12 +99,12 @@ namespace quickbook
local.doc_types = "book", "article", "library", "chapter", "part",
"appendix", "preface", "qandadiv", "qandaset", "reference", "set";
BOOST_FOREACH (value::tag_type t, doc_attributes::tags()) {
QUICKBOOK_FOR (value::tag_type t, doc_attributes::tags()) {
local.doc_attributes.add(doc_attributes::name(t), t);
local.doc_info_attributes.add(doc_attributes::name(t), t);
}
BOOST_FOREACH (value::tag_type t, doc_info_attributes::tags()) {
QUICKBOOK_FOR (value::tag_type t, doc_info_attributes::tags()) {
local.doc_info_attributes.add(doc_info_attributes::name(t), t);
}

View File

@@ -12,10 +12,10 @@
#include <iterator>
#include <vector>
#include <boost/filesystem/fstream.hpp>
#include <boost/foreach.hpp>
#include <boost/range/algorithm/transform.hpp>
#include <boost/range/algorithm/upper_bound.hpp>
#include <boost/unordered_map.hpp>
#include "for.hpp"
namespace quickbook
{

25
src/for.hpp Normal file
View File

@@ -0,0 +1,25 @@
/*=============================================================================
Copyright (c) 2017 Daniel James
Use, modification and distribution is subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
// Macro for C++11 range based for loop, with BOOST_FOREACH as a fallback.
// Can't use C++11 loop in Visual C++ 10/Visual Studio 2010 or gcc 4.4.
// BOOST_FOREACH was causing warnings in Visual C++ 14.11/Visual Studio 2017
#if !defined(BOOST_QUICKBOOK_FOR_HPP)
#define BOOST_QUICKBOOK_FOR_HPP
#include <boost/config.hpp>
#if !defined(BOOST_NO_CXX11_RANGE_BASED_FOR)
#define QUICKBOOK_FOR(x, y) for (x : y)
#else
#include <boost/foreach.hpp>
#define QUICKBOOK_FOR(x, y) BOOST_FOREACH (x, y)
#endif
#endif

View File

@@ -7,12 +7,12 @@
=============================================================================*/
#include <cctype>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/make_shared.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/unordered_map.hpp>
#include "document_state_impl.hpp"
#include "for.hpp"
namespace quickbook
{
@@ -132,7 +132,7 @@ namespace quickbook
placeholder_index sorted_placeholders;
sorted_placeholders.reserve(state.placeholders.size());
BOOST_FOREACH (id_placeholder const& p, state.placeholders)
QUICKBOOK_FOR (id_placeholder const& p, state.placeholders)
if (order[p.index]) sorted_placeholders.push_back(&p);
boost::sort(sorted_placeholders, placeholder_compare(order));

View File

@@ -12,8 +12,8 @@
#include "include_paths.hpp"
#include <cassert>
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>
#include <boost/range/algorithm/replace.hpp>
#include "for.hpp"
#include "glob.hpp"
#include "path.hpp"
#include "quickbook.hpp" // For the include_path global (yuck)
@@ -206,7 +206,7 @@ namespace quickbook
// Search the include path dirs accumulating to the result.
unsigned count = 0;
BOOST_FOREACH (fs::path dir, include_path) {
QUICKBOOK_FOR (fs::path dir, include_path) {
++count;
state.dependencies.add_glob(dir / parameter.value);
include_search_glob(
@@ -234,7 +234,7 @@ namespace quickbook
// Search in each of the include path locations.
unsigned count = 0;
BOOST_FOREACH (fs::path full, include_path) {
QUICKBOOK_FOR (fs::path full, include_path) {
++count;
full /= path;

View File

@@ -11,8 +11,8 @@
#include "markups.hpp"
#include <map>
#include <ostream>
#include <boost/foreach.hpp>
#include "block_tags.hpp"
#include "for.hpp"
#include "phrase_tags.hpp"
#include "quickbook.hpp"
@@ -67,7 +67,7 @@ namespace quickbook
"<!--quickbook-escape-postfix-->"},
{phrase_tags::break_mark, "<sbr/>\n", 0}};
BOOST_FOREACH (markup m, init_markups) {
QUICKBOOK_FOR (markup m, init_markups) {
markups[m.tag] = m;
}
}

View File

@@ -12,8 +12,8 @@
#include "path.hpp"
#include <cassert>
#include <boost/filesystem/operations.hpp>
#include <boost/foreach.hpp>
#include <boost/range/algorithm/replace.hpp>
#include "for.hpp"
#include "glob.hpp"
#include "include_paths.hpp"
#include "state.hpp"
@@ -35,7 +35,7 @@ namespace quickbook
std::vector<fs::path> parts;
BOOST_FOREACH (fs::path const& part, path) {
QUICKBOOK_FOR (fs::path const& part, path) {
if (part.empty() || part == ".") {
}
else if (part == "..") {

View File

@@ -8,7 +8,6 @@
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/foreach.hpp>
#include <boost/spirit/include/classic_assign_actor.hpp>
#include <boost/spirit/include/classic_clear_actor.hpp>
#include <boost/spirit/include/classic_core.hpp>
@@ -16,6 +15,7 @@
#include <boost/spirit/include/phoenix1_casts.hpp>
#include <boost/spirit/include/phoenix1_primitives.hpp>
#include "actions.hpp"
#include "for.hpp"
#include "grammar_impl.hpp"
#include "phrase_tags.hpp"
#include "state.hpp"
@@ -178,7 +178,7 @@ namespace quickbook
cl::eps_p [state.values.entry(ph::arg1, ph::arg2)]
>> source_modes [state.values.entry(ph::arg1)];
BOOST_FOREACH(int tag, source_mode_tags::tags()) {
QUICKBOOK_FOR(int tag, source_mode_tags::tags()) {
source_modes.add(source_mode_tags::name(tag), tag);
elements.add(source_mode_tags::name(tag),
element_info(element_info::phrase, &local.empty, tag));

View File

@@ -13,7 +13,6 @@
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/foreach.hpp>
#include <boost/program_options.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/ref.hpp>
@@ -21,6 +20,7 @@
#include "actions.hpp"
#include "document_state.hpp"
#include "files.hpp"
#include "for.hpp"
#include "grammar.hpp"
#include "path.hpp"
#include "post_process.hpp"
@@ -453,7 +453,7 @@ int main(int argc, char* argv[])
unsigned flags = 0;
BOOST_FOREACH (std::string const& flag, flag_names) {
QUICKBOOK_FOR (std::string const& flag, flag_names) {
if (flag == "checked") {
flags |= quickbook::dependency_tracker::checked;
}

View File

@@ -9,8 +9,8 @@
http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "state.hpp"
#include <boost/foreach.hpp>
#include "document_state.hpp"
#include "for.hpp"
#include "grammar.hpp"
#include "path.hpp"
#include "phrase_tags.hpp"
@@ -109,7 +109,7 @@ namespace quickbook
{
source_mode_info result;
BOOST_FOREACH (source_mode_info const& s, tagged_source_mode_stack) {
QUICKBOOK_FOR (source_mode_info const& s, tagged_source_mode_stack) {
result.update(s);
}
@@ -122,7 +122,7 @@ namespace quickbook
result.update(document.section_source_mode());
BOOST_FOREACH (source_mode_info const& s, tagged_source_mode_stack) {
QUICKBOOK_FOR (source_mode_info const& s, tagged_source_mode_stack) {
result.update(s);
}