1 | #include <fstream.hfa> // sout |
---|
2 | #include <stdlib.hfa> // ato |
---|
3 | |
---|
4 | // integral |
---|
5 | enum Letter { |
---|
6 | A = 'A', B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, |
---|
7 | a = 'a', b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z |
---|
8 | }; |
---|
9 | enum( Letter ) Greek { Alph = A, Beta = B, Gamma = G, /* more enums */ Zeta = Z }; // alphabet intersection |
---|
10 | |
---|
11 | enum( char ) Currency { Dollar = '$', Cent = '¢', Yen = '¥', Pound = '£', Euro = 'E' }; // iso-latin-1 |
---|
12 | enum( Currency ) Europe { Euro = Currency.Euro, Pound = Currency.Pound }; |
---|
13 | |
---|
14 | enum( signed char ) srgb { Red = -1, Green = 0, Blue = 1 }; |
---|
15 | enum( long long int ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; |
---|
16 | // non-integral |
---|
17 | enum( double ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 }; |
---|
18 | enum( _Complex ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i }; |
---|
19 | // pointer |
---|
20 | enum( const char * ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; |
---|
21 | int i, j, k; |
---|
22 | enum( int * ) ptr { I = &i, J = &j, K = &k }; |
---|
23 | //enum( int & ) ref { I = i, J = j, K = k }; |
---|
24 | // tuple |
---|
25 | //enum( [int, int] ) { T = [ 1, 2 ] }; |
---|
26 | // function |
---|
27 | void f() {} void g() {} |
---|
28 | enum( void (*)() ) funs { F = f, G = g }; |
---|
29 | // aggregate |
---|
30 | struct Person { char * name; int age, height; }; |
---|
31 | enum( Person ) friends { Liz = { "ELIZABETH", 22, 170 }, //Beth = Liz, |
---|
32 | Jon = { "Jonathan", 35, 190 } }; |
---|
33 | |
---|
34 | enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND }; |
---|
35 | Mode iomode = O_RDONLY; |
---|
36 | //bool b = iomode == O_RDONLY || iomode < O_APPEND; // disallowed |
---|
37 | //int www = iomode; // disallowed |
---|
38 | |
---|
39 | enum( char * ) Colour { Red = "red", Green = "green", Blue = "blue" }; |
---|
40 | |
---|
41 | void fred() { |
---|
42 | Letter letter = A; |
---|
43 | Greek greek = Beta; |
---|
44 | letter = Beta; // allowed, letter == B |
---|
45 | //greek = A; // disallowed |
---|
46 | |
---|
47 | for ( Greek l = Alph; posn(l) <= posn(Gamma); l = succ( l ) ) { |
---|
48 | printf( "%s %c %d\n", label( l ), value( l ), posn( l ) ); |
---|
49 | } |
---|
50 | for ( Currency c = Dollar; posn(c) <= posn(Currency.Euro); c = succ( c ) ) { |
---|
51 | printf( "%s %c %d\n", label( c ), value( c ), posn( c ) ); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | enum( char * ) Names { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; |
---|
57 | enum( char * ) Names2 { inline Names, Jack = "JACK", Jill = "JILL" }; |
---|
58 | enum( char * ) Names3 { inline Names2, Sue = "SUE", Tom = "TOM" }; |
---|
59 | |
---|
60 | void f( Names n ) { sout | "Name" | posn( n ); } |
---|
61 | void g( Names2 ); |
---|
62 | void h( Names3 ); |
---|
63 | void j( char * ); |
---|
64 | |
---|
65 | enum color { red, blue, green }; |
---|
66 | //color c = 0; |
---|
67 | //color c = 1; |
---|
68 | color c = 2; |
---|
69 | int w = red; |
---|
70 | |
---|
71 | // enum(int) Week ! { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; |
---|
72 | // enum(int) RGB ! { Red, Green, Blue }; |
---|
73 | |
---|
74 | // void foo() { |
---|
75 | // with ( Week, RGB ) { |
---|
76 | // weekday = Sun; |
---|
77 | // rgb = Green; |
---|
78 | // } |
---|
79 | // } |
---|
80 | |
---|
81 | int main() { |
---|
82 | fred(); |
---|
83 | Names name = Fred; |
---|
84 | // f( name ); |
---|
85 | |
---|
86 | int jane_pos = posn( Names.Jane ); |
---|
87 | char * jane_value = value( Names.Jane ); |
---|
88 | char * jane_label = label( Names.Jane ); |
---|
89 | sout | Names.Jane | posn( Names.Jane) | label( Names.Jane ) | value( Names.Jane ); |
---|
90 | } |
---|