|
From: stephan b. <st...@s1...> - 2004-12-28 01:46:53
|
moved to list, just to show off s11n...
On Tuesday 28 December 2004 02:24, stephan beal wrote:
> > If load() / save() is not part of Prefs anymore it will be less
> > straight forward to make Prefs a singleton.
>
> No it won't! s11n CAN handle that case! i promise - i've done it
> before! It's sooooo easy to do with s11n.
>
> :)
In fact, i'll walk you through it right now...
Gehen wir davon aus... du willst den Koenig besuchen. When you visit The
King, you don't see The King directly. You get his Ambassador. Let's
see what a Singleton Ambassator for s11n might look like:
Let's assume Prefs has functions to iterate over, set and set the
contents.
Here's a proxy class which should work for ANY singleton type supporting
an instance() method.
template <typename SingletonT>
struct S11nSingletonProxy {
// serialize
bool operator()( P::SIO::S11nNode & dest ) const
{
typedef ::P::SIO::NodeTraits TR;
TR::class_name( dest, ::class_name<SingletonT>() );
// ^^^^^^^ s11n docs cover this in gory detail
SingletonT & s = SingletonT::instance();
// ... store s's state to dest ...
return true;
}
// deserialize
template <typename NodeT>
bool operator()( const P::SIO::S11nNode & src )
{
typedef ::P::SIO::NodeTraits TR;
SingletonT & s = SingletonT::instance();
// ... restore p's state from src...
return true;
}
};
We'd have to do a small bit of other back-end work, but i'm confident i
could get it working in under an hour, assuming Prefs has the necessary
mutator/getter API (in practice, all generic containers do).
We could also add another layer of indirection, to use this proxy for
ALL Singletons, but supply a functor type which performs the actual
logic of the de/ser operations. Again - no problem with s11n. :)
Now, to save the singleton:
using namespace P::SIO;
save( S11nSingletonProxy<MyT>(), std::cout );
Saves the singleton's state!
Loading, in this unusual case, requires a non-const proxy object, and we
must do a small extra step of loading an intermediary data node (the
container holding the object's data):
S11nNode * node = load_node( istream|file );
if( ! node ) { ... error ... }
S11nSingletonProxy<MyT> foo;
bool worked = deserialize( *node, foo );
delete node;
--
----- st...@s1... http://s11n.net
"...pleasure is a grace and is not obedient to the commands
of the will." -- Alan W. Watts
|