The Sparta Modeling Framework
Loading...
Searching...
No Matches
Printing.hpp
Go to the documentation of this file.
1// <Printing.hpp> -*- C++ -*-
2
3
4#pragma once
5
6#include <iostream>
7#include <string>
8#include <ostream>
9#include <vector>
10#include <sstream>
11
13
18namespace sparta
19{
20namespace utils
21{
22
28 BASE_DEC = 0,
30 BASE_OCT = 2
31 };
32
43 inline std::ios_base::fmtflags setIOSFlags(std::ios_base& stream, DisplayBase base) {
44 std::ios_base::fmtflags f;
45 switch(base){
46 case BASE_DEC:
47 f = stream.setf(std::ios::dec, std::ios::basefield);
48 break;
49 case BASE_HEX:
50 f = stream.setf(std::ios::hex, std::ios::basefield);
51 break;
52 case BASE_OCT:
53 f = stream.setf(std::ios::oct, std::ios::basefield);
54 break;
55 default:
56 SpartaException ex("Unsupported SPARTA display flag: ");
57 ex << base;
58 throw ex;
59 };
60 stream.setf(std::ios::showbase | std::ios::boolalpha);
61 return f;
62 }
63
79 template <class T>
80 inline std::string stringize_value(const std::vector<T>& v, DisplayBase base=BASE_DEC,
81 const std::string& string_quote="") {
82 std::stringstream out;
83 out << "[";
84 typename std::vector<T>::const_iterator itr = v.begin();
85 if(itr != v.end()){
86 std::ios_base::fmtflags old = setIOSFlags(out, base);
87 out << stringize_value((T)(*itr++), base, string_quote);
88 out.setf(old);
89 for(; itr != v.end(); ++itr){
90 out << ", ";
91 old = setIOSFlags(out, base);
92 out << stringize_value((T)(*itr), base, string_quote);
93 out.setf(old);
94 }
95 }
96 out << "]";
97 return out.str();
98 }
99
110 template <class T, class U>
111 inline std::string stringize_value(const std::pair<T,U>& p, DisplayBase base=BASE_DEC,
112 const std::string& string_quote="") {
113 std::stringstream out;
114 std::ios_base::fmtflags old = setIOSFlags(out, base);
115 out << stringize_value(p.first, base, string_quote) << ":"
116 << stringize_value(p.second, base, string_quote);
117 out.setf(old);
118 return out.str();
119 }
120
130 template <class... Ts>
131 inline std::string stringize_value(const std::tuple<Ts...>& t, DisplayBase base=BASE_DEC,
132 const std::string& string_quote="") {
133 std::stringstream out;
134 std::ios_base::fmtflags old = setIOSFlags(out, base);
135 stringize_tuple(out, t, base, string_quote, std::index_sequence_for<Ts...>{});
136 out.setf(old);
137 return out.str();
138 }
139
148 template <class T>
149 inline std::string stringize_value(const T& o, DisplayBase base=BASE_DEC,
150 const std::string& string_quote="") {
151 (void) string_quote;
152 std::stringstream out;
153 std::ios_base::fmtflags old = setIOSFlags(out, base);
154 out << o;
155 out.setf(old);
156 return out.str();
157 }
158
160 template <>
161 inline std::string stringize_value<std::string>(const std::string& o, DisplayBase base,
162 const std::string& string_quote) {
163 (void)base;
164 std::stringstream out;
165 out << string_quote << o << string_quote;
166 return out.str();
167 }
168
170 inline std::vector<std::string>& operator<< (std::vector<std::string>& v, const char * e){
171 v.push_back(e);
172 return v;
173 }
174
176 template <class T, class U>
177 inline std::vector<T>& operator<< (std::vector<T>& v, U& e){
178 const T& val = (const T&)e;
179 v.push_back(val);
180 return v;
181 }
182
184 template <class T, class U>
185 inline std::vector<T>& operator<< (std::vector<T>& v, const U& e){
186 const T& val = (const T&)e;
187 v.push_back(val);
188 return v;
189 }
190
192 template <class T, std::size_t... Is>
193 inline void stringize_tuple(std::stringstream& ss,
194 const T& t, DisplayBase base,
195 const std::string& string_quote,
196 std::index_sequence<Is...>) {
197 ((ss << (Is == 0 ? "" : ":") << stringize_value(std::get<Is>(t), base, string_quote)), ...);
198 }
199
200} // namespace utils
201} // namespace sparta
202
203
204namespace std {
206 template<class Ch,class T, class Tr>
207 inline std::basic_ostream<Ch,Tr>&
208 operator<< (std::basic_ostream<Ch,Tr>& out, std::vector<T> const & v){
210 return out;
211 }
212
214 template<class Ch, class T, class U, class Tr>
215 inline std::basic_ostream<Ch,Tr>&
216 operator<< (std::basic_ostream<Ch,Tr>& out, std::pair<T,U> const & p){
218 return out;
219 }
220
222 template<class Ch, class... Ts, class Tr>
223 inline std::basic_ostream<Ch,Tr>&
224 operator<< (std::basic_ostream<Ch,Tr>& out, std::tuple<Ts...> const & t){
226 return out;
227 }
228}
229
231inline std::vector<std::string>& operator<< (std::vector<std::string>& v, const char * e){
232 v.push_back(e);
233 return v;
234}
235
237template <class T, class U>
238inline std::vector<T>& operator<< (std::vector<T>& v, U& e){
239 const T& val = (const T&)e;
240 v.push_back(val);
241 return v;
242}
243
245template <class T, class U>
246inline std::vector<T>& operator<< (std::vector<T>& v, const U& e){
247 const T& val = (const T&)e;
248 v.push_back(val);
249 return v;
250}
251
252
void stringize_tuple(std::stringstream &ss, const T &t, DisplayBase base, const std::string &string_quote, std::index_sequence< Is... >)
Helper for printing STL tuple.
Definition Printing.hpp:193
std::vector< std::string > & operator<<(std::vector< std::string > &v, const char *e)
Specialization for populating STL vector of string with char*s.
Definition Printing.hpp:231
std::string stringize_value(const std::vector< T > &v, DisplayBase base=BASE_DEC, const std::string &string_quote="")
Converting a vector of intrinsic types (and std::string) to a string.
Definition Printing.hpp:80
DisplayBase
Numeric display options used by Parameter printing routines.
Definition Printing.hpp:27
@ BASE_OCT
Hex display.
Definition Printing.hpp:30
@ BASE_HEX
Decimal display.
Definition Printing.hpp:29
std::ios_base::fmtflags setIOSFlags(std::ios_base &stream, DisplayBase base)
Configure an ios stream to use given numeric base from sparta::DisplayBase.
Definition Printing.hpp:43
Exception class for all of Sparta.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Macros for handling exponential backoff.