|
From: stephan b. <st...@s1...> - 2004-12-27 23:06:27
|
Yo!
Marc sent me some code today for P1 with his latest, feature-filled
Property classes. Those aren't yet ported to p2. In the mean time, i
took and old property store class of mine and use LexT to simplify the
interface (cut the interface in half!). See
P::Util::SimplePropertyStore. It's simply a key/val pair holder, and
doesn't support multiple keys at the moment - we should decide if it
should use multimap or map. i vote for map, because that's what all of
my anticipated use-cases will need, and multimap doesn't support
operator[].
Here's a short demo, including SIO support:
using namespace ::P::SIO;
using namespace ::P::Util;
P::Util::SimplePropertyStoreTest m;
m["fred"] = "wilma";
m["barney"] = "betty";
m["days_in_december"] = 31;
m["days_in_february"] = 28.5;
m["the_letter_a"] = 'a';
save( m, std::cout );
Output:
<!DOCTYPE s11n::io::expat_serializer>
<s11n_node class="SomeSillyPropertyMap">
<barney>betty</barney>
<days_in_december>31</days_in_december>
<days_in_february>28.500000</days_in_february>
<fred>wilma</fred>
<the_letter_a>a</the_letter_a>
</s11n_node>
Couldn't get much simpler than that. :)
The i/o support works with so little effort because:
a) LexT is i/ostreamable, which means we can inherently treat it as a
POD.
b) SimplePropertyStore is close to API-compatible with std::map, which
means we can plug it in to P::SIO using the already-available map
serialization proxies:
#define PS11N_TYPE P::Util::SimplePropertyStore
#define PS11N_TYPE_NAME "SomeSillyPropertyMap"
#define PS11N_SERIALIZE_FUNCTOR \
::P::s11n::map::streamable_map_serializable_proxy
#include <pclasses/s11n/reg_serializable_traits.h>
What does that mean? It means streamable_..._proxy will act on behalf of
our type for serialization calls. If we swap that proxy out with the
default map proxy:
::P::s11n::map::map_serializable_proxy
then we get MUCH fatter output. The advantage to the default proxy is
that it works with all objects, not just i/ostreamables. The output
when we treat each map entry as a POD:
<!DOCTYPE s11n::io::expat_serializer>
<s11n_node class="SomeSillyPropertyMap">
<pair class="pair">
<first class="string">
<v>barney</v>
</first>
<second class="LexT">
<v>betty</v>
</second>
</pair>
... repeat for each pair ...
</s11n_node>
When we KNOW we're saving POD-style data we can plug in lighter-weight,
more specialized proxies.
See ya!
--
----- st...@s1... http://s11n.net
"...pleasure is a grace and is not obedient to the commands
of the will." -- Alan W. Watts
|