CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2008
    Posts
    902

    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?

  2. #2
    Join Date
    Oct 2008
    Posts
    1,456

    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 ?

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: Heterogeneous container

    Quote Originally Posted by superbonzo View Post
    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.

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    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.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

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

  6. #6
    Join Date
    Aug 2008
    Posts
    902

    Re: Heterogeneous container

    Quote Originally Posted by superbonzo View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured