From 9a764d4ed5606c5f171ae05a7409128a88184b11 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 21 May 2010 19:18:52 +0200 Subject: Pool polymorphic type-id strings in binary representations Add support for an implicit string pool in istream and ostream templates. Use this support to pool type namespace and name when dealing with polymorphic types. --- libxsd/xsd/cxx/tree/ostream.hxx | 47 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'libxsd/xsd/cxx/tree/ostream.hxx') diff --git a/libxsd/xsd/cxx/tree/ostream.hxx b/libxsd/xsd/cxx/tree/ostream.hxx index cf2d9ee..7c76793 100644 --- a/libxsd/xsd/cxx/tree/ostream.hxx +++ b/libxsd/xsd/cxx/tree/ostream.hxx @@ -6,6 +6,9 @@ #ifndef XSD_CXX_TREE_OSTREAM_HXX #define XSD_CXX_TREE_OSTREAM_HXX +#include +#include +#include // std::auto_ptr #include // std::size_t namespace xsd @@ -126,7 +129,7 @@ namespace xsd public: explicit ostream (S& s) - : s_ (s) + : s_ (s), seq_ (1) { } @@ -136,13 +139,55 @@ namespace xsd return s_; } + // If the string is not in the pool, add it and return 0. Otherwise + // return the string's pool id. In the former case the application + // should serialize the original string. + // + // The returned ids are sequential and start with 1. 0 is reserved + // as a special marker to be used by the application for the first + // encounter of the string. + // + template + std::size_t + pool_string (const std::basic_string& s) + { + typedef pool_impl pool_type; + + if (pool_.get () == 0) + pool_.reset (new pool_type); + + pool_type& p (*static_cast (pool_.get ())); + + std::pair r ( + p.insert (std::pair, std::size_t> (s, seq_))); + + if (!r.second) + return r.first->second; + + seq_++; + return 0; + } + private: ostream (const ostream&); ostream& operator= (const ostream&); private: + struct pool + { + virtual + ~pool () {} + }; + + template + struct pool_impl: pool, std::map, std::size_t> + { + }; + S& s_; + std::size_t seq_; + std::auto_ptr pool_; }; -- cgit v1.1