00001 #ifndef __LOOP_H__
00002 #define __LOOP_H__
00003
00004 #include "magic/mobject.h"
00005
00007
00008
00009
00010
00011
00012
00013
00015
00016 class AnyLoop : public Object {
00017 };
00018
00019 template <class ttype>
00020 class TypedLoop : public AnyLoop {
00021 public:
00022 ttype i;
00023
00024 operator ttype () const {return i;}
00025 virtual DumpContext& operator>> (DumpContext& out) const=0;
00026 };
00027
00028 template <class ttype>
00029 class Loop : public TypedLoop<ttype> {
00030 ttype m_start, m_end, m_step;
00031 public:
00032 Loop (ttype s, ttype e, ttype stp=1) {
00033 m_start = s;
00034 m_end = e;
00035 m_step = stp;
00036 start ();
00037 }
00038 void start () {i = m_start;}
00039 void next () {i += m_step;}
00040 bool exhausted () const {return (m_step>0)? i>m_end:i<m_end;}
00041 bool next_ends () const {return (m_step>0)? i+m_step>m_end:i+m_step<m_end;}
00042 ttype max () const {return m_end;}
00043 DumpContext& operator>> (DumpContext& out) const {
00044 out.name ("m_start") << m_start;
00045 out.name ("m_end") << m_end;
00046 out.name ("m_step") << m_step;
00047 }
00048 };
00049
00050 template <class ttype>
00051 class NoLoop : public TypedLoop<ttype> {
00052 public:
00053 NoLoop (ttype s) {
00054 i = s;
00055 }
00056 DumpContext& operator>> (DumpContext& out) const {
00057 out.name ("i") << i;
00058 }
00059 };
00060
00061 #define ForLoop(name) for (name.start(); !name.exhausted(); name.next())
00062
00063
00064
00066
00067
00068
00069
00070
00071
00072
00074
00075 template <class ttype>
00076 class Variant : public Object {
00077 public:
00078 ttype i;
00079
00080 operator ttype () const {return i;}
00081 virtual DumpContext& operator>> (DumpContext& out) const=0;
00082 };
00083
00084 template <class ttype>
00085 class DynaSet : public Variant<ttype> {
00086 ttype m_start, m_end, m_step;
00087 public:
00088
00089 DynaSet (ttype s, ttype e=-666666, ttype stp=1) {
00090 m_start = s;
00091 m_end = (e==-666666)? s:e;
00092 m_step = stp;
00093 start ();
00094 }
00095 void start () {i = m_start;}
00096 void next () {i += m_step;}
00097 bool exhausted () const {return (m_step>0)? i>m_end:i<m_end;}
00098 bool next_ends () const {return (m_step>0)? i+m_step>m_end:i+m_step<m_end;}
00099 ttype max () const {return m_end;}
00100 DumpContext& operator>> (DumpContext& out) const {
00101 out.name ("m_start") << m_start;
00102 out.name ("m_end") << m_end;
00103 out.name ("m_step") << m_step;
00104 }
00105
00106 ttype getStart () const {return m_start;}
00107 ttype getEnd () const {return m_end;}
00108 ttype getStep () const {return m_step;}
00109 };
00110
00111 #define ForSet(name) for (name.start(); !name.exhausted(); name.next())
00112
00113
00114
00115 #endif