os/ossrv/ossrv_pub/boost_apis/boost/format/internals.hpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/format/internals.hpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,199 @@
     1.4 +// ----------------------------------------------------------------------------
     1.5 +// internals.hpp :  internal structs : stream_format_state, format_item. 
     1.6 +//                  included by format.hpp
     1.7 +// ----------------------------------------------------------------------------
     1.8 +
     1.9 +//  Copyright Samuel Krempp 2003. Use, modification, and distribution are
    1.10 +//  subject to the Boost Software License, Version 1.0. (See accompanying
    1.11 +//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
    1.12 +
    1.13 +//  See http://www.boost.org/libs/format for library home page
    1.14 +
    1.15 +// ----------------------------------------------------------------------------
    1.16 +
    1.17 +#ifndef BOOST_FORMAT_INTERNALS_HPP
    1.18 +#define BOOST_FORMAT_INTERNALS_HPP
    1.19 +
    1.20 +
    1.21 +#include <string>
    1.22 +#include <boost/assert.hpp>
    1.23 +#include <boost/optional.hpp>
    1.24 +#include <boost/limits.hpp>
    1.25 +#include <boost/format/detail/compat_workarounds.hpp>
    1.26 +#include <boost/format/alt_sstream.hpp> // used as a dummy stream
    1.27 +
    1.28 +namespace boost {
    1.29 +namespace io {
    1.30 +namespace detail {
    1.31 +
    1.32 +
    1.33 +//---- stream_format_state --------------------------------------------------//
    1.34 +
    1.35 +//   set of params that define the format state of a stream
    1.36 +    template<class Ch, class Tr> 
    1.37 +    struct stream_format_state 
    1.38 +    {
    1.39 +        typedef BOOST_IO_STD basic_ios<Ch, Tr>   basic_ios;
    1.40 +
    1.41 +        stream_format_state(Ch fill)                 { reset(fill); }
    1.42 +//        stream_format_state(const basic_ios& os)     { set_by_stream(os); }
    1.43 +
    1.44 +        void reset(Ch fill);                     //- sets to default state.
    1.45 +        void set_by_stream(const basic_ios& os); //- sets to os's state.
    1.46 +        void apply_on(basic_ios & os,            //- applies format_state to the stream
    1.47 +                      boost::io::detail::locale_t * loc_default = 0) const;
    1.48 +        template<class T> 
    1.49 +        void apply_manip(T manipulator)          //- modifies state by applying manipulator
    1.50 +            { apply_manip_body<Ch, Tr, T>( *this, manipulator) ; }
    1.51 +
    1.52 +        // --- data ---
    1.53 +        std::streamsize width_;
    1.54 +        std::streamsize precision_;
    1.55 +        Ch fill_; 
    1.56 +        std::ios_base::fmtflags flags_;
    1.57 +        std::ios_base::iostate  rdstate_;
    1.58 +        std::ios_base::iostate  exceptions_;
    1.59 +        boost::optional<boost::io::detail::locale_t>  loc_;
    1.60 +    };  
    1.61 +
    1.62 +
    1.63 +//---- format_item  ---------------------------------------------------------//
    1.64 +
    1.65 +//   stores all parameters that can be specified in format strings
    1.66 +    template<class Ch, class Tr, class Alloc>  
    1.67 +    struct format_item 
    1.68 +    {     
    1.69 +        enum pad_values { zeropad = 1, spacepad =2, centered=4, tabulation = 8 };
    1.70 +                         // 1. if zeropad is set, all other bits are not, 
    1.71 +                         // 2. if tabulation is set, all others are not.
    1.72 +                         // centered and spacepad can be mixed freely.
    1.73 +        enum arg_values { argN_no_posit   = -1, // non-positional directive. will set argN later
    1.74 +                          argN_tabulation = -2, // tabulation directive. (no argument read) 
    1.75 +                          argN_ignored    = -3  // ignored directive. (no argument read)
    1.76 +        };
    1.77 +        typedef BOOST_IO_STD basic_ios<Ch, Tr>                    basic_ios;
    1.78 +        typedef detail::stream_format_state<Ch, Tr>               stream_format_state;
    1.79 +        typedef ::std::basic_string<Ch, Tr, Alloc>                string_type;
    1.80 +
    1.81 +        format_item(Ch fill) :argN_(argN_no_posit), fmtstate_(fill), 
    1.82 +                              truncate_(max_streamsize()), pad_scheme_(0)  {}
    1.83 +        void reset(Ch fill);
    1.84 +        void compute_states(); // sets states  according to truncate and pad_scheme.
    1.85 +
    1.86 +        static std::streamsize max_streamsize() { 
    1.87 +            return (std::numeric_limits<std::streamsize>::max)();
    1.88 +        }
    1.89 +
    1.90 +        // --- data ---
    1.91 +        int         argN_;  //- argument number (starts at 0,  eg : %1 => argN=0)
    1.92 +                            //  negative values for items that don't process an argument
    1.93 +        string_type  res_;      //- result of the formatting of this item
    1.94 +        string_type  appendix_; //- piece of string between this item and the next
    1.95 +
    1.96 +        stream_format_state fmtstate_;// set by parsing, is only affected by modify_item
    1.97 +
    1.98 +        std::streamsize truncate_;//- is set for directives like %.5s that ask truncation
    1.99 +        unsigned int pad_scheme_;//- several possible padding schemes can mix. see pad_values
   1.100 +    }; 
   1.101 +
   1.102 +
   1.103 +
   1.104 +//--- Definitions  ------------------------------------------------------------
   1.105 +
   1.106 +// -   stream_format_state:: -------------------------------------------------
   1.107 +    template<class Ch, class Tr>
   1.108 +    void stream_format_state<Ch,Tr>:: apply_on (basic_ios & os,
   1.109 +                      boost::io::detail::locale_t * loc_default) const {
   1.110 +        // set the state of this stream according to our params
   1.111 +        if(width_ != -1)
   1.112 +            os.width(width_);
   1.113 +        if(precision_ != -1)
   1.114 +            os.precision(precision_);
   1.115 +        if(fill_ != 0)
   1.116 +            os.fill(fill_);
   1.117 +        os.flags(flags_);
   1.118 +        os.clear(rdstate_);
   1.119 +        os.exceptions(exceptions_);
   1.120 +#if !defined(BOOST_NO_STD_LOCALE)
   1.121 +        if(loc_)
   1.122 +            os.imbue(loc_.get());
   1.123 +        else if(loc_default)
   1.124 +            os.imbue(*loc_default);
   1.125 +#endif        
   1.126 +    }
   1.127 +
   1.128 +    template<class Ch, class Tr>
   1.129 +    void stream_format_state<Ch,Tr>:: set_by_stream(const basic_ios& os) {
   1.130 +        // set our params according to the state of this stream
   1.131 +        flags_ = os.flags();
   1.132 +        width_ = os.width();
   1.133 +        precision_ = os.precision();
   1.134 +        fill_ = os.fill();
   1.135 +        rdstate_ = os.rdstate();
   1.136 +        exceptions_ = os.exceptions();
   1.137 +    }
   1.138 +
   1.139 +
   1.140 +    template<class Ch, class Tr, class T>
   1.141 +    void apply_manip_body( stream_format_state<Ch, Tr>& self,
   1.142 +                           T manipulator) {
   1.143 +        // modify our params according to the manipulator
   1.144 +        basic_oaltstringstream<Ch, Tr>  ss;
   1.145 +        self.apply_on( ss );
   1.146 +        ss << manipulator;
   1.147 +        self.set_by_stream( ss );
   1.148 +    }
   1.149 +
   1.150 +    template<class Ch, class Tr> inline
   1.151 +    void stream_format_state<Ch,Tr>:: reset(Ch fill) {
   1.152 +        // set our params to standard's default state.   cf § 27.4.4.1 of the C++ norm
   1.153 +        width_=0; precision_=6; 
   1.154 +        fill_=fill; // default is widen(' '), but we cant compute it without the locale
   1.155 +        flags_ = std::ios_base::dec | std::ios_base::skipws; 
   1.156 +        // the adjust_field part is left equal to 0, which means right.
   1.157 +        exceptions_ = std::ios_base::goodbit;
   1.158 +        rdstate_ = std::ios_base::goodbit;
   1.159 +    }
   1.160 +
   1.161 +
   1.162 +// ---   format_item:: --------------------------------------------------------
   1.163 +
   1.164 +    template<class Ch, class Tr, class Alloc> 
   1.165 +    void format_item<Ch, Tr, Alloc>:: 
   1.166 +    reset (Ch fill) { 
   1.167 +        argN_=argN_no_posit; truncate_ = max_streamsize(); pad_scheme_ =0; 
   1.168 +        res_.resize(0); appendix_.resize(0);
   1.169 +        fmtstate_.reset(fill);
   1.170 +    }
   1.171 +
   1.172 +    template<class Ch, class Tr, class Alloc> 
   1.173 +    void format_item<Ch, Tr, Alloc>:: 
   1.174 +    compute_states() {
   1.175 +        // reflect pad_scheme_   on  fmt_state_
   1.176 +        //   because some pad_schemes has complex consequences on several state params.
   1.177 +        if(pad_scheme_ & zeropad) {
   1.178 +            // ignore zeropad in left alignment :
   1.179 +            if(fmtstate_.flags_ & std::ios_base::left) {
   1.180 +              BOOST_ASSERT(!(fmtstate_.flags_ &(std::ios_base::adjustfield ^std::ios_base::left)));
   1.181 +              // only left bit might be set. (not right, nor internal)
   1.182 +              pad_scheme_ = pad_scheme_ & (~zeropad); 
   1.183 +            }
   1.184 +            else { 
   1.185 +                pad_scheme_ &= ~spacepad; // printf ignores spacepad when zeropadding
   1.186 +                fmtstate_.fill_='0'; 
   1.187 +                fmtstate_.flags_ = (fmtstate_.flags_ & ~std::ios_base::adjustfield) 
   1.188 +                    | std::ios_base::internal;
   1.189 +                // removes all adjustfield bits, and adds internal.
   1.190 +            }
   1.191 +        }
   1.192 +        if(pad_scheme_ & spacepad) {
   1.193 +            if(fmtstate_.flags_ & std::ios_base::showpos)
   1.194 +                pad_scheme_ &= ~spacepad;
   1.195 +        }
   1.196 +    }
   1.197 +
   1.198 +
   1.199 +} } } // namespaces boost :: io :: detail
   1.200 +
   1.201 +
   1.202 +#endif // BOOST_FORMAT_INTERNALS_HPP