Opened 13 years ago

Closed 12 years ago

#3700 closed Feature Requests (wontfix)

boost::archive should support basic_strings with different allocators

Reported by: anonymous Owned by: Robert Ramey
Milestone: Component: serialization
Version: Boost 1.41.0 Severity: Optimization
Keywords: serialization archive allocator Cc:

Description

Hello

boost::archive supporting std::basic_string natively is obviously very useful. It would be even more useful if boost::archive supported std::basic_string with different allocators. It currently only supports std::basic_strings with the default allocator. If boost::archive supported std::basic_strings with different allocators, it would be very useful in writing performance code, and would be more C++ compliant.

Thanks.

Change History (3)

comment:1 by Robert Ramey, 13 years ago

Resolution: invalid
Status: newclosed
Type: BugsFeature Requests

the string and wstring are special in that they were needed internally by the library so they were tagged a primitive.

If you want to use your own variation, you can do it as you would for any primitive type. Derive from basic_string<char> (or basic_string<wchar_t>) and reimplement you're own special behavior.

struct my_string : public basic_string<char> {
   template<class Archive>
   void serialize, ...
   my_string(...) :
      basic_string<char>( ... my special allocator, etc)
   {
       ...
   }
};

...

If this doesn't address what you want, feel free to reopen this track item

Robert Ramey

comment:2 by anonymous, 12 years ago

Milestone: Boost 1.42.0
Resolution: invalid
Status: closedreopened

Hi,

The problem here is that we have our string object defined as:

typedef std::basic_string<std::string::value_type,std::string::traits_type,MyAllocator> MyString;

boost::serialization should be able to serialize this object the same way it does a std::string. Instead it serializes it as a char array which causes very large and ugly xml.

The reason is that in the boost serialization class it uses std::string directly. Instead all functions that are hardcoded as std::string as primitive types, they should instead be templates that use std::basic_string<A,B,C> so they work with any basic_string type.

comment:3 by Robert Ramey, 12 years ago

Resolution: wontfix
Status: reopenedclosed

"boost::serialization should be able to serialize this object the same way it does a std::string. Instead it serializes it as a char array which causes very large and ugly xml."

OK you're halfway home. Now that you've got your own string type you can define the serialization anyway you want. This is not hard, but it will take a little bit of effort. You can see how it's done by checking out how it's done for std::string.

the std:string is "special" because I use it in the archive header. Making this "special" HAS created a pain in the neck. I tried to avoid it, but at the time it seemed like it was the least worse alternatives.

Good news is, you can define your own - as you have done - and implement any kind of scheme you wish. Carefully consider the implementation level - I would guess you want to use "object_serializable" for this. And you will have to decide how you want to represent the string in the file. Not that this last part is not trivial as you can't really know what a string is going to contain. (E.G. it can contain '\0' which so if you want some non-ugly representation, you'll have to have an escape mechanism. Good news is that the library already has tools for dealing with this - Bad news is that you have to invest effort to figure out how they have been used (data flow iterators).

I'm sure with a little effort you can use the facilities of the library to get exactly what you want. Good Luck.

Robert Ramey

Note: See TracTickets for help on using tickets.