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