source: libcfa/src/enum.hfa@ 7fe4adbb

Last change on this file since 7fe4adbb was 062467b, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago

inline enum relational and increment operators, comment out loop causing compiler segment fault

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