libDwm-0.9.45
DwmArgumentIO.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2020
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#include <array>
42#include <iostream>
43#include <set>
44#include <vector>
45
46#include "DwmTupleHelpers.hh"
47
48namespace Dwm {
49
50 namespace ArgumentIO {
51
52 //------------------------------------------------------------------------
54 //------------------------------------------------------------------------
55 template <class T>
56 std::ostream & operator << (std::ostream & os, const std::vector<T> & t)
57 {
58 std::string sep;
59 for (const auto & e : t) {
60 os << sep << e;
61 sep = ",";
62 }
63 return os;
64 }
65
66 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
69 template <class... Ts>
70 std::ostream & operator << (std::ostream & os, const std::tuple<Ts...> & t)
71 {
72 namespace Tuples = Dwm::TupleHelpers;
73 std::string sep;
74 Tuples::ForEach(t,
75 [&os, &sep] (auto && e)
76 { os << sep << e; sep = ","; });
77 return os;
78 }
79
80 //------------------------------------------------------------------------
82 //------------------------------------------------------------------------
83 template <class T>
84 std::ostream & operator << (std::ostream & os, const std::set<T> & s)
85 {
86 std::string sep;
87 for (const auto & e : s) {
88 os << sep << e;
89 sep = ",";
90 }
91 return os;
92 }
93
94 //------------------------------------------------------------------------
96 //------------------------------------------------------------------------
97 template <class T1, class T2>
98 std::ostream & operator << (std::ostream & os, const std::pair<T1,T2> & t)
99 {
100 os << t.first << ',' << t.second;
101 return os;
102 }
103
104 //------------------------------------------------------------------------
106 //------------------------------------------------------------------------
107 template <class T, size_t N>
108 std::ostream & operator << (std::ostream & os, const std::array<T,N> & t)
109 {
110 std::string sep;
111 for (const auto & e : t) {
112 os << sep << e;
113 sep = ",";
114 }
115 return os;
116 }
117
118 //------------------------------------------------------------------------
120 //------------------------------------------------------------------------
121 template <class T>
122 std::istream & operator >> (std::istream & is, std::vector<T> & v)
123 {
124 v.clear();
125 while (is) {
126 std::string s;
127 if (std::getline(is, s, ',')) {
128 std::istringstream iss(s);
129 T e;
130 iss >> e;
131 v.push_back(e);
132 }
133 }
134 if (is.rdstate() & std::ios_base::eofbit) {
135 is.clear();
136 }
137 return is;
138 }
139
140 //------------------------------------------------------------------------
142 //------------------------------------------------------------------------
143 template <class... Ts>
144 std::istream & operator >> (std::istream & is, std::tuple<Ts...> & t)
145 {
146 namespace Tuples = Dwm::TupleHelpers;
147
148 std::string s;
149 std::istringstream iss;
150
151 Tuples::ForEach(t,
152 [&is,&s] (auto && e)
153 {
154 std::getline(is, s, ',');
155 if constexpr (std::is_same<std::remove_reference_t<decltype(e)>, std::string>::value) {
156 e = s;
157 }
158 else {
159 std::istringstream iss(s);
160 iss >> e;
161 }
162 });
163 if (is.rdstate() & std::ios_base::eofbit) {
164 is.clear();
165 }
166 return is;
167 }
168
169 //------------------------------------------------------------------------
171 //------------------------------------------------------------------------
172 std::istream & operator >> (std::istream & is, std::string & s)
173 {
174 std::getline(is, s); // , ',');
175 return is;
176 }
177
178 //------------------------------------------------------------------------
180 //------------------------------------------------------------------------
181 template <class T>
182 std::istream & operator >> (std::istream & is, std::set<T> & t)
183 {
184 t.clear();
185 while (is) {
186 std::string s;
187 if (std::getline(is, s, ',')) {
188 std::istringstream iss(s);
189 T e;
190 iss >> e;
191 if (! t.insert(e).second) {
192 is.setstate(std::ios_base::failbit);
193 break;
194 }
195 }
196 }
197 if (is.rdstate() & std::ios_base::eofbit) {
198 is.clear();
199 }
200 return is;
201 }
202
203 //------------------------------------------------------------------------
205 //------------------------------------------------------------------------
206 template <class T1, class T2>
207 std::istream & operator >> (std::istream & is, std::pair<T1,T2> & t)
208 {
209 std::string s;
210 if (getline(is, s, ',')) {
211 if constexpr (std::is_same<T1,std::string>::value) {
212 t.first = s;
213 }
214 else {
215 std::istringstream iss(s);
216 iss >> t.first;
217 }
218 getline(is, s);
219 if constexpr (std::is_same<T2,std::string>::value) {
220 t.second = s;
221 }
222 else {
223 std::istringstream iss(s);
224 iss >> t.second;
225 }
226 }
227 return is;
228 }
229
230 //------------------------------------------------------------------------
232 //------------------------------------------------------------------------
233 template <class T, size_t N>
234 std::istream & operator >> (std::istream & is, std::array<T,N> & a)
235 {
236 for (auto i = 0; i < N; ++i) {
237 if (is) {
238 std::string s;
239 if (std::getline(is, s, ',')) {
240 std::istringstream iss(s);
241 T e;
242 iss >> e;
243 a[i] = e;
244 }
245 }
246 else {
247 break;
248 }
249 }
250 if (is.rdstate() & std::ios_base::eofbit) {
251 is.clear();
252 }
253 return is;
254 }
255
256 } // namespace ArgumentIO
257
258} // namespace Dwm
259
260//---------------------------- emacs settings -----------------------------
261// Local Variables:
262// mode: C++
263// tab-width: 2
264// indent-tabs-mode: nil
265// c-basic-offset: 2
266// End:
267//-------------------------------------------------------------------------
std::ostream & operator<<(std::ostream &os, const Dwm::ProcessInfo &pi)
ostream output operator
tuple helper templates