2 // Boost general library 'format' ---------------------------
3 // See http://www.boost.org for updates, documentation, and revision history.
5 // (C) Samuel Krempp 2001
6 // krempp@crans.ens-cachan.fr
7 // Permission to copy, use, modify, sell and
8 // distribute this software is granted provided this copyright notice appears
9 // in all copies. This software is provided "as is" without express or implied
10 // warranty, and with no claim as to its suitability for any purpose.
12 // ideas taken from RĂ¼diger Loos's format class
13 // and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing)
15 // ------------------------------------------------------------------------------
16 // format_class.hpp : class interface
17 // ------------------------------------------------------------------------------
19 #ifndef BOOST_FORMAT_CLASS_HPP
20 #define BOOST_FORMAT_CLASS_HPP
26 #include <boost/format/format_fwd.hpp>
27 #include <boost/format/internals_fwd.hpp>
28 #include <boost/format/internals.hpp>
29 #include <boost/format/outsstream.hpp>
33 template<class Ch, class Tr>
37 typedef Ch CharT; // borland fails if we use Ch and Tr directly
38 typedef Tr Traits; // in the body of the operator% template.
39 typedef std::basic_string<Ch, Tr> string_t;
40 typedef typename std::basic_string<Ch, Tr>::size_type size_type;
42 typedef io::basic_outsstream<Ch, Tr> internal_stream_t;
44 explicit basic_format(const Ch* str=NULL);
45 explicit basic_format(const string_t& s);
46 #ifndef BOOST_NO_STD_LOCALE
47 explicit basic_format(const Ch* str, const std::locale & loc);
48 explicit basic_format(const string_t& s, const std::locale & loc);
50 basic_format(const basic_format& x);
51 basic_format& operator= (const basic_format& x);
52 void swap(basic_format& x);
54 basic_format& clear(); // empty all converted string buffers (except bound items)
55 basic_format& clear_binds(); // unbind all bound items, and call clear()
56 basic_format& parse(const string_t&); // resets buffers and parse a new format string
58 // pass arguments through those operators :
60 basic_format& operator%(const T& x)
61 { return io::detail::feed<CharT, Traits, const T&>(*this,x); }
63 #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
64 template<class T> basic_format& operator%(T& x)
65 { return io::detail::feed<CharT, Traits, T&>(*this,x); }
67 // modifying a format object
69 basic_format& bind_arg(int argN, const T& val)
70 { return io::detail::bind_arg_body(*this, argN, val); }
71 basic_format& clear_bind(int argN);
73 basic_format& modify_item(int itemN, T manipulator)
74 { return io::detail::modify_item_body<Ch,Tr,T> (*this, itemN, manipulator);}
76 // Choosing which errors will throw exceptions :
77 unsigned char exceptions() const;
78 unsigned char exceptions(unsigned char newexcept);
81 size_type size() const; // sum of the current string pieces sizes
83 friend BOOST_IO_STD basic_ostream<Ch, Tr>&
84 #if BOOST_WORKAROUND( BOOST_MSVC, <= 1300)
85 operator<< (BOOST_IO_STD basic_ostream<Ch, Tr>& , const basic_format& );
87 operator<< <Ch, Tr> (BOOST_IO_STD basic_ostream<Ch, Tr>&, const basic_format&);
90 #if !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) \
91 && !BOOST_WORKAROUND(__BORLANDC__, <= 0x570) \
92 && !BOOST_WORKAROUND( _CRAYC, != 0)
93 // use friend templates and private members only if supported
94 template<class Ch2, class Tr2, class T> friend basic_format<Ch2, Tr2>&
95 io::detail::feed(basic_format<Ch2,Tr2>&, T);
96 template<class Ch2, class Tr2, class T> friend
97 void io::detail::distribute(basic_format<Ch2,Tr2>&, T);
98 template<class Ch2, class Tr2, class T> friend
99 basic_format<Ch2, Tr2>& io::detail::modify_item_body(basic_format<Ch2, Tr2>&, int, T);
100 template<class Ch2, class Tr2, class T> friend
101 basic_format<Ch2, Tr2>& io::detail::bind_arg_body(basic_format<Ch2, Tr2>&, int, const T&);
105 typedef BOOST_IO_STD basic_ostream<Ch, Tr> stream_t;
106 typedef io::detail::stream_format_state<Ch, Tr> stream_format_state;
107 typedef io::detail::format_item<Ch, Tr> format_item_t;
108 // flag bits, used for style_
109 enum style_values { ordered = 1, // set only if all directives are positional
112 void make_or_reuse_data(std::size_t nbitems);// used for (re-)initialisation
114 std::vector<format_item_t> items_; // each '%..' directive leads to a format_item
115 std::vector<bool> bound_; // stores which arguments were bound. size() == 0 || num_args
116 int style_; // style of format-string : positional or not, etc
117 int cur_arg_; // keep track of wich argument is current
118 int num_args_; // number of expected arguments
119 mutable bool dumped_; // true only after call to str() or <<
120 string_t prefix_; // piece of string to insert before first item
121 internal_stream_t oss_; // the internal stream.
122 unsigned char exceptions_;
123 }; // class basic_format
128 #endif // BOOST_FORMAT_CLASS_HPP