Boost logo

Boost :

Subject: [boost] [iostreams][io_state_savers] Request for interest in some ios_base basic utilities
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2011-11-09 13:39:48


Hi,

I'm a beginner of the standard IOStream library. While restructuring
Boost.Chrono I/O I have find some classes that could be of general use
(if the design is correct, of course).

The first one is quite simple, is used to define manipulators with
parameters. Instead of defining a class and the correspondent stream
inserters and extractors the manip template class do all this at once.

It can be used as follows

     class mendl: public manip<mendl>
     {
     public:
       explicit mendl(size_t how_many) :
         count(how_many) {}
       template <typename out_stream>
       void operator()(out_stream &out) const
       {
         for (size_t line = 0; line < count; ++line)
         {
           out.put(out.widen('\n'));
         }
         out.flush();
       }
     private:
       size_t count;
     };

Now mendl can be use to insert n new lines at once as follows

  cout << mendl(3);

The second one is an ios specific smart pointer ios_base_state_ptr
(based on the use of ios_base::pword and ios_base::xalloc). The class
ios_base_state is a ios_base_state_ptr that can not be null.

It can be used like

     struct MyData
     {
       std::string time_fmt;
       std::string duration_fmt;
     };

     std::string get_time_fmt(std::ios_base & ios)
     {
       return ios_base_state<MyData>(ios)->time_fmt;
     }

ios_base_state stores a new MyData in the ios if not already stored and
provides an smart pointer interface to it.

The 3rd one (ios_base_flags) uses the ios_base::iword and xalloc to
provide access to specific ios flags. It can be used as follows:

     class fmt_masks : ios_base_flags<fmt_masks>
     {
       typedef ios_base_flags<fmt_masks> base_type;

     public:
       fmt_masks(std::ios_base& ios): base_type(ios) {}
       enum type
       {
         uses_symbol = 1 << 0,
         uses_local = 1 << 1
       };
     };

Usage of this class can be

       duration_style::type get_duration_style(std::ios_base& ios)
       {
         return (fmt_masks(ios).flags() & fmt_masks::uses_symbol) ?
duration_style::symbol : duration_style::prefix;
       }

The interface and the implementation of these classes is attached.

Do you see a design flag on these classes?
If not, do you think these kind of utilities could be added to boost?
If yes, could these be merged with Boost.io_state_savers or
Boost.IOStreams to form a larger io utilities libraries?

Best,
Vicente





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