1 // ----------------------------------------------------------------------------
2 // format_class.hpp : class interface
3 // ----------------------------------------------------------------------------
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)
9 // See http://www.boost.org/libs/format for library home page
11 // ----------------------------------------------------------------------------
13 #ifndef BOOST_FORMAT_CLASS_HPP
14 #define BOOST_FORMAT_CLASS_HPP
20 #include <boost/optional.hpp> // to store locale when needed
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>
29 template<class Ch, class Tr, class Alloc>
32 typedef typename io::CompatTraits<Tr>::compatible_type compat_traits;
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;
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);
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);
51 io::detail::locale_t getloc() const;
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
57 // ** formatted result ** //
58 size_type size() const; // sum of the current string pieces sizes
59 string_type str() const; // final string
61 // ** arguments passing ** //
63 basic_format& operator%(const T& x)
64 { return io::detail::feed<CharT, Tr, Alloc, const T&>(*this,x); }
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); }
71 // ** object modifying **//
73 basic_format& bind_arg(int argN, const T& val)
74 { return io::detail::bind_arg_body(*this, argN, val); }
75 basic_format& clear_bind(int argN);
77 basic_format& modify_item(int itemN, T manipulator)
78 { return io::detail::modify_item_body<Ch,Tr, Alloc, T> (*this, itemN, manipulator);}
80 // Choosing which errors will throw exceptions :
81 unsigned char exceptions() const;
82 unsigned char exceptions(unsigned char newexcept);
84 #if !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) \
85 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x570) \
86 && !BOOST_WORKAROUND( _CRAYC, != 0) \
87 && !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
88 // 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>& );
96 template<class Ch2, class Tr2, class Alloc2>
98 operator<<( std::ostream & ,
99 const basic_format<Ch2, Tr2, Alloc2>& );
102 template<class Ch2, class Tr2, class Alloc2, class T>
103 friend basic_format<Ch2, Tr2, Alloc2>&
104 io::detail::feed (basic_format<Ch2, Tr2, Alloc2>&, T);
106 template<class Ch2, class Tr2, class Alloc2, class T> friend
107 void io::detail::distribute (basic_format<Ch2, Tr2, Alloc2>&, T);
109 template<class Ch2, class Tr2, class Alloc2, class T> friend
110 basic_format<Ch2, Tr2, Alloc2>&
111 io::detail::modify_item_body (basic_format<Ch2, Tr2, Alloc2>&, int, T);
113 template<class Ch2, class Tr2, class Alloc2, class T> friend
114 basic_format<Ch2, Tr2, Alloc2>&
115 io::detail::bind_arg_body (basic_format<Ch2, Tr2, Alloc2>&, int, const T&);
119 typedef io::detail::stream_format_state<Ch, Tr> stream_format_state;
120 // flag bits, used for style_
121 enum style_values { ordered = 1, // set only if all directives are positional
124 void make_or_reuse_data(std::size_t nbitems);// used for (re-)initialisation
126 // member data --------------------------------------------//
127 std::vector<format_item_t> items_; // each '%..' directive leads to a format_item
128 std::vector<bool> bound_; // stores which arguments were bound. size() == 0 || num_args
130 int style_; // style of format-string : positional or not, etc
131 int cur_arg_; // keep track of wich argument is current
132 int num_args_; // number of expected arguments
133 mutable bool dumped_; // true only after call to str() or <<
134 string_type prefix_; // piece of string to insert before first item
135 unsigned char exceptions_;
136 internal_streambuf_t buf_; // the internal stream buffer.
137 boost::optional<io::detail::locale_t> loc_;
138 }; // class basic_format
143 #endif // BOOST_FORMAT_CLASS_HPP