• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* boost random/vector_io.hpp header file
2  *
3  * Copyright Steven Watanabe 2011
4  * Distributed under the Boost Software License, Version 1.0. (See
5  * accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * See http://www.boost.org for most recent version including documentation.
9  *
10  * $Id$
11  */
12 
13 #ifndef BOOST_RANDOM_DETAIL_VECTOR_IO_HPP
14 #define BOOST_RANDOM_DETAIL_VECTOR_IO_HPP
15 
16 #include <vector>
17 #include <iosfwd>
18 #include <istream>
19 #include <boost/io/ios_state.hpp>
20 
21 namespace boost {
22 namespace random {
23 namespace detail {
24 
25 template<class CharT, class Traits, class T>
print_vector(std::basic_ostream<CharT,Traits> & os,const std::vector<T> & vec)26 void print_vector(std::basic_ostream<CharT, Traits>& os,
27                   const std::vector<T>& vec)
28 {
29     typename std::vector<T>::const_iterator
30         iter = vec.begin(),
31         end =  vec.end();
32     os << os.widen('[');
33     if(iter != end) {
34         os << *iter;
35         ++iter;
36         for(; iter != end; ++iter)
37         {
38             os << os.widen(' ') << *iter;
39         }
40     }
41     os << os.widen(']');
42 }
43 
44 template<class CharT, class Traits, class T>
read_vector(std::basic_istream<CharT,Traits> & is,std::vector<T> & vec)45 void read_vector(std::basic_istream<CharT, Traits>& is, std::vector<T>& vec)
46 {
47     CharT ch;
48     if(!(is >> ch)) {
49         return;
50     }
51     if(ch != is.widen('[')) {
52         is.putback(ch);
53         is.setstate(std::ios_base::failbit);
54         return;
55     }
56     boost::io::basic_ios_exception_saver<CharT, Traits> e(is, std::ios_base::goodbit);
57     T val;
58     while(is >> std::ws >> val) {
59         vec.push_back(val);
60     }
61     if(is.fail()) {
62         is.clear();
63         e.restore();
64         if(!(is >> ch)) {
65             return;
66         }
67         if(ch != is.widen(']')) {
68             is.putback(ch);
69             is.setstate(std::ios_base::failbit);
70         }
71     }
72 }
73 
74 }
75 }
76 }
77 
78 #endif // BOOST_RANDOM_DETAIL_VECTOR_IO_HPP
79