-
October 26th, 2011, 04:24 AM
#1
Heterogeneous container
I'm faced with a problem.
I'd like to have something along the lines of a heterogeneous map class, eg:
Code:
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?
-
October 26th, 2011, 04:38 AM
#2
Re: Heterogeneous container
AFAIR, variant's are serializable, but you will need to predefine the set of storable types. Do you really need a boost::any like level of heterogeneity ?
-
October 26th, 2011, 04:52 AM
#3
Re: Heterogeneous container
Originally Posted by superbonzo
AFAIR, variant's are serializable, but you will need to predefine the set of storable types. Do you really need a boost::any like level of heterogeneity ?
Actually, I've never used Boost::Variant before, and am not sure how it would be used to make a heterogeneous container.
EDIT: Ok, I just found the function boost::get which makes things a lot easier. This may work for my needs.
Last edited by Chris_F; October 26th, 2011 at 05:24 AM.
-
October 26th, 2011, 06:00 AM
#4
Re: Heterogeneous container
Actually, that may not be necessary.
There is Boost.PropertyTree that has basically that exact interface, and furthermore, it already works with the serializer!
Code:
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);
Last edited by Chris_F; October 26th, 2011 at 06:03 AM.
-
October 26th, 2011, 07:51 AM
#5
Re: Heterogeneous container
beware that boost ptree will store everything as strings, using iostream operator, lexical cast and user defined translators to map types to strings and viceversa. So, it's a very different design from <any> or <variant> that store the real objects on the heap and on the stack, respectively ...
-
October 26th, 2011, 07:54 AM
#6
Re: Heterogeneous container
Originally Posted by superbonzo
beware that boost ptree will store everything as strings, using iostream operator, lexical cast and user defined translators to map types to strings and viceversa. So, it's a very different design from <any> or <variant> that store the real objects on the heap and on the stack, respectively ...
Well, I suppose I'll be using it for configuration structures of basic type values which need to be written to/from file. In other words, I think I'm using it for exactly what is was meant for.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|