annotate DEPENDENCIES/generic/include/boost/format/feed_args.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 // ----------------------------------------------------------------------------
Chris@16 2 // feed_args.hpp : functions for processing each argument
Chris@16 3 // (feed, feed_manip, and distribute)
Chris@16 4 // ----------------------------------------------------------------------------
Chris@16 5
Chris@16 6 // Copyright Samuel Krempp 2003. Use, modification, and distribution are
Chris@16 7 // subject to the Boost Software License, Version 1.0. (See accompanying
Chris@16 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9
Chris@16 10 // See http://www.boost.org/libs/format for library home page
Chris@16 11
Chris@16 12 // ----------------------------------------------------------------------------
Chris@16 13
Chris@16 14 #ifndef BOOST_FORMAT_FEED_ARGS_HPP
Chris@16 15 #define BOOST_FORMAT_FEED_ARGS_HPP
Chris@16 16
Chris@16 17 #include <boost/config.hpp>
Chris@16 18 #include <boost/assert.hpp>
Chris@16 19 #include <boost/throw_exception.hpp>
Chris@16 20
Chris@16 21 #include <boost/format/format_class.hpp>
Chris@16 22 #include <boost/format/group.hpp>
Chris@16 23 #include <boost/format/detail/msvc_disambiguater.hpp>
Chris@16 24
Chris@16 25 namespace boost {
Chris@16 26 namespace io {
Chris@16 27 namespace detail {
Chris@16 28
Chris@16 29 template<class Ch, class Tr, class Alloc>
Chris@16 30 void mk_str( std::basic_string<Ch,Tr, Alloc> & res,
Chris@16 31 const Ch * beg,
Chris@16 32 typename std::basic_string<Ch,Tr,Alloc>::size_type size,
Chris@16 33 std::streamsize w,
Chris@16 34 const Ch fill_char,
Chris@16 35 std::ios_base::fmtflags f,
Chris@16 36 const Ch prefix_space, // 0 if no space-padding
Chris@16 37 bool center)
Chris@16 38 // applies centered/left/right padding to the string [beg, beg+size[
Chris@16 39 // Effects : the result is placed in res.
Chris@16 40 {
Chris@16 41 typedef typename std::basic_string<Ch,Tr,Alloc>::size_type size_type;
Chris@16 42 res.resize(0);
Chris@16 43 if(w<=0 || static_cast<size_type>(w) <=size) {
Chris@16 44 // no need to pad.
Chris@16 45 res.reserve(size + !!prefix_space);
Chris@16 46 if(prefix_space)
Chris@16 47 res.append(1, prefix_space);
Chris@16 48 if (size)
Chris@16 49 res.append(beg, size);
Chris@16 50 }
Chris@16 51 else {
Chris@16 52 std::streamsize n=static_cast<std::streamsize>(w-size-!!prefix_space);
Chris@16 53 std::streamsize n_after = 0, n_before = 0;
Chris@16 54 res.reserve(static_cast<size_type>(w)); // allocate once for the 2 inserts
Chris@16 55 if(center)
Chris@16 56 n_after = n/2, n_before = n - n_after;
Chris@16 57 else
Chris@16 58 if(f & std::ios_base::left)
Chris@16 59 n_after = n;
Chris@16 60 else
Chris@16 61 n_before = n;
Chris@16 62 // now make the res string :
Chris@16 63 if(n_before) res.append(static_cast<size_type>(n_before), fill_char);
Chris@16 64 if(prefix_space)
Chris@16 65 res.append(1, prefix_space);
Chris@16 66 if (size)
Chris@16 67 res.append(beg, size);
Chris@16 68 if(n_after) res.append(static_cast<size_type>(n_after), fill_char);
Chris@16 69 }
Chris@16 70 } // -mk_str(..)
Chris@16 71
Chris@16 72
Chris@101 73 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
Chris@101 74 // __DECCXX needs to be tricked to disambiguate this simple overload..
Chris@16 75 // the trick is in "boost/format/msvc_disambiguater.hpp"
Chris@16 76
Chris@16 77 template< class Ch, class Tr, class T> inline
Chris@16 78 void put_head (BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
Chris@16 79 disambiguater<Ch, Tr, T>::put_head(os, x, 1L);
Chris@16 80 }
Chris@16 81 template< class Ch, class Tr, class T> inline
Chris@16 82 void put_last (BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
Chris@16 83 disambiguater<Ch, Tr, T>::put_last(os, x, 1L);
Chris@16 84 }
Chris@16 85
Chris@16 86 #else
Chris@16 87
Chris@16 88 template< class Ch, class Tr, class T> inline
Chris@16 89 void put_head (BOOST_IO_STD basic_ostream<Ch, Tr> &, const T& ) {
Chris@16 90 }
Chris@16 91
Chris@16 92 template< class Ch, class Tr, class T> inline
Chris@16 93 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const group1<T>& x ) {
Chris@16 94 os << group_head(x.a1_); // send the first N-1 items, not the last
Chris@16 95 }
Chris@16 96
Chris@16 97 template< class Ch, class Tr, class T> inline
Chris@16 98 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const T& x ) {
Chris@16 99 os << x ;
Chris@16 100 }
Chris@16 101
Chris@16 102 template< class Ch, class Tr, class T> inline
Chris@16 103 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const group1<T>& x ) {
Chris@16 104 os << group_last(x.a1_); // this selects the last element
Chris@16 105 }
Chris@16 106
Chris@16 107 #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
Chris@16 108 template< class Ch, class Tr, class T> inline
Chris@16 109 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> &, T& ) {
Chris@16 110 }
Chris@16 111
Chris@16 112 template< class Ch, class Tr, class T> inline
Chris@16 113 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, T& x) {
Chris@16 114 os << x ;
Chris@16 115 }
Chris@16 116 #endif
Chris@101 117 #endif // -__DECCXX workaround
Chris@101 118
Chris@101 119 template< class Ch, class Tr, class T>
Chris@101 120 void call_put_head(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x) {
Chris@101 121 put_head(os, *(typename ::boost::remove_reference<T>::type*)x);
Chris@101 122 }
Chris@101 123
Chris@101 124 template< class Ch, class Tr, class T>
Chris@101 125 void call_put_last(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x) {
Chris@101 126 put_last(os, *(T*)x);
Chris@101 127 }
Chris@101 128
Chris@101 129 template< class Ch, class Tr>
Chris@101 130 struct put_holder {
Chris@101 131 template<class T>
Chris@101 132 put_holder(T& t)
Chris@101 133 : arg(&t),
Chris@101 134 put_head(&call_put_head<Ch, Tr, T>),
Chris@101 135 put_last(&call_put_last<Ch, Tr, T>)
Chris@101 136 {}
Chris@101 137 const void* arg;
Chris@101 138 void (*put_head)(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x);
Chris@101 139 void (*put_last)(BOOST_IO_STD basic_ostream<Ch, Tr> & os, const void* x);
Chris@101 140 };
Chris@101 141
Chris@101 142 template< class Ch, class Tr> inline
Chris@101 143 void put_head( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const put_holder<Ch, Tr>& t) {
Chris@101 144 t.put_head(os, t.arg);
Chris@101 145 }
Chris@101 146
Chris@101 147 template< class Ch, class Tr> inline
Chris@101 148 void put_last( BOOST_IO_STD basic_ostream<Ch, Tr> & os, const put_holder<Ch, Tr>& t) {
Chris@101 149 t.put_last(os, t.arg);
Chris@101 150 }
Chris@16 151
Chris@16 152
Chris@16 153 template< class Ch, class Tr, class Alloc, class T>
Chris@16 154 void put( T x,
Chris@16 155 const format_item<Ch, Tr, Alloc>& specs,
Chris@16 156 typename basic_format<Ch, Tr, Alloc>::string_type& res,
Chris@16 157 typename basic_format<Ch, Tr, Alloc>::internal_streambuf_t & buf,
Chris@16 158 io::detail::locale_t *loc_p = NULL)
Chris@16 159 {
Chris@16 160 #ifdef BOOST_MSVC
Chris@16 161 // If std::min<unsigned> or std::max<unsigned> are already instantiated
Chris@16 162 // at this point then we get a blizzard of warning messages when we call
Chris@16 163 // those templates with std::size_t as arguments. Weird and very annoyning...
Chris@16 164 #pragma warning(push)
Chris@16 165 #pragma warning(disable:4267)
Chris@16 166 #endif
Chris@16 167 // does the actual conversion of x, with given params, into a string
Chris@16 168 // using the supplied stringbuf.
Chris@16 169
Chris@16 170 typedef typename basic_format<Ch, Tr, Alloc>::string_type string_type;
Chris@16 171 typedef typename basic_format<Ch, Tr, Alloc>::format_item_t format_item_t;
Chris@16 172 typedef typename string_type::size_type size_type;
Chris@16 173
Chris@16 174 basic_oaltstringstream<Ch, Tr, Alloc> oss( &buf);
Chris@16 175 specs.fmtstate_.apply_on(oss, loc_p);
Chris@16 176
Chris@16 177 // the stream format state can be modified by manipulators in the argument :
Chris@16 178 put_head( oss, x );
Chris@16 179 // in case x is a group, apply the manip part of it,
Chris@16 180 // in order to find width
Chris@16 181
Chris@16 182 const std::ios_base::fmtflags fl=oss.flags();
Chris@16 183 const bool internal = (fl & std::ios_base::internal) != 0;
Chris@16 184 const std::streamsize w = oss.width();
Chris@16 185 const bool two_stepped_padding= internal && (w!=0);
Chris@16 186
Chris@16 187 res.resize(0);
Chris@16 188 if(! two_stepped_padding) {
Chris@16 189 if(w>0) // handle padding via mk_str, not natively in stream
Chris@16 190 oss.width(0);
Chris@16 191 put_last( oss, x);
Chris@16 192 const Ch * res_beg = buf.pbase();
Chris@16 193 Ch prefix_space = 0;
Chris@16 194 if(specs.pad_scheme_ & format_item_t::spacepad)
Chris@16 195 if(buf.pcount()== 0 ||
Chris@16 196 (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-') ))
Chris@16 197 prefix_space = oss.widen(' ');
Chris@16 198 size_type res_size = (std::min)(
Chris@16 199 static_cast<size_type>(specs.truncate_ - !!prefix_space),
Chris@16 200 buf.pcount() );
Chris@16 201 mk_str(res, res_beg, res_size, w, oss.fill(), fl,
Chris@16 202 prefix_space, (specs.pad_scheme_ & format_item_t::centered) !=0 );
Chris@16 203 }
Chris@16 204 else { // 2-stepped padding
Chris@16 205 // internal can be implied by zeropad, or user-set.
Chris@16 206 // left, right, and centered alignment overrule internal,
Chris@16 207 // but spacepad or truncate might be mixed with internal (using manipulator)
Chris@16 208 put_last( oss, x); // may pad
Chris@16 209 const Ch * res_beg = buf.pbase();
Chris@16 210 size_type res_size = buf.pcount();
Chris@16 211 bool prefix_space=false;
Chris@16 212 if(specs.pad_scheme_ & format_item_t::spacepad)
Chris@16 213 if(buf.pcount()== 0 ||
Chris@16 214 (res_beg[0] !=oss.widen('+') && res_beg[0] !=oss.widen('-') ))
Chris@16 215 prefix_space = true;
Chris@16 216 if(res_size == static_cast<size_type>(w) && w<=specs.truncate_ && !prefix_space) {
Chris@16 217 // okay, only one thing was printed and padded, so res is fine
Chris@16 218 res.assign(res_beg, res_size);
Chris@16 219 }
Chris@16 220 else { // length w exceeded
Chris@16 221 // either it was multi-output with first output padding up all width..
Chris@16 222 // either it was one big arg and we are fine.
Chris@16 223 // Note that res_size<w is possible (in case of bad user-defined formatting)
Chris@16 224 res.assign(res_beg, res_size);
Chris@16 225 res_beg=NULL; // invalidate pointers.
Chris@16 226
Chris@16 227 // make a new stream, to start re-formatting from scratch :
Chris@16 228 buf.clear_buffer();
Chris@16 229 basic_oaltstringstream<Ch, Tr, Alloc> oss2( &buf);
Chris@16 230 specs.fmtstate_.apply_on(oss2, loc_p);
Chris@16 231 put_head( oss2, x );
Chris@16 232
Chris@16 233 oss2.width(0);
Chris@16 234 if(prefix_space)
Chris@16 235 oss2 << ' ';
Chris@16 236 put_last(oss2, x );
Chris@16 237 if(buf.pcount()==0 && specs.pad_scheme_ & format_item_t::spacepad) {
Chris@16 238 prefix_space =true;
Chris@16 239 oss2 << ' ';
Chris@16 240 }
Chris@16 241 // we now have the minimal-length output
Chris@16 242 const Ch * tmp_beg = buf.pbase();
Chris@16 243 size_type tmp_size = (std::min)(static_cast<size_type>(specs.truncate_),
Chris@16 244 buf.pcount() );
Chris@16 245
Chris@16 246
Chris@16 247 if(static_cast<size_type>(w) <= tmp_size) {
Chris@16 248 // minimal length is already >= w, so no padding (cool!)
Chris@16 249 res.assign(tmp_beg, tmp_size);
Chris@16 250 }
Chris@16 251 else { // hum.. we need to pad (multi_output, or spacepad present)
Chris@16 252 //find where we should pad
Chris@16 253 size_type sz = (std::min)(res_size + (prefix_space ? 1 : 0), tmp_size);
Chris@16 254 size_type i = prefix_space;
Chris@16 255 for(; i<sz && tmp_beg[i] == res[i - (prefix_space ? 1 : 0)]; ++i) {}
Chris@16 256 if(i>=tmp_size) i=prefix_space;
Chris@16 257 res.assign(tmp_beg, i);
Chris@16 258 std::streamsize d = w - static_cast<std::streamsize>(tmp_size);
Chris@16 259 BOOST_ASSERT(d>0);
Chris@16 260 res.append(static_cast<size_type>( d ), oss2.fill());
Chris@16 261 res.append(tmp_beg+i, tmp_size-i);
Chris@16 262 BOOST_ASSERT(i+(tmp_size-i)+(std::max)(d,(std::streamsize)0)
Chris@16 263 == static_cast<size_type>(w));
Chris@16 264 BOOST_ASSERT(res.size() == static_cast<size_type>(w));
Chris@16 265 }
Chris@16 266 }
Chris@16 267 }
Chris@16 268 buf.clear_buffer();
Chris@16 269 #ifdef BOOST_MSVC
Chris@16 270 #pragma warning(pop)
Chris@16 271 #endif
Chris@16 272 } // end- put(..)
Chris@16 273
Chris@16 274
Chris@16 275 template< class Ch, class Tr, class Alloc, class T>
Chris@16 276 void distribute (basic_format<Ch,Tr, Alloc>& self, T x) {
Chris@16 277 // call put(x, ..) on every occurence of the current argument :
Chris@16 278 if(self.cur_arg_ >= self.num_args_) {
Chris@16 279 if( self.exceptions() & too_many_args_bit )
Chris@16 280 boost::throw_exception(too_many_args(self.cur_arg_, self.num_args_));
Chris@16 281 else return;
Chris@16 282 }
Chris@16 283 for(unsigned long i=0; i < self.items_.size(); ++i) {
Chris@16 284 if(self.items_[i].argN_ == self.cur_arg_) {
Chris@16 285 put<Ch, Tr, Alloc, T> (x, self.items_[i], self.items_[i].res_,
Chris@16 286 self.buf_, boost::get_pointer(self.loc_) );
Chris@16 287 }
Chris@16 288 }
Chris@16 289 }
Chris@16 290
Chris@16 291 template<class Ch, class Tr, class Alloc, class T>
Chris@16 292 basic_format<Ch, Tr, Alloc>&
Chris@101 293 feed_impl (basic_format<Ch,Tr, Alloc>& self, T x) {
Chris@16 294 if(self.dumped_) self.clear();
Chris@16 295 distribute<Ch, Tr, Alloc, T> (self, x);
Chris@16 296 ++self.cur_arg_;
Chris@16 297 if(self.bound_.size() != 0) {
Chris@16 298 while( self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_] )
Chris@16 299 ++self.cur_arg_;
Chris@16 300 }
Chris@16 301 return self;
Chris@16 302 }
Chris@101 303
Chris@101 304 template<class Ch, class Tr, class Alloc, class T> inline
Chris@101 305 basic_format<Ch, Tr, Alloc>&
Chris@101 306 feed (basic_format<Ch,Tr, Alloc>& self, T x) {
Chris@101 307 return feed_impl<Ch, Tr, Alloc, const put_holder<Ch, Tr>&>(self, put_holder<Ch, Tr>(x));
Chris@101 308 }
Chris@16 309
Chris@16 310 } // namespace detail
Chris@16 311 } // namespace io
Chris@16 312 } // namespace boost
Chris@16 313
Chris@16 314
Chris@16 315 #endif // BOOST_FORMAT_FEED_ARGS_HPP