format_implementation.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // ----------------------------------------------------------------------------
  2. // format_implementation.hpp Implementation of the basic_format class
  3. // ----------------------------------------------------------------------------
  4. // Copyright Samuel Krempp 2003. Use, modification, and distribution are
  5. // subject to the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // See http://www.boost.org/libs/format for library home page
  8. // ----------------------------------------------------------------------------
  9. #ifndef BOOST_FORMAT_IMPLEMENTATION_HPP
  10. #define BOOST_FORMAT_IMPLEMENTATION_HPP
  11. #include <boost/config.hpp>
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/format/format_class.hpp>
  15. #include <algorithm> // std::swap
  16. namespace boost {
  17. // --- basic_format implementation -----------------------------------------//
  18. template< class Ch, class Tr, class Alloc>
  19. basic_format<Ch, Tr, Alloc>:: basic_format(const Ch* s)
  20. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  21. exceptions_(io::all_error_bits)
  22. {
  23. if( s)
  24. parse( s );
  25. }
  26. #if !defined(BOOST_NO_STD_LOCALE)
  27. template< class Ch, class Tr, class Alloc>
  28. basic_format<Ch, Tr, Alloc>:: basic_format(const Ch* s, const std::locale & loc)
  29. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  30. exceptions_(io::all_error_bits), loc_(loc)
  31. {
  32. if(s) parse( s );
  33. }
  34. template< class Ch, class Tr, class Alloc>
  35. basic_format<Ch, Tr, Alloc>:: basic_format(const string_type& s, const std::locale & loc)
  36. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  37. exceptions_(io::all_error_bits), loc_(loc)
  38. {
  39. parse(s);
  40. }
  41. #endif // ! BOOST_NO_STD_LOCALE
  42. template< class Ch, class Tr, class Alloc>
  43. io::detail::locale_t basic_format<Ch, Tr, Alloc>::
  44. getloc() const {
  45. return loc_ ? loc_.get() : io::detail::locale_t();
  46. }
  47. template< class Ch, class Tr, class Alloc>
  48. basic_format<Ch, Tr, Alloc>:: basic_format(const string_type& s)
  49. : style_(0), cur_arg_(0), num_args_(0), dumped_(false),
  50. exceptions_(io::all_error_bits)
  51. {
  52. parse(s);
  53. }
  54. template< class Ch, class Tr, class Alloc> // just don't copy the buf_ member
  55. basic_format<Ch, Tr, Alloc>:: basic_format(const basic_format& x)
  56. : items_(x.items_), bound_(x.bound_), style_(x.style_),
  57. cur_arg_(x.cur_arg_), num_args_(x.num_args_), dumped_(x.dumped_),
  58. prefix_(x.prefix_), exceptions_(x.exceptions_), loc_(x.loc_)
  59. {
  60. }
  61. template< class Ch, class Tr, class Alloc> // just don't copy the buf_ member
  62. basic_format<Ch, Tr, Alloc>& basic_format<Ch, Tr, Alloc>::
  63. operator= (const basic_format& x) {
  64. if(this == &x)
  65. return *this;
  66. (basic_format<Ch, Tr, Alloc>(x)).swap(*this);
  67. return *this;
  68. }
  69. template< class Ch, class Tr, class Alloc>
  70. void basic_format<Ch, Tr, Alloc>::
  71. swap (basic_format & x) {
  72. std::swap(exceptions_, x.exceptions_);
  73. std::swap(style_, x.style_);
  74. std::swap(cur_arg_, x.cur_arg_);
  75. std::swap(num_args_, x.num_args_);
  76. std::swap(dumped_, x.dumped_);
  77. items_.swap(x.items_);
  78. prefix_.swap(x.prefix_);
  79. bound_.swap(x.bound_);
  80. }
  81. template< class Ch, class Tr, class Alloc>
  82. unsigned char basic_format<Ch,Tr, Alloc>:: exceptions() const {
  83. return exceptions_;
  84. }
  85. template< class Ch, class Tr, class Alloc>
  86. unsigned char basic_format<Ch,Tr, Alloc>:: exceptions(unsigned char newexcept) {
  87. unsigned char swp = exceptions_;
  88. exceptions_ = newexcept;
  89. return swp;
  90. }
  91. template<class Ch, class Tr, class Alloc>
  92. void basic_format<Ch, Tr, Alloc>::
  93. make_or_reuse_data (std::size_t nbitems) {
  94. #if !defined(BOOST_NO_STD_LOCALE)
  95. Ch fill = ( BOOST_USE_FACET(std::ctype<Ch>, getloc()) ). widen(' ');
  96. #else
  97. Ch fill = ' ';
  98. #endif
  99. if(items_.size() == 0)
  100. items_.assign( nbitems, format_item_t(fill) );
  101. else {
  102. if(nbitems>items_.size())
  103. items_.resize(nbitems, format_item_t(fill));
  104. bound_.resize(0);
  105. for(std::size_t i=0; i < nbitems; ++i)
  106. items_[i].reset(fill); // strings are resized, instead of reallocated
  107. }
  108. prefix_.resize(0);
  109. }
  110. template< class Ch, class Tr, class Alloc>
  111. basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
  112. clear () {
  113. // empty the string buffers (except bound arguments)
  114. // and make the format object ready for formatting a new set of arguments
  115. BOOST_ASSERT( bound_.size()==0 || num_args_ == static_cast<int>(bound_.size()) );
  116. for(unsigned long i=0; i<items_.size(); ++i) {
  117. // clear converted strings only if the corresponding argument is not bound :
  118. if( bound_.size()==0 || items_[i].argN_<0 || !bound_[ items_[i].argN_ ] )
  119. items_[i].res_.resize(0);
  120. }
  121. cur_arg_=0; dumped_=false;
  122. // maybe first arg is bound:
  123. if(bound_.size() != 0) {
  124. for(; cur_arg_ < num_args_ && bound_[cur_arg_]; ++cur_arg_)
  125. {}
  126. }
  127. return *this;
  128. }
  129. template< class Ch, class Tr, class Alloc>
  130. basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
  131. clear_binds () {
  132. // remove all binds, then clear()
  133. bound_.resize(0);
  134. clear();
  135. return *this;
  136. }
  137. template< class Ch, class Tr, class Alloc>
  138. basic_format<Ch,Tr, Alloc>& basic_format<Ch,Tr, Alloc>::
  139. clear_bind (int argN) {
  140. // remove the bind of ONE argument then clear()
  141. if(argN<1 || argN > num_args_ || bound_.size()==0 || !bound_[argN-1] ) {
  142. if( exceptions() & io::out_of_range_bit)
  143. boost::throw_exception(io::out_of_range(argN, 1, num_args_+1 ) );
  144. else return *this;
  145. }
  146. bound_[argN-1]=false;
  147. clear();
  148. return *this;
  149. }
  150. template< class Ch, class Tr, class Alloc>
  151. int basic_format<Ch,Tr, Alloc>::
  152. bound_args() const {
  153. if(bound_.size()==0)
  154. return 0;
  155. int n=0;
  156. for(int i=0; i<num_args_ ; ++i)
  157. if(bound_[i])
  158. ++n;
  159. return n;
  160. }
  161. template< class Ch, class Tr, class Alloc>
  162. int basic_format<Ch,Tr, Alloc>::
  163. fed_args() const {
  164. if(bound_.size()==0)
  165. return cur_arg_;
  166. int n=0;
  167. for(int i=0; i<cur_arg_ ; ++i)
  168. if(!bound_[i])
  169. ++n;
  170. return n;
  171. }
  172. template< class Ch, class Tr, class Alloc>
  173. int basic_format<Ch,Tr, Alloc>::
  174. cur_arg() const {
  175. return cur_arg_+1; }
  176. template< class Ch, class Tr, class Alloc>
  177. int basic_format<Ch,Tr, Alloc>::
  178. remaining_args() const {
  179. if(bound_.size()==0)
  180. return num_args_-cur_arg_;
  181. int n=0;
  182. for(int i=cur_arg_; i<num_args_ ; ++i)
  183. if(!bound_[i])
  184. ++n;
  185. return n;
  186. }
  187. template< class Ch, class Tr, class Alloc>
  188. typename basic_format<Ch, Tr, Alloc>::string_type
  189. basic_format<Ch,Tr, Alloc>::
  190. str () const {
  191. if(items_.size()==0)
  192. return prefix_;
  193. if( cur_arg_ < num_args_)
  194. if( exceptions() & io::too_few_args_bit )
  195. // not enough variables supplied
  196. boost::throw_exception(io::too_few_args(cur_arg_, num_args_));
  197. unsigned long i;
  198. string_type res;
  199. res.reserve(size());
  200. res += prefix_;
  201. for(i=0; i < items_.size(); ++i) {
  202. const format_item_t& item = items_[i];
  203. res += item.res_;
  204. if( item.argN_ == format_item_t::argN_tabulation) {
  205. BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
  206. if( static_cast<size_type>(item.fmtstate_.width_) > res.size() )
  207. res.append( static_cast<size_type>(item.fmtstate_.width_) - res.size(),
  208. item.fmtstate_.fill_ );
  209. }
  210. res += item.appendix_;
  211. }
  212. dumped_=true;
  213. return res;
  214. }
  215. template< class Ch, class Tr, class Alloc>
  216. typename std::basic_string<Ch, Tr, Alloc>::size_type basic_format<Ch,Tr, Alloc>::
  217. size () const {
  218. #ifdef BOOST_MSVC
  219. // If std::min<unsigned> or std::max<unsigned> are already instantiated
  220. // at this point then we get a blizzard of warning messages when we call
  221. // those templates with std::size_t as arguments. Weird and very annoyning...
  222. #pragma warning(push)
  223. #pragma warning(disable:4267)
  224. #endif
  225. BOOST_USING_STD_MAX();
  226. size_type sz = prefix_.size();
  227. unsigned long i;
  228. for(i=0; i < items_.size(); ++i) {
  229. const format_item_t& item = items_[i];
  230. sz += item.res_.size();
  231. if( item.argN_ == format_item_t::argN_tabulation)
  232. sz = max BOOST_PREVENT_MACRO_SUBSTITUTION (sz,
  233. static_cast<size_type>(item.fmtstate_.width_) );
  234. sz += item.appendix_.size();
  235. }
  236. return sz;
  237. #ifdef BOOST_MSVC
  238. #pragma warning(pop)
  239. #endif
  240. }
  241. namespace io {
  242. namespace detail {
  243. template<class Ch, class Tr, class Alloc, class T>
  244. basic_format<Ch, Tr, Alloc>&
  245. bind_arg_body (basic_format<Ch, Tr, Alloc>& self, int argN, const T& val) {
  246. // bind one argument to a fixed value
  247. // this is persistent over clear() calls, thus also over str() and <<
  248. if(self.dumped_)
  249. self.clear(); // needed because we will modify cur_arg_
  250. if(argN<1 || argN > self.num_args_) {
  251. if( self.exceptions() & io::out_of_range_bit )
  252. boost::throw_exception(io::out_of_range(argN, 1, self.num_args_+1 ) );
  253. else return self;
  254. }
  255. if(self.bound_.size()==0)
  256. self.bound_.assign(self.num_args_,false);
  257. else
  258. BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
  259. int o_cur_arg = self.cur_arg_;
  260. self.cur_arg_ = argN-1; // arrays begin at 0
  261. self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
  262. self.operator%(val); // put val at the right place, because cur_arg is set
  263. // Now re-position cur_arg before leaving :
  264. self.cur_arg_ = o_cur_arg;
  265. self.bound_[argN-1]=true;
  266. if(self.cur_arg_ == argN-1 ) {
  267. // hum, now this arg is bound, so move to next free arg
  268. while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_])
  269. ++self.cur_arg_;
  270. }
  271. // In any case, we either have all args, or are on an unbound arg :
  272. BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
  273. return self;
  274. }
  275. template<class Ch, class Tr, class Alloc, class T> basic_format<Ch, Tr, Alloc>&
  276. modify_item_body (basic_format<Ch, Tr, Alloc>& self, int itemN, T manipulator) {
  277. // applies a manipulator to the format_item describing a given directive.
  278. // this is a permanent change, clear or reset won't cancel that.
  279. if(itemN<1 || itemN > static_cast<signed int>(self.items_.size() )) {
  280. if( self.exceptions() & io::out_of_range_bit )
  281. boost::throw_exception(io::out_of_range(itemN, 1, static_cast<int>(self.items_.size()) ));
  282. else return self;
  283. }
  284. self.items_[itemN-1].fmtstate_. template apply_manip<T> ( manipulator );
  285. return self;
  286. }
  287. } // namespace detail
  288. } // namespace io
  289. } // namespace boost
  290. #endif // BOOST_FORMAT_IMPLEMENTATION_HPP