Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:51:09

0001 /* boost random/vector_io.hpp header file
0002  *
0003  * Copyright Steven Watanabe 2011
0004  * Distributed under the Boost Software License, Version 1.0. (See
0005  * accompanying file LICENSE_1_0.txt or copy at
0006  * http://www.boost.org/LICENSE_1_0.txt)
0007  *
0008  * See http://www.boost.org for most recent version including documentation.
0009  *
0010  * $Id$
0011  */
0012 
0013 #ifndef BOOST_RANDOM_DETAIL_VECTOR_IO_HPP
0014 #define BOOST_RANDOM_DETAIL_VECTOR_IO_HPP
0015 
0016 #include <vector>
0017 #include <iosfwd>
0018 #include <istream>
0019 #include <boost/io/ios_state.hpp>
0020 
0021 namespace boost {
0022 namespace random {
0023 namespace detail {
0024 
0025 template<class CharT, class Traits, class T>
0026 void print_vector(std::basic_ostream<CharT, Traits>& os,
0027                   const std::vector<T>& vec)
0028 {
0029     typename std::vector<T>::const_iterator
0030         iter = vec.begin(),
0031         end =  vec.end();
0032     os << os.widen('[');
0033     if(iter != end) {
0034         os << *iter;
0035         ++iter;
0036         for(; iter != end; ++iter)
0037         {
0038             os << os.widen(' ') << *iter;
0039         }
0040     }
0041     os << os.widen(']');
0042 }
0043 
0044 template<class CharT, class Traits, class T>
0045 void read_vector(std::basic_istream<CharT, Traits>& is, std::vector<T>& vec)
0046 {
0047     CharT ch;
0048     if(!(is >> ch)) {
0049         return;
0050     }
0051     if(ch != is.widen('[')) {
0052         is.putback(ch);
0053         is.setstate(std::ios_base::failbit);
0054         return;
0055     }
0056     boost::io::basic_ios_exception_saver<CharT, Traits> e(is, std::ios_base::goodbit);
0057     T val;
0058     while(is >> std::ws >> val) {
0059         vec.push_back(val);
0060     }
0061     if(is.fail()) {
0062         is.clear();
0063         e.restore();
0064         if(!(is >> ch)) {
0065             return;
0066         }
0067         if(ch != is.widen(']')) {
0068             is.putback(ch);
0069             is.setstate(std::ios_base::failbit);
0070         }
0071     }
0072 }
0073 
0074 }
0075 }
0076 }
0077 
0078 #endif // BOOST_RANDOM_DETAIL_VECTOR_IO_HPP