Need ideas -> generic storage of type registration with unknown type

Discussion in 'C++' started by Noah Roberts, Mar 15, 2010.

  1. Noah Roberts

    Noah Roberts Guest

    I'm using the boost::serialization library for the IO for my objects.
    I'd like to come up with a way to register objects with the archive in a
    generic manner something like so:

    struct type_registrar
    {
    template < typename T >
    static bool register_type()
    {
    singleton()->register_functions.push_back(boost::bind
    (&Archive::register_type<T>, _1));
    }
    static type_registrar * singleton()
    {
    static type_registrar * singleton_ = new type_registrar;
    return singleton_;
    }
    static void register_all(Archive & ar)
    {
    BOOST_FOREACH(boost::function<void(Archive&)> rf, singleton()->
    register_functions)
    {
    rf(ar);
    }
    }
    private:
    std::vector< boost::function<void(Archive&)> > register_functions;
    };

    Then in some .cpp file I'd do this:

    namespace { bool reg = type_registrar::register_type<my_local_type>(); }

    Then, upon a load/save operation I'd do:

    Archive ar; // this isn't how to create one, just illustrating.
    type_registrar::register_all(ar);

    The problem is that "Archive" needs to be any type (in other words,
    "register_all()" needs to be templated by Archive) since I could be:

    a) registering with either an input or output archive and each is its
    own type or...

    b) registering with a binary archive in one example and an xml or text
    archive in some other.

    I need the former now, the latter is just something that would be in
    line with the boost::serialization design.

    I can't think of any way to do this. I can answer the former with two
    sets of functions for the register_all to go through, but it would be
    nice to loose the redundancy if possible.

    So I'm asking for ideas.

    Thanks.
    Noah Roberts, Mar 15, 2010
    #1
    1. Advertising

  2. Noah Roberts

    Noah Roberts Guest

    In article <>,
    says...
    >
    > I'm using the boost::serialization library for the IO for my objects.
    > I'd like to come up with a way to register objects with the archive in a
    > generic manner something like so:


    ..... blah blah blah....

    I was looking at the wrong place in the code for the problem. There's
    already mechanics to do what I needed to do and I was using them, I just
    thought it wasn't working.
    Noah Roberts, Mar 15, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    3
    Views:
    299
    Walter Wang [MSFT]
    Jan 8, 2007
  2. sarathy
    Replies:
    2
    Views:
    652
    sarathy
    Jul 17, 2006
  3. Replies:
    5
    Views:
    5,205
    Joachim Schmitz
    Sep 23, 2007
  4. minlearn
    Replies:
    2
    Views:
    445
    red floyd
    Mar 13, 2009
  5. Vincent Arnoux
    Replies:
    1
    Views:
    236
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page