Jump to page: 1 2
Thread overview
Endian ENUM
Sep 20, 2003
Matthew Wilson
Sep 20, 2003
Matthew Wilson
Sep 20, 2003
Walter
Sep 20, 2003
Matthew Wilson
Re: Endian ENUM (101 ways to not get what you want)
Sep 20, 2003
Mike Wynn
Sep 20, 2003
Matthew Wilson
Sep 20, 2003
Sean L. Palmer
Sep 20, 2003
Matthew Wilson
Sep 21, 2003
Walter
Sep 20, 2003
Matthew Wilson
Sep 21, 2003
Walter
Sep 20, 2003
Nicolas Repiquet
Sep 20, 2003
Nicolas Repiquet
September 20, 2003
I need/want to define an enum to represent endianness (in order that people can request an endianness to their int in the registry API).

I've currently defined it in the synsoft.types module as

    public enum Endian
    {
            Unknown      =   0
        ,   Little             =   1
        ,   Big                =   2
        ,   Middle          =   3
        ,   ByteSex        =   4
      version(LittleEndian)
      {
        ,   Ambient        =   Little
      }
      version(BigEndian)
      {
        ,   Ambient        =   Big
      }
    }

Anyone have any violent objections? Have I missed something obvious? (I know I'm being a little optimistic in imagining D being ported to middle-endian machines, and I don't know whether we'd ever want to go byte-sexual, but what the hell!)

If this is something agreeable, it'd be cool if this could go into Phobos asap, as I don't want any synsoft dependencies in the registry library when it goes into Phobos, and I'm sure you don't either. ;)

Comments, please



September 20, 2003
Aaargh!

That won't even compile, since it seems that we can't have a version clause inside an enum.

Oh unhappy days. :(




"Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjrj$2m5f$1@digitaldaemon.com...
> I need/want to define an enum to represent endianness (in order that
people
> can request an endianness to their int in the registry API).
>
> I've currently defined it in the synsoft.types module as
>
>     public enum Endian
>     {
>             Unknown      =   0
>         ,   Little             =   1
>         ,   Big                =   2
>         ,   Middle          =   3
>         ,   ByteSex        =   4
>       version(LittleEndian)
>       {
>         ,   Ambient        =   Little
>       }
>       version(BigEndian)
>       {
>         ,   Ambient        =   Big
>       }
>     }
>
> Anyone have any violent objections? Have I missed something obvious? (I
know
> I'm being a little optimistic in imagining D being ported to middle-endian machines, and I don't know whether we'd ever want to go byte-sexual, but what the hell!)
>
> If this is something agreeable, it'd be cool if this could go into Phobos asap, as I don't want any synsoft dependencies in the registry library
when
> it goes into Phobos, and I'm sure you don't either. ;)
>
> Comments, please
>
>
>


September 20, 2003
Write it like this:

enum Endian { .... }

version (LittleEndian)
{
    const Endian Ambient = Endian.Little;
}
version (BigEndian)
{
    const Endian Ambient = Endian.Big;
}

"Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjv6$2mks$1@digitaldaemon.com...
> Aaargh!
>
> That won't even compile, since it seems that we can't have a version
clause
> inside an enum.
>
> Oh unhappy days. :(
>
>
>
>
> "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjrj$2m5f$1@digitaldaemon.com...
> > I need/want to define an enum to represent endianness (in order that
> people
> > can request an endianness to their int in the registry API).
> >
> > I've currently defined it in the synsoft.types module as
> >
> >     public enum Endian
> >     {
> >             Unknown      =   0
> >         ,   Little             =   1
> >         ,   Big                =   2
> >         ,   Middle          =   3
> >         ,   ByteSex        =   4
> >       version(LittleEndian)
> >       {
> >         ,   Ambient        =   Little
> >       }
> >       version(BigEndian)
> >       {
> >         ,   Ambient        =   Big
> >       }
> >     }
> >
> > Anyone have any violent objections? Have I missed something obvious? (I
> know
> > I'm being a little optimistic in imagining D being ported to
middle-endian
> > machines, and I don't know whether we'd ever want to go byte-sexual, but what the hell!)
> >
> > If this is something agreeable, it'd be cool if this could go into
Phobos
> > asap, as I don't want any synsoft dependencies in the registry library
> when
> > it goes into Phobos, and I'm sure you don't either. ;)
> >
> > Comments, please
> >
> >
> >
>
>


September 20, 2003
But then Ambient is not part of Endian!

"Walter" <walter@digitalmars.com> wrote in message news:bkgm7g$2ufv$1@digitaldaemon.com...
> Write it like this:
>
> enum Endian { .... }
>
> version (LittleEndian)
> {
>     const Endian Ambient = Endian.Little;
> }
> version (BigEndian)
> {
>     const Endian Ambient = Endian.Big;
> }
>
> "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjv6$2mks$1@digitaldaemon.com...
> > Aaargh!
> >
> > That won't even compile, since it seems that we can't have a version
> clause
> > inside an enum.
> >
> > Oh unhappy days. :(
> >
> >
> >
> >
> > "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjrj$2m5f$1@digitaldaemon.com...
> > > I need/want to define an enum to represent endianness (in order that
> > people
> > > can request an endianness to their int in the registry API).
> > >
> > > I've currently defined it in the synsoft.types module as
> > >
> > >     public enum Endian
> > >     {
> > >             Unknown      =   0
> > >         ,   Little             =   1
> > >         ,   Big                =   2
> > >         ,   Middle          =   3
> > >         ,   ByteSex        =   4
> > >       version(LittleEndian)
> > >       {
> > >         ,   Ambient        =   Little
> > >       }
> > >       version(BigEndian)
> > >       {
> > >         ,   Ambient        =   Big
> > >       }
> > >     }
> > >
> > > Anyone have any violent objections? Have I missed something obvious?
(I
> > know
> > > I'm being a little optimistic in imagining D being ported to
> middle-endian
> > > machines, and I don't know whether we'd ever want to go byte-sexual,
but
> > > what the hell!)
> > >
> > > If this is something agreeable, it'd be cool if this could go into
> Phobos
> > > asap, as I don't want any synsoft dependencies in the registry library
> > when
> > > it goes into Phobos, and I'm sure you don't either. ;)
> > >
> > > Comments, please
> > >
> > >
> > >
> >
> >
>
>


September 20, 2003
You knew it was going to be an uphill battle with making version parseable in many conceivable parts of the grammar.  ;)

