source: libcfa/src/enum.hfa @ 9c447e2

Last change on this file since 9c447e2 was 236f133, checked in by JiadaL <j82liang@…>, 3 months ago

Remove quasi_void for enums. The idea of quasi_void from Mike was to get around some resolution problem that enum pick function defines for Bounded over CfaEnum/TypedEnum?. But it is not clear that how often this can happen, and cfa might change the cast function scheme in the future. (change cost comparison scheme) Deprecate quasi_void for now so that enum traits looks cleaner without the dummy type for opaque enums

  • 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( unsigned i );
13    E succ( E e );
14    E pred( E e );
15};
16
17forall( E | Serial( E ) ) trait CfaEnum {
18    const char * label( E e );
19    unsigned int posn( E e );
20};
21
22forall( E, V | CfaEnum( E ) ) trait TypedEnum {
23    V value( E e );
24};
25
26// I/O
27
28forall( istype & | istream( istype ), E | CfaEnum( E ) )
29istype & ?|?( istype &, E & );
30
31forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) {
32        ostype & ?|?( ostype &, E );
33        OSTYPE_VOID( E );
34}
35
36// forall( ostype & | ostream( ostype ), E | CfaEnum( E, quasi_void ) ) {
37//      ostype & ?|?( ostype &, E );
38//      OSTYPE_VOID( E );
39// }
40
41// Design two <- should go for this if we have change the cost model
42// forall( E | Serial( E ) ) trait CfaEnum {
43//     char * label( E e );
44//     unsigned int posn( E e );
45// };
46
47// forall( E, V| CfaEnum( E)) trait TypedEnum {
48//     V value( E e);
49// };
50
51static inline
52forall( E | CfaEnum( E ) ) {
53    int ?==?( E l, E r ) { return posn( l ) == posn( r ); }     // relational operators
54    int ?!=?( E l, E r ) { return posn( l ) != posn( r ); }
55    int ?<?( E l, E r ) { return posn( l ) < posn( r ); }
56    int ?<=?( E l, E r ) { return posn( l ) <= posn( r ); }
57    int ?>?( E l, E r ) { return posn( l ) > posn( r ); }
58    int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); }
59
60    E ++?( E & l ) {                                                                    // increment operators
61        l = succ( l );
62        return l;
63    }
64   
65    E ?++( E & l ) {
66        E ret = l;
67        l = succ( l );
68        return ret;
69    }
70
71    E --?( E & l ) {
72        l = pred( l );
73        return l;
74    }
75
76    E ?--( E & l ) {
77        E ret = l;
78        l = pred( l );
79        return ret;
80    }
81}
Note: See TracBrowser for help on using the repository browser.