12 #ifndef GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H
13 #define GAUDIPROPERTYPARSERS_PARSERVALUETOSTREAM_H 1
27 #include <unordered_set>
57 std::ostream&
toStream(
const TYPE& obj, std::ostream&
s );
70 template <
class ITERATOR>
71 inline std::ostream&
toStream( ITERATOR first,
74 const std::string& open,
75 const std::string& close,
76 const std::string& delim );
84 inline std::ostream&
toStream(
const std::string& obj, std::ostream&
s ) {
return s << std::quoted( obj,
'\'' ); }
89 inline std::ostream&
toStream(
const bool obj, std::ostream&
s ) {
return s << ( obj ?
"True" :
"False" ); }
94 inline std::ostream&
toStream(
const float obj, std::ostream&
s,
const int prec = 6 ) {
95 const int p =
static_cast<int>(
s.precision() );
96 return s << std::setprecision( prec ) << obj << std::setprecision( p );
102 inline std::ostream&
toStream(
const double obj, std::ostream&
s,
const int prec = 8 ) {
103 const int p =
static_cast<int>(
s.precision() );
104 return s << std::setprecision( prec ) << obj << std::setprecision( p );
110 inline std::ostream&
toStream(
const long double obj, std::ostream&
s,
const int prec = 10 ) {
111 const int p =
static_cast<int>(
s.precision() );
112 return s << std::setprecision( prec ) << obj << std::setprecision( p );
122 template <
class KTYPE,
class VTYPE>
123 inline std::ostream&
toStream(
const std::pair<KTYPE, VTYPE>& obj, std::ostream&
s ) {
124 return toStream( obj.second,
toStream( obj.first,
s <<
"( " ) <<
" , " ) <<
" )";
127 template <
typename... Args>
128 inline std::ostream&
toStream(
const std::tuple<Args...>& tuple, std::ostream&
s );
137 template <
class TYPE,
class ALLOCATOR>
138 inline std::ostream&
toStream(
const std::vector<TYPE, ALLOCATOR>& obj, std::ostream&
s ) {
139 return toStream( obj.begin(), obj.end(),
s,
"[ ",
" ]",
" , " );
148 template <
class TYPE,
class ALLOCATOR>
149 inline std::ostream&
toStream(
const std::list<TYPE, ALLOCATOR>& obj, std::ostream&
s ) {
150 return toStream( obj.begin(), obj.end(),
s,
"[ ",
" ]",
" , " );
159 template <
class TYPE,
class CMP,
class ALLOCATOR>
160 inline std::ostream&
toStream(
const std::set<TYPE, CMP, ALLOCATOR>& obj, std::ostream&
s ) {
161 return toStream( obj.begin(), obj.end(),
s,
"[ ",
" ]",
" , " );
167 template <
class TYPE,
class HASH,
class CMP,
class ALLOCATOR>
168 inline std::ostream&
toStream(
const std::unordered_set<TYPE, HASH, CMP, ALLOCATOR>& obj, std::ostream&
s ) {
169 auto ordered = std::set( obj.begin(), obj.end() );
170 return obj.empty() ?
s <<
"set()" :
toStream( ordered.begin(), ordered.end(),
s,
"{ ",
" }",
" , " );
180 template <
class KTYPE,
class VTYPE,
class CMP,
class ALLOCATOR>
181 inline std::ostream&
toStream(
const std::map<KTYPE, VTYPE, CMP, ALLOCATOR>& obj, std::ostream&
s ) {
184 []( std::ostream& os,
const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
198 template <
class KTYPE,
class VTYPE,
class CMP,
class ALLOCATOR>
202 []( std::ostream& os,
const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
216 template <
class KTYPE,
class VTYPE,
class MAP>
220 []( std::ostream& os,
const std::pair<const KTYPE, VTYPE>& i ) -> std::ostream& {
234 template <
class KTYPE,
class VTYPE,
class HASH,
class MAP>
244 template <
class TYPE,
unsigned int N>
245 std::ostream&
toStream(
const TYPE ( &obj )[
N], std::ostream&
s ) {
246 if constexpr (
N == 1 ) {
247 return toStream( obj[0],
s <<
"( " ) <<
" , )";
249 return toStream( obj, obj +
N,
s,
"( ",
" )",
" , " );
257 template <
class TYPE, std::
size_t N>
258 std::ostream&
toStream(
const std::array<TYPE, N>& obj, std::ostream&
s ) {
259 if constexpr (
N == 1 ) {
260 return toStream( obj[0],
s <<
"( " ) <<
" , )";
270 template <
unsigned int N>
271 std::ostream&
toStream(
const char ( &obj )[
N], std::ostream&
s ) {
272 return toStream( std::string( obj, obj +
N ),
s );
279 inline std::ostream&
toStream(
const char* obj, std::ostream&
s ) {
return toStream( std::string( obj ),
s ); }
286 template <
class TYPE>
287 inline std::ostream&
toStream(
const TYPE& obj, std::ostream&
s ) {
292 template <
class Tuple, std::
size_t N>
294 static std::ostream&
toStream(
const Tuple&
t, std::ostream&
s ) {
300 template <
class Tuple>
302 static std::ostream&
toStream(
const Tuple&
t, std::ostream&
s ) {
313 template <
typename... Args>
314 inline std::ostream&
toStream(
const std::tuple<Args...>& tuple, std::ostream&
s ) {
316 if constexpr ( std::tuple_size_v<std::tuple<Args...>> == 1 ) {
333 template <
class ITERATOR>
337 const std::string& open,
338 const std::string& close,
339 const std::string& delim )
344 []( std::ostream& os, ref_t i ) -> std::ostream& {
return toStream( i, os ); } )
356 template <
class TYPE>
358 std::ostringstream
s;
359 std::ios::fmtflags orig_flags =
s.flags();
360 s.setf( std::ios::showpoint );
362 s.flags( orig_flags );