source: libcfa/src/enum.hfa @ 0097d08

Last change on this file since 0097d08 was 68ea8d2, checked in by JiadaL <j82liang@…>, 4 months ago

Seperate CfaEnum? and Serial.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#pragma once
2
3#include "iostream.hfa"
4
5forall( E ) trait Bounded {
6    E lowerBound();
7    E upperBound();
8};
9
10forall( E | Bounded( E ) ) trait Serial {
11    unsigned fromInstance( E e );
12    E fromInt_unsafe( unsigned i );
13    E succ_unsafe( E e );
14    E pred_unsafe( E e );
15};
16
17forall( E | Serial( E ) ) {
18    E fromInt( unsigned i );
19    E succ( E e );
20    E pred( E e );
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// };
35
36forall( E ) trait CfaEnum {
37    const char * label( E e );
38    unsigned int posn( E e );
39};
40
41forall( E, V | CfaEnum( E ) ) trait TypedEnum {
42    V value( E e );
43};
44
45// I/O
46
47forall( istype & | istream( istype ), E | CfaEnum( E ) | Serial(E) )
48istype & ?|?( istype &, E & );
49
50forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) {
51        ostype & ?|?( ostype &, E );
52        OSTYPE_VOID( E );
53}
54
55static inline
56forall( E | Serial(E) | CfaEnum(E) ) {
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    }
85}
Note: See TracBrowser for help on using the repository browser.