/src/contrib/boost/format/format_implementation.hpp

http://pythonocc.googlecode.com/ · C++ Header · 288 lines · 227 code · 35 blank · 26 comment · 52 complexity · e82317cedd43f8b42224b0133667b54b MD5 · raw file

  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_(false),
  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. typename basic_format<Ch, Tr, Alloc>::string_type
  152. basic_format<Ch,Tr, Alloc>::
  153. str () const {
  154. if(items_.size()==0)
  155. return prefix_;
  156. if( cur_arg_ < num_args_)
  157. if( exceptions() & io::too_few_args_bit )
  158. // not enough variables supplied
  159. boost::throw_exception(io::too_few_args(cur_arg_, num_args_));
  160. unsigned long i;
  161. string_type res;
  162. res.reserve(size());
  163. res += prefix_;
  164. for(i=0; i < items_.size(); ++i) {
  165. const format_item_t& item = items_[i];
  166. res += item.res_;
  167. if( item.argN_ == format_item_t::argN_tabulation) {
  168. BOOST_ASSERT( item.pad_scheme_ & format_item_t::tabulation);
  169. if( static_cast<size_type>(item.fmtstate_.width_) > res.size() )
  170. res.append( static_cast<size_type>(item.fmtstate_.width_) - res.size(),
  171. item.fmtstate_.fill_ );
  172. }
  173. res += item.appendix_;
  174. }
  175. dumped_=true;
  176. return res;
  177. }
  178. template< class Ch, class Tr, class Alloc>
  179. typename std::basic_string<Ch, Tr, Alloc>::size_type basic_format<Ch,Tr, Alloc>::
  180. size () const {
  181. #ifdef BOOST_MSVC
  182. // If std::min<unsigned> or std::max<unsigned> are already instantiated
  183. // at this point then we get a blizzard of warning messages when we call
  184. // those templates with std::size_t as arguments. Weird and very annoyning...
  185. #pragma warning(push)
  186. #pragma warning(disable:4267)
  187. #endif
  188. BOOST_USING_STD_MAX();
  189. size_type sz = prefix_.size();
  190. unsigned long i;
  191. for(i=0; i < items_.size(); ++i) {
  192. const format_item_t& item = items_[i];
  193. sz += item.res_.size();
  194. if( item.argN_ == format_item_t::argN_tabulation)
  195. sz = max BOOST_PREVENT_MACRO_SUBSTITUTION (sz,
  196. static_cast<size_type>(item.fmtstate_.width_) );
  197. sz += item.appendix_.size();
  198. }
  199. return sz;
  200. #ifdef BOOST_MSVC
  201. #pragma warning(pop)
  202. #endif
  203. }
  204. namespace io {
  205. namespace detail {
  206. template<class Ch, class Tr, class Alloc, class T>
  207. basic_format<Ch, Tr, Alloc>&
  208. bind_arg_body (basic_format<Ch, Tr, Alloc>& self, int argN, const T& val) {
  209. // bind one argument to a fixed value
  210. // this is persistent over clear() calls, thus also over str() and <<
  211. if(self.dumped_)
  212. self.clear(); // needed because we will modify cur_arg_
  213. if(argN<1 || argN > self.num_args_) {
  214. if( self.exceptions() & io::out_of_range_bit )
  215. boost::throw_exception(io::out_of_range(argN, 1, self.num_args_+1 ) );
  216. else return self;
  217. }
  218. if(self.bound_.size()==0)
  219. self.bound_.assign(self.num_args_,false);
  220. else
  221. BOOST_ASSERT( self.num_args_ == static_cast<signed int>(self.bound_.size()) );
  222. int o_cur_arg = self.cur_arg_;
  223. self.cur_arg_ = argN-1; // arrays begin at 0
  224. self.bound_[self.cur_arg_]=false; // if already set, we unset and re-sets..
  225. self.operator%(val); // put val at the right place, because cur_arg is set
  226. // Now re-position cur_arg before leaving :
  227. self.cur_arg_ = o_cur_arg;
  228. self.bound_[argN-1]=true;
  229. if(self.cur_arg_ == argN-1 ) {
  230. // hum, now this arg is bound, so move to next free arg
  231. while(self.cur_arg_ < self.num_args_ && self.bound_[self.cur_arg_])
  232. ++self.cur_arg_;
  233. }
  234. // In any case, we either have all args, or are on a non-binded arg :
  235. BOOST_ASSERT( self.cur_arg_ >= self.num_args_ || ! self.bound_[self.cur_arg_]);
  236. return self;
  237. }
  238. template<class Ch, class Tr, class Alloc, class T> basic_format<Ch, Tr, Alloc>&
  239. modify_item_body (basic_format<Ch, Tr, Alloc>& self, int itemN, T manipulator) {
  240. // applies a manipulator to the format_item describing a given directive.
  241. // this is a permanent change, clear or reset won't cancel that.
  242. if(itemN<1 || itemN > static_cast<signed int>(self.items_.size() )) {
  243. if( self.exceptions() & io::out_of_range_bit )
  244. boost::throw_exception(io::out_of_range(itemN, 1, static_cast<int>(self.items_.size()) ));
  245. else return self;
  246. }
  247. self.items_[itemN-1].fmtstate_. template apply_manip<T> ( manipulator );
  248. return self;
  249. }
  250. } // namespace detail
  251. } // namespace io
  252. } // namespace boost
  253. #endif // BOOST_FORMAT_IMPLEMENTATION_HPP