]> git.lyx.org Git - lyx.git/blobdiff - boost/boost/format/format_class.hpp
Don't allow newline characters in document settings.
[lyx.git] / boost / boost / format / format_class.hpp
index 0c9b252299c88dd4028b45d9ccaf1b851f6e07d9..4555e56ec37cb07d8fa050af5d177e4350a25bbb 100644 (file)
-// -*- C++ -*-
-//  Boost general library 'format'   ---------------------------
-//  See http://www.boost.org for updates, documentation, and revision history.
+// ----------------------------------------------------------------------------
+//  format_class.hpp :  class interface
+// ----------------------------------------------------------------------------
 
-//  (C) Samuel Krempp 2001
-//                  krempp@crans.ens-cachan.fr
-//  Permission to copy, use, modify, sell and
-//  distribute this software is granted provided this copyright notice appears
-//  in all copies. This software is provided "as is" without express or implied
-//  warranty, and with no claim as to its suitability for any purpose.
+//  Copyright Samuel Krempp 2003. Use, modification, and distribution are
+//  subject to the Boost Software License, Version 1.0. (See accompanying
+//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
-// ideas taken from RĂ¼diger Loos's format class
-// and Karl Nelson's ofstream (also took its parsing code as basis for printf parsing)
-
-// ------------------------------------------------------------------------------
-// format_class.hpp :  class interface
-// ------------------------------------------------------------------------------
+//  See http://www.boost.org/libs/format for library home page
 
+// ----------------------------------------------------------------------------
 
 #ifndef BOOST_FORMAT_CLASS_HPP
 #define BOOST_FORMAT_CLASS_HPP
 
+
 #include <vector>
-#include <sstream>
 #include <string>
-#include <ostream>
-#include <locale>
-#include "boost/format/format_fwd.hpp"
-#include "boost/format/internals_fwd.hpp"
 
