www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Associating enumeration values with strings

reply "Matthew" <admin.hat stlsoft.dot.org> writes:
I have no fixed idea about how to do this as yet - a template solution is
brimming slowly - but I'm thinking it'd be
perhaps better to have something built-in, e.g.

    public enum ORJ_FLAG
    {
            ORDERFIELDS                     =   0x0001 /*!< Arranges the fields
in alphabetical order */
        ,   ELIDEBLANKRECORDS     =   0x0002 /*!< Causes blank records to be
ignored */
    };

would become:

    public enum ORJ_FLAG
    {
            ORDERFIELDS                     =   0x0001     "Arranges the fields
in alphabetical order"
        ,   ELIDEBLANKRECORDS     =   0x0002     "Causes blank records to be
ignored"
    };


Since enum values may never be strings, there's no (immediately obvious)
syntactic conflict.

These would be associated with a mechasism for intrinsically creating the
toString() for the given enum. Any values not 
marked would be given the stringised form of the enum value name.

There are issues of localisation that might mitigate against this, but I think
an overloadable mechanism would be of 
great benefit to many users who would avoid large amounts of hassle.
Mar 06 2005
parent reply "Lionello Lunesu" <lio lunesu.removethis.com> writes:
"Matthew" <admin.hat stlsoft.dot.org> wrote in message 
news:d0gu1a$1n8b$1 digitaldaemon.com...
I have no fixed idea about how to do this as yet - a template solution is 
brimming slowly - but I'm thinking it'd be
 perhaps better to have something built-in, e.g.

    public enum ORJ_FLAG
    {
            ORDERFIELDS                     =   0x0001 /*!< Arranges the 
 fields in alphabetical order */
        ,   ELIDEBLANKRECORDS     =   0x0002 /*!< Causes blank records to 
 be ignored */
    };

 would become:

    public enum ORJ_FLAG
    {
            ORDERFIELDS                     =   0x0001     "Arranges the 
 fields in alphabetical order"
        ,   ELIDEBLANKRECORDS     =   0x0002     "Causes blank records to 
 be ignored"
    };
This could be easily done by using a statically initialised AA.. Which isn't yet possible, unfortunately. Anyway, enum.toString() would be the right method to get this string (and the literal enum value's name if no custom is given?) L.
Mar 07 2005
parent "Matthew" <admin.hat stlsoft.dot.org> writes:
"Lionello Lunesu" <lio lunesu.removethis.com> wrote in message
news:d0h8ck$21rg$1 digitaldaemon.com...
 "Matthew" <admin.hat stlsoft.dot.org> wrote in message
news:d0gu1a$1n8b$1 digitaldaemon.com...
I have no fixed idea about how to do this as yet - a template solution is
brimming slowly - but I'm thinking it'd be
 perhaps better to have something built-in, e.g.

    public enum ORJ_FLAG
    {
            ORDERFIELDS                     =   0x0001 /*!< Arranges the fields
in alphabetical order */
        ,   ELIDEBLANKRECORDS     =   0x0002 /*!< Causes blank records to be
ignored */
    };

 would become:

    public enum ORJ_FLAG
    {
            ORDERFIELDS                     =   0x0001     "Arranges the fields
in alphabetical order"
        ,   ELIDEBLANKRECORDS     =   0x0002     "Causes blank records to be
ignored"
    };
This could be easily done by using a statically initialised AA.. Which isn't yet possible, unfortunately. Anyway, enum.toString() would be the right method to get this string (and the literal enum value's name if no custom is given?)
This is where I'm at so far: /* ///////////////////////////////////////////////////////////////////////////// * Structs */ private struct EnumString { int value; char[] str; }; private template enum_to_string(T) { char[] enum_to_string(EnumString[] strings, T t) { // First version is simple - just a linear search foreach(EnumString s; strings) { if(cast(int)(t) == s.value) { return s.str; } } return "<unknown>"; } } /* ///////////////////////////////////////////////////////////////////////////// * Enumerations */ /** Flags that moderate the creation of Databases */ public enum ORJ_FLAG { ORDER_FIELDS = 0x0001 /*!< Arranges the fields in alphabetical order */ , ELIDE_BLANK_RECORDS = 0x0002 /*!< Causes blank records to be ignored */ } public char[] toString(ORJ_FLAG f) { const EnumString strings[] = [ { ORJ_FLAG.ORDER_FIELDS, "Arranges the fields in alphabetical order" } , { ORJ_FLAG.ELIDE_BLANK_RECORDS, "Causes blank records to be ignored" } ]; return enum_to_string!(ORJ_FLAG)(strings, f); }
Mar 07 2005