From f7a3314a0aac589e1b86f76181da3d14313aaeb3 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 26 Feb 2016 16:19:28 +0100 Subject: [PATCH] - added an iterator to TArray so that it can be used with range-bases 'for' statements. --- src/tarray.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/tarray.h b/src/tarray.h index d62d75e28..63d693261 100644 --- a/src/tarray.h +++ b/src/tarray.h @@ -50,6 +50,23 @@ class FArchive; + +template class TIterator +{ +public: + TIterator(T* ptr = nullptr) { m_ptr = ptr; } + bool operator==(const TIterator& other) const { return (m_ptr == other.m_ptr); } + bool operator!=(const TIterator& other) const { return (m_ptr != other.m_ptr); } + TIterator &operator++() { ++m_ptr; return (*this); } + T &operator*() { return *m_ptr; } + const T &operator*() const { return *m_ptr; } + T* operator->() { return m_ptr; } + +protected: + T* m_ptr; +}; + + // TArray ------------------------------------------------------------------- // Must match TArray's layout. @@ -68,6 +85,32 @@ class TArray template friend FArchive &operator<< (FArchive &arc, TArray &self); public: + + typedef TIterator iterator; + typedef TIterator const_iterator; + + iterator begin() + { + return &Array[0]; + } + + iterator end() + { + return &Array[Count]; + } + + const_iterator cbegin() const + { + return &Array[0]; + } + + const_iterator cend() const + { + return &Array[Count]; + } + + + //////// // This is a dummy constructor that does nothing. The purpose of this // is so you can create a global TArray in the data segment that gets