-#include "boost/format/internals.hpp" 
+#include <boost/optional.hpp> // to store locale when needed
+
+#include <boost/format/format_fwd.hpp>
+#include <boost/format/internals_fwd.hpp>
+#include <boost/format/internals.hpp>
+#include <boost/format/alt_sstream.hpp>
 
 namespace boost {
 
-template<class Ch, class Tr>
-class basic_format 
-{
-  typedef BOOST_IO_STD basic_ostream<Ch, Tr>               stream_t;
-  typedef io::detail::stream_format_state<Ch, Tr>  stream_format_state;
-  typedef io::detail::format_item<Ch, Tr>          format_item_t;
-public:
-  typedef std::basic_string<Ch, Tr>                string_t;
-  typedef BOOST_IO_STD basic_ostringstream<Ch, Tr>         internal_stream_t;
-
-public:
-  basic_format(const Ch* str);
-  basic_format(const Ch* str, const std::locale & loc);
-  basic_format(const string_t& s);
-  basic_format(const string_t& s, const std::locale & loc);
-  basic_format(const basic_format& x);
-  basic_format& operator= (const basic_format& x);
-
-  basic_format& clear(); // empty the string buffers (except bound arguments, see clear_binds() )
-
-  // pass arguments through those operators :
-  template<class T>  basic_format&   operator%(const T& x) 
-  { 
-    return io::detail::feed<Ch, Tr, const T&>(*this,x);
-  }
-
-#ifdef BOOST_OVERLOAD_FOR_NON_CONST
-  template<class T>  basic_format&   operator%(T& x) 
-  {
-    return io::detail::feed<Ch, Tr, T&>(*this,x);
-  }
+    template<class Ch, class Tr, class Alloc>
+    class basic_format 
+    {
+        typedef typename io::CompatTraits<Tr>::compatible_type compat_traits;  
+    public:
+        typedef Ch  CharT;   // borland fails in operator% if we use Ch and Tr directly
+        typedef std::basic_string<Ch, Tr, Alloc>              string_type;
+        typedef typename string_type::size_type               size_type;
+        typedef io::detail::format_item<Ch, Tr, Alloc>        format_item_t;
+        typedef io::basic_altstringbuf<Ch, Tr, Alloc>         internal_streambuf_t;
+        
+
+        explicit basic_format(const Ch* str=NULL);
+        explicit basic_format(const string_type& s);
+        basic_format(const basic_format& x);
+        basic_format& operator= (const basic_format& x);
+        void swap(basic_format& x);
+
+#if !defined(BOOST_NO_STD_LOCALE)
+        explicit basic_format(const Ch* str, const std::locale & loc);
+        explicit basic_format(const string_type& s, const std::locale & loc);
 #endif
+        io::detail::locale_t  getloc() const;
 
+        basic_format& clear();       // empty all converted string buffers (except bound items)
+        basic_format& clear_binds(); // unbind all bound items, and call clear()
+        basic_format& parse(const string_type&); // resets buffers and parse a new format string
 
-  // system for binding arguments :
-  template<class T>  
-  basic_format&         bind_arg(int argN, const T& val) 
-  {
-    return io::detail::bind_arg_body(*this, argN, val); 
-  }
-  basic_format&         clear_bind(int argN);
-  basic_format&         clear_binds();
-
-  // modify the params of a directive, by applying a manipulator :
-  template<class T> 
-  basic_format&  modify_item(int itemN, const T& manipulator) 
-  {
-    return io::detail::modify_item_body(*this, itemN, manipulator) ;
-  }
-
-  // Choosing which errors will throw exceptions :
-  unsigned char exceptions() const;
-  unsigned char exceptions(unsigned char newexcept);
-
-  // final output
-  string_t str() const;
-  friend BOOST_IO_STD basic_ostream<Ch, Tr>& 
-#if !defined(BOOST_MSVC) || BOOST_MSVC > 1300
-  operator<< <Ch, Tr> ( BOOST_IO_STD basic_ostream<Ch, Tr>& , const basic_format& ); 
-#else
-  operator<< ( BOOST_IO_STD basic_ostream<Ch, Tr>& , const basic_format& ); 
-#endif
-                      
-
-#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
-  template<class Ch2, class Tr2, class T>  friend basic_format<Ch2, Tr2>&  
-  io::detail::feed(basic_format<Ch2,Tr2>&, T);
-    
-  template<class Ch2, class Tr2, class T>  friend   
-  void io::detail::distribute(basic_format<Ch2,Tr2>&, T);
-  
-  template<class Ch2, class Tr2, class T>  friend
-  basic_format<Ch2, Tr2>&  io::detail::modify_item_body(basic_format<Ch2, Tr2>&, int, const T&);
-
-  template<class Ch2, class Tr2, class T> friend
-  basic_format<Ch2, Tr2>&  io::detail::bind_arg_body(basic_format<Ch2, Tr2>&, int, const T&);
-
-// make the members private only if the friend templates are supported
-private:
-#endif
+        // ** formatted result ** //
+        size_type   size() const;    // sum of the current string pieces sizes
+        string_type str()  const;    // final string 
 
-  // flag bits, used for style_
-  enum style_values  { ordered = 1,        // set only if all directives are  positional directives
-                       special_needs = 4 };     
+        // ** arguments passing ** //
+        template<class T>  
+        basic_format&   operator%(const T& x)
+            { return io::detail::feed<CharT, Tr, Alloc, const T&>(*this,x); }
 
-  // parse the format string :
-  void parse(const string_t&);
+#ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
+        template<class T>  basic_format&   operator%(T& x) 
+            { return io::detail::feed<CharT, Tr, Alloc, T&>(*this,x); }
+#endif
 
-  int                           style_;         // style of format-string :  positional or not, etc
-  int                           cur_arg_;       // keep track of wich argument will come
-  int                           num_args_;      // number of expected arguments
-  mutable bool                  dumped_;        // true only after call to str() or <<
-  std::vector<format_item_t>    items_;         // vector of directives (aka items)
-  string_t                      prefix_;        // piece of string to insert before first item
+#if defined(__GNUC__)
+        // GCC can't handle anonymous enums without some help
+        // ** arguments passing ** //
+        basic_format&   operator%(const int& x)
+            { return io::detail::feed<CharT, Tr, Alloc, const int&>(*this,x); }
 
-  std::vector<bool>             bound_;         // stores which arguments were bound
-                                                //   size = num_args OR zero
-  internal_stream_t             oss_;           // the internal stream.
-  stream_format_state           state0_;        // reference state for oss_
-  unsigned char                 exceptions_;
-}; // class basic_format
+#ifndef BOOST_NO_OVERLOAD_FOR_NON_CONST
+        basic_format&   operator%(int& x)
+            { return io::detail::feed<CharT, Tr, Alloc, int&>(*this,x); }
+#endif
+#endif
+
+        // The total number of arguments expected to be passed to the format objectt
+        int expected_args() const
+            { return num_args_; }
+        // The number of arguments currently bound (see bind_arg(..) )
+        int bound_args() const;
+        // The number of arguments currently fed to the format object
+        int fed_args() const;
+        // The index (1-based) of the current argument (i.e. next to be formatted)
+        int cur_arg() const;
+        // The number of arguments still required to be fed
+        int remaining_args() const; // same as expected_args() - bound_args() - fed_args()
+
+
+        // ** object modifying **//
+        template<class T>
+        basic_format&  bind_arg(int argN, const T& val) 
+            { return io::detail::bind_arg_body(*this, argN, val); }
+        basic_format&  clear_bind(int argN);
+        template<class T> 
+        basic_format&  modify_item(int itemN, T manipulator) 
+            { return io::detail::modify_item_body<Ch,Tr, Alloc, T> (*this, itemN, manipulator);}
+
+        // Choosing which errors will throw exceptions :
+        unsigned char exceptions() const;
+        unsigned char exceptions(unsigned char newexcept);
+
+#if !defined( BOOST_NO_MEMBER_TEMPLATE_FRIENDS )  \
+    && !BOOST_WORKAROUND(__BORLANDC__, <= 0x570) \
+    && !BOOST_WORKAROUND( _CRAYC, != 0) \
+    && !BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042))
+        // use friend templates and private members only if supported
+
+#ifndef  BOOST_NO_TEMPLATE_STD_STREAM
+        template<class Ch2, class Tr2, class Alloc2>
+        friend std::basic_ostream<Ch2, Tr2> & 
+        operator<<( std::basic_ostream<Ch2, Tr2> & ,
+                    const basic_format<Ch2, Tr2, Alloc2>& );
+#else
+        template<class Ch2, class Tr2, class Alloc2>
+        friend std::ostream & 
+        operator<<( std::ostream & ,
+                    const basic_format<Ch2, Tr2, Alloc2>& );
+#endif
 
