[1725989] | 1 | #include <fstream.hfa> // sout |
---|
| 2 | #include <stdlib.hfa> // ato |
---|
[5ca5263] | 3 | #include <enum.hfa> |
---|
[1725989] | 4 | |
---|
| 5 | // integral |
---|
| 6 | enum Letter { |
---|
| 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 | 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 |
---|
| 9 | }; |
---|
| 10 | enum( Letter ) Greek { Alph = A, Beta = B, Gamma = G, /* more enums */ Zeta = Z }; // alphabet intersection |
---|
| 11 | |
---|
[5ca5263] | 12 | // integral |
---|
[1725989] | 13 | enum( char ) Currency { Dollar = '$', Cent = '¢', Yen = '¥', Pound = '£', Euro = 'E' }; // iso-latin-1 |
---|
| 14 | enum( Currency ) Europe { Euro = Currency.Euro, Pound = Currency.Pound }; |
---|
| 15 | |
---|
| 16 | enum( signed char ) srgb { Red = -1, Green = 0, Blue = 1 }; |
---|
| 17 | enum( long long int ) BigNum { X = 123_456_789_012_345, Y = 345_012_789_456_123 }; |
---|
| 18 | // non-integral |
---|
| 19 | enum( double ) Math { PI_2 = 1.570796, PI = 3.141597, E = 2.718282 }; |
---|
| 20 | enum( _Complex ) Plane { X = 1.5+3.4i, Y = 7+3i, Z = 0+0.5i }; |
---|
| 21 | // pointer |
---|
| 22 | enum( const char * ) Name { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; |
---|
| 23 | int i, j, k; |
---|
| 24 | enum( int * ) ptr { I = &i, J = &j, K = &k }; |
---|
| 25 | //enum( int & ) ref { I = i, J = j, K = k }; |
---|
| 26 | // tuple |
---|
| 27 | //enum( [int, int] ) { T = [ 1, 2 ] }; |
---|
| 28 | // function |
---|
| 29 | void f() {} void g() {} |
---|
| 30 | enum( void (*)() ) funs { F = f, G = g }; |
---|
| 31 | // aggregate |
---|
| 32 | struct Person { char * name; int age, height; }; |
---|
| 33 | enum( Person ) friends { Liz = { "ELIZABETH", 22, 170 }, //Beth = Liz, |
---|
| 34 | Jon = { "Jonathan", 35, 190 } }; |
---|
| 35 | |
---|
| 36 | enum() Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND }; |
---|
[5ca5263] | 37 | Mode mode = O_RDONLY; |
---|
| 38 | void opaque() { |
---|
| 39 | bool b = mode == O_RDONLY || mode < O_APPEND; // disallowed |
---|
| 40 | //int www = mode; // disallowed |
---|
| 41 | } |
---|
[1725989] | 42 | |
---|
| 43 | enum( char * ) Colour { Red = "red", Green = "green", Blue = "blue" }; |
---|
| 44 | |
---|
[5ca5263] | 45 | enum E1 { A1, B1, C1 = A1, D1 = B1 }; |
---|
| 46 | enum(float) E2 { A2 = 3.5, B2 = 4.5, C2 = A, D2 = B }; |
---|
| 47 | |
---|
[1725989] | 48 | void fred() { |
---|
| 49 | Letter letter = A; |
---|
| 50 | Greek greek = Beta; |
---|
| 51 | letter = Beta; // allowed, letter == B |
---|
| 52 | //greek = A; // disallowed |
---|
| 53 | |
---|
[5ca5263] | 54 | for ( Greek l = Alph; posn(l) < posn(Gamma); l = succ( l ) ) { |
---|
[c40a982] | 55 | printf( "%s %c %d\n", label( l ), value( l ), posn( l ) ); |
---|
[1725989] | 56 | } |
---|
[5ca5263] | 57 | for ( Currency c = Dollar; posn(c) < posn(Currency.Euro); c = succ( c ) ) { |
---|
[c40a982] | 58 | printf( "%s %c %d\n", label( c ), value( c ), posn( c ) ); |
---|
[1725989] | 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
[5ca5263] | 62 | enum( const char * ) Names { Fred = "FRED", Mary = "MARY", Jane = "JANE" }; |
---|
| 63 | enum( const char * ) Names2 { inline Names, Jack = "JACK", Jill = "JILL" }; |
---|
| 64 | enum( const char * ) Names3 { inline Names2, Sue = "SUE", Tom = "TOM" }; |
---|
| 65 | void bar() { |
---|
| 66 | Names fred = Names.Fred; |
---|
| 67 | (Names2)fred; (Names3)fred; (Names3)Names2.Jack; // cast to super type |
---|
| 68 | Names2 fred2 = fred; Names3 fred3 = fred2; // assign to super type |
---|
| 69 | const char * name = fred; |
---|
| 70 | Names name = Fred; |
---|
| 71 | sout | name | label( name ) | posn( name ) | value( name ); |
---|
| 72 | } |
---|
[c40a982] | 73 | void f( Names n ) { sout | "Name" | posn( n ); } |
---|
[1725989] | 74 | void g( Names2 ); |
---|
| 75 | void h( Names3 ); |
---|
| 76 | void j( char * ); |
---|
| 77 | |
---|
[5ca5263] | 78 | enum CColour { Red, Blue, Green }; |
---|
| 79 | CColour c0 = 0; |
---|
| 80 | CColour c1 = 1; |
---|
| 81 | CColour c = 2; |
---|
| 82 | int w = Red; |
---|
| 83 | |
---|
| 84 | void coo() { |
---|
| 85 | enum(int) Color { Red, Blue, Green }; |
---|
| 86 | Colour c = Red; |
---|
| 87 | sout | countof( Colour ) | Countof( c ); |
---|
| 88 | // sout | Countof( Colour ); |
---|
| 89 | sout | countof( c ); |
---|
| 90 | } |
---|
[1725989] | 91 | |
---|
| 92 | // enum(int) Week ! { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; |
---|
| 93 | // enum(int) RGB ! { Red, Green, Blue }; |
---|
| 94 | |
---|
| 95 | // void foo() { |
---|
| 96 | // with ( Week, RGB ) { |
---|
| 97 | // weekday = Sun; |
---|
| 98 | // rgb = Green; |
---|
| 99 | // } |
---|
| 100 | // } |
---|
| 101 | |
---|
[5ca5263] | 102 | void baz() { |
---|
| 103 | enum(int) Count { First, Second, Third/* = First*/, Fourth/* = Second*/ }; |
---|
| 104 | enum CCount { First, Second, Third/* = First*/, Fourth/* = Second*/ }; |
---|
| 105 | Count cnt = Second; |
---|
| 106 | CCount ccnt = Second; |
---|
| 107 | if ( cnt < Third ) sout | "less than Third"; |
---|
| 108 | if ( cnt ) sout | "XXX"; |
---|
| 109 | if ( ccnt ) sout | "YYY"; |
---|
| 110 | enum(float) F {WWW = 0.0}; |
---|
| 111 | F f; |
---|
| 112 | if ( f ) sout | "FFF"; |
---|
| 113 | bool ?!=?( Name n, zero_t ) { sout | "DDD"; return n != Fred; } |
---|
| 114 | Name n = Mary; |
---|
| 115 | if ( n ) sout | "NAME"; |
---|
| 116 | choose( cnt ) { |
---|
| 117 | case First: sout | "First"; |
---|
| 118 | case Second: sout | "Second"; |
---|
| 119 | case Third: sout | "Third"; |
---|
| 120 | case Fourth: sout | "Fourth"; |
---|
| 121 | } |
---|
| 122 | // for (d; Week) { sout | d; } |
---|
| 123 | // for (p; +~=Planet) { sout | p; } |
---|
| 124 | for ( cx; Count ) { sout | cx | nonl; } sout | nl; |
---|
| 125 | for ( cx; +~= Count ) { sout | cx | nonl; } sout | nl; |
---|
| 126 | for ( cx; -~= Count ) { sout | cx | nonl; } sout | nl; |
---|
| 127 | for ( Count cx = lowerBound();; ) { |
---|
| 128 | sout | cx | nonl; |
---|
| 129 | if ( cx == upperBound() ) break; |
---|
| 130 | cx = succ( cx ); |
---|
| 131 | } |
---|
| 132 | sout | nl; |
---|
| 133 | } |
---|
| 134 | |
---|
[1725989] | 135 | int main() { |
---|
| 136 | fred(); |
---|
[5ca5263] | 137 | Names name = Names.Fred; |
---|
[1725989] | 138 | // f( name ); |
---|
| 139 | |
---|
[c40a982] | 140 | int jane_pos = posn( Names.Jane ); |
---|
[5ca5263] | 141 | const char * jane_value = value( Names.Jane ); |
---|
| 142 | const char * jane_label = label( Names.Jane ); |
---|
[c40a982] | 143 | sout | Names.Jane | posn( Names.Jane) | label( Names.Jane ) | value( Names.Jane ); |
---|
[5ca5263] | 144 | |
---|
| 145 | bar(); |
---|
| 146 | baz(); |
---|
| 147 | coo(); |
---|
| 148 | |
---|
| 149 | enum Ex { Ax, Bx, Cx, Nx }; |
---|
| 150 | float H1[Nx] = { [Ax] : 3.4, [Bx] : 7.1, [Cx] : 0.01 }; // C |
---|
| 151 | // float H2[Ex] = { [Ax] : 3.4, [Bx] : 7.1, [Cx] : 0.01 }; // CFA |
---|
| 152 | |
---|
| 153 | enum(int) E { A = 3 } e = A; |
---|
| 154 | sout | A | label( A ) | posn( A ) | value( A ); |
---|
| 155 | sout | e | label( e ) | posn( e ) | value( e ); |
---|
[1725989] | 156 | } |
---|