issue #2790: Adds const version of Vector<T>::forEach.

This commit is contained in:
James Chen 2013-12-07 10:46:40 +08:00
parent 6cd573fa7f
commit 23ad9f4e1a
1 changed files with 28 additions and 6 deletions

View File

@ -25,10 +25,12 @@ THE SOFTWARE.
#ifndef __CCVECTOR_H__ #ifndef __CCVECTOR_H__
#define __CCVECTOR_H__ #define __CCVECTOR_H__
#include <vector>
#include <algorithm> // std::for_each
#include "ccMacros.h" #include "ccMacros.h"
#include <vector>
#include <functional>
#include <algorithm> // std::for_each
NS_CC_BEGIN NS_CC_BEGIN
template<class T> template<class T>
@ -299,9 +301,9 @@ public:
_data.shrink_to_fit(); _data.shrink_to_fit();
} }
void forEach(std::function<void(T)> callback) void forEach(const std::function<void(T)>& callback)
{ {
if (size() <= 0) if (empty())
return; return;
std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){
@ -309,9 +311,29 @@ public:
}); });
} }
void forEachReverse(std::function<void(T)> callback) void forEach(const std::function<void(T)>& callback) const
{ {
if (size() <= 0) if (empty())
return;
std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){
callback(obj);
});
}
void forEachReverse(const std::function<void(T)>& callback)
{
if (empty())
return;
std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){
callback(obj);
});
}
void forEachReverse(const std::function<void(T)>& callback) const
{
if (empty())
return; return;
std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){ std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){