Sean

"Walter" <walter@digitalmars.com> wrote in message news:bkgm7g$2ufv$1@digitaldaemon.com...
> Write it like this:
>
> enum Endian { .... }
>
> version (LittleEndian)
> {
>     const Endian Ambient = Endian.Little;
> }
> version (BigEndian)
> {
>     const Endian Ambient = Endian.Big;
> }
>
> "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjv6$2mks$1@digitaldaemon.com...
> > Aaargh!
> >
> > That won't even compile, since it seems that we can't have a version
> clause
> > inside an enum.
> >
> > Oh unhappy days. :(
> >
> >
> >
> >
> > "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjrj$2m5f$1@digitaldaemon.com...
> > > I need/want to define an enum to represent endianness (in order that
> > people
> > > can request an endianness to their int in the registry API).
> > >
> > > I've currently defined it in the synsoft.types module as
> > >
> > >     public enum Endian
> > >     {
> > >             Unknown      =   0
> > >         ,   Little             =   1
> > >         ,   Big                =   2
> > >         ,   Middle          =   3
> > >         ,   ByteSex        =   4
> > >       version(LittleEndian)
> > >       {
> > >         ,   Ambient        =   Little
> > >       }
> > >       version(BigEndian)
> > >       {
> > >         ,   Ambient        =   Big
> > >       }
> > >     }
> > >
> > > Anyone have any violent objections? Have I missed something obvious?
(I
> > know
> > > I'm being a little optimistic in imagining D being ported to
> middle-endian
> > > machines, and I don't know whether we'd ever want to go byte-sexual,
but
> > > what the hell!)
> > >
> > > If this is something agreeable, it'd be cool if this could go into
> Phobos
> > > asap, as I don't want any synsoft dependencies in the registry library
> > when
> > > it goes into Phobos, and I'm sure you don't either. ;)
> > >
> > > Comments, please
> > >
> > >
> > >
> >
> >
>
>


