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