format_class.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. template<class T>
  54. basic_format& operator%(volatile const T& x)
  55. { /* make a non-volatile copy */ const T v(x);
  56. /* pass the copy along */ return io::detail::feed<CharT, Tr, Alloc, const T&>(*this, v); }
  57. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  58. template<class T>
  59. basic_format& operator%(volatile T& x)
  60. { /* make a non-volatile copy */ T v(x);
  61. /* pass the copy along */ return io::detail::feed<CharT, Tr, Alloc, T&>(*this, v); }
  62. #endif
  63. #if defined(__GNUC__)
  64. // GCC can't handle anonymous enums without some help
  65. // ** arguments passing ** //
  66. basic_format& operator%(const int& x)
  67. { return io::detail::feed<CharT, Tr, Alloc, const int&>(*this,x); }
  68. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  69. basic_format& operator%(int& x)
  70. { return io::detail::feed<CharT, Tr, Alloc, int&>(*this,x); }
  71. #endif
  72. #endif
  73. // The total number of arguments expected to be passed to the format objectt
  74. int expected_args() const
  75. { return num_args_; }
  76. // The number of arguments currently bound (see bind_arg(..) )
  77. int bound_args() const;
  78. // The number of arguments currently fed to the format object
  79. int fed_args() const;
  80. // The index (1-based) of the current argument (i.e. next to be formatted)
  81. int cur_arg() const;
  82. // The number of arguments still required to be fed
  83. int remaining_args() const; // same as expected_args() - bound_args() - fed_args()
  84. // ** object modifying **//
  85. template<class T>
  86. basic_format& bind_arg(int argN, const T& val)
  87. { return io::detail::bind_arg_body(*this, argN, val); }
  88. basic_format& clear_bind(int argN);
  89. template<class T>
  90. basic_format& modify_item(int itemN, T manipulator)
  91. { return io::detail::modify_item_body<Ch,Tr, Alloc, T> (*this, itemN, manipulator);}
  92. // Choosing which errors will throw exceptions :
  93. unsigned char exceptions() const;
  94. unsigned char exceptions(unsigned char newexcept);
  95. #if !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) \
  96. && !BOOST_WORKAROUND(BOOST_BORLANDC, <= 0x570) \
  97. && !BOOST_WORKAROUND( _CRAYC, != 0) \
  98. && !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
  99. // use friend templates and private members only if supported
  100. #ifndef BOOST_NO_TEMPLATE_STD_STREAM
  101. template<class Ch2, class Tr2, class Alloc2>
  102. friend std::basic_ostream<Ch2, Tr2> &
  103. operator<<( std::basic_ostream<Ch2, Tr2> & ,
  104. const basic_format<Ch2, Tr2, Alloc2>& );
  105. #else
  106. template<class Ch2, class Tr2, class Alloc2>
  107. friend std::ostream &
  108. operator<<( std::ostream & ,
  109. const basic_format<Ch2, Tr2, Alloc2>& );
  110. #endif
  111. template<class Ch2, class Tr2, class Alloc2, class T>
  112. friend basic_format<Ch2, Tr2, Alloc2>&
  113. io::detail::feed_impl (basic_format<Ch2, Tr2, Alloc2>&, T);
  114. template<class Ch2, class Tr2, class Alloc2, class T> friend
  115. void io::detail::distribute (basic_format<Ch2, Tr2, Alloc2>&, T);
  116. template<class Ch2, class Tr2, class Alloc2, class T> friend
  117. basic_format<Ch2, Tr2, Alloc2>&
  118. io::detail::modify_item_body (basic_format<Ch2, Tr2, Alloc2>&, int, T);
  119. template<class Ch2, class Tr2, class Alloc2, class T> friend
  120. basic_format<Ch2, Tr2, Alloc2>&
  121. io::detail::bind_arg_body (basic_format<Ch2, Tr2, Alloc2>&, int, const T&);
  122. private:
  123. #endif
  124. typedef io::detail::stream_format_state<Ch, Tr> stream_format_state;
  125. // flag bits, used for style_
  126. enum style_values { ordered = 1, // set only if all directives are positional
  127. special_needs = 4 };
  128. void make_or_reuse_data(std::size_t nbitems);// used for (re-)initialisation
  129. // member data --------------------------------------------//
  130. std::vector<format_item_t> items_; // each '%..' directive leads to a format_item
  131. std::vector<bool> bound_; // stores which arguments were bound. size() == 0 || num_args
  132. int style_; // style of format-string : positional or not, etc
  133. int cur_arg_; // keep track of wich argument is current
  134. int num_args_; // number of expected arguments
  135. mutable bool dumped_; // true only after call to str() or <<
  136. string_type prefix_; // piece of string to insert before first item
  137. unsigned char exceptions_;
  138. internal_streambuf_t buf_; // the internal stream buffer.
  139. boost::optional<io::detail::locale_t> loc_;
  140. }; // class basic_format
  141. } // namespace boost
  142. #endif // BOOST_FORMAT_CLASS_HPP