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