September 20, 2003
Yes, but this seems a pretty simple, and quite important, one.

    Over to you, big W.
    Relieve my burden, give me version
    In my enum, or I'll kick your bum!

(I used to write limericks in a previous life. Not very good ones, mind ...)


"Sean L. Palmer" <palmer.sean@verizon.net> wrote in message news:bkgmub$30bt$1@digitaldaemon.com...
> You knew it was going to be an uphill battle with making version parseable in many conceivable parts of the grammar.  ;)
>
> Sean
>
> "Walter" <walter@digitalmars.com> wrote in message news:bkgm7g$2ufv$1@digitaldaemon.com...
> > Write it like this:
> >
> > enum Endian { .... }
> >
> > version (LittleEndian)
> > {
> >     const Endian Ambient = Endian.Little;
> > }
> > version (BigEndian)
> > {
> >     const Endian Ambient = Endian.Big;
> > }
> >
> > "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjv6$2mks$1@digitaldaemon.com...
> > > Aaargh!
> > >
> > > That won't even compile, since it seems that we can't have a version
> > clause
> > > inside an enum.
> > >
> > > Oh unhappy days. :(
> > >
> > >
> > >
> > >
> > > "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjrj$2m5f$1@digitaldaemon.com...
> > > > I need/want to define an enum to represent endianness (in order that
> > > people
> > > > can request an endianness to their int in the registry API).
> > > >
> > > > I've currently defined it in the synsoft.types module as
> > > >
> > > >     public enum Endian
> > > >     {
> > > >             Unknown      =   0
> > > >         ,   Little             =   1
> > > >         ,   Big                =   2
> > > >         ,   Middle          =   3
> > > >         ,   ByteSex        =   4
> > > >       version(LittleEndian)
> > > >       {
> > > >         ,   Ambient        =   Little
> > > >       }
> > > >       version(BigEndian)
> > > >       {
> > > >         ,   Ambient        =   Big
> > > >       }
> > > >     }
> > > >
> > > > Anyone have any violent objections? Have I missed something obvious?
> (I
> > > know
> > > > I'm being a little optimistic in imagining D being ported to
> > middle-endian
> > > > machines, and I don't know whether we'd ever want to go byte-sexual,
> but
> > > > what the hell!)
> > > >
> > > > If this is something agreeable, it'd be cool if this could go into
> > Phobos
> > > > asap, as I don't want any synsoft dependencies in the registry
library
> > > when
> > > > it goes into Phobos, and I'm sure you don't either. ;)
> > > >
> > > > Comments, please
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>


September 20, 2003
For the moment I'd satisfying myself with


version(LittleEndian)
{
 private const int Endian_Ambient =   1;
}
version(BigEndian)
{
 private const int Endian_Ambient =   2;
}


public enum Endian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
    ,   Ambient =   Endian_Ambient
/+
  version(LittleEndian)
  {
    ,   Ambient =   Little
  }
  version(BigEndian)
  {
    ,   Ambient =   Big
  }
+/
}

It's not pretty, but it works.

Anyway, to the second part of the question. Is this something we'd like to see in Phobos, perhaps in "d.arch.types"?

"Walter" <walter@digitalmars.com> wrote in message news:bkgm7g$2ufv$1@digitaldaemon.com...
> Write it like this:
>
> enum Endian { .... }
>
> version (LittleEndian)
> {
>     const Endian Ambient = Endian.Little;
> }
> version (BigEndian)
> {
>     const Endian Ambient = Endian.Big;
> }
>
> "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjv6$2mks$1@digitaldaemon.com...
> > Aaargh!
> >
> > That won't even compile, since it seems that we can't have a version
> clause
> > inside an enum.
> >
> > Oh unhappy days. :(
> >
> >
> >
> >
> > "Matthew Wilson" <matthew@stlsoft.org> wrote in message news:bkgjrj$2m5f$1@digitaldaemon.com...
> > > I need/want to define an enum to represent endianness (in order that
> > people
> > > can request an endianness to their int in the registry API).
> > >
> > > I've currently defined it in the synsoft.types module as
> > >
> > >     public enum Endian
> > >     {
> > >             Unknown      =   0
> > >         ,   Little             =   1
> > >         ,   Big                =   2
> > >         ,   Middle          =   3
> > >         ,   ByteSex        =   4
> > >       version(LittleEndian)
> > >       {
> > >         ,   Ambient        =   Little
> > >       }
> > >       version(BigEndian)
> > >       {
> > >         ,   Ambient        =   Big
> > >       }
> > >     }
> > >
> > > Anyone have any violent objections? Have I missed something obvious?
(I
> > know
> > > I'm being a little optimistic in imagining D being ported to
> middle-endian
> > > machines, and I don't know whether we'd ever want to go byte-sexual,
but
> > > what the hell!)
> > >
> > > If this is something agreeable, it'd be cool if this could go into
> Phobos
> > > asap, as I don't want any synsoft dependencies in the registry library
> > when
> > > it goes into Phobos, and I'm sure you don't either. ;)
> > >
> > > Comments, please
> > >
> > >
> > >
> >
> >
>
>


