Newer
Older

Christian Godenschwager
committed
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
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla is free software: you can
// redistribute it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
//
// waLBerla is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file STLIO.h
//! \ingroup core
//! \author Michael Kuron <mkuron@icp.uni-stuttgart.de>
//
//======================================================================================================================
#pragma once
#include <vector>
#include <iostream>
namespace walberla {
template<typename T>
class ParameterList: public std::vector<T> {
public:
ParameterList(): std::vector<T>() {};
template<typename C> ParameterList(C v): std::vector<T>(v.begin(), v.end()) {}
template<typename C> operator C() { return C(std::vector<T>::begin(), std::vector<T>::end()); }
};
//**********************************************************************************************************************
/*!\fn std::ostream& operator<<( std::ostream& os, const vector<Type>& v )
// \brief Global output operator for vectors.
//
// \param os Reference to the output stream.
// \param v Reference to a constant vector object.
// \return Reference to the output stream.
*/
template< typename Type >
std::ostream& operator<<( std::ostream& os, const ParameterList<Type>& v )
{
os << "<";
for (auto it = v.begin(); it != v.end(); ++it)
{
os << *it;
if (it != v.end()-1)
os << ",";
}
os << ">";
return os;
}
//**********************************************************************************************************************
//**********************************************************************************************************************
/*! RAII Object to read an STL container (e.g. vector or list) from a stream
*/
//**********************************************************************************************************************
template< typename Container >
class ContainerStreamReader
{
public:
ContainerStreamReader( std::istream& is ) : is_( is ), success_(false)
{
}
~ContainerStreamReader()
{
if (!success_)
setFailed();
restoreFlags();
}
void read( Container & v )
{
if( !is_ )
return;
storeState();
is_ >> std::skipws;
char character;
if( !( is_ >> character ) || character != '<' )
return;
typename Container::value_type x;
do {
if( !( is_ >> x ) )
return;
v.push_back( x );
if( !( is_ >> character ) )
return;
} while( character == ',' );
if( character != '>' )
return;
success_ = true;
}
private:
void storeState()
{
oldPos_ = is_.tellg();
oldFlags_ = is_.flags();
}
void restoreFlags()
{
is_.flags( oldFlags_ );
}
void setFailed()
{
is_.clear();
is_.seekg( oldPos_ );
is_.setstate( std::istream::failbit );
}
std::istream& is_;
std::istream::pos_type oldPos_;
std::istream::fmtflags oldFlags_;
bool success_;
};
//**********************************************************************************************************************
/*!\fn std::istream& operator>>( std::istream& is, vector<Type>& v )
// \brief Global input operator for vectors.
//
// \param is Reference to the input stream.
// \param v Reference to a vector object.
// \return The input stream.
*/
template< typename Type >
std::istream& operator>>( std::istream& is, ParameterList<Type>& v )
{
ContainerStreamReader< ParameterList<Type> > reader( is );
reader.read( v );
return is;
}
//**********************************************************************************************************************
}