digitalmars.D.learn - Meta-programming detecting anonymous unions inside structs.
- TheFlyingFiddle (26/26) Oct 21 2016 I am trying to port a serialization library I wrote in Lua some
- rikki cattermole (2/28) Oct 21 2016 You're gonna have to use UDA's for that.
- TheFlyingFiddle (10/11) Oct 21 2016 Yes, to do the serialization you're right.
- rikki cattermole (2/12) Oct 21 2016 I suppose you could use .offsetof to determine this.
- TheFlyingFiddle (3/23) Oct 21 2016 This is what I was looking for. Thanks!
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.
Oct 21 2016
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.
Oct 21 2016
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".
Oct 21 2016
On 21/10/2016 9:13 PM, TheFlyingFiddle wrote:On Friday, 21 October 2016 at 07:56:27 UTC, rikki cattermole wrote:I suppose you could use .offsetof to determine this.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".
Oct 21 2016
On Friday, 21 October 2016 at 08:18:58 UTC, rikki cattermole wrote:On 21/10/2016 9:13 PM, TheFlyingFiddle wrote:This is what I was looking for. Thanks!On Friday, 21 October 2016 at 07:56:27 UTC, rikki cattermole wrote:I suppose you could use .offsetof to determine this.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".
Oct 21 2016