source: libcfa/src/enum.hfa @ ecaedf35

Last change on this file since ecaedf35 was 2dd5c6d, checked in by JiadaL <j82liang@…>, 3 days ago

Update +=/-= for enums

  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[72713e5]1#pragma once
2
[85855b0]3#include "iostream.hfa"
4
[d5efcb7]5forall( E ) trait Bounded {
[6804f38]6        E lowerBound();
7        E upperBound();
[85855b0]8};
9
[64eeb06]10forall( E | Bounded( E ) ) trait Serial {
[6804f38]11        unsigned fromInstance( E e );
12        E fromInt_unsafe( unsigned i );
13        E succ_unsafe( E e );
14        E pred_unsafe( E e );
[0c327ce]15};
16
17forall( E | Serial( E ) ) {
[6804f38]18        E fromInt( unsigned i );
19        E succ( E e );
20        E pred( E e );
21        int Countof( E e );
[0c327ce]22}
23
24// forall( E | Bounded(E) ) trait SafeSerial {
[6804f38]25//       // unsigned fromInstance( E e );
26//       E fromInt_unsafe( unsigned i );
27//       // E succ_unsafe( E e );
28//       //E pred_unsafe( E e );
[0c327ce]29
[6804f38]30//       unsigned fromInstance( E e );
31//       E fromInt( unsigned i );
32//       E succ( E e );
33//       E pred( E e );
[0c327ce]34// };
[85855b0]35
[68ea8d2]36forall( E ) trait CfaEnum {
[6804f38]37        const char * label( E e );
38        unsigned int posn( E e );
[236f133]39};
40
41forall( E, V | CfaEnum( E ) ) trait TypedEnum {
[6804f38]42        V value( E e );
[85855b0]43};
44
[d5efcb7]45// I/O
[85855b0]46
[68ea8d2]47forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) )
[bc48c0d]48istype & ?|?( istype &, E & );
[64eeb06]49
[236f133]50forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) {
[d5efcb7]51        ostype & ?|?( ostype &, E );
52        OSTYPE_VOID( E );
53}
54
[062467b]55static inline
[68ea8d2]56forall( E | Serial(E) | CfaEnum(E) ) {
[6804f38]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 ); }
[062467b]63
[6804f38]64        E ++?( E & l ) {                                                                        // increment operators
65                l = succ( l );
66                return l;
67        }
[1571e4d]68
69        E ?+=? ( E & l, one_t ) {
70                l = succ(l);
71                return l;
72        }
[6804f38]73       
[2dd5c6d]74        E ?-=? ( E & l, one_t ) {
75                l = pred(l);
76                return l;
77        }
78
79        E ?+=? ( E & l, int i ) {
80                int pos = posn(l) + i;
81                return fromInt(pos);
82        }
83
84        E ?-=? ( E & l, int i ) {
85                int pos = posn(l) - i;
86                return fromInt(pos);
87        }
88       
[6804f38]89        E ?++( E & l ) {
90                E ret = l;
91                l = succ( l );
92                return ret;
93        }
[062467b]94
[6804f38]95        E --?( E & l ) {
96                l = pred( l );
97                return l;
98        }
[062467b]99
[6804f38]100        E ?--( E & l ) {
101                E ret = l;
102                l = pred( l );
103                return ret;
104        }
[64eeb06]105}
Note: See TracBrowser for help on using the repository browser.