1 /*=============================================================================
2 Copyright (C) 1999-2003 Jaakko Jarvi
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/fusion/container/vector/vector.hpp>
9 #include <boost/fusion/container/generation/make_vector.hpp>
10 #include <boost/fusion/sequence/comparison/equal_to.hpp>
11 #include <boost/fusion/sequence/io/out.hpp>
12 #include <boost/fusion/sequence/io/in.hpp>
13
14 #include <fstream>
15 #include <iterator>
16 #include <algorithm>
17 #include <string>
18
19 #if defined BOOST_NO_STRINGSTREAM
20 # include <strstream>
21 #else
22 # include <sstream>
23 #endif
24
25 using boost::fusion::vector;
26 using boost::fusion::make_vector;
27 using boost::fusion::tuple_close;
28 using boost::fusion::tuple_open;
29 using boost::fusion::tuple_delimiter;
30
31 #if defined BOOST_NO_STRINGSTREAM
32 using std::ostrstream;
33 using std::istrstream;
34 typedef ostrstream useThisOStringStream;
35 typedef istrstream useThisIStringStream;
36 #else
37 using std::ostringstream;
38 using std::istringstream;
39 typedef ostringstream useThisOStringStream;
40 typedef istringstream useThisIStringStream;
41 #endif
42
43 using std::endl;
44 using std::ofstream;
45 using std::ifstream;
46 using std::string;
47
48 int
main()49 main()
50 {
51 using boost::fusion::tuple_close;
52 using boost::fusion::tuple_open;
53 using boost::fusion::tuple_delimiter;
54
55 useThisOStringStream os1;
56
57 // Set format [a, b, c] for os1
58 os1 << tuple_open('[');
59 os1 << tuple_close(']');
60 os1 << tuple_delimiter(',');
61 os1 << make_vector(1, 2, 3);
62
63 BOOST_TEST (os1.str() == std::string("[1,2,3]") );
64
65 {
66 useThisOStringStream os2;
67 // Set format (a:b:c) for os2;
68 os2 << tuple_open('(');
69 os2 << tuple_close(')');
70 os2 << tuple_delimiter(':');
71
72 os2 << make_vector("TUPU", "HUPU", "LUPU", 4.5);
73 BOOST_TEST (os2.str() == std::string("(TUPU:HUPU:LUPU:4.5)") );
74 }
75
76 {
77 useThisOStringStream os2;
78 // Set format (a:b:c) for os2;
79 os2 << tuple_open('(');
80 os2 << tuple_close(')');
81 os2 << tuple_delimiter(':');
82 // overwrite previous setting
83 os2 << tuple_open("< ");
84 os2 << tuple_close('>');
85 os2 << tuple_delimiter(", ");
86
87 os2 << make_vector("TUPU", "HUPU", "LUPU", 4.5);
88 BOOST_TEST (os2.str() == std::string("< TUPU, HUPU, LUPU, 4.5>") );
89 }
90
91 // The format is still [a, b, c] for os1
92 os1 << make_vector(1, 2, 3);
93 BOOST_TEST (os1.str() == std::string("[1,2,3][1,2,3]") );
94
95 std::ofstream tmp("temp.tmp");
96
97 tmp << make_vector("One", "Two", 3);
98 tmp << tuple_delimiter(':');
99 tmp << make_vector(1000, 2000, 3000) << endl;
100
101 tmp.close();
102
103 // When reading tuples from a stream, manipulators must be set correctly:
104 ifstream tmp3("temp.tmp");
105 vector<string, string, int> j;
106
107 tmp3 >> j;
108 BOOST_TEST (tmp3.good() );
109
110 tmp3 >> tuple_delimiter(':');
111 vector<int, int, int> i;
112 tmp3 >> i;
113 BOOST_TEST (tmp3.good() );
114
115 tmp3.close();
116
117 // reading vector<int, int, int> in format (a b c);
118 useThisIStringStream is("(100 200 300)");
119
120 vector<int, int, int> ti;
121 BOOST_TEST(!!(is >> ti));
122 BOOST_TEST(ti == make_vector(100, 200, 300));
123
124 // Note that strings are problematic:
125 // writing a tuple on a stream and reading it back doesn't work in
126 // general. If this is wanted, some kind of a parseable string class
127 // should be used.
128
129 return boost::report_errors();
130 }
131
132