View mode: basic / threaded / horizontal-split · Log in · Help
March 07, 2005
How to initialise a struct??
struct EnumString
{
   int     value;
   char*   str;
};

/* /////////////////////////////////////////////////////////////////////////////
* 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[2] =
   {
           {   ORDER_FIELDS,           "Arranges the fields in alphabetical order" }
       ,   {   ELIDE_BLANK_RECORDS,    "Causes blank records to be ignored"        }
   };

   return "";
}



The compiler informs me:

   openrj.d(83): Error: a struct is not a valid initializer for a EnumString [2]

All help gratefully received.

:-)
March 07, 2005
Re: How to initialise a struct??
Matthew wrote:

> The compiler informs me:
> 
>     openrj.d(83): Error: a struct is not a valid initializer for a EnumString [2]
> 
> All help gratefully received.

You need to use [] for arrays in D. You also need to name the enums:

>     const EnumString    strings[2] =
>     [
>             {   ORJ_FLAG.ORDER_FIELDS,           "Arranges the fields in alphabetical order" }
>         ,   {   ORJ_FLAG.ELIDE_BLANK_RECORDS,    "Causes blank records to be ignored"        }
>     ];

--anders
March 07, 2005
Re: How to initialise a struct??
*MANY* thanks!

:-)


"Anders F Björklund" <afb@algonet.se> wrote in message news:d0hash$23hk$2@digitaldaemon.com...
> Matthew wrote:
>
>> The compiler informs me:
>>
>>     openrj.d(83): Error: a struct is not a valid initializer for a EnumString [2]
>>
>> All help gratefully received.
>
> You need to use [] for arrays in D. You also need to name the enums:
>
>>     const EnumString    strings[2] =
>>     [
>>             {   ORJ_FLAG.ORDER_FIELDS,           "Arranges the fields in alphabetical order" }
>>         ,   {   ORJ_FLAG.ELIDE_BLANK_RECORDS,    "Causes blank records to be ignored"        }
>>     ];
>
> --anders
March 07, 2005
Re: How to initialise a struct??
On Mon, 7 Mar 2005 21:33:39 +1100, Matthew wrote:

> struct EnumString
> {
>     int     value;
>     char*   str;
> };
> 
> /* /////////////////////////////////////////////////////////////////////////////
>  * 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[2] =
>     {
>             {   ORDER_FIELDS,           "Arranges the fields in alphabetical order" }
>         ,   {   ELIDE_BLANK_RECORDS,    "Causes blank records to be ignored"        }
>     };
> 
>     return "";
> }

Is this what you are trying to do???

<code>
import std.stdio;
/** 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                         */
}

struct EnumString
{
   ORJ_FLAG     value;
   char[]   str;
};

/*
/////////////////////////////////////////////////////////////////////////////
* Enumerations
*/

