free_funcs.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // -*- C++ -*-
  2. // Boost general library 'format' ---------------------------
  3. // See http://www.boost.org for updates, documentation, and revision history.
  4. // (C) Samuel Krempp 2001
  5. // krempp@crans.ens-cachan.fr
  6. // Permission to copy, use, modify, sell and
  7. // distribute this software is granted provided this copyright notice appears
  8. // in all copies. This software is provided "as is" without express or implied
  9. // warranty, and with no claim as to its suitability for any purpose.
  10. // ideas taken from Rüdiger Loos's format class
  11. // and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing)
  12. // ------------------------------------------------------------------------------
  13. // free_funcs.hpp : implementation of the free functions declared in namespace format
  14. // ------------------------------------------------------------------------------
  15. #ifndef BOOST_FORMAT_FUNCS_HPP
  16. #define BOOST_FORMAT_FUNCS_HPP
  17. #include "boost/format.hpp"
  18. #include "boost/throw_exception.hpp"
  19. namespace boost {
  20. namespace io {
  21. inline
  22. std::string str(const basic_format& f)
  23. // adds up all pieces of strings and converted items, and return the formatted string
  24. {
  25. return f.str();
  26. }
  27. } // - namespace io
  28. BOOST_IO_STD ostream&
  29. operator<<( BOOST_IO_STD ostream& os,
  30. const boost::basic_format& f)
  31. // effect: "return os << str(f);" but we can try to do it faster
  32. {
  33. typedef boost::basic_format format_t;
  34. if(f.items_.size()==0)
  35. os << f.prefix_;
  36. else {
  37. if(f.cur_arg_ < f.num_args_)
  38. if( f.exceptions() & io::too_few_args_bit )
  39. boost::throw_exception(io::too_few_args()); // not enough variables have been supplied !
  40. if(f.style_ & format_t::special_needs)
  41. os << f.str();
  42. else {
  43. // else we dont have to count chars output, so we dump directly to os :
  44. os << f.prefix_;
  45. for(unsigned long i=0; i<f.items_.size(); ++i)
  46. {
  47. const format_t::format_item_t& item = f.items_[i];
  48. os << item.res_;
  49. os << item.appendix_;
  50. }
  51. }
  52. }
  53. f.dumped_=true;
  54. return os;
  55. }
  56. } // namespace boost
  57. #endif // BOOST_FORMAT_FUNCS_HPP