From 23ad9f4e1a4489c3315803397bc1adc8049bd946 Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 7 Dec 2013 10:46:40 +0800 Subject: [PATCH] issue #2790: Adds const version of Vector::forEach. --- cocos/base/CCVector.h | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 2a12c66d76..2314e325ba 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -25,10 +25,12 @@ THE SOFTWARE. #ifndef __CCVECTOR_H__ #define __CCVECTOR_H__ -#include -#include // std::for_each #include "ccMacros.h" +#include +#include +#include // std::for_each + NS_CC_BEGIN template @@ -299,9 +301,19 @@ public: _data.shrink_to_fit(); } - void forEach(std::function callback) + void forEach(const std::function& callback) { - if (size() <= 0) + if (empty()) + return; + + std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ + callback(obj); + }); + } + + void forEach(const std::function& callback) const + { + if (empty()) return; std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ @@ -309,9 +321,19 @@ public: }); } - void forEachReverse(std::function callback) + void forEachReverse(const std::function& callback) { - if (size() <= 0) + if (empty()) + return; + + std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){ + callback(obj); + }); + } + + void forEachReverse(const std::function& callback) const + { + if (empty()) return; std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){