format_class.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // ----------------------------------------------------------------------------
  2. // format_class.hpp : class interface
  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_CLASS_HPP
  10. #define BOOST_FORMAT_CLASS_HPP
  11. #include <vector>
  12. #include <string>
  13. #include <boost/optional.hpp> // to store locale when needed
  14. #include <boost/format/format_fwd.hpp>
  15. #include <boost/format/internals_fwd.hpp>
  16. #include <boost/format/internals.hpp>
  17. #include <boost/format/alt_sstream.hpp>
  18. namespace boost {
  19. template<class Ch, class Tr, class Alloc>
  20. class basic_format
  21. {
  22. typedef typename io::CompatTraits<Tr>::compatible_type compat_traits;
  23. public:
  24. typedef Ch CharT; // borland fails in operator% if we use Ch and Tr directly
  25. typedef std::basic_string<Ch, Tr, Alloc> string_type;
  26. typedef typename string_type::size_type size_type;
  27. typedef io::detail::format_item<Ch, Tr, Alloc> format_item_t;
  28. typedef io::basic_altstringbuf<Ch, Tr, Alloc> internal_streambuf_t;
  29. explicit basic_format(const Ch* str=NULL);
  30. explicit basic_format(const string_type& s);
  31. basic_format(const basic_format& x);
  32. basic_format& operator= (const basic_format& x);
  33. void swap(basic_format& x);
  34. #if !defined(BOOST_NO_STD_LOCALE)
  35. explicit basic_format(const Ch* str, const std::locale & loc);
  36. explicit basic_format(const string_type& s, const std::locale & loc);
  37. #endif
  38. io::detail::locale_t getloc() const;
  39. basic_format& clear(); // empty all converted string buffers (except bound items)
  40. basic_format& clear_binds(); // unbind all bound items, and call clear()
  41. basic_format& parse(const string_type&); // resets buffers and parse a new format string
  42. // ** formatted result ** //
  43. size_type size() const; // sum of the current string pieces sizes
  44. string_type str() const; // final string
  45. // ** arguments passing ** //
  46. template<class T>
  47. basic_format& operator%(const T& x)
  48. { return io::detail::feed<CharT, Tr, Alloc, const T&>(*this,x); }
  49. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  50. template<class T> basic_format& operator%(T& x)
  51. { return io::detail::feed<CharT, Tr, Alloc, T&>(*this,x); }
  52. #endif
  53. #if defined(__GNUC__)
  54. // GCC can't handle anonymous enums without some help
  55. // ** arguments passing ** //
  56. basic_format& operator%(const int& x)
  57. { return io::detail::feed<CharT, Tr, Alloc, const int&>(*this,x); }
  58. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  59. basic_format& operator%(int& x)
  60. { return io::detail::feed<CharT, Tr, Alloc, int&>(*this,x); }
  61. #endif
  62. #endif
  63. // The total number of arguments expected to be passed to the format objectt
  64. int expected_args() const
  65. { return num_args_; }
  66. // The number of arguments currently bound (see bind_arg(..) )
  67. int bound_args() const;
  68. // The number of arguments currently fed to the format object
  69. int fed_args() const;
  70. // The index (1-based) of the current argument (i.e. next to be formatted)
  71. int cur_arg() const;
  72. // The number of arguments still required to be fed
  73. int remaining_args() const; // same as expected_args() - bound_args() - fed_args()
  74. // ** object modifying **//
  75. template<class T>
  76. basic_format& bind_arg(int argN, const T& val)
  77. { return io::detail::bind_arg_body(*this, argN, val); }
  78. basic_format& clear_bind(int argN);
  79. template<class T>
  80. basic_format& modify_item(int itemN, T manipulator)
  81. { return io::detail::modify_item_body<Ch,Tr, Alloc, T> (*this, itemN, manipulator);}
  82. // Choosing which errors will throw exceptions :
  83. unsigned char exceptions() const;
  84. unsigned char exceptions(unsigned char newexcept);
  85. #if !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) \
  86. && !BOOST_WORKAROUND(__BORLANDC__, <= 0x570) \
  87. && !BOOST_WORKAROUND( _CRAYC, != 0) \
  88. && !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
  89. // use friend templates and private members only if supported
  90. #ifndef BOOST_NO_TEMPLATE_STD_STREAM
  91. template<class Ch2, class Tr2, class Alloc2>
  92. friend std::basic_ostream<Ch2, Tr2> &
  93. operator<<( std::basic_ostream<Ch2, Tr2> & ,
  94. const basic_format<Ch2, Tr2, Alloc2>& );
  95. #else
  96. template<class Ch2, class Tr2, class Alloc2>
  97. friend std::ostream &
  98. operator<<( std::ostream & ,
  99. const basic_format<Ch2, Tr2, Alloc2>& );
  100. #endif
  101. template<class Ch2, class Tr2, class Alloc2, class T>
  102. friend basic_format<Ch2, Tr2, Alloc2>&
  103. io::detail::feed (basic_format<Ch2, Tr2, Alloc2>&, T);
  104. template<class Ch2, class Tr2, class Alloc2, class T> friend
  105. void io::detail::distribute (basic_format<Ch2, Tr2, Alloc2>&, T);
  106. template<class Ch2, class Tr2, class Alloc2, class T> friend
  107. basic_format<Ch2, Tr2, Alloc2>&
  108. io::detail::modify_item_body (basic_format<Ch2, Tr2, Alloc2>&, int, T);
  109. template<class Ch2, class Tr2, class Alloc2, class T> friend
  110. basic_format<Ch2, Tr2, Alloc2>&
  111. io::detail::bind_arg_body (basic_format<Ch2, Tr2, Alloc2>&, int, const T&);
  112. private:
  113. #endif
  114. typedef io::detail::stream_format_state<Ch, Tr> stream_format_state;
  115. // flag bits, used for style_
  116. enum style_values { ordered = 1, // set only if all directives are positional
  117. special_needs = 4 };
  118. void make_or_reuse_data(std::size_t nbitems);// used for (re-)initialisation
  119. // member data --------------------------------------------//
  120. std::vector<format_item_t> items_; // each '%..' directive leads to a format_item
  121. std::vector<bool> bound_; // stores which arguments were bound. size() == 0 || num_args
  122. int style_; // style of format-string : positional or not, etc
  123. int cur_arg_; // keep track of wich argument is current
  124. int num_args_; // number of expected arguments
  125. mutable bool dumped_; // true only after call to str() or <<
  126. string_type prefix_; // piece of string to insert before first item
  127. unsigned char exceptions_;
  128. internal_streambuf_t buf_; // the internal stream buffer.
  129. boost::optional<io::detail::locale_t> loc_;
  130. }; // class basic_format
  131. } // namespace boost
  132. #endif // BOOST_FORMAT_CLASS_HPP