format_class.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // format_class.hpp : class interface
  14. // ------------------------------------------------------------------------------
  15. #ifndef BOOST_FORMAT_CLASS_HPP
  16. #define BOOST_FORMAT_CLASS_HPP
  17. #include <vector>
  18. #include <string>
  19. #include <boost/format/format_fwd.hpp>
  20. #include <boost/format/internals_fwd.hpp>
  21. #include <boost/format/internals.hpp>
  22. namespace boost {
  23. class basic_format
  24. {
  25. public:
  26. typedef std::string string_t;
  27. typedef BOOST_IO_STD ostringstream internal_stream_t;
  28. private:
  29. typedef BOOST_IO_STD ostream stream_t;
  30. typedef io::detail::stream_format_state stream_format_state;
  31. typedef io::detail::format_item format_item_t;
  32. public:
  33. basic_format(const char* str);
  34. basic_format(const string_t& s);
  35. #ifndef BOOST_NO_STD_LOCALE
  36. basic_format(const char* str, const std::locale & loc);
  37. basic_format(const string_t& s, const std::locale & loc);
  38. #endif // no locale
  39. basic_format(const basic_format& x);
  40. basic_format& operator= (const basic_format& x);
  41. basic_format& clear(); // empty the string buffers (except bound arguments, see clear_binds() )
  42. // pass arguments through those operators :
  43. template<class T> basic_format& operator%(const T& x)
  44. {
  45. return io::detail::feed<const T&>(*this,x);
  46. }
  47. #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
  48. template<class T> basic_format& operator%(T& x)
  49. {
  50. return io::detail::feed<T&>(*this,x);
  51. }
  52. #endif
  53. // system for binding arguments :
  54. template<class T>
  55. basic_format& bind_arg(int argN, const T& val)
  56. {
  57. return io::detail::bind_arg_body(*this, argN, val);
  58. }
  59. basic_format& clear_bind(int argN);
  60. basic_format& clear_binds();
  61. // modify the params of a directive, by applying a manipulator :
  62. template<class T>
  63. basic_format& modify_item(int itemN, const T& manipulator)
  64. {
  65. return io::detail::modify_item_body(*this, itemN, manipulator) ;
  66. }
  67. // Choosing which errors will throw exceptions :
  68. unsigned char exceptions() const;
  69. unsigned char exceptions(unsigned char newexcept);
  70. // final output
  71. string_t str() const;
  72. friend BOOST_IO_STD ostream&
  73. operator<< ( BOOST_IO_STD ostream& , const basic_format& );
  74. template<class T> friend basic_format&
  75. io::detail::feed(basic_format&, T);
  76. template<class T> friend
  77. void io::detail::distribute(basic_format&, T);
  78. template<class T> friend
  79. basic_format& io::detail::modify_item_body(basic_format&, int, const T&);
  80. template<class T> friend
  81. basic_format& io::detail::bind_arg_body(basic_format&, int, const T&);
  82. // make the members private only if the friend templates are supported
  83. private:
  84. // flag bits, used for style_
  85. enum style_values { ordered = 1, // set only if all directives are positional directives
  86. special_needs = 4 };
  87. // parse the format string :
  88. void parse(const string_t&);
  89. int style_; // style of format-string : positional or not, etc
  90. int cur_arg_; // keep track of wich argument will come
  91. int num_args_; // number of expected arguments
  92. mutable bool dumped_; // true only after call to str() or <<
  93. std::vector<format_item_t> items_; // vector of directives (aka items)
  94. string_t prefix_; // piece of string to insert before first item
  95. std::vector<bool> bound_; // stores which arguments were bound
  96. // size = num_args OR zero
  97. internal_stream_t oss_; // the internal stream.
  98. stream_format_state state0_; // reference state for oss_
  99. unsigned char exceptions_;
  100. }; // class basic_format
  101. } // namespace boost
  102. #endif // BOOST_FORMAT_CLASS_HPP