2022-05-21 09:44 AM
Hi. Is there a way to save std::map or std::pair (or any other pairing or nesting datatype) in the local preferences file by an addon?
Thanks.
2022-05-30 03:46 PM
Hello,
Yes, it is possible, but you have to write the serialization yourself.
There is another way, though: use GS containers:
std::pair → GS::Pair
std::map → GS::HashTable
Most GS containers have methods to read from and write to Channels, which can be saved in preferences. Hint: use GS::MemoryOChannel32 and GS::MemoryIChannel32.
Best, Akos
2022-06-06 10:02 PM - edited 2022-06-06 10:23 PM
@Akos Somorjai, when I tried something like:
'GS::Read': none of the 14 overloads could convert all the argument types.
So do we have to manually store each member of GS::HashTable too, by converting it to an acceptable type, like for std::map?
The reason to store a map directly is that its size (number of members) is dynamic and unknown, so while reading from preferences into an empty map, I don't know how much to iterate..
2022-06-07 01:05 PM
Hi,
Yes, the std::map doesn't know how to serialize itself to a GS::Channel, so you'll have to "teach" it by writing the input/output code yourself. GS::Channel just has support for basic types.
You can also write the number of items to the channel upfront, so you'll know how many items you are dealing with when you read the info back.
Also, GS::HashTable is equivalent to std::map; is there a good reason why you store a map inside another map?
Best, Akos
2022-06-07 02:51 PM - edited 2022-06-07 02:53 PM
Hi Akos,
You can also write the number of items to the channel upfront, so you'll know how many items you are dealing with when you read the info back.
I was thinking about that, but wanted to check if there was any better way to store a map without breaking it down to so many levels.
The reason I have a map within a map is because the data set is in the form of entities having pairs. Like: { Slab { {Flooring, Area}, {Ceiling, Area}, {Panel, Number} }, Wall { {Partition, Area}, {Exterior, Volume} } }
Yes, I figured a GS::HashTable is like std::map, but since you mentioned it, I just wanted to check if it was accepted as a valid type that could be saved directly.