2 // Boost general library format ---------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
5 // (C) Samuel Krempp 2001
6 // krempp@crans.ens-cachan.fr
7 // Permission to copy, use, modify, sell and
8 // distribute this software is granted provided this copyright notice appears
9 // in all copies. This software is provided "as is" without express or implied
10 // warranty, and with no claim as to its suitability for any purpose.
12 // ideas taken from RĂ¼diger Loos's format class
13 // and Karl Nelson's ofstream
15 // ----------------------------------------------------------------------------
16 // format_implementation.hpp Implementation of the basic_format class
17 // ----------------------------------------------------------------------------
20 #ifndef BOOST_FORMAT_IMPLEMENTATION_HPP
21 #define BOOST_FORMAT_IMPLEMENTATION_HPP
23 #include <boost/throw_exception.hpp>
24 #include <boost/assert.hpp>
25 #include <boost/format/format_class.hpp>
26 #include <algorithm> // std::swap
30 // -------- format:: -------------------------------------------
31 template< class Ch, class Tr>
32 basic_format<Ch, Tr>:: basic_format(const Ch* str)
33 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
34 exceptions_(io::all_error_bits)
40 #ifndef BOOST_NO_STD_LOCALE
41 template< class Ch, class Tr>
42 basic_format<Ch, Tr>:: basic_format(const Ch* str, const std::locale & loc)
43 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
44 exceptions_(io::all_error_bits)
50 template< class Ch, class Tr>
51 basic_format<Ch, Tr>:: basic_format(const string_t& s, const std::locale & loc)
52 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
53 exceptions_(io::all_error_bits)
58 #endif //BOOST_NO_STD_LOCALE
60 template< class Ch, class Tr>
61 basic_format<Ch, Tr>:: basic_format(const string_t& s)
62 : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
63 exceptions_(io::all_error_bits)
68 template< class Ch, class Tr>
69 basic_format<Ch, Tr>:: basic_format(const basic_format& x)
70 : items_(x.items_), bound_(x.bound_), style_(x.style_),
71 cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(false),
72 prefix_(x.prefix_), exceptions_(x.exceptions_)
76 template< class Ch, class Tr>
77 void basic_format<Ch, Tr>:: swap (basic_format & x) {
78 std::swap(exceptions_, x.exceptions_);
79 std::swap(style_, x.style_);
80 std::swap(cur_arg_, x.cur_arg_);
81 std::swap(num_args_, x.num_args_);
82 std::swap(dumped_, x.dumped_);
84 items_.swap(x.items_);
85 prefix_.swap(x.prefix_);
86 bound_.swap(x.bound_);
89 template< class Ch, class Tr>
90 basic_format<Ch, Tr>& basic_format<Ch, Tr>:: operator= (const basic_format& x) {
93 (basic_format<Ch, Tr>(x)).swap(*this);
97 template< class Ch, class Tr>
98 unsigned char basic_format<Ch,Tr>:: exceptions() const {
102 template< class Ch, class Tr>
103 unsigned char basic_format<Ch,Tr>:: exceptions(unsigned char newexcept) {
104 unsigned char swp = exceptions_;
105 exceptions_ = newexcept;
109 template<class Ch, class Tr>
110 void basic_format<Ch, Tr>:: make_or_reuse_data(std::size_t nbitems) {
111 Ch fill = oss_.widen(' ');
112 if(items_.size() == 0)
113 items_.assign( nbitems, format_item_t(fill) );
116 items_.resize(nbitems, format_item_t(fill));
117 for(std::size_t i=0; i < nbitems; ++i)
118 items_[i].reset(fill); // strings are resized to "", instead of reallocated
122 template< class Ch, class Tr>
123 basic_format<Ch,Tr>& basic_format<Ch,Tr>:: clear() {
124 // empty the string buffers (except bound arguments)
125 // and make the format object ready for formatting a new set of arguments
127 BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
129 for(unsigned long i=0; i<items_.size(); ++i) {
130 // clear converted strings only if the corresponding argument is not bound :
131 if( bound_.size()==0 || !bound_[ items_[i].argN_ ] ) items_[i].res_.resize(0);
133 cur_arg_=0; dumped_=false;
134 // maybe first arg is bound:
135 if(bound_.size() != 0) {
136 while(cur_arg_ < num_args_ && bound_[cur_arg_] )
142 template< class Ch, class Tr>
143 basic_format<Ch,Tr>& basic_format<Ch,Tr>:: clear_binds() {
144 // remove all binds, then clear()
150 template< class Ch, class Tr>
151 basic_format<Ch,Tr>& basic_format<Ch,Tr>:: clear_bind(int argN) {
152 // remove the bind of ONE argument then clear()
154 if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] ) {
155 if( exceptions() & io::out_of_range_bit )
156 boost::throw_exception(io::out_of_range()); // arg not in range.
159 bound_[argN-1]=false;
166 template< class Ch, class Tr>
167 std::basic_string<Ch,Tr> basic_format<Ch,Tr>:: str() const {
171 if( cur_arg_ < num_args_)
172 if( exceptions() & io::too_few_args_bit )
173 boost::throw_exception(io::too_few_args()); // not enough variables supplied
178 for(i=0; i < items_.size(); ++i) {
179 const format_item_t& item = items_[i];
181 if( item.argN_ == format_item_t::argN_tabulation) {
182 BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
183 std::streamsize n = item.fmtstate_.width_ - res.size();
185 res.append( n, item.fmtstate_.fill_ );
187 res += item.appendix_;
191 template< class Ch, class Tr>
192 typename basic_format<Ch, Tr>::size_type basic_format<Ch,Tr>::
194 std::streamsize sz = prefix_.size();
196 for(i=0; i < items_.size(); ++i) {
197 const format_item_t& item = items_[i];
198 sz += item.res_.size();
199 if( item.argN_ == format_item_t::argN_tabulation)
200 sz = std::max(sz, item.fmtstate_.width_);
201 sz += + item.appendix_.size();
203 return static_cast<size_type> (sz);
209 template<class Ch, class Tr, class T>
210 basic_format<Ch, Tr>& bind_arg_body( basic_format<Ch, Tr>& self,
211 int argN, const T& val) {
212 // bind one argument to a fixed value
213 // this is persistent over clear() calls, thus also over str() and <<
215 self.clear(); // needed because we will modify cur_arg_
216 if(argN<1 || argN > self.num_args_) {
217 if( self.exceptions() & io::out_of_range_bit )
218 boost::throw_exception(io::out_of_range()); // arg not in range.
221 if(self.bound_.size()==0)
222 self.bound_.assign(self.num_args_,false);
224 BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
225 int o_cur_arg = self.cur_arg_;
226 self.cur_arg_ = argN-1; // arrays begin at 0
228 self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
229 self.operator%(val); // put val at the right place, because cur_arg is set
232 // Now re-position cur_arg before leaving :
233 self.cur_arg_ = o_cur_arg;
234 self.bound_[argN-1]=true;
235 if(self.cur_arg_ == argN-1 ) {
236 // hum, now this arg is bound, so move to next free arg
237 while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_])
240 // In any case, we either have all args, or are on a non-binded arg :
241 BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
245 template<class Ch, class Tr, class T>
246 basic_format<Ch, Tr>& modify_item_body( basic_format<Ch, Tr>& self,
247 int itemN, T manipulator) {
248 // applies a manipulator to the format_item describing a given directive.
249 // this is a permanent change, clear or reset won't cancel that.
250 if(itemN<1 || itemN > static_cast<signed int>(self.items_.size() )) {
251 if( self.exceptions() & io::out_of_range_bit )
252 boost::throw_exception(io::out_of_range()); // item not in range.
255 self.items_[itemN-1].fmtstate_. template apply_manip<T> ( manipulator );
259 } // namespace detail
265 #endif // BOOST_FORMAT_IMPLEMENTATION_HPP