|
Boost : |
From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2007-12-04 06:12:49
On Tuesday 04 December 2007 09:29:45 Corrado Zoccolo wrote:
> You can improve things by swapping the string instead of using assignment.
>
> template<class Archive, class Elem, class Tr>
> BOOST_ARCHIVE_OR_WARCHIVE_DECL(void)
> -basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & s)
> +basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & res_s)
> {
> + std::string s;
> std::size_t l;
> this->This()->load(l);
> // borland de-allocator fixup
> @@ -96,6 +97,9 @@
> s.resize(l);
> // note breaking a rule here - could be a problem on some platform
> load_binary(const_cast<char *>(s.data()), l);
> +
> + // Respect other references to the chars by assigning result string
> *+ res_s.swap(s);*
> }
How about this:
std::size_t l;
this->This()->load(l);
std::string tmp(l, '\0');
/* We write directly into the string's buffer using a const_cast
Note: this relies on the implementation to provide an own buffer
to every std::string, which we try to assure by using a temporary
here. We are _not_ using resize() on the existing string because
if the size remains the same this is a no-op and does not cause a
buffer to be allocated on e.g. GCC4's libstdc++. */
load_binary(const_cast<char*>(tmp.data()), tmp.size());
res.swap(tmp);
This might even make the Borland workaround unnecessary. Note also that I used
tmp.data() and tmp.size() in the load_binary call just for symmetry.
Uli
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk