From 665abd0b92f21c542caed7a645e755a80a79a484 Mon Sep 17 00:00:00 2001 From: syntheticpp Date: Sat, 21 Nov 2009 10:40:59 +0000 Subject: [PATCH] add foreach git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@1059 7ec92016-0320-0410-acc4-a06ded1c099a --- include/loki/ForEachType.h | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 include/loki/ForEachType.h diff --git a/include/loki/ForEachType.h b/include/loki/ForEachType.h new file mode 100644 index 0000000..c020165 --- /dev/null +++ b/include/loki/ForEachType.h @@ -0,0 +1,101 @@ + +//////////////////////////////////////////////////////////////////////////////// +// The Loki Library +// Copyright (C) 2009 Andy Balaam +// Copyright (c) 2009 Peter Kümmel +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this +// permission notice appear in supporting documentation. +// The author makes no representations about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. +//////////////////////////////////////////////////////////////////////////////// + +#ifndef LOKI_FOR_EACH_TYPE +#define LOKI_FOR_EACH_TYPE + +#include +#include + +namespace Loki +{ + + //////////////////////////////////////////////////////////////////////////////// + // class template ForEachType + // Calls a templated callable for every element of a Typelist + // Supplies an int template parameter for the position in the TypeList. + // Invocation (TList is a typelist): + // ForEachType dummy(); + // Calls the supplied method during construction of the object dummy. + //////////////////////////////////////////////////////////////////////////////// + + namespace Private + { + // type of recursive function + template + struct ForEachTypeImpl; + + // Recursion rule + template + struct ForEachTypeImpl, Callable> + : public ForEachTypeImpl + { + enum { value = 1 + ForEachTypeImpl::value }; + + ForEachTypeImpl( Callable& callable ) : ForEachTypeImpl(callable) + { + callable.operator()(); + } + + }; + + // Recursion end + template + struct ForEachTypeImpl, Callable> + { + public: + + enum { value = 0 }; + + ForEachTypeImpl( Callable& callable ) + { + callable.operator()(); + } + }; + + + } + + + struct OrderPolicyForward; + struct OrderPolicyBackward; + + template + struct ForEachType; + + template + struct ForEachType + : public Private::ForEachTypeImpl::Result, Callable > + { + ForEachType( Callable& callable ) + : Private::ForEachTypeImpl::Result, Callable >( callable ) + { + } + }; + + template + struct ForEachType + : public Private::ForEachTypeImpl< TList, Callable > + { + ForEachType( Callable& callable ) + : Private::ForEachTypeImpl< TList, Callable >( callable ) + { + } + }; + + +} + +#endif +