From f58cbf0b98f6fc36e1746107a5095d55eb1ffc94 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 6 Sep 2011 18:33:35 +0000 Subject: [PATCH] utils: Add reversed template helper for container iteration this permits to use range-based for loops to iterate on reversed containers. Should work on any container implementing rbegin and rend. --- src/utils.hh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/utils.hh b/src/utils.hh index 75640cd8..88d95e11 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -13,6 +13,24 @@ struct LineAndColumn : line(line), column(column) {} }; +template +struct ReversedContainer +{ + ReversedContainer(Container& container) : container(container) {} + Container& container; + + decltype(container.rbegin()) begin() { return container.rbegin(); } + decltype(container.rend()) end() { return container.rend(); } +}; + +template +ReversedContainer reversed(Container& container) +{ + return ReversedContainer(container); +} + + + } #endif // utils_hh_INCLUDED