+        template<class Ch2, class Tr2, class Alloc2, class T>  
+        friend basic_format<Ch2, Tr2, Alloc2>&  
+        io::detail::feed (basic_format<Ch2, Tr2, Alloc2>&, T);
+
+        template<class Ch2, class Tr2, class Alloc2, class T>  friend   
+        void io::detail::distribute (basic_format<Ch2, Tr2, Alloc2>&, T);
+        
+        template<class Ch2, class Tr2, class Alloc2, class T>  friend
+        basic_format<Ch2, Tr2, Alloc2>& 
+        io::detail::modify_item_body (basic_format<Ch2, Tr2, Alloc2>&, int, T);
+        
+        template<class Ch2, class Tr2, class Alloc2, class T> friend
+        basic_format<Ch2, Tr2, Alloc2>&  
+        io::detail::bind_arg_body (basic_format<Ch2, Tr2, Alloc2>&, int, const T&);
+
+    private:
+#endif
+        typedef io::detail::stream_format_state<Ch, Tr>  stream_format_state;
+        // flag bits, used for style_
+        enum style_values  { ordered = 1, // set only if all directives are  positional
+                             special_needs = 4 };     
+
+        void make_or_reuse_data(std::size_t nbitems);// used for (re-)initialisation
+
+        // member data --------------------------------------------//
+        std::vector<format_item_t>  items_; // each '%..' directive leads to a format_item
+        std::vector<bool> bound_; // stores which arguments were bound. size() == 0 || num_args
+
+        int              style_; // style of format-string :  positional or not, etc
+        int             cur_arg_; // keep track of wich argument is current
+        int            num_args_; // number of expected arguments
+        mutable bool     dumped_; // true only after call to str() or <<
+        string_type      prefix_; // piece of string to insert before first item
+        unsigned char exceptions_;
+        internal_streambuf_t   buf_; // the internal stream buffer.
+        boost::optional<io::detail::locale_t>     loc_;
+    }; // class basic_format
 
 } // namespace boost