Re: Meta-programming detecting anonymous unions inside structs.

2016-10-21 Thread TheFlyingFiddle via Digitalmars-d-learn
On Friday, 21 October 2016 at 08:18:58 UTC, rikki cattermole 
wrote:

On 21/10/2016 9:13 PM, TheFlyingFiddle wrote:
On Friday, 21 October 2016 at 07:56:27 UTC, rikki cattermole 
wrote:

You're gonna have to use UDA's for that.


Yes, to do the serialization you're right.

But my usecase for this is for error reporting. Basically any 
struct
that contains unions without serialization instructions cannot 
be

serialized and I want to make such structures errors.

So when I try to serialize the example struct Foo. It should 
assert with

something along the lines of:

"Don't know how to serialize overlapping fields: "Foo.integer",
"Foo.floating" and "Foo.array".


I suppose you could use .offsetof to determine this.


This is what I was looking for. Thanks!


Re: Meta-programming detecting anonymous unions inside structs.

2016-10-21 Thread rikki cattermole via Digitalmars-d-learn

On 21/10/2016 9:13 PM, TheFlyingFiddle wrote:

On Friday, 21 October 2016 at 07:56:27 UTC, rikki cattermole wrote:

You're gonna have to use UDA's for that.


Yes, to do the serialization you're right.

But my usecase for this is for error reporting. Basically any struct
that contains unions without serialization instructions cannot be
serialized and I want to make such structures errors.

So when I try to serialize the example struct Foo. It should assert with
something along the lines of:

"Don't know how to serialize overlapping fields: "Foo.integer",
"Foo.floating" and "Foo.array".


I suppose you could use .offsetof to determine this.



Meta-programming detecting anonymous unions inside structs.

2016-10-21 Thread TheFlyingFiddle via Digitalmars-d-learn
I am trying to port a serialization library I wrote in Lua some 
time ago. I've ran into a problem relating to types with 
anonymous unions inside.


Given this code:
enum Kind
{
   none = 0,
   array,
   integer,
   floating,
}

struct Foo
{
   Kind type;
   union
   {
   ulong  integer;
   double floating;
   void[] array;
   }
   int nonUnionField;
   //...
}

How can I tell that "integer", "floating" and "array" are part 
the union while "nonUnionField" is not?


Thanks in advance.


Re: Meta-programming detecting anonymous unions inside structs.

2016-10-21 Thread rikki cattermole via Digitalmars-d-learn

On 21/10/2016 8:55 PM, TheFlyingFiddle wrote:

I am trying to port a serialization library I wrote in Lua some time
ago. I've ran into a problem relating to types with anonymous unions
inside.

Given this code:
enum Kind
{
   none = 0,
   array,
   integer,
   floating,
}

struct Foo
{
   Kind type;
   union
   {
   ulong  integer;
   double floating;
   void[] array;
   }
   int nonUnionField;
   //...
}

How can I tell that "integer", "floating" and "array" are part the union
while "nonUnionField" is not?

Thanks in advance.


You're gonna have to use UDA's for that.


Re: Unions and Structs

2016-01-19 Thread Russel Winder via Digitalmars-d-learn
On Mon, 2016-01-18 at 21:50 +0100, Jacob Carlborg via Digitalmars-d-
learn wrote:
> On 2016-01-18 19:11, Russel Winder via Digitalmars-d-learn wrote:
> 
> > It seems DStep is producing somewhat strange D from complicated C
> > unions.
> 
> Please report any issues to [1] with a test case. C code, expected D 
> code, actual D code.
> 
> [1] https://github.com/jacob-carlborg/dstep/issues

I made a start, but they aren't full reports as yet.

https://github.com/jacob-carlborg/dstep/issues/46
https://github.com/jacob-carlborg/dstep/issues/47

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Unions and Structs

2016-01-18 Thread Russel Winder via Digitalmars-d-learn
So this is an error?

union flob {
ulong data;
struct thingy {
uint data;
uint bits;
}
thingy burble;
};

because you cannot have a union field with a name that is also the name
of a struct field defined within the union.
  
-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Unions and Structs

2016-01-18 Thread anonymous via Digitalmars-d-learn

On 18.01.2016 18:10, Russel Winder via Digitalmars-d-learn wrote:

So this is an error?

union flob {
ulong data;
struct thingy {
uint data;
uint bits;
}
thingy burble;
};

because you cannot have a union field with a name that is also the name
of a struct field defined within the union.


I don't see the problem. You have to access the thingy's 'data' through 
the 'burble' member, so there is no ambiguity, is there?


This would be different, and dmd rejects it accordingly:

union flob {
ulong data;
struct {
		uint data; /* test.d(4): Error: variable test.flob.data conflicts with 
variable test.flob.data at test.d(2) */

uint bits;
}
}




Re: Unions and Structs

2016-01-18 Thread Russel Winder via Digitalmars-d-learn
On Mon, 2016-01-18 at 18:17 +0100, anonymous via Digitalmars-d-learn
wrote:
> On 18.01.2016 18:10, Russel Winder via Digitalmars-d-learn wrote:
> > So this is an error?
> > 
> > union flob {
> > ulong data;
> > struct thingy {
> > uint data;
> > uint bits;
> > }
> > thingy burble;
> > };
> > 
> > because you cannot have a union field with a name that is also the
> > name
> > of a struct field defined within the union.
> 
> I don't see the problem. You have to access the thingy's 'data'
> through 
> the 'burble' member, so there is no ambiguity, is there?

That is what I thought. There is a bit of this stuff in the real more
complicated case. But it transpires I was looking at the wrong bits…

> This would be different, and dmd rejects it accordingly:
> 
> union flob {
>   ulong data;
>   struct {
>   uint data; /* test.d(4): Error: variable test.flob.data
> conflicts with 
> variable test.flob.data at test.d(2) */
>   uint bits;
>   }
> }

Ah, this is actually what is there and what the problem is.

It seems DStep is producing somewhat strange D from complicated C
unions. The problem is real as you point out in the generated code, but
the generated code is nothing like what I would have generated by hand,
which wouldn't have the problem.

I shall go away and procrastinate for some, to be decided, time about
whether to continue with D or revert back to C++.

-- 
Russel.
=
Dr Russel Winder  t: +44 20 7585 2200   voip: sip:russel.win...@ekiga.net
41 Buckmaster Roadm: +44 7770 465 077   xmpp: rus...@winder.org.uk
London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder



signature.asc
Description: This is a digitally signed message part


Re: Unions and Structs

2016-01-18 Thread Jacob Carlborg via Digitalmars-d-learn

On 2016-01-18 19:11, Russel Winder via Digitalmars-d-learn wrote:


It seems DStep is producing somewhat strange D from complicated C
unions.


Please report any issues to [1] with a test case. C code, expected D 
code, actual D code.


[1] https://github.com/jacob-carlborg/dstep/issues

--
/Jacob Carlborg