summaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/tree/ostream.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-05-21 19:18:52 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-05-21 19:18:52 +0200
commit9a764d4ed5606c5f171ae05a7409128a88184b11 (patch)
tree8f1c862354e5c6f112e7e52e3641ad87e4d276df /libxsd/xsd/cxx/tree/ostream.hxx
parentefb2476d01b2bcc62569c8748327754a836195c3 (diff)
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.
Diffstat (limited to 'libxsd/xsd/cxx/tree/ostream.hxx')
-rw-r--r--libxsd/xsd/cxx/tree/ostream.hxx47
1 files changed, 46 insertions, 1 deletions
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 <map>
+#include <string>
+#include <memory> // std::auto_ptr
#include <cstddef> // 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 <typename C>
+ std::size_t
+ pool_string (const std::basic_string<C>& s)
+ {
+ typedef pool_impl<C> pool_type;
+
+ if (pool_.get () == 0)
+ pool_.reset (new pool_type);
+
+ pool_type& p (*static_cast<pool_type*> (pool_.get ()));
+
+ std::pair<typename pool_type::iterator, bool> r (
+ p.insert (std::pair<std::basic_string<C>, 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 <typename C>
+ struct pool_impl: pool, std::map<std::basic_string<C>, std::size_t>
+ {
+ };
+
S& s_;
+ std::size_t seq_;
+ std::auto_ptr<pool> pool_;
};