public char[] toString(ORJ_FLAG f)
{
   const EnumString[2]    strings =
   [
           {   ORJ_FLAG.ORDER_FIELDS,           "Arranges the fields in
alphabetical order"}, 
           {   ORJ_FLAG.ELIDE_BLANK_RECORDS,    "Causes blank records to
be ignored"       }
   ];
   int i;
   for (i = 0; i < strings.length; i++)
   {
       if (strings[i].value == f)
           return strings[i].str;
   }
   return "";
}

void main()
{
   ORJ_FLAG x;
   
   x = ORJ_FLAG.ORDER_FIELDS;
   writefln(toString(x));
       
   x = ORJ_FLAG.ELIDE_BLANK_RECORDS;
   writefln(toString(x));
}
<endcode>
-- 
Derek Parnell
Melbourne, Australia
7/03/2005 10:12:17 PM
March 07, 2005
Re: How to initialise a struct??
Derek Parnell wrote:

> Is this what you are trying to do???
[linear search snipped]

No, probably this: ;-)

private char[][ORJ_FLAG] orj_strings;

static this()
{
  orj_strings[ORJ_FLAG.ORDER_FIELDS] =
  "Arranges the fields in alphabetical order";
  orj_strings[ORJ_FLAG.ELIDE_BLANK_RECORDS] =
  "Causes blank records to be ignored";
}

public char[] toString(ORJ_FLAG f)
{
    return (f in orj_strings) ? orj_strings[f] : "";
}

void main()
{
    writefln("%s", toString(ORJ_FLAG.ORDER_FIELDS));
    writefln("%s", toString(ORJ_FLAG.ELIDE_BLANK_RECORDS));
}

--anders
March 07, 2005
Re: How to initialise a struct??
Anders F Björklund wrote:
<snip>
> You need to use [] for arrays in D. You also need to name the enums:
> 
>>     const EnumString    strings[2] =
>>     [
>>             {   ORJ_FLAG.ORDER_FIELDS,           "Arranges the fields 
>> in alphabetical order" }
>>         ,   {   ORJ_FLAG.ELIDE_BLANK_RECORDS,    "Causes blank records 
>> to be ignored"        }
>>     ];

Added to

http://www.wikiservice.at/wiki4d/wiki.cgi?ErrorMessages

Moreover, why have the struct at all?  Why not save a bit of code with this:

    const char[][OBJ_FLAG.max + 1] strings = [
        ORJ_FLAG.ORDER_FIELDS:
            "Arranges the fields in alphabetical order",
        ORJ_FLAG.ELIDE_BLANK_RECORDS:
            "Causes blank records to be ignored"
    ];

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.
March 07, 2005
Re: How to initialise a struct??
On Mon, 07 Mar 2005 12:20:28 +0100, Anders F Björklund wrote:

> Derek Parnell wrote:
> 
>> Is this what you are trying to do???
> [linear search snipped]
> 
> No, probably this: ;-)
> 
> private char[][ORJ_FLAG] orj_strings;
> 
> static this()
> {
>    orj_strings[ORJ_FLAG.ORDER_FIELDS] =
>    "Arranges the fields in alphabetical order";
>    orj_strings[ORJ_FLAG.ELIDE_BLANK_RECORDS] =
>    "Causes blank records to be ignored";
> }
> 
> public char[] toString(ORJ_FLAG f)
> {
>      return (f in orj_strings) ? orj_strings[f] : "";
> }
> 
> void main()
> {
>      writefln("%s", toString(ORJ_FLAG.ORDER_FIELDS));
>      writefln("%s", toString(ORJ_FLAG.ELIDE_BLANK_RECORDS));
> }
> 
> --anders

Well maybe, but it looks like this enum is designed to be combined with
other enums to form a set of flags. For example, how could you do this ...

writefln("%s", toString( 
		ORJ_FLAG.ELIDE_BLANK_RECORDS | ORJ_FLAG.ORDER_FIELDS);


So maybe this is more like it...

import std.stdio;
/** 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                         */
}

struct EnumString
{
   ORJ_FLAG     value;
   char[]   str;
};

/*
/////////////////////////////////////////////////////////////////////////////
* Enumerations
*/

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"       }
   ];
   char[] result;
   uint mask;
   
   foreach (EnumString x; strings)
   {
       mask = 1;
       while(mask)
       {
           if (x.value == (mask & f))
           {
               if (result.length !=0)
                   result ~= ',';
               result ~= x.str;
           }
           mask  <<= 1;
       }
   }
   return result;
}

void main()
{
   ORJ_FLAG x;
   
   x = ORJ_FLAG.ORDER_FIELDS;    
   writefln(toString(x));
       
   x = ORJ_FLAG.ELIDE_BLANK_RECORDS;
   writefln(toString(x));
   
   x = ORJ_FLAG.ELIDE_BLANK_RECORDS | ORJ_FLAG.ORDER_FIELDS;
   writefln(toString(x));
}
-- 
Derek Parnell
Melbourne, Australia
7/03/2005 10:37:12 PM
Top | Discussion index | About this forum | D home