[opengtl-commits] [434] add a foreach macro

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 434
Author:   cyrille
Date:     2008-10-09 13:06:34 +0200 (Thu, 09 Oct 2008)

Log Message:
-----------
add a foreach macro

Modified Paths:
--------------
    trunk/OpenGTL/OpenGTL/GTLCore/Macros_p.h


Modified: trunk/OpenGTL/OpenGTL/GTLCore/Macros_p.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/Macros_p.h	2008-10-09 07:29:00 UTC (rev 433)
+++ trunk/OpenGTL/OpenGTL/GTLCore/Macros_p.h	2008-10-09 11:06:34 UTC (rev 434)
@@ -81,4 +81,51 @@
   
 #define UNUSED( _var_) (void)_var_;
 
+namespace GTLCore {
+  namespace Details {
+
+    struct ForeachInfoBase {};
+
+    template<typename T>
+    struct ForeachInfo : public ForeachInfoBase {
+      inline ForeachInfo( const T& t ) : it( t.begin() ), end( t.end() ), count(0)
+      {
+      }
+      inline bool finished() const
+      {
+        return it == end;
+      }
+      inline void next() const
+      {
+        count = 0;
+        ++it;
+      }
+      mutable typename T::const_iterator it, end;
+      mutable int count;
+    };
+    template <typename T>
+    inline ForeachInfo<T> CreateForeachInfo(const T& t)
+    {
+      return ForeachInfo<T>( t );
+    }
+
+    template <typename T>
+    inline const ForeachInfo<T> *getForeachInfo(const ForeachInfoBase *base, const T *)
+    {
+      return static_cast<const ForeachInfo<T> *>(base); 
+    }
+    template <typename T>
+    inline T* getForeachType(const T &) { return 0; }
+
+  }
+}
+
+#define foreach(var, cont ) \
+  for( const GTLCore::Details::ForeachInfoBase&  contInfo = GTLCore::Details::CreateForeachInfo( cont ); \
+       not GTLCore::Details::getForeachInfo( &contInfo, true ? 0 : GTLCore::Details::getForeachType(cont) )->finished(); \
+       GTLCore::Details::getForeachInfo( &contInfo, true ? 0 : GTLCore::Details::getForeachType(cont) )->next() ) \
+    for( var = *GTLCore::Details::getForeachInfo( &contInfo, true ? 0 : GTLCore::Details::getForeachType(cont) )->it; \
+         GTLCore::Details::getForeachInfo( &contInfo, true ? 0 : GTLCore::Details::getForeachType(cont) )->count < 1; \
+         ++GTLCore::Details::getForeachInfo( &contInfo, true ? 0 : GTLCore::Details::getForeachType(cont) )->count )
+
 #endif


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/