From 609db2639ef8d87598a448f8ebb58a61c72aa523 Mon Sep 17 00:00:00 2001 From: castano Date: Mon, 28 Feb 2011 09:05:03 +0000 Subject: [PATCH] Move foreach to its own header. --- src/nvcore/Array.h | 44 ++---------------------------- src/nvcore/CMakeLists.txt | 1 + src/nvcore/ForEach.h | 56 +++++++++++++++++++++++++++++++++++++++ src/nvcore/HashMap.h | 2 +- 4 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 src/nvcore/ForEach.h diff --git a/src/nvcore/Array.h b/src/nvcore/Array.h index f85caf6..73905df 100644 --- a/src/nvcore/Array.h +++ b/src/nvcore/Array.h @@ -18,55 +18,15 @@ standard foreach as in Qt. #include "Debug.h" #include "Stream.h" #include "Utils.h" // swap +#include "ForEach.h" // swap #include // memmove #include // for placement new -#if NV_CC_GNUC // If typeof is available: - -#define NV_FOREACH(i, container) \ - typedef typeof(container) NV_STRING_JOIN2(cont,__LINE__); \ - for(NV_STRING_JOIN2(cont,__LINE__)::PseudoIndex i((container).start()); !(container).isDone(i); (container).advance(i)) -/* -#define NV_FOREACH(i, container) \ -for(typename typeof(container)::PseudoIndex i((container).start()); !(container).isDone(i); (container).advance(i)) -*/ - -#else // If typeof not available: - -struct PseudoIndexWrapper { - template - PseudoIndexWrapper(const T & container) { - nvStaticCheck(sizeof(typename T::PseudoIndex) <= sizeof(memory)); - new (memory) typename T::PseudoIndex(container.start()); - } - // PseudoIndex cannot have a dtor! - - template typename T::PseudoIndex & operator()(const T * container) { - return *reinterpret_cast(memory); - } - template const typename T::PseudoIndex & operator()(const T * container) const { - return *reinterpret_cast(memory); - } - - uint8 memory[4]; // Increase the size if we have bigger enumerators. -}; - -#define NV_FOREACH(i, container) \ - for(PseudoIndexWrapper i(container); !(container).isDone(i(&(container))); (container).advance(i(&(container)))) - -#endif - -// Declare foreach keyword. -#if !defined NV_NO_USE_KEYWORDS -# define foreach NV_FOREACH -#endif - - - namespace nv { + // @@ Move this to utils? /// Delete all the elements of a container. template void deleteAll(T & container) diff --git a/src/nvcore/CMakeLists.txt b/src/nvcore/CMakeLists.txt index 0b9c42a..5840b11 100644 --- a/src/nvcore/CMakeLists.txt +++ b/src/nvcore/CMakeLists.txt @@ -9,6 +9,7 @@ SET(CORE_SRCS DefsGnucWin32.h DefsVcWin32.h FileSystem.h FileSystem.cpp + ForEach.h HashMap.h Library.h Library.cpp Memory.h Memory.cpp diff --git a/src/nvcore/ForEach.h b/src/nvcore/ForEach.h new file mode 100644 index 0000000..8207bb0 --- /dev/null +++ b/src/nvcore/ForEach.h @@ -0,0 +1,56 @@ +// This code is in the public domain -- Ignacio Castaņo + +#pragma once +#ifndef NV_CORE_FOREACH_H +#define NV_CORE_FOREACH_H + +/* +The foreach macros that I use are very non-standard and somewhat confusing, but I like them. +*/ + + +#include "nvcore.h" + + +#if NV_CC_GNUC // If typeof is available: + +#define NV_FOREACH(i, container) \ + typedef typeof(container) NV_STRING_JOIN2(cont,__LINE__); \ + for(NV_STRING_JOIN2(cont,__LINE__)::PseudoIndex i((container).start()); !(container).isDone(i); (container).advance(i)) +/* +#define NV_FOREACH(i, container) \ +for(typename typeof(container)::PseudoIndex i((container).start()); !(container).isDone(i); (container).advance(i)) +*/ + +#else // If typeof not available: + +struct PseudoIndexWrapper { + template + PseudoIndexWrapper(const T & container) { + nvStaticCheck(sizeof(typename T::PseudoIndex) <= sizeof(memory)); + new (memory) typename T::PseudoIndex(container.start()); + } + // PseudoIndex cannot have a dtor! + + template typename T::PseudoIndex & operator()(const T * container) { + return *reinterpret_cast(memory); + } + template const typename T::PseudoIndex & operator()(const T * container) const { + return *reinterpret_cast(memory); + } + + uint8 memory[4]; // Increase the size if we have bigger enumerators. +}; + +#define NV_FOREACH(i, container) \ + for(PseudoIndexWrapper i(container); !(container).isDone(i(&(container))); (container).advance(i(&(container)))) + +#endif + +// Declare foreach keyword. +#if !defined NV_NO_USE_KEYWORDS +# define foreach NV_FOREACH +#endif + + +#endif // NV_CORE_FOREACH_H diff --git a/src/nvcore/HashMap.h b/src/nvcore/HashMap.h index b9362f9..044f277 100644 --- a/src/nvcore/HashMap.h +++ b/src/nvcore/HashMap.h @@ -14,7 +14,7 @@ HashMap based on Thatcher Ulrich container, donated to the Publ #include "Debug.h" #include "Stream.h" #include "Utils.h" // hash -#include "Array.h" // foreach/pseudoindex +#include "ForEach.h" #include // for placement new