NativeBoost nested structures

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

NativeBoost nested structures

Matthieu
Hello everyone,

I have a nested struct, let's say "struct1" which contains "struct2" which contains an int field named "field".

When I try to update the field from struct 1 directly I can't. What I mean is if I do : "struct1 struct2 field: 4" for instance, the value in field is not updated. Even if I inspect "struct1 struct2" and I manually set the "field variable by doing : "self field: 4" then if I go back to my struct1 and do "self struct2 field" the value has not been updated.
So basically the only way I have to update a field in a nested structure is to recreate the second struct entirely : "struct1 struct2: (Struct2 new field: 4)" and now it works.

It seems odd because in C you can do "struct1.struct2.field = 4;" and it works fine.

You can reproduce this with the NBTestNestedStructure class from NativeBoost itself :

| myStruct |

myStruct := (NBTestNestedStructure new oneByte: (NBTestStructure1byte new field: 1)).
Transcript show: myStruct oneByte field; cr.
myStruct oneByte field: 4.
Transcript show: myStruct oneByte field; cr.
myStruct oneByte: (NBTestStructure1byte new field: 4).
Transcript show: myStruct oneByte field; cr.

For me the Transcript shows 1 1 4 whereas in my opinion it should show 1 4 4.

So am I doing something wrong or is this some kind of a bug ?

Thanks,

Matthieu
Reply | Threaded
Open this post in threaded view
|

Re: NativeBoost nested structures

Nicolai Hess


2015-06-15 17:14 GMT+02:00 Matthieu Lacaton <[hidden email]>:
Hello everyone,

I have a nested struct, let's say "struct1" which contains "struct2" which contains an int field named "field".

When I try to update the field from struct 1 directly I can't. What I mean is if I do : "struct1 struct2 field: 4" for instance, the value in field is not updated. Even if I inspect "struct1 struct2" and I manually set the "field variable by doing : "self field: 4" then if I go back to my struct1 and do "self struct2 field" the value has not been updated.
So basically the only way I have to update a field in a nested structure is to recreate the second struct entirely : "struct1 struct2: (Struct2 new field: 4)" and now it works.

It seems odd because in C you can do "struct1.struct2.field = 4;" and it works fine.

yes, self struct2 will create a copy of the struct2 and any change to struct2 value only applies to this copy.

 

You can reproduce this with the NBTestNestedStructure class from NativeBoost itself :

| myStruct |

myStruct := (NBTestNestedStructure new oneByte: (NBTestStructure1byte new field: 1)).
Transcript show: myStruct oneByte field; cr.
myStruct oneByte field: 4.
Transcript show: myStruct oneByte field; cr.
myStruct oneByte: (NBTestStructure1byte new field: 4).
Transcript show: myStruct oneByte field; cr.

For me the Transcript shows 1 1 4 whereas in my opinion it should show 1 4 4.

So am I doing something wrong or is this some kind of a bug ?

I am not sure, I think there is no other way to handle this kind of nested structures.

Are you bound to exactly this structure?
If you can define the structure, you can use a nested structure that uses pointers instead of values:

NBTestNestedStructure2>>
fieldsDesc

    ^ #(
        NBTestStructure1byte* oneByte;
        int otherField
        )
 
Now you can create and access the innerstructure like this

myStruct := (NBTestNestedStructure2 new oneByte: (NBTestStructure1byte externalNew field: 1)).
myStruct oneByte field. " -> 1 "
myStruct oneByte field: 4.
myStruct oneByte field. " -> 4 "

Whereas I don't know if the external memory is freed.





Thanks,

Matthieu

Reply | Threaded
Open this post in threaded view
|

Re: NativeBoost nested structures

Matthieu
Are you bound to exactly this structure?
If you can define the structure, you can use a nested structure that uses pointers instead of values:

NBTestNestedStructure2>>
fieldsDesc

    ^ #(
        NBTestStructure1byte* oneByte;
        int otherField
        )
 
Now you can create and access the innerstructure like this

myStruct := (NBTestNestedStructure2 new oneByte: (NBTestStructure1byte externalNew field: 1)).
myStruct oneByte field. " -> 1 "
myStruct oneByte field: 4.
myStruct oneByte field. " -> 4 "

Well I'm not bound to anything really, but what you did here seems nice and should do the trick for me.
Initially i just got rid of the second struct and incorporated each element of the second struct inside the first one. I guess if the size they take in memory is the same it should work too.
But I'll use your solution cause it looks nicer :)

Whereas I don't know if the external memory is freed.

From what is written in NBExternalStructure it probably isn't but I'll just have to remember to do so.


On the other hand, I am curious about the internal mechanisms here.

yes, self struct2 will create a copy of the struct2 and any change to struct2 value only applies to this copy.

If "self struct2" returns a copy it means that even if it is not a struct and simply an int it also returns me a copy right ?
However, when you call "self struct2: ..." or "self otherField: ..." it actually modifies the object itself.
So why doesn't it let me access the actual object directly ? If I can modify the whole object why can't I modify just a part of it ? What is the inherent issue ?