-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
141 lines (94 loc) · 3.48 KB
/
README
File metadata and controls
141 lines (94 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
C++ range streaming proposal
============================
Version: 2.0
What is it?
-----------
This is a reference implementation for a proposed extension to the C++ standard
library to add facilities to make using ranges with input and output streams
easier.
Latest version
--------------
The latest release can be downloaded from the [github project downloads
page](https://github.com/DarkerStar/cpp-range-streaming/downloads).
Introduction
------------
Range `for` made ranges a first-class citizen in C++, but streaming the contents
of ranges to/from IOstreams is a clumsy and difficult operation. The canonical
way is to use the Algorithms library and stream iterators, but these are
difficult to use, and do not handle formatting or error-checking very well.
This proposal suggests a set of range I/O functions that return range I/O
objects, which can be used in conventional stream I/O expressions. For output,
this can be as simple as:
```C++
auto const a = std::array<int, 3>{1, 4, 6};
std::cout << std::write_all(a);
// Prints: "146"
```
Or with delimiters:
```C++
std::cout << "{ " << std::write_all({1, 4, 6}, ", ") << " }";
// Prints: "{ 1, 4, 6 }"
```
For input, four different standard operations are provided: overwriting, back
inserting, front inserting, and general inserting:
```C++
auto r = std::array<double, 3>{};
std::cin >> std::overwrite(r);
// Reads 3 double values from cin into r
auto v = std::vector<int>{};
std::cin >> std::back_insert(v);
// Reads ints from cin until a read failure,
// adding them to v via v.push_back()
auto l = std::list<std::string>{};
std::cin >> std::front_insert_n(l, 10);
// Reads up to 10 strings from cin, stopping if there is a read failure,
// adding them to l via l.push_front()
```
Formatting is supported properly:
```C++
std::cout << "{ " << setw(3) << setfill('_') <<
std::write_all({1, 4, 6}, ", ") << " }";
// Prints: "{ __1, __4, __6 }"
auto iss = std::istringstream{"abcdef"};
auto v = std::vector<std::string>{};
iss >> setw(3) >> back_insert(v);
// v = { "abc", "def" }
```
You can get a lot of information about the previous read/write by capturing
the range I/O object and querying it:
```C++
auto a = std::array<int, 100>{};
auto p = overwrite(a);
std::cin >> p;
if (p.next != a.end())
{
std::cerr << "There was an error reading.\n";
std::cerr << "Only " << p.count << " values were read,\n";
std::cerr << "and " << p.stored << " values were stored in a,\n";
std::cerr << "which are: { " <<
std::write_all(boost::make_iterator_range(a.begin(), p.next) ", ") << " }.\n";
}
```
For more information, check out the specification and the reference in the `doc`
directory.
Installation
------------
The entire proposal is contained in a set of header files. Just make sure they
can be found on the include path, and you can use it.
For more details, please see the file called “INSTALL”.
Possible future directions
--------------------------
See `doc/Ideas` for some ideas being considered.
Licensing
---------
The Google Test code is available under a [New BSD
License](http://opensource.org/licenses/BSD-3-Clause). For more details about
Google Test, see [the Google Test project
site](https://code.google.com/p/googletest/).
All other code and content in this package is licenced under the GPLv3, as
described in the COPYING file.
For more details, please see the file called "COPYING".
Contacts
--------
For contact information, check out the
[github project page](https://github.com/DarkerStar/cpp-range-streaming).