[72713e5] | 1 | #pragma once |
---|
| 2 | |
---|
[85855b0] | 3 | #include "iostream.hfa" |
---|
| 4 | |
---|
[d5efcb7] | 5 | forall( E ) trait Bounded { |
---|
[85855b0] | 6 | E lowerBound(); |
---|
| 7 | E upperBound(); |
---|
| 8 | }; |
---|
| 9 | |
---|
[64eeb06] | 10 | forall( E | Bounded( E ) ) trait Serial { |
---|
[d5efcb7] | 11 | unsigned fromInstance( E e ); |
---|
[0c327ce] | 12 | E fromInt_unsafe( unsigned i ); |
---|
| 13 | E succ_unsafe( E e ); |
---|
| 14 | E pred_unsafe( E e ); |
---|
| 15 | }; |
---|
| 16 | |
---|
| 17 | forall( E | Serial( E ) ) { |
---|
[d5efcb7] | 18 | E fromInt( unsigned i ); |
---|
| 19 | E succ( E e ); |
---|
| 20 | E pred( E e ); |
---|
[0c327ce] | 21 | int Countof( E e ); |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | // forall( E | Bounded(E) ) trait SafeSerial { |
---|
| 25 | // // unsigned fromInstance( E e ); |
---|
| 26 | // E fromInt_unsafe( unsigned i ); |
---|
| 27 | // // E succ_unsafe( E e ); |
---|
| 28 | // //E pred_unsafe( E e ); |
---|
| 29 | |
---|
| 30 | // unsigned fromInstance( E e ); |
---|
| 31 | // E fromInt( unsigned i ); |
---|
| 32 | // E succ( E e ); |
---|
| 33 | // E pred( E e ); |
---|
| 34 | // }; |
---|
[85855b0] | 35 | |
---|
[236f133] | 36 | forall( E | Serial( E ) ) trait CfaEnum { |
---|
[bb336a6] | 37 | const char * label( E e ); |
---|
[d5efcb7] | 38 | unsigned int posn( E e ); |
---|
[236f133] | 39 | }; |
---|
| 40 | |
---|
| 41 | forall( E, V | CfaEnum( E ) ) trait TypedEnum { |
---|
[d5efcb7] | 42 | V value( E e ); |
---|
[85855b0] | 43 | }; |
---|
| 44 | |
---|
[d5efcb7] | 45 | // I/O |
---|
[85855b0] | 46 | |
---|
[236f133] | 47 | forall( istype & | istream( istype ), E | CfaEnum( E ) ) |
---|
[bc48c0d] | 48 | istype & ?|?( istype &, E & ); |
---|
[64eeb06] | 49 | |
---|
[236f133] | 50 | forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) { |
---|
[d5efcb7] | 51 | ostype & ?|?( ostype &, E ); |
---|
| 52 | OSTYPE_VOID( E ); |
---|
| 53 | } |
---|
| 54 | |
---|
[062467b] | 55 | static inline |
---|
[236f133] | 56 | forall( E | CfaEnum( E ) ) { |
---|
[062467b] | 57 | int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators |
---|
| 58 | int ?!=?( E l, E r ) { return posn( l ) != posn( r ); } |
---|
| 59 | int ?<?( E l, E r ) { return posn( l ) < posn( r ); } |
---|
| 60 | int ?<=?( E l, E r ) { return posn( l ) <= posn( r ); } |
---|
| 61 | int ?>?( E l, E r ) { return posn( l ) > posn( r ); } |
---|
| 62 | int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); } |
---|
| 63 | |
---|
| 64 | E ++?( E & l ) { // increment operators |
---|
| 65 | l = succ( l ); |
---|
| 66 | return l; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | E ?++( E & l ) { |
---|
| 70 | E ret = l; |
---|
| 71 | l = succ( l ); |
---|
| 72 | return ret; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | E --?( E & l ) { |
---|
| 76 | l = pred( l ); |
---|
| 77 | return l; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | E ?--( E & l ) { |
---|
| 81 | E ret = l; |
---|
| 82 | l = pred( l ); |
---|
| 83 | return ret; |
---|
| 84 | } |
---|
[64eeb06] | 85 | } |
---|