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