Chris@16: // ---------------------------------------------------------------------------- Chris@16: // format_class.hpp : class interface Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: // Copyright Samuel Krempp 2003. Use, modification, and distribution are Chris@16: // subject to the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/format for library home page Chris@16: Chris@16: // ---------------------------------------------------------------------------- Chris@16: Chris@16: #ifndef BOOST_FORMAT_CLASS_HPP Chris@16: #define BOOST_FORMAT_CLASS_HPP Chris@16: Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include // to store locale when needed Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: template Chris@16: class basic_format Chris@16: { Chris@16: typedef typename io::CompatTraits::compatible_type compat_traits; Chris@16: public: Chris@16: typedef Ch CharT; // borland fails in operator% if we use Ch and Tr directly Chris@16: typedef std::basic_string string_type; Chris@16: typedef typename string_type::size_type size_type; Chris@16: typedef io::detail::format_item format_item_t; Chris@16: typedef io::basic_altstringbuf internal_streambuf_t; Chris@16: Chris@16: Chris@16: explicit basic_format(const Ch* str=NULL); Chris@16: explicit basic_format(const string_type& s); Chris@16: basic_format(const basic_format& x); Chris@16: basic_format& operator= (const basic_format& x); Chris@16: void swap(basic_format& x); Chris@16: Chris@16: #if !defined(BOOST_NO_STD_LOCALE) Chris@16: explicit basic_format(const Ch* str, const std::locale & loc); Chris@16: explicit basic_format(const string_type& s, const std::locale & loc); Chris@16: #endif Chris@16: io::detail::locale_t getloc() const; Chris@16: Chris@16: basic_format& clear(); // empty all converted string buffers (except bound items) Chris@16: basic_format& clear_binds(); // unbind all bound items, and call clear() Chris@16: basic_format& parse(const string_type&); // resets buffers and parse a new format string Chris@16: Chris@16: // ** formatted result ** // Chris@16: size_type size() const; // sum of the current string pieces sizes Chris@16: string_type str() const; // final string Chris@16: Chris@16: // ** arguments passing ** // Chris@16: template Chris@16: basic_format& operator%(const T& x) Chris@16: { return io::detail::feed(*this,x); } Chris@16: Chris@16: #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST Chris@16: template basic_format& operator%(T& x) Chris@16: { return io::detail::feed(*this,x); } Chris@16: #endif Chris@16: Chris@16: #if defined(__GNUC__) Chris@16: // GCC can't handle anonymous enums without some help Chris@16: // ** arguments passing ** // Chris@16: basic_format& operator%(const int& x) Chris@16: { return io::detail::feed(*this,x); } Chris@16: Chris@16: #ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST Chris@16: basic_format& operator%(int& x) Chris@16: { return io::detail::feed(*this,x); } Chris@16: #endif Chris@16: #endif Chris@16: Chris@16: // The total number of arguments expected to be passed to the format objectt Chris@16: int expected_args() const Chris@16: { return num_args_; } Chris@16: // The number of arguments currently bound (see bind_arg(..) ) Chris@16: int bound_args() const; Chris@16: // The number of arguments currently fed to the format object Chris@16: int fed_args() const; Chris@16: // The index (1-based) of the current argument (i.e. next to be formatted) Chris@16: int cur_arg() const; Chris@16: // The number of arguments still required to be fed Chris@16: int remaining_args() const; // same as expected_args() - bound_args() - fed_args() Chris@16: Chris@16: Chris@16: // ** object modifying **// Chris@16: template Chris@16: basic_format& bind_arg(int argN, const T& val) Chris@16: { return io::detail::bind_arg_body(*this, argN, val); } Chris@16: basic_format& clear_bind(int argN); Chris@16: template Chris@16: basic_format& modify_item(int itemN, T manipulator) Chris@16: { return io::detail::modify_item_body (*this, itemN, manipulator);} Chris@16: Chris@16: // Choosing which errors will throw exceptions : Chris@16: unsigned char exceptions() const; Chris@16: unsigned char exceptions(unsigned char newexcept); Chris@16: Chris@16: #if !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS ) \ Chris@16: && !BOOST_WORKAROUND(__BORLANDC__, <= 0x570) \ Chris@16: && !BOOST_WORKAROUND( _CRAYC, != 0) \ Chris@16: && !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) Chris@16: // use friend templates and private members only if supported Chris@16: Chris@16: #ifndef BOOST_NO_TEMPLATE_STD_STREAM Chris@16: template Chris@16: friend std::basic_ostream & Chris@16: operator<<( std::basic_ostream & , Chris@16: const basic_format& ); Chris@16: #else Chris@16: template Chris@16: friend std::ostream & Chris@16: operator<<( std::ostream & , Chris@16: const basic_format& ); Chris@16: #endif Chris@16: Chris@16: template Chris@16: friend basic_format& Chris@101: io::detail::feed_impl (basic_format&, T); Chris@16: Chris@16: template friend Chris@16: void io::detail::distribute (basic_format&, T); Chris@16: Chris@16: template friend Chris@16: basic_format& Chris@16: io::detail::modify_item_body (basic_format&, int, T); Chris@16: Chris@16: template friend Chris@16: basic_format& Chris@16: io::detail::bind_arg_body (basic_format&, int, const T&); Chris@16: Chris@16: private: Chris@16: #endif Chris@16: typedef io::detail::stream_format_state stream_format_state; Chris@16: // flag bits, used for style_ Chris@16: enum style_values { ordered = 1, // set only if all directives are positional Chris@16: special_needs = 4 }; Chris@16: Chris@16: void make_or_reuse_data(std::size_t nbitems);// used for (re-)initialisation Chris@16: Chris@16: // member data --------------------------------------------// Chris@16: std::vector items_; // each '%..' directive leads to a format_item Chris@16: std::vector bound_; // stores which arguments were bound. size() == 0 || num_args Chris@16: Chris@16: int style_; // style of format-string : positional or not, etc Chris@16: int cur_arg_; // keep track of wich argument is current Chris@16: int num_args_; // number of expected arguments Chris@16: mutable bool dumped_; // true only after call to str() or << Chris@16: string_type prefix_; // piece of string to insert before first item Chris@16: unsigned char exceptions_; Chris@16: internal_streambuf_t buf_; // the internal stream buffer. Chris@16: boost::optional loc_; Chris@16: }; // class basic_format Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: Chris@16: #endif // BOOST_FORMAT_CLASS_HPP