-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDwmArgumentIO.hh
263 lines (241 loc) · 8.25 KB
/
DwmArgumentIO.hh
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
//===========================================================================
// @(#) $DwmPath: dwm/mkfbsdmnfst/tags/mkfbsdmnfst-1.0.0/DwmArgumentIO.hh 11856 $
//===========================================================================
// Copyright (c) Daniel W. McRobb 2020
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The names of the authors and copyright holders may not be used to
// endorse or promote products derived from this software without
// specific prior written permission.
//
// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
// TRADEMARK OR OTHER RIGHTS.
//===========================================================================
//---------------------------------------------------------------------------
//! \file DwmArgumentIO.hh
//! \brief iostream helpers for Argument values
//---------------------------------------------------------------------------
#ifndef _DWMARGUMENTIO_HH_
#define _DWMARGUMENTIO_HH_
#include <array>
#include <iostream>
#include <set>
#include <vector>
#include "DwmTupleHelpers.hh"
namespace Dwm {
namespace ArgumentIO {
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T>
std::ostream & operator << (std::ostream & os, const std::vector<T> & t)
{
std::string sep;
for (const auto & e : t) {
os << sep << e;
sep = ",";
}
return os;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class... Ts>
std::ostream & operator << (std::ostream & os, const std::tuple<Ts...> & t)
{
namespace Tuples = Dwm::TupleHelpers;
std::string sep;
Tuples::ForEach(t,
[&os, &sep] (auto && e)
{ os << sep << e; sep = ","; });
return os;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T>
std::ostream & operator << (std::ostream & os, const std::set<T> & s)
{
std::string sep;
for (const auto & e : s) {
os << sep << e;
sep = ",";
}
return os;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T1, class T2>
std::ostream & operator << (std::ostream & os, const std::pair<T1,T2> & t)
{
os << t.first << ',' << t.second;
return os;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T, size_t N>
std::ostream & operator << (std::ostream & os, const std::array<T,N> & t)
{
std::string sep;
for (const auto & e : t) {
os << sep << e;
sep = ",";
}
return os;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T>
std::istream & operator >> (std::istream & is, std::vector<T> & v)
{
v.clear();
while (is) {
std::string s;
if (std::getline(is, s, ',')) {
std::istringstream iss(s);
T e;
iss >> e;
v.push_back(e);
}
}
if (is.rdstate() & std::ios_base::eofbit) {
is.clear();
}
return is;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class... Ts>
std::istream & operator >> (std::istream & is, std::tuple<Ts...> & t)
{
namespace Tuples = Dwm::TupleHelpers;
std::string s;
std::istringstream iss;
Tuples::ForEach(t,
[&is,&s] (auto && e)
{
std::getline(is, s, ',');
if constexpr (std::is_same<std::remove_reference_t<decltype(e)>, std::string>::value) {
e = s;
}
else {
std::istringstream iss(s);
iss >> e;
}
});
if (is.rdstate() & std::ios_base::eofbit) {
is.clear();
}
return is;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
std::istream & operator >> (std::istream & is, std::string & s)
{
std::getline(is, s); // , ',');
return is;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T>
std::istream & operator >> (std::istream & is, std::set<T> & t)
{
t.clear();
while (is) {
std::string s;
if (std::getline(is, s, ',')) {
std::istringstream iss(s);
T e;
iss >> e;
if (! t.insert(e).second) {
is.setstate(std::ios_base::failbit);
break;
}
}
}
if (is.rdstate() & std::ios_base::eofbit) {
is.clear();
}
return is;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T1, class T2>
std::istream & operator >> (std::istream & is, std::pair<T1,T2> & t)
{
std::string s;
if (getline(is, s, ',')) {
if constexpr (std::is_same<T1,std::string>::value) {
t.first = s;
}
else {
std::istringstream iss(s);
iss >> t.first;
}
getline(is, s);
if constexpr (std::is_same<T2,std::string>::value) {
t.second = s;
}
else {
std::istringstream iss(s);
iss >> t.second;
}
}
return is;
}
//----------------------------------------------------------------------
//!
//----------------------------------------------------------------------
template <class T, size_t N>
std::istream & operator >> (std::istream & is, std::array<T,N> & a)
{
for (auto i = 0; i < N; ++i) {
if (is) {
std::string s;
if (std::getline(is, s, ',')) {
std::istringstream iss(s);
T e;
iss >> e;
a[i] = e;
}
}
else {
break;
}
}
if (is.rdstate() & std::ios_base::eofbit) {
is.clear();
}
return is;
}
} // namespace ArgumentIO
} // namespace Dwm
#endif // _DWMMCLOCARGUMENTIO_HH_