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