[efd055c] | 1 | #include <fstream.hfa> |
---|
| 2 | #include <enum.hfa> |
---|
| 3 | #include <string.hfa> |
---|
| 4 | |
---|
[ee2f11f] | 5 | forall( E, V | TypedEnum( E, V ) | { string str( V ); } ) // routine to format value}$ |
---|
[efd055c] | 6 | string format_enum( E e ) { |
---|
[ee2f11f] | 7 | return label( e ) + '(' + str( value( e ) ) + ')'; // "label( value )" |
---|
[efd055c] | 8 | } |
---|
| 9 | enum(size_t) RGB { Red = 0xFF0000, Green = 0x00FF00, Blue = 0x0000FF }; |
---|
| 10 | // string library has conversion from size_t to string |
---|
| 11 | |
---|
| 12 | struct color_code { int R, G, B; }; |
---|
| 13 | enum(color_code) Rainbow { |
---|
[ee2f11f] | 14 | Red = {255, 0, 0}, Orange = {255, 127, 0}, Yellow = {255, 255, 0}, Green = {0, 255, 0}, // ... |
---|
[efd055c] | 15 | }; |
---|
| 16 | string str( color_code cc ) with( cc ) { |
---|
[ee2f11f] | 17 | return str( R ) + ',' + str( G ) + ',' + str( B ); // "R,G,B" |
---|
[efd055c] | 18 | } |
---|
| 19 | |
---|
[ee2f11f] | 20 | enum Fruit { Apple, Banana, Cherry }; // C enum |
---|
[efd055c] | 21 | const char * label( Fruit f ) { |
---|
[ee2f11f] | 22 | static const char * labels[] = { "Apple", "Banana", "Cherry" }; |
---|
| 23 | return labels[f]; |
---|
[efd055c] | 24 | } |
---|
| 25 | int posn( Fruit f ) { return f; } |
---|
| 26 | int value( Fruit f ) { |
---|
[ee2f11f] | 27 | static const char values[] = { 'a', 'b', 'c' }; |
---|
| 28 | return values[f]; |
---|
[efd055c] | 29 | } |
---|
| 30 | string str( int f ) { |
---|
[ee2f11f] | 31 | string s = (char)f; |
---|
| 32 | return s; |
---|
[efd055c] | 33 | } |
---|
| 34 | int main() { |
---|
[ee2f11f] | 35 | sout | format_enum( RGB.Green ); // "Green(65280)"}$ |
---|
| 36 | sout | format_enum( Rainbow.Green ); // "Green(0,255,0)"}$ |
---|
| 37 | sout | format_enum( Cherry ); // "Cherry(c)" |
---|
| 38 | } |
---|