source: libcfa/src/enum.hfa@ 76b507d

Last change on this file since 76b507d was 0c327ce, checked in by JiadaL <j82liang@…>, 15 months ago
  1. Add bound check to Serial function: now compiler generates the unchecked functions in ImplementEnumFunc, and enum.hfa implements the bound check on top. Todo: Wrapped the checked version into a trait; 2. countof is now works on any type that implement Countof(). Because Countof() is implemented in enum.hfa for all CfaEnum, we can call countof on { T | CfaEnum(T) }
  • Property mode set to 100644
File size: 1.9 KB
RevLine 
[72713e5]1#pragma once
2
[0c327ce]3#include <assert.h>
[85855b0]4#include "iostream.hfa"
5
[d5efcb7]6forall( E ) trait Bounded {
[85855b0]7 E lowerBound();
8 E upperBound();
9};
10
[64eeb06]11forall( E | Bounded( E ) ) trait Serial {
[d5efcb7]12 unsigned fromInstance( E e );
[0c327ce]13 E fromInt_unsafe( unsigned i );
14 E succ_unsafe( E e );
15 E pred_unsafe( E e );
16};
17
18forall( E | Serial( E ) ) {
[d5efcb7]19 E fromInt( unsigned i );
20 E succ( E e );
21 E pred( E e );
[0c327ce]22 int Countof( E e );
23}
24
25// forall( E | Bounded(E) ) trait SafeSerial {
26// // unsigned fromInstance( E e );
27// E fromInt_unsafe( unsigned i );
28// // E succ_unsafe( E e );
29// //E pred_unsafe( E e );
30
31// unsigned fromInstance( E e );
32// E fromInt( unsigned i );
33// E succ( E e );
34// E pred( E e );
35// };
[85855b0]36
[236f133]37forall( E | Serial( E ) ) trait CfaEnum {
[bb336a6]38 const char * label( E e );
[d5efcb7]39 unsigned int posn( E e );
[236f133]40};
41
42forall( E, V | CfaEnum( E ) ) trait TypedEnum {
[d5efcb7]43 V value( E e );
[85855b0]44};
45
[d5efcb7]46// I/O
[85855b0]47
[236f133]48forall( istype & | istream( istype ), E | CfaEnum( E ) )
[bc48c0d]49istype & ?|?( istype &, E & );
[64eeb06]50
[236f133]51forall( ostype & | ostream( ostype ), E | CfaEnum( E ) ) {
[d5efcb7]52 ostype & ?|?( ostype &, E );
53 OSTYPE_VOID( E );
54}
55
[062467b]56static inline
[236f133]57forall( E | CfaEnum( E ) ) {
[062467b]58 int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators
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 int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); }
64
65 E ++?( E & l ) { // increment operators
66 l = succ( l );
67 return l;
68 }
69
70 E ?++( E & l ) {
71 E ret = l;
72 l = succ( l );
73 return ret;
74 }
75
76 E --?( E & l ) {
77 l = pred( l );
78 return l;
79 }
80
81 E ?--( E & l ) {
82 E ret = l;
83 l = pred( l );
84 return ret;
85 }
[64eeb06]86}
Note: See TracBrowser for help on using the repository browser.