free_funcs.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // ----------------------------------------------------------------------------
  2. // free_funcs.hpp : implementation of the free functions of boost::format
  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_FUNCS_HPP
  10. #define BOOST_FORMAT_FUNCS_HPP
  11. #include <boost/format/format_class.hpp>
  12. #include <boost/throw_exception.hpp>
  13. namespace boost {
  14. template<class Ch, class Tr, class Alloc> inline
  15. std::basic_string<Ch, Tr, Alloc> str(const basic_format<Ch, Tr, Alloc>& f) {
  16. // adds up all pieces of strings and converted items, and return the formatted string
  17. return f.str();
  18. }
  19. namespace io {
  20. using ::boost::str; // keep compatibility with when it was defined in this N.S.
  21. } // - namespace io
  22. #ifndef BOOST_NO_TEMPLATE_STD_STREAM
  23. template<class Ch, class Tr, class Alloc>
  24. std::basic_ostream<Ch, Tr> &
  25. operator<<( std::basic_ostream<Ch, Tr> & os,
  26. const basic_format<Ch, Tr, Alloc>& f)
  27. #else
  28. template<class Ch, class Tr, class Alloc>
  29. std::ostream &
  30. operator<<( std::ostream & os,
  31. const basic_format<Ch, Tr, Alloc>& f)
  32. #endif
  33. // effect: "return os << str(f);" but we can do it faster
  34. {
  35. typedef boost::basic_format<Ch, Tr, Alloc> format_t;
  36. if(f.items_.size()==0)
  37. os << f.prefix_;
  38. else {
  39. if(f.cur_arg_ < f.num_args_)
  40. if( f.exceptions() & io::too_few_args_bit )
  41. // not enough variables supplied
  42. boost::throw_exception(io::too_few_args(f.cur_arg_, f.num_args_));
  43. if(f.style_ & format_t::special_needs)
  44. os << f.str();
  45. else {
  46. // else we dont have to count chars output, so we dump directly to os :
  47. os << f.prefix_;
  48. for(unsigned long i=0; i<f.items_.size(); ++i) {
  49. const typename format_t::format_item_t& item = f.items_[i];
  50. os << item.res_;
  51. os << item.appendix_;
  52. }
  53. }
  54. }
  55. f.dumped_=true;
  56. return os;
  57. }
  58. } // namespace boost
  59. #endif // BOOST_FORMAT_FUNCS_HPP