gramods
Loading...
Searching...
No Matches
Stringify.hh
1
2#ifndef GRAMODS_CORE_STRINGIFY
3#define GRAMODS_CORE_STRINGIFY
4
5#include <sstream>
6#include <vector>
7#include <map>
8
9#define GM_STR(X) (static_cast<std::stringstream&>(std::stringstream().flush() << X).str())
10
11BEGIN_NAMESPACE_GMCORE;
12
13template<class T>
14std::string stringify(std::vector<T> list) {
15
16 std::stringstream ss;
17 bool is_first = true;
18
19 for (auto &item : list) {
20 if (!is_first) ss << ", ";
21 else is_first = false;
22 ss << item;
23 }
24
25 return ss.str();
26}
27
28template<class T1, class T2>
29std::string stringify(std::map<T1, T2> list, bool print_first = true) {
30
31 std::stringstream ss;
32 bool is_first = true;
33
34 if (print_first)
35 for (auto &item : list) {
36 if (!is_first) ss << ", ";
37 else is_first = false;
38 ss << item.first;
39 }
40 else
41 for (auto &item : list) {
42 if (!is_first) ss << ", ";
43 else is_first = false;
44 ss << item.second;
45 }
46
47 return ss.str();
48}
49
50END_NAMESPACE_GMCORE;
51
52#endif