#pragma once #include "iostream.hfa" forall( E ) trait Bounded { E lowerBound(); E upperBound(); }; forall( E | Bounded( E ) ) trait Serial { int fromInstance( E e ); E fromInt_unsafe( int i ); E succ_unsafe( E e ); E pred_unsafe( E e ); }; forall( E | Serial( E ) ) { E fromInt( int i ); E succ( E e ); E pred( E e ); int Countof( E e ); } // forall( E | Bounded(E) ) trait SafeSerial { // // unsigned fromInstance( E e ); // E fromInt_unsafe( unsigned i ); // // E succ_unsafe( E e ); // //E pred_unsafe( E e ); // unsigned fromInstance( E e ); // E fromInt( unsigned i ); // E succ( E e ); // E pred( E e ); // }; forall( E ) trait CfaEnum { const char * label( E e ); int posn( E e ); }; forall( E, V | CfaEnum( E ) ) trait TypedEnum { V value( E e ); }; // I/O forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) ) istype & ?|?( istype &, E & ); forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { ostype & ?|?( ostype &, E ); OSTYPE_VOID( E ); } static inline forall( E | Serial(E) | CfaEnum(E) ) { int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators int ?!=?( E l, E r ) { return posn( l ) != posn( r ); } int ??( E l, E r ) { return posn( l ) > posn( r ); } int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); } E ++?( E & l ) { // increment operators int pos = posn(l); l = fromInt_unsafe(pos+1); return l; } E --?( E & l ) { int pos = posn(l); l = fromInt_unsafe(pos-1); return l; } E ?+=? ( E & l, one_t ) { int pos = posn(l); l = fromInt_unsafe(pos+1); return l; } E ?-=? ( E & l, one_t ) { int pos = posn(l); l = fromInt_unsafe(pos-1); return l; } E ?+=? ( E & l, int i ) { int pos = posn(l); l = fromInt_unsafe(pos+i); return fromInt(pos); } E ?-=? ( E & l, int i ) { int pos = posn(l); l = fromInt_unsafe(pos-i); return l; } E ?++( E & l ) { int pos = posn(l); l = fromInt_unsafe(pos+1); return fromInt_unsafe(pos); } E ?--( E & l ) { int pos = posn(l); l = fromInt_unsafe(pos-1); return fromInt_unsafe(pos); } }