Jump to content
  • Advertisement
Sign in to follow this  
Chris_F

Heterogeneous map

This topic is 2604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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<std::string, boost::any>, 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?

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!