Skip to main content
GameDev.net gamedev.net
🔒 Locked

Heterogeneous map

Started by Chris_F Oct 26, 2011 at 9:34 AM 1 replies 850+ views
Original Post
Chris_F
Chris_F
I'm faced with a problem.

I'd like to have something along the lines of a heterogeneous map class, eg:


heterogeneous_map map;
map.set<std::string>("name", "Chris");
map.set<int>("age", 23);
std::string name = map.get<std::string>("name");
int age = map.get<int>("age");


This isn't particularly hard to achieve with a std::map, the problem arises because I need to be able to serialize and de-serialize these heterogeneous maps. I'm using Boost::Serialization, which works very well for most things I've tried so far, but it has no support for serializing a boost::any object. What can I do?
Chris_F
Chris_F
Actually, that may not be necessary.

There is Boost.PropertyTree that has basically that exact interface, and furthermore, it already works with the serializer!


boost::property_tree::ptree pt;
pt.put<std::string>("name", "Chris");
pt.put("age", 23);
std::ofstream ofs("out.xml");
boost::archive::xml_oarchive xml(ofs);
xml << BOOST_SERIALIZATION_NVP(pt);


Is this a good solution?

Edit: Eh, on second thought. Ptree doesn't seem to support anything but basic C types and std::string. Is there a way to expand this?
Zahlman
Zahlman
How are you planning to remember which type to use for what data? If there is a set protocol, maybe it's possible to just use a struct? :

This kind of thing is fighting the language to an extraordinary degree. You might also consider using a language that actually supports (built-in) what you're trying to do.

Also, this isn't really a game-specific question so I'm moving it to General Programming.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.