Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-05-19 05:51:58


Pavel Vozenilek wrote:
>"Max Khesin" <MKhesin_at_[hidden]> wrote:
>
> > Just curious:
> > what do you think of Andrei's work on policy-based strings:
> > http://www.cuj.com/documents/s=7994/cujcexp1906alexandr/alexandr.htm
> > http://moderncppdesign.com/code/flex_string.zip

I have only previously come across it in passing and have not had a chance
to take a look at it. I am studying it at the moment: a very good library!

> > - does it do what you want (or leave flexibility to do what you want)
> > - if so why not just use it
> > - if not what doesn't it do

>From what I understand thus far, usage would be like this:

   boost::flex_string< char, std::char_traits< char >, std::allocator< char
>, VectorStringPolicy< char > > vecstr;

which is too weildy. It would be possible to use the template typedef
concept to have something like:
   template< typename CharT, template< typename C, class A > class Storage,
class Traits, class AllocT >
   struct cust_string
   {
      typedef boost::flex_string< CharT, Traits, AllocT, Storage< CharT,
AllocT > > type;
   };

   cust_string< char, VectorStringPolicy >::type vecstr;

If you want a special string type, you will need to do something like this:

   template< typename CharT, class A = std::allocator< CharT > >
   class MyCustStorage{ ... };

   template< typename CharT, class Traits >
   class mybasic_string: public boost::flex_string< CharT, Traits,
std::allocator< CharT >, Storage< CharT > >
   {
       public:
          mybasic_string(){}
          // ...
   };

Where you are essentially having to write more code. What I am ideally
looking for is something similar to what the iterator adaptors do, allowing:

   template< typename CharT, class Traits >
   class mybasic_string: public boost::string_adaptor< mybasic_string,
CharT, Traits >
   {
      public:
         const CharT * c_str(){ ... }
         // ...
      public:
         mybasic_string(){}
         // ...
   };

Another minor point is that the policy classes do not meet the
standard/Boost naming convention.

Also: what if you want to use a different string type for substrings than
the type being used? As an example, I have a fixed-capacity string
fixed_string< n, CharT > that is equivalent to CharT[ n ], and guards
against buffer overflow on the string operations (as a safe replacement for
the equivalent C operations).

In order to write code generically on them without using template code, I
have an abstract class:

   class char_string
   {
      public:
         virtual const char * c_str() = 0;
         virtual size_t length() = 0;
         // ...
   };

   template< size_t n, typename CharT = char, ... >
   class fixed_string: public char_string // note: MPL code to select char
or wchar_t version
   {
      CharT str[ n ];
      size_t len;
      public:
         inline const CharT * c_str(){ return( str ); }
         inline size_t length(){ return( len ); }
         // ...
   };

   inline void printme( const char_string & s )
   {
      std::cout << s.c_str();
   }

With this, you can't use char_string as a standard type (it needs to be a
reference or pointer) because you'd get an error about instantiating an
abstract base class.

>Just remark: Andrei's flex_string is already included in Wave, (future)
>Boost library.

:)

Regards,
Reece

_________________________________________________________________
Get a FREE connection, FREE modem and one month's FREE line rental, plus a
US or European flight when you sign up for BT Broadband!
http://www.msn.co.uk/specials/btbroadband


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk