Jump to page: 1 2 3
Thread overview
A method for enum2str conversions. Reactions?
Mar 10, 2005
Matthew
Mar 10, 2005
Andrew Fedoniouk
Mar 10, 2005
Matthew
Mar 10, 2005
Andrew Fedoniouk
Mar 10, 2005
Kris
Mar 10, 2005
Andrew Fedoniouk
Mar 10, 2005
Paul Bonser
Mar 10, 2005
Paul Bonser
Mar 10, 2005
Andrew Fedoniouk
Mar 10, 2005
pragma
Mar 10, 2005
kris
Mar 10, 2005
Matthew
Mar 11, 2005
Kris
Mar 11, 2005
Matthew
Mar 11, 2005
Kris
Mar 11, 2005
Matthew
Mar 11, 2005
pragma
Mar 11, 2005
Sean Kelly
Mar 10, 2005
Matthew
And for non-continious enums - Re: A method for enum2str conversions. Reactions?
Mar 10, 2005
Andrew Fedoniouk
Mar 10, 2005
Matthew
Mar 10, 2005
Andrew Fedoniouk
Mar 10, 2005
Matthew
Mar 10, 2005
Jason Mills
March 10, 2005
Just thought I'd share what I came up with for the enum => string stuff, which is currently resident in std/openrj.d. If it receives a +ve reaction we can move it into somewhere common, and use it for all enums. (That's assuming, of course, we don't get something in the language to do it for us.)

Let me know what you think.

    private struct EnumString
    {
        int     value;
        char[]  str;
    };

    private template enum_to_string(T)
    {
        char[] enum_to_string(EnumString[] strings, T t)
        {
            // 'Optimised' search.
            //
            // Since many enums start at 0 and are contiguously ordered,
it's quite
            // likely that the value will equal the index. If it does,
we can just
            // return the string from that index.
            int index   =   cast(int)(t);

            if( index >= 0 &&
                index < strings.length &&
                strings[index].value == index)
            {
                return strings[index].str;
            }

            // Otherwise, just do 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);
    }

    /** General error codes */
    public enum ORJRC
    {
            SUCCESS                      =   0          /*!< Operation
was successful                                   */
        ,   CANNOT_OPEN_JAR_FILE                        /*!< The given
file does not exist, or cannot be accessed       */
        ,   NO_RECORDS                                  /*!< The
database file contained no records                     */
        ,   OUT_OF_MEMORY                               /*!< The API
suffered memory exhaustion                         */
        ,   BAD_FILE_READ                               /*!< A read
operation failed                                    */
        ,   PARSE_ERROR                                 /*!< Parsing of
the database file failed due to a syntax error  */
        ,   INVALID_INDEX                               /*!< An invalid
index was specified                             */
        ,   UNEXPECTED                                  /*!< An
unexpected condition was encountered                    */
        ,   INVALID_CONTENT                             /*!< The
database file contained invalid content                */
    }

    public char[] toString(ORJRC f)
    {
        const EnumString    strings[] =
        [
                {   ORJRC.SUCCESS,              "Operation was
successful"                                      }
            ,   {   ORJRC.CANNOT_OPEN_JAR_FILE, "The given file does not
exist, or cannot be accessed"          }
            ,   {   ORJRC.NO_RECORDS,           "The database file
contained no records"                        }
            ,   {   ORJRC.OUT_OF_MEMORY,        "The API suffered memory
exhaustion"                            }
            ,   {   ORJRC.BAD_FILE_READ,        "A read operation
failed"                                       }
            ,   {   ORJRC.PARSE_ERROR,          "Parsing of the database
file failed due to a syntax error"     }
            ,   {   ORJRC.INVALID_INDEX,        "An invalid index was
specified"                                }
            ,   {   ORJRC.UNEXPECTED,           "An unexpected condition
was encountered"                       }
            ,   {   ORJRC.INVALID_CONTENT,      "The database file
contained invalid content"                   }
        ];

        return enum_to_string!(ORJRC)(strings, f);
    }

    /** Parsing error codes */
    public enum ORJ_PARSE_ERROR
    {
            SUCCESS                         =   0       /*!< Parsing was
successful                                                         */
        ,   RECORD_SEPARATOR_IN_CONTINUATION            /*!< A record
separator was encountered during a content line continuation          */
        ,   UNFINISHED_LINE                             /*!< The last
line in the database was not terminated by a line-feed                */
        ,   UNFINISHED_FIELD                            /*!< The last
field in the database file was not terminated by a record separator   */
        ,   UNFINISHED_RECORD                           /*!< The last
record in the database file was not terminated by a record separator  */
    }

    public char[] toString(ORJ_PARSE_ERROR f)
    {
        const EnumString    strings[] =
        [
                {   ORJ_PARSE_ERROR.SUCCESS,
"Parsing was
            }
            ,   {   ORJ_PARSE_ERROR.RECORD_SEPARATOR_IN_CONTINUATION,
"A record separator was encountered during a content line
tion"         }
            ,   {   ORJ_PARSE_ERROR.UNFINISHED_LINE,
"The last line in the database was not terminated by a
           }
            ,   {   ORJ_PARSE_ERROR.UNFINISHED_FIELD,
"The last field in the database file was not terminated by a record
separator"  }
            ,   {   ORJ_PARSE_ERROR.UNFINISHED_RECORD,
"The last record in the database file was not terminated by a record
separator" }
        ];

        return enum_to_string!(ORJ_PARSE_ERROR)(strings, f);
    }



March 10, 2005
Matthew, could you attach this as  d source file?
Beg my pardon, but it is hardly readable.

Thanks in advance,

Andrew Fedoniouk.


"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0oiuc$13r3$1@digitaldaemon.com...
> Just thought I'd share what I came up with for the enum => string stuff, which is currently resident in std/openrj.d. If it receives a +ve reaction we can move it into somewhere common, and use it for all enums. (That's assuming, of course, we don't get something in the language to do it for us.)
>
> Let me know what you think.
>
>    private struct EnumString
>    {
>        int     value;
>        char[]  str;
>    };
>
>    private template enum_to_string(T)
>    {
>        char[] enum_to_string(EnumString[] strings, T t)
>        {
>            // 'Optimised' search.
>            //
>            // Since many enums start at 0 and are contiguously ordered,
> it's quite
>            // likely that the value will equal the index. If it does, we
> can just
>            // return the string from that index.
>            int index   =   cast(int)(t);
>
>            if( index >= 0 &&
>                index < strings.length &&
>                strings[index].value == index)
>            {
>                return strings[index].str;
>            }
>
>            // Otherwise, just do 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);
>    }
>
>    /** General error codes */
>    public enum ORJRC
>    {
>            SUCCESS                      =   0          /*!< Operation was
> successful                                   */
>        ,   CANNOT_OPEN_JAR_FILE                        /*!< The given file
> does not exist, or cannot be accessed       */
>        ,   NO_RECORDS                                  /*!< The database
> file contained no records                     */
>        ,   OUT_OF_MEMORY                               /*!< The API
> suffered memory exhaustion                         */
>        ,   BAD_FILE_READ                               /*!< A read
> operation failed                                    */
>        ,   PARSE_ERROR                                 /*!< Parsing of the
> database file failed due to a syntax error  */
>        ,   INVALID_INDEX                               /*!< An invalid
> index was specified                             */
>        ,   UNEXPECTED                                  /*!< An unexpected
> condition was encountered                    */
>        ,   INVALID_CONTENT                             /*!< The database
> file contained invalid content                */
>    }
>
>    public char[] toString(ORJRC f)
>    {
>        const EnumString    strings[] =
>        [
>                {   ORJRC.SUCCESS,              "Operation was
>             }
>            ,   {   ORJRC.CANNOT_OPEN_JAR_FILE, "The given file does not
> exist, or cannot be accessed"          }
>            ,   {   ORJRC.NO_RECORDS,           "The database file
> contained no records"                        }
>            ,   {   ORJRC.OUT_OF_MEMORY,        "The API suffered memory
> exhaustion"                            }
>            ,   {   ORJRC.BAD_FILE_READ,        "A read operation
>         }
>            ,   {   ORJRC.PARSE_ERROR,          "Parsing of the database
> file failed due to a syntax error"     }
>            ,   {   ORJRC.INVALID_INDEX,        "An invalid index was
> specified"                                }
>            ,   {   ORJRC.UNEXPECTED,           "An unexpected condition
> was encountered"                       }
>            ,   {   ORJRC.INVALID_CONTENT,      "The database file
> contained invalid content"                   }
>        ];
>
>        return enum_to_string!(ORJRC)(strings, f);
>    }
>
>    /** Parsing error codes */
>    public enum ORJ_PARSE_ERROR
>    {
>            SUCCESS                         =   0       /*!< Parsing was
> successful                                                         */
>        ,   RECORD_SEPARATOR_IN_CONTINUATION            /*!< A record
> separator was encountered during a content line continuation          */
>        ,   UNFINISHED_LINE                             /*!< The last line
> in the database was not terminated by a line-feed                */
>        ,   UNFINISHED_FIELD                            /*!< The last field
> in the database file was not terminated by a record separator   */
>        ,   UNFINISHED_RECORD                           /*!< The last
> record in the database file was not terminated by a record separator  */
>    }
>
>    public char[] toString(ORJ_PARSE_ERROR f)
>    {
>        const EnumString    strings[] =
>        [
>                {   ORJ_PARSE_ERROR.SUCCESS, "Parsing was }
>            ,   {   ORJ_PARSE_ERROR.RECORD_SEPARATOR_IN_CONTINUATION, "A
> record separator was encountered during a content line tion"         }
>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_LINE, "The last line in the
> database was not terminated by a }
>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_FIELD, "The last field in
> the database file was not terminated by a record separator"  }
>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_RECORD, "The last record in
> the database file was not terminated by a record separator" }
>        ];
>
>        return enum_to_string!(ORJ_PARSE_ERROR)(strings, f);
>    }
>
>
> 


March 10, 2005
No worries

"Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d0ollq$17ud$1@digitaldaemon.com...
> Matthew, could you attach this as  d source file?
> Beg my pardon, but it is hardly readable.
>
> Thanks in advance,
>
> Andrew Fedoniouk.
>
>
> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0oiuc$13r3$1@digitaldaemon.com...
>> Just thought I'd share what I came up with for the enum => string
>> stuff,
>> which is currently resident in std/openrj.d. If it receives a +ve
>> reaction
>> we can move it into somewhere common, and use it for all enums.
>> (That's
>> assuming, of course, we don't get something in the language to do it
>> for
>> us.)
>>
>> Let me know what you think.
>>
>>    private struct EnumString
>>    {
>>        int     value;
>>        char[]  str;
>>    };
>>
>>    private template enum_to_string(T)
>>    {
>>        char[] enum_to_string(EnumString[] strings, T t)
>>        {
>>            // 'Optimised' search.
>>            //
>>            // Since many enums start at 0 and are contiguously
>> ordered,
>> it's quite
>>            // likely that the value will equal the index. If it does,
>> we
>> can just
>>            // return the string from that index.
>>            int index   =   cast(int)(t);
>>
>>            if( index >= 0 &&
>>                index < strings.length &&
>>                strings[index].value == index)
>>            {
>>                return strings[index].str;
>>            }
>>
>>            // Otherwise, just do 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);
>>    }
>>
>>    /** General error codes */
>>    public enum ORJRC
>>    {
>>            SUCCESS                      =   0          /*!< Operation
>> was
>> successful                                   */
>>        ,   CANNOT_OPEN_JAR_FILE                        /*!< The given
>> file
>> does not exist, or cannot be accessed       */
>>        ,   NO_RECORDS                                  /*!< The
>> database
>> file contained no records                     */
>>        ,   OUT_OF_MEMORY                               /*!< The API
>> suffered memory exhaustion                         */
>>        ,   BAD_FILE_READ                               /*!< A read
>> operation failed                                    */
>>        ,   PARSE_ERROR                                 /*!< Parsing
>> of the
>> database file failed due to a syntax error  */
>>        ,   INVALID_INDEX                               /*!< An
>> invalid
>> index was specified                             */
>>        ,   UNEXPECTED                                  /*!< An
>> unexpected
>> condition was encountered                    */
>>        ,   INVALID_CONTENT                             /*!< The
>> database
>> file contained invalid content                */
>>    }
>>
>>    public char[] toString(ORJRC f)
>>    {
>>        const EnumString    strings[] =
>>        [
>>                {   ORJRC.SUCCESS,              "Operation was
>>             }
>>            ,   {   ORJRC.CANNOT_OPEN_JAR_FILE, "The given file does
>> not
>> exist, or cannot be accessed"          }
>>            ,   {   ORJRC.NO_RECORDS,           "The database file
>> contained no records"                        }
>>            ,   {   ORJRC.OUT_OF_MEMORY,        "The API suffered
>> memory
>> exhaustion"                            }
>>            ,   {   ORJRC.BAD_FILE_READ,        "A read operation
>>         }
>>            ,   {   ORJRC.PARSE_ERROR,          "Parsing of the
>> database
>> file failed due to a syntax error"     }
>>            ,   {   ORJRC.INVALID_INDEX,        "An invalid index was
>> specified"                                }
>>            ,   {   ORJRC.UNEXPECTED,           "An unexpected
>> condition
>> was encountered"                       }
>>            ,   {   ORJRC.INVALID_CONTENT,      "The database file
>> contained invalid content"                   }
>>        ];
>>
>>        return enum_to_string!(ORJRC)(strings, f);
>>    }
>>
>>    /** Parsing error codes */
>>    public enum ORJ_PARSE_ERROR
>>    {
>>            SUCCESS                         =   0       /*!< Parsing
>> was
>> successful                                                         */
>>        ,   RECORD_SEPARATOR_IN_CONTINUATION            /*!< A record
>> separator was encountered during a content line continuation
>> */
>>        ,   UNFINISHED_LINE                             /*!< The last
>> line
>> in the database was not terminated by a line-feed                */
>>        ,   UNFINISHED_FIELD                            /*!< The last
>> field
>> in the database file was not terminated by a record separator   */
>>        ,   UNFINISHED_RECORD                           /*!< The last
>> record in the database file was not terminated by a record separator
>> */
>>    }
>>
>>    public char[] toString(ORJ_PARSE_ERROR f)
>>    {
>>        const EnumString    strings[] =
>>        [
>>                {   ORJ_PARSE_ERROR.SUCCESS, "Parsing was }
>>            ,   {   ORJ_PARSE_ERROR.RECORD_SEPARATOR_IN_CONTINUATION,
>> "A
>> record separator was encountered during a content line
>>       }
>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_LINE, "The last line in
>> the
>> database was not terminated by a }
>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_FIELD, "The last field
>> in
>> the database file was not terminated by a record separator"  }
>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_RECORD, "The last
>> record in
>> the database file was not terminated by a record separator" }
>>        ];
>>
>>        return enum_to_string!(ORJ_PARSE_ERROR)(strings, f);
>>    }
>>
>>
>>
>
> 



March 10, 2005
Thanks a lot,

What about this:

enum E {
  ZERO,
  ONE,
  TWO
}

char[] toString(E e)
{
  static char[][] map =
  [
    E.ZERO  :"primordial",
    E.ONE   :"first",
    E.TWO   :"second"
  ];
  return map[e];
}

for simple continuous enums?

Anyway enums which are sets of bits (0x01,0x02, 0x04...) in fact
need different toString implementation (string composition)

Andrew.




"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0omiq$19o7$1@digitaldaemon.com...
> No worries
>
> "Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d0ollq$17ud$1@digitaldaemon.com...
>> Matthew, could you attach this as  d source file?
>> Beg my pardon, but it is hardly readable.
>>
>> Thanks in advance,
>>
>> Andrew Fedoniouk.
>>
>>
>> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0oiuc$13r3$1@digitaldaemon.com...
>>> Just thought I'd share what I came up with for the enum => string stuff,
>>> which is currently resident in std/openrj.d. If it receives a +ve
>>> reaction
>>> we can move it into somewhere common, and use it for all enums. (That's
>>> assuming, of course, we don't get something in the language to do it for
>>> us.)
>>>
>>> Let me know what you think.
>>>
>>>    private struct EnumString
>>>    {
>>>        int     value;
>>>        char[]  str;
>>>    };
>>>
>>>    private template enum_to_string(T)
>>>    {
>>>        char[] enum_to_string(EnumString[] strings, T t)
>>>        {
>>>            // 'Optimised' search.
>>>            //
>>>            // Since many enums start at 0 and are contiguously ordered,
>>> it's quite
>>>            // likely that the value will equal the index. If it does, we
>>> can just
>>>            // return the string from that index.
>>>            int index   =   cast(int)(t);
>>>
>>>            if( index >= 0 &&
>>>                index < strings.length &&
>>>                strings[index].value == index)
>>>            {
>>>                return strings[index].str;
>>>            }
>>>
>>>            // Otherwise, just do 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);
>>>    }
>>>
>>>    /** General error codes */
>>>    public enum ORJRC
>>>    {
>>>            SUCCESS                      =   0          /*!< Operation
>>> was
>>> successful                                   */
>>>        ,   CANNOT_OPEN_JAR_FILE                        /*!< The given
>>> file
>>> does not exist, or cannot be accessed       */
>>>        ,   NO_RECORDS                                  /*!< The database
>>> file contained no records                     */
>>>        ,   OUT_OF_MEMORY                               /*!< The API
>>> suffered memory exhaustion                         */
>>>        ,   BAD_FILE_READ                               /*!< A read
>>> operation failed                                    */
>>>        ,   PARSE_ERROR                                 /*!< Parsing of
>>> the
>>> database file failed due to a syntax error  */
>>>        ,   INVALID_INDEX                               /*!< An invalid
>>> index was specified                             */
>>>        ,   UNEXPECTED                                  /*!< An
>>> unexpected
>>> condition was encountered                    */
>>>        ,   INVALID_CONTENT                             /*!< The database
>>> file contained invalid content                */
>>>    }
>>>
>>>    public char[] toString(ORJRC f)
>>>    {
>>>        const EnumString    strings[] =
>>>        [
>>>                {   ORJRC.SUCCESS,              "Operation was
>>>             }
>>>            ,   {   ORJRC.CANNOT_OPEN_JAR_FILE, "The given file does not
>>> exist, or cannot be accessed"          }
>>>            ,   {   ORJRC.NO_RECORDS,           "The database file
>>> contained no records"                        }
>>>            ,   {   ORJRC.OUT_OF_MEMORY,        "The API suffered memory
>>> exhaustion"                            }
>>>            ,   {   ORJRC.BAD_FILE_READ,        "A read operation
>>>         }
>>>            ,   {   ORJRC.PARSE_ERROR,          "Parsing of the database
>>> file failed due to a syntax error"     }
>>>            ,   {   ORJRC.INVALID_INDEX,        "An invalid index was
>>> specified"                                }
>>>            ,   {   ORJRC.UNEXPECTED,           "An unexpected condition
>>> was encountered"                       }
>>>            ,   {   ORJRC.INVALID_CONTENT,      "The database file
>>> contained invalid content"                   }
>>>        ];
>>>
>>>        return enum_to_string!(ORJRC)(strings, f);
>>>    }
>>>
>>>    /** Parsing error codes */
>>>    public enum ORJ_PARSE_ERROR
>>>    {
>>>            SUCCESS                         =   0       /*!< Parsing was
>>> successful                                                         */
>>>        ,   RECORD_SEPARATOR_IN_CONTINUATION            /*!< A record
>>> separator was encountered during a content line continuation */
>>>        ,   UNFINISHED_LINE                             /*!< The last
>>> line
>>> in the database was not terminated by a line-feed                */
>>>        ,   UNFINISHED_FIELD                            /*!< The last
>>> field
>>> in the database file was not terminated by a record separator   */
>>>        ,   UNFINISHED_RECORD                           /*!< The last
>>> record in the database file was not terminated by a record separator */
>>>    }
>>>
>>>    public char[] toString(ORJ_PARSE_ERROR f)
>>>    {
>>>        const EnumString    strings[] =
>>>        [
>>>                {   ORJ_PARSE_ERROR.SUCCESS, "Parsing was }
>>>            ,   {   ORJ_PARSE_ERROR.RECORD_SEPARATOR_IN_CONTINUATION, "A
>>> record separator was encountered during a content line }
>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_LINE, "The last line in
>>> the
>>> database was not terminated by a }
>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_FIELD, "The last field in
>>> the database file was not terminated by a record separator"  }
>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_RECORD, "The last record
>>> in
>>> the database file was not terminated by a record separator" }
>>>        ];
>>>
>>>        return enum_to_string!(ORJ_PARSE_ERROR)(strings, f);
>>>    }
>>>
>>>
>>>
>>
>>
>
>
> 


March 10, 2005
enum E {
  FIRST = 0x01,
  SECOND =  0x02 ,
  THIRD =  0x04
}

alias char[] string8;

string8 toString(E e)
{
  static string8[int] map; if(!map.length)
  {
    map[E.FIRST] = "first";
    map[E.SECOND]  = "second";
    map[E.THIRD] = "third one";
  }
  return map[e];
}

Huh?

Andrew.


March 10, 2005
Why not push to get the enum-names exposed via reflection instead? I wouldn't wish to have to re-type them all over again, in a different manner. Doesn't make sense.

- Kris



In article <d0onfb$1afp$1@digitaldaemon.com>, Andrew Fedoniouk says...
>
>Thanks a lot,
>
>What about this:
>
>enum E {
>  ZERO,
>  ONE,
>  TWO
>}
>
>char[] toString(E e)
>{
>  static char[][] map =
>  [
>    E.ZERO  :"primordial",
>    E.ONE   :"first",
>    E.TWO   :"second"
>  ];
>  return map[e];
>}
>
>for simple continuous enums?
>
>Anyway enums which are sets of bits (0x01,0x02, 0x04...) in fact
>need different toString implementation (string composition)
>
>Andrew.
>
>
>
>
>"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0omiq$19o7$1@digitaldaemon.com...
>> No worries
>>
>> "Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d0ollq$17ud$1@digitaldaemon.com...
>>> Matthew, could you attach this as  d source file?
>>> Beg my pardon, but it is hardly readable.
>>>
>>> Thanks in advance,
>>>
>>> Andrew Fedoniouk.
>>>
>>>
>>> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0oiuc$13r3$1@digitaldaemon.com...
>>>> Just thought I'd share what I came up with for the enum => string stuff,
>>>> which is currently resident in std/openrj.d. If it receives a +ve
>>>> reaction
>>>> we can move it into somewhere common, and use it for all enums. (That's
>>>> assuming, of course, we don't get something in the language to do it for
>>>> us.)
>>>>
>>>> Let me know what you think.
>>>>
>>>>    private struct EnumString
>>>>    {
>>>>        int     value;
>>>>        char[]  str;
>>>>    };
>>>>
>>>>    private template enum_to_string(T)
>>>>    {
>>>>        char[] enum_to_string(EnumString[] strings, T t)
>>>>        {
>>>>            // 'Optimised' search.
>>>>            //
>>>>            // Since many enums start at 0 and are contiguously ordered,
>>>> it's quite
>>>>            // likely that the value will equal the index. If it does, we
>>>> can just
>>>>            // return the string from that index.
>>>>            int index   =   cast(int)(t);
>>>>
>>>>            if( index >= 0 &&
>>>>                index < strings.length &&
>>>>                strings[index].value == index)
>>>>            {
>>>>                return strings[index].str;
>>>>            }
>>>>
>>>>            // Otherwise, just do 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);
>>>>    }
>>>>
>>>>    /** General error codes */
>>>>    public enum ORJRC
>>>>    {
>>>>            SUCCESS                      =   0          /*!< Operation
>>>> was
>>>> successful                                   */
>>>>        ,   CANNOT_OPEN_JAR_FILE                        /*!< The given
>>>> file
>>>> does not exist, or cannot be accessed       */
>>>>        ,   NO_RECORDS                                  /*!< The database
>>>> file contained no records                     */
>>>>        ,   OUT_OF_MEMORY                               /*!< The API
>>>> suffered memory exhaustion                         */
>>>>        ,   BAD_FILE_READ                               /*!< A read
>>>> operation failed                                    */
>>>>        ,   PARSE_ERROR                                 /*!< Parsing of
>>>> the
>>>> database file failed due to a syntax error  */
>>>>        ,   INVALID_INDEX                               /*!< An invalid
>>>> index was specified                             */
>>>>        ,   UNEXPECTED                                  /*!< An
>>>> unexpected
>>>> condition was encountered                    */
>>>>        ,   INVALID_CONTENT                             /*!< The database
>>>> file contained invalid content                */
>>>>    }
>>>>
>>>>    public char[] toString(ORJRC f)
>>>>    {
>>>>        const EnumString    strings[] =
>>>>        [
>>>>                {   ORJRC.SUCCESS,              "Operation was
>>>>             }
>>>>            ,   {   ORJRC.CANNOT_OPEN_JAR_FILE, "The given file does not
>>>> exist, or cannot be accessed"          }
>>>>            ,   {   ORJRC.NO_RECORDS,           "The database file
>>>> contained no records"                        }
>>>>            ,   {   ORJRC.OUT_OF_MEMORY,        "The API suffered memory
>>>> exhaustion"                            }
>>>>            ,   {   ORJRC.BAD_FILE_READ,        "A read operation
>>>>         }
>>>>            ,   {   ORJRC.PARSE_ERROR,          "Parsing of the database
>>>> file failed due to a syntax error"     }
>>>>            ,   {   ORJRC.INVALID_INDEX,        "An invalid index was
>>>> specified"                                }
>>>>            ,   {   ORJRC.UNEXPECTED,           "An unexpected condition
>>>> was encountered"                       }
>>>>            ,   {   ORJRC.INVALID_CONTENT,      "The database file
>>>> contained invalid content"                   }
>>>>        ];
>>>>
>>>>        return enum_to_string!(ORJRC)(strings, f);
>>>>    }
>>>>
>>>>    /** Parsing error codes */
>>>>    public enum ORJ_PARSE_ERROR
>>>>    {
>>>>            SUCCESS                         =   0       /*!< Parsing was
>>>> successful                                                         */
>>>>        ,   RECORD_SEPARATOR_IN_CONTINUATION            /*!< A record
>>>> separator was encountered during a content line continuation */
>>>>        ,   UNFINISHED_LINE                             /*!< The last
>>>> line
>>>> in the database was not terminated by a line-feed                */
>>>>        ,   UNFINISHED_FIELD                            /*!< The last
>>>> field
>>>> in the database file was not terminated by a record separator   */
>>>>        ,   UNFINISHED_RECORD                           /*!< The last
>>>> record in the database file was not terminated by a record separator */
>>>>    }
>>>>
>>>>    public char[] toString(ORJ_PARSE_ERROR f)
>>>>    {
>>>>        const EnumString    strings[] =
>>>>        [
>>>>                {   ORJ_PARSE_ERROR.SUCCESS, "Parsing was }
>>>>            ,   {   ORJ_PARSE_ERROR.RECORD_SEPARATOR_IN_CONTINUATION, "A
>>>> record separator was encountered during a content line }
>>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_LINE, "The last line in
>>>> the
>>>> database was not terminated by a }
>>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_FIELD, "The last field in
>>>> the database file was not terminated by a record separator"  }
>>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_RECORD, "The last record
>>>> in
>>>> the database file was not terminated by a record separator" }
>>>>        ];
>>>>
>>>>        return enum_to_string!(ORJ_PARSE_ERROR)(strings, f);
>>>>    }
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>> 
>
>


March 10, 2005
Hi, Kris,

Ideally these Enum strings should come in different languages. Right? So reflection will not help you here.

Reflection might help in other places but not too much.
I can see only couple areas where it may help:
persistent storages/OODB, and SOAP alike use cases.
Does anybody know other areas where reflection is desirable?

Andrew.



"Kris" <Kris_member@pathlink.com> wrote in message news:d0op0e$1bnh$1@digitaldaemon.com...
> Why not push to get the enum-names exposed via reflection instead? I
> wouldn't
> wish to have to re-type them all over again, in a different manner.
> Doesn't make
> sense.
>
> - Kris
>
>
>
> In article <d0onfb$1afp$1@digitaldaemon.com>, Andrew Fedoniouk says...
>>
>>Thanks a lot,
>>
>>What about this:
>>
>>enum E {
>>  ZERO,
>>  ONE,
>>  TWO
>>}
>>
>>char[] toString(E e)
>>{
>>  static char[][] map =
>>  [
>>    E.ZERO  :"primordial",
>>    E.ONE   :"first",
>>    E.TWO   :"second"
>>  ];
>>  return map[e];
>>}
>>
>>for simple continuous enums?
>>
>>Anyway enums which are sets of bits (0x01,0x02, 0x04...) in fact
>>need different toString implementation (string composition)
>>
>>Andrew.
>>
>>
>>
>>
>>"Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0omiq$19o7$1@digitaldaemon.com...
>>> No worries
>>>
>>> "Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d0ollq$17ud$1@digitaldaemon.com...
>>>> Matthew, could you attach this as  d source file?
>>>> Beg my pardon, but it is hardly readable.
>>>>
>>>> Thanks in advance,
>>>>
>>>> Andrew Fedoniouk.
>>>>
>>>>
>>>> "Matthew" <admin@stlsoft.dot.dot.dot.dot.org> wrote in message news:d0oiuc$13r3$1@digitaldaemon.com...
>>>>> Just thought I'd share what I came up with for the enum => string
>>>>> stuff,
>>>>> which is currently resident in std/openrj.d. If it receives a +ve
>>>>> reaction
>>>>> we can move it into somewhere common, and use it for all enums.
>>>>> (That's
>>>>> assuming, of course, we don't get something in the language to do it
>>>>> for
>>>>> us.)
>>>>>
>>>>> Let me know what you think.
>>>>>
>>>>>    private struct EnumString
>>>>>    {
>>>>>        int     value;
>>>>>        char[]  str;
>>>>>    };
>>>>>
>>>>>    private template enum_to_string(T)
>>>>>    {
>>>>>        char[] enum_to_string(EnumString[] strings, T t)
>>>>>        {
>>>>>            // 'Optimised' search.
>>>>>            //
>>>>>            // Since many enums start at 0 and are contiguously
>>>>> ordered,
>>>>> it's quite
>>>>>            // likely that the value will equal the index. If it does,
>>>>> we
>>>>> can just
>>>>>            // return the string from that index.
>>>>>            int index   =   cast(int)(t);
>>>>>
>>>>>            if( index >= 0 &&
>>>>>                index < strings.length &&
>>>>>                strings[index].value == index)
>>>>>            {
>>>>>                return strings[index].str;
>>>>>            }
>>>>>
>>>>>            // Otherwise, just do 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);
>>>>>    }
>>>>>
>>>>>    /** General error codes */
>>>>>    public enum ORJRC
>>>>>    {
>>>>>            SUCCESS                      =   0          /*!< Operation
>>>>> was
>>>>> successful                                   */
>>>>>        ,   CANNOT_OPEN_JAR_FILE                        /*!< The given
>>>>> file
>>>>> does not exist, or cannot be accessed       */
>>>>>        ,   NO_RECORDS                                  /*!< The
>>>>> database
>>>>> file contained no records                     */
>>>>>        ,   OUT_OF_MEMORY                               /*!< The API
>>>>> suffered memory exhaustion                         */
>>>>>        ,   BAD_FILE_READ                               /*!< A read
>>>>> operation failed                                    */
>>>>>        ,   PARSE_ERROR                                 /*!< Parsing of
>>>>> the
>>>>> database file failed due to a syntax error  */
>>>>>        ,   INVALID_INDEX                               /*!< An invalid
>>>>> index was specified                             */
>>>>>        ,   UNEXPECTED                                  /*!< An
>>>>> unexpected
>>>>> condition was encountered                    */
>>>>>        ,   INVALID_CONTENT                             /*!< The
>>>>> database
>>>>> file contained invalid content                */
>>>>>    }
>>>>>
>>>>>    public char[] toString(ORJRC f)
>>>>>    {
>>>>>        const EnumString    strings[] =
>>>>>        [
>>>>>                {   ORJRC.SUCCESS,              "Operation was
>>>>>             }
>>>>>            ,   {   ORJRC.CANNOT_OPEN_JAR_FILE, "The given file does
>>>>> not
>>>>> exist, or cannot be accessed"          }
>>>>>            ,   {   ORJRC.NO_RECORDS,           "The database file
>>>>> contained no records"                        }
>>>>>            ,   {   ORJRC.OUT_OF_MEMORY,        "The API suffered
>>>>> memory
>>>>> exhaustion"                            }
>>>>>            ,   {   ORJRC.BAD_FILE_READ,        "A read operation
>>>>>         }
>>>>>            ,   {   ORJRC.PARSE_ERROR,          "Parsing of the
>>>>> database
>>>>> file failed due to a syntax error"     }
>>>>>            ,   {   ORJRC.INVALID_INDEX,        "An invalid index was
>>>>> specified"                                }
>>>>>            ,   {   ORJRC.UNEXPECTED,           "An unexpected
>>>>> condition
>>>>> was encountered"                       }
>>>>>            ,   {   ORJRC.INVALID_CONTENT,      "The database file
>>>>> contained invalid content"                   }
>>>>>        ];
>>>>>
>>>>>        return enum_to_string!(ORJRC)(strings, f);
>>>>>    }
>>>>>
>>>>>    /** Parsing error codes */
>>>>>    public enum ORJ_PARSE_ERROR
>>>>>    {
>>>>>            SUCCESS                         =   0       /*!< Parsing
>>>>> was
>>>>> successful                                                         */
>>>>>        ,   RECORD_SEPARATOR_IN_CONTINUATION            /*!< A record
>>>>> separator was encountered during a content line continuation */
>>>>>        ,   UNFINISHED_LINE                             /*!< The last
>>>>> line
>>>>> in the database was not terminated by a line-feed                */
>>>>>        ,   UNFINISHED_FIELD                            /*!< The last
>>>>> field
>>>>> in the database file was not terminated by a record separator   */
>>>>>        ,   UNFINISHED_RECORD                           /*!< The last
>>>>> record in the database file was not terminated by a record separator
>>>>> */
>>>>>    }
>>>>>
>>>>>    public char[] toString(ORJ_PARSE_ERROR f)
>>>>>    {
>>>>>        const EnumString    strings[] =
>>>>>        [
>>>>>                {   ORJ_PARSE_ERROR.SUCCESS, "Parsing was }
>>>>>            ,   {   ORJ_PARSE_ERROR.RECORD_SEPARATOR_IN_CONTINUATION,
>>>>> "A
>>>>> record separator was encountered during a content line }
>>>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_LINE, "The last line in
>>>>> the
>>>>> database was not terminated by a }
>>>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_FIELD, "The last field
>>>>> in
>>>>> the database file was not terminated by a record separator"  }
>>>>>            ,   {   ORJ_PARSE_ERROR.UNFINISHED_RECORD, "The last record
>>>>> in
>>>>> the database file was not terminated by a record separator" }
>>>>>        ];
>>>>>
>>>>>        return enum_to_string!(ORJ_PARSE_ERROR)(strings, f);
>>>>>    }
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>>
>>
>>
>
> 


March 10, 2005
> Does anybody know other areas where reflection is desirable?


Embedded Scripting, I'd imagine.

I'm working on a game engine (in D, of course) and I'm going to want to embed a scripting language of some sort into it (haven't decided what to use yet, but I really want something that is bytecode-compiled...but that's another topic). It would be really handy to be able to call any function (and access any variable) I wanted from a script, without having to manually add a hook for each function I wanted to call or variable I wanted to access. Just add a couple of functions that look up the name you gave, call it if it's there, give an error if it isn't. It would make the scripting language better integrated into the program, without all the extra coding of doing it manually.

Also, I'd like to be able to pop up a scripting console in-game (for when I'm developing it, at least..) and instantiate objects of various types and call functions wilily-nilly, as well as modifying objects and such that already exist. (again, without having to manually tell the scripting language about each and every data type that's out there.)

Oh it would make me so happy to be able to do that.

I believe it is also good for debugging and adding class-browsers and did I mention scripting? :P

So I guess until reflection is made part of the language (or something) I'll have to just do a "sorta" reflection by making every class a subclass of a Reflection class and have to keep an associative array of all the function names and variable names with delegates and pointers and...blah, that's what I want to not have to do :(

-- 
-PIB

--
"C++ also supports the notion of *friends*: cooperative classes that
are permitted to see each other's private parts." - Grady Booch
March 10, 2005
"Andrew Fedoniouk" <news@terrainformatica.com> wrote in message news:d0oodt$1ba0$1@digitaldaemon.com...
> enum E {
>  FIRST = 0x01,
>  SECOND =  0x02 ,
>  THIRD =  0x04
> }
>
> alias char[] string8;
>
> string8 toString(E e)
> {
>  static string8[int] map; if(!map.length)
>  {
>    map[E.FIRST] = "first";
>    map[E.SECOND]  = "second";
>    map[E.THIRD] = "third one";
>  }
>  return map[e];
> }
>
> Huh?

It's going to use more memory, though it's only a bit.

It allocates memory at runtime, with the consequent, though unlikely, possibility of being out of memory

It's going to run slower, though it's only a bit. Linear searches are generally more efficient for small data sets, which most enums are.

It doesn't optimise for the common case where an enum value is equal to its index.

It doesn't handle the case where the value is unrecognised, though that could be added by using some horrible 'in' construction.

All these are small matters, to be sure, but are persuasive, at least to me.

Please note: the solution I gave is what I would hope would become built-in. In that case it's only drawback, its verbosity, would be moot.




March 10, 2005
Paul Bonser wrote:
> 
>> Does anybody know other areas where reflection is desirable?
> 
> 
> 
> Embedded Scripting, I'd imagine.
> 
> I'm working on a game engine (in D, of course) and I'm going to want to embed a scripting language of some sort into it (haven't decided what to use yet, but I really want something that is bytecode-compiled...but that's another topic). It would be really handy to be able to call any function (and access any variable) I wanted from a script, without having to manually add a hook for each function I wanted to call or variable I wanted to access. Just add a couple of functions that look up the name you gave, call it if it's there, give an error if it isn't. It would make the scripting language better integrated into the program, without all the extra coding of doing it manually.
> 
> Also, I'd like to be able to pop up a scripting console in-game (for when I'm developing it, at least..) and instantiate objects of various types and call functions wilily-nilly, as well as modifying objects and such that already exist. (again, without having to manually tell the scripting language about each and every data type that's out there.)
> 
> Oh it would make me so happy to be able to do that.
> 
> I believe it is also good for debugging and adding class-browsers and did I mention scripting? :P
> 
> So I guess until reflection is made part of the language (or something) I'll have to just do a "sorta" reflection by making every class a subclass of a Reflection class and have to keep an associative array of all the function names and variable names with delegates and pointers and...blah, that's what I want to not have to do :(
> 

Well, I should say that I would probably write a script or program or whatnot to filter my files and add all this "artificial reflection" in for me. But again, I'd much rather not have to do this...

-- 
-PIB

--
"C++ also supports the notion of *friends*: cooperative classes that
are permitted to see each other's private parts." - Grady Booch
« First   ‹ Prev
1 2 3