Archicad C++ API
About Archicad add-on development using the C++ API.

Saving std::map / std::pair in local user preferences

dushyant
Enthusiast

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.

4 REPLIES 4
Akos Somorjai
Graphisoft
Graphisoft

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

@Akos Somorjai, when I tried something like:

    GS::HashTable<API_ElemTypeID, std::map<GS::UniString, int>> myHashTable;
    myHashTable.Read(ic);
I got the error:

'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..

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

 

 

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.