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

N

Noah Roberts

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

Noah Roberts

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top