September 20, 2003
you can do as you require ... see final verson if you not interested in the vast array of possible error msgs from reconfiguring that same code.

Matthew Wilson wrote:
> But then Ambient is not part of Endian!
> 
> "Walter" <walter@digitalmars.com> wrote in message
> news:bkgm7g$2ufv$1@digitaldaemon.com...
> 
>>Write it like this:
>>
>>enum Endian { .... }
>>
>>version (LittleEndian)
>>{
>>    const Endian Ambient = Endian.Little;
>>}
>>version (BigEndian)
>>{
>>    const Endian Ambient = Endian.Big;
>>}
>>
>>"Matthew Wilson" <matthew@stlsoft.org> wrote in message
>>news:bkgjv6$2mks$1@digitaldaemon.com...
>>
>>>Aaargh!
>>>
>>>That won't even compile, since it seems that we can't have a version
>>
>>clause
>>
>>>inside an enum.
>>>
>>>Oh unhappy days. :(
>>>
>>>
>>>
>>>
>>>"Matthew Wilson" <matthew@stlsoft.org> wrote in message
>>>news:bkgjrj$2m5f$1@digitaldaemon.com...
>>>
>>>>I need/want to define an enum to represent endianness (in order that
>>>
>>>people
>>>
>>>>can request an endianness to their int in the registry API).
>>>>
>>>>I've currently defined it in the synsoft.types module as
>>>>
>>>>    public enum Endian
>>>>    {
>>>>            Unknown      =   0
>>>>        ,   Little             =   1
>>>>        ,   Big                =   2
>>>>        ,   Middle          =   3
>>>>        ,   ByteSex        =   4
>>>>      version(LittleEndian)
>>>>      {
>>>>        ,   Ambient        =   Little
>>>>      }
>>>>      version(BigEndian)
>>>>      {
>>>>        ,   Ambient        =   Big
>>>>      }
>>>>    }
>>>>
> 

I tried ....
version( LittleEndian ) {
 private const int Endian_Ambient =   Endian.Little;
} else {
	version( BigEndian ) {
	 private const int Endian_Ambient =   Endian.Big;
	} else {
		//#error unknown endian
		static assert(0);
	}
}

public enum Endian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
    ,   Ambient =   Endian_Ambient
}
// gives error
enumtest.d(2): Endian.Little is not an expression

then realising my error changed the int to endian (same error)

so tried an alias ....
version( LittleEndian ) {
// private const Endian Endian_Ambient =   Endian.Little;
	private alias Endian.Little Endian_Ambient;
} else {
	version( BigEndian ) {
//	 private const Endian Endian_Ambient =   Endian.Big;
		private alias Endian.Big Endian_Ambient;
	} else {
		//#error unknown endian
		static assert(0);
	}
}

public enum Endian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
    ,   Ambient =   Endian_Ambient
}

big error ....
dmd: mtype.c:2685: Expression *TypeEnum::getProperty (Loc, Identifier *): Assertion `sym->memtype' failed.
Aborted
(linux(RH9) dmd 0.73)

so moved the Endian_Ambient from before to after the enum....
public enum Endian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
    ,   Ambient =   Endian_Ambient
}

version( LittleEndian ) {
// private const Endian Endian_Ambient =   Endian.Little;
	private alias Endian.Little Endian_Ambient;
} else {
	version( BigEndian ) {
//	 private const Endian Endian_Ambient =   Endian.Big;
		private alias Endian.Big Endian_Ambient;
	} else {
		//#error unknown endian
		static assert(0);
	}
}

enumtest.d(9): cannot implicitly convert Endian.Little to int
(from the line `, Ambient = Endian_Ambient` )

so changed back to using the const Endian as oposed to alias....
(same error).

so I tried a cast

public enum Endian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
    ,   Ambient =   cast(Endian)Endian_Ambient
}

version( LittleEndian ) {
// private const Endian Endian_Ambient =   Endian.Little;
	private alias Endian.Little Endian_Ambient;
} else {
	version( BigEndian ) {
//	 private const Endian Endian_Ambient =   Endian.Big;
	
		private alias Endian.Big Endian_Ambient;
	} else {
		//#error unknown endian
		static assert(0);
	}
}

but that's no good ...
enumtest.d(9): Integer constant expression expected instead of cast(int)(Endian.Little)
tried the const Endian ...
got the error
enumtest.d(9): Integer constant expression expected instead of cast(int)(Endian_Ambient)

so thought ... lets use 2 enums (one public one private)
private enum BaseEndian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
}

version( LittleEndian ) {
 private const BaseEndian Endian_Ambient =   BaseEndian.Little;
//	private alias BaseEndian.Little Endian_Ambient;
} else {
	version( BigEndian ) {
	 private const BaseEndian Endian_Ambient =   BaseEndian.Big;
	
//		private alias BaseEndian.Big Endian_Ambient;
	} else {
		//#error unknown endian
		static assert(0);
	}
}
public enum Endian
{
        Unknown =   BaseEndian.Unknown
    ,   Little  =   BaseEndian.Little
    ,   Big     =   BaseEndian.Big
    ,   Middle  =   BaseEndian.Middle
    ,   ByteSex =   BaseEndian.ByteSex
    ,   Ambient =   Endian_Ambient
}

that give the error

enumtest.d(12): BaseEndian.Little is not an expression

so resorted to the alias again .... and finally it works!
Endian.Ambient is part of the enum.

private enum BaseEndian
{
        Unknown =   0
    ,   Little  =   1
    ,   Big     =   2
    ,   Middle  =   3
    ,   ByteSex =   4
}

version( LittleEndian ) {
// private const BaseEndian Endian_Ambient =   BaseEndian.Little;
	private alias BaseEndian.Little Endian_Ambient;
} else {
	version( BigEndian ) {
//	 private const BaseEndian Endian_Ambient =   BaseEndian.Big;
	private alias BaseEndian.Big Endian_Ambient;
	} else {
		//#error unknown endian
		static assert(0);
	}
}
public enum Endian
{
        Unknown =   BaseEndian.Unknown
    ,   Little  =   BaseEndian.Little
    ,   Big     =   BaseEndian.Big
    ,   Middle  =   BaseEndian.Middle
    ,   ByteSex =   BaseEndian.ByteSex
    ,   Ambient =   Endian_Ambient
}




September 20, 2003
"Walter" <walter@digitalmars.com> a écrit dans le message news: bkgm7g$2ufv$1@digitaldaemon.com...
> Write it like this:
>
> enum Endian { .... }
>
> version (LittleEndian)
> {
>     const Endian Ambient = Endian.Little;
> }
> version (BigEndian)
> {
>     const Endian Ambient = Endian.Big;
> }

Cant you add to d's expressions set something like "VersionExpression : version ( Identifier | Integer )" that return a constant boolean ? So the following code will works :

   const Endian Ambient = version(LittleEndian) ? Endian.Little : Endian.Big
;


Maybe "DebugExpression : debug ( Identifier | Integer )" may be added too ?


-- Nicolas Repiquet


September 20, 2003
"Nicolas Repiquet" <deadcow-remove-this@free.fr> a écrit dans le message news: bki884$2ejv$1@digitaldaemon.com...

>    const Endian Ambient = version(LittleEndian) ? Endian.Little :
Endian.Big
> ;

i mean

enum Endian {
    Little,
    Big,
    Ambient = version(LittleEndian) ? Little : Big
}

-- Nicolas Repiquet



« First   ‹ Prev
1 2