wc-duck / datalibrary

Open Source Data Library for data serialization.
Other
42 stars 8 forks source link

Inline-arrays of types need defaults for all members of each instance #105

Closed lundmark closed 5 years ago

lundmark commented 5 years ago

For example if you have a type a, with members b, c, d who all have defaults. Then you want to have an inline-array k of n amount of a and want that to be default-initialized and you're going to run into issues. You'll need to define default for k as n amounts of a with all of its members set to default values.

Can't get more abstract than that in the example. So here we go:

        "my_type" : {
            "members" : [
                { "name" : "a", "type" : "uint64", "default" : 0 },
                { "name" : "b", "type" : "fp32", "default" : 0 },
                { "name" : "c", "type" : "fp32", "default" : 0 },
            ]
        },
        "my_other_type" : {
            "members" : [
                { "name" : "stuffs", "type" : "my_type[3]", "default" : [
                        {
                            "a" : 0,
                            "b" : 0,
                            "c" : 0
                        },
                        {
                            "a" : 0,
                            "b" : 0,
                            "c" : 0
                        },
                        {
                            "a" : 0,
                            "b" : 0,
                            "c" : 0
                        }
                    ]
                }

It would be a LOT nicer if this was possible:

        "my_type" : {
            "members" : [
                { "name" : "a", "type" : "uint64", "default" : 0 },
                { "name" : "b", "type" : "fp32", "default" : 0 },
                { "name" : "c", "type" : "fp32", "default" : 0 },
            ]
        },
        "my_other_type" : {
            "members" : [
                { "name" : "stuffs", "type" : "my_type[3]", "default" : [ ] }
lundmark commented 5 years ago

Wow the formatting of that went to shit :( Sorry about that.

lundmark commented 5 years ago

Here's a test-case that you can drop into unittest.tld:

        "struct_with_defaults" : {
            "members" : [
                {   "name" : "some_variable_1",     "type" : "uint32"           "default" : 777 },
                {   "name" : "some_variable_2",     "type" : "fp32",            "default" : 0.5 },
                {   "name" : "some_variable_3",     "type" : "fp32",            "default" : 0 },
                {   "name" : "some_variable_4",     "type" : "fp32",            "default" : 20 },
                {   "name" : "some_variable_5",     "type" : "fp32",            "default" : 0.1 }
            ]
        },
        "struct_with_inline_array_of_defaults" : {
            "members" : [
                { "name" : "dummy1", "type" : "struct_with_defaults[8]" },
                { "name" : "dummy2", "type" : "struct_with_defaults[4]", "default" : [] },
                { "name" : "dummy3", "type" : "struct_with_defaults[6]", "default" : [ { "some_variable_4" : 0 }, { "some_variable_4" : 1 }, { "some_variable_4" : 2 } ] }
            ]
        }

preferably all three of those should work.

wc-duck commented 5 years ago

As a workaround until I get this going you could try to just initialize it as

{ "name" : "dummy2", "type" : "struct_with_defaults[4]", "default" : [{},{},{},{}] }

I think, without trying it, that that should work already.

this one I still think should be an error as you havn't specified that it has a default-value, i.e. it need a value. Also to get this to work we would need to store default-values for all inline-arrays in the tld and that seems wasteful.

{ "name" : "dummy1", "type" : "struct_with_defaults[8]" }

lundmark commented 5 years ago

Yes that probably works (trying it now).

There are situations where you have for example, an inline-char-array of a certain size (say 256) and you want it to be default zeroed... how would we do that reasonably? It's not very nice to have a default with 256 0's in it.

wc-duck commented 5 years ago

I'll fix default of pods to memset(0).

wc-duck commented 5 years ago

I'll fix default of pods to memset(0). Observe that the fix that is coming will also default all non-set values in an inline-array when just setting them, not only in a "default". Is that a good thing, I'm not really sure yet :)

lundmark commented 5 years ago

I think that's a really good thing. IF that would ever be a problem, I think that a flag on that member can be used to turn that feature off.

wc-duck commented 5 years ago

First revision submitted... report bugs with the feature as new bugs please, I'll close this :)