Apache Mesos
stringify.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_STRINGIFY_HPP__
14 #define __STOUT_STRINGIFY_HPP__
15 
16 #include <iostream> // For 'std::cerr' and 'std::endl'.
17 #include <list>
18 #include <map>
19 #include <set>
20 #include <sstream> // For 'std::ostringstream'.
21 #include <string>
22 #include <vector>
23 
24 #ifdef __WINDOWS__
25 // `codecvt` is not available on older versions of Linux. Until it is needed on
26 // other platforms, it's easiest to just build the UTF converter for Windows.
27 #include <codecvt>
28 #include <locale>
29 #endif // __WINDOWS__
30 
31 #include "abort.hpp"
32 #include "hashmap.hpp"
33 #include "set.hpp"
34 
35 template <typename T>
36 std::string stringify(const T& t)
37 {
38  std::ostringstream out;
39  out << t;
40  if (!out.good()) {
41  ABORT("Failed to stringify!");
42  }
43  return out.str();
44 }
45 
46 
47 // We provide an explicit overload for strings so we do not incur the overhead
48 // of a stringstream in generic code (e.g., when stringifying containers of
49 // strings below).
50 inline std::string stringify(const std::string& str)
51 {
52  return str;
53 }
54 
55 
56 #ifdef __WINDOWS__
57 inline std::string stringify(const std::wstring& str)
58 {
59  // Convert UTF-16 `wstring` to UTF-8 `string`.
60  static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>
61  converter(
62  "UTF-16 to UTF-8 conversion failed",
63  L"UTF-16 to UTF-8 conversion failed");
64 
65  return converter.to_bytes(str);
66 }
67 
68 
69 inline std::wstring wide_stringify(const std::string& str)
70 {
71  // Convert UTF-8 `string` to UTF-16 `wstring`.
72  static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>
73  converter(
74  "UTF-8 to UTF-16 conversion failed",
75  L"UTF-8 to UTF-16 conversion failed");
76 
77  return converter.from_bytes(str);
78 }
79 #endif // __WINDOWS__
80 
81 
82 inline std::string stringify(bool b)
83 {
84  return b ? "true" : "false";
85 }
86 
87 
88 template <typename T>
89 std::string stringify(const std::set<T>& set)
90 {
91  std::ostringstream out;
92  out << "{ ";
93  typename std::set<T>::const_iterator iterator = set.begin();
94  while (iterator != set.end()) {
95  out << stringify(*iterator);
96  if (++iterator != set.end()) {
97  out << ", ";
98  }
99  }
100  out << " }";
101  return out.str();
102 }
103 
104 
105 template <typename T>
106 std::string stringify(const std::list<T>& list)
107 {
108  std::ostringstream out;
109  out << "[ ";
110  typename std::list<T>::const_iterator iterator = list.begin();
111  while (iterator != list.end()) {
112  out << stringify(*iterator);
113  if (++iterator != list.end()) {
114  out << ", ";
115  }
116  }
117  out << " ]";
118  return out.str();
119 }
120 
121 
122 template <typename T>
123 std::string stringify(const std::vector<T>& vector)
124 {
125  std::ostringstream out;
126  out << "[ ";
127  typename std::vector<T>::const_iterator iterator = vector.begin();
128  while (iterator != vector.end()) {
129  out << stringify(*iterator);
130  if (++iterator != vector.end()) {
131  out << ", ";
132  }
133  }
134  out << " ]";
135  return out.str();
136 }
137 
138 
139 template <typename K, typename V>
140 std::string stringify(const std::map<K, V>& map)
141 {
142  std::ostringstream out;
143  out << "{ ";
144  typename std::map<K, V>::const_iterator iterator = map.begin();
145  while (iterator != map.end()) {
146  out << stringify(iterator->first);
147  out << ": ";
148  out << stringify(iterator->second);
149  if (++iterator != map.end()) {
150  out << ", ";
151  }
152  }
153  out << " }";
154  return out.str();
155 }
156 
157 
158 template <typename T>
159 std::string stringify(const hashset<T>& set)
160 {
161  std::ostringstream out;
162  out << "{ ";
163  typename hashset<T>::const_iterator iterator = set.begin();
164  while (iterator != set.end()) {
165  out << stringify(*iterator);
166  if (++iterator != set.end()) {
167  out << ", ";
168  }
169  }
170  out << " }";
171  return out.str();
172 }
173 
174 
175 template <typename K, typename V>
176 std::string stringify(const hashmap<K, V>& map)
177 {
178  std::ostringstream out;
179  out << "{ ";
180  typename hashmap<K, V>::const_iterator iterator = map.begin();
181  while (iterator != map.end()) {
182  out << stringify(iterator->first);
183  out << ": ";
184  out << stringify(iterator->second);
185  if (++iterator != map.end()) {
186  out << ", ";
187  }
188  }
189  out << " }";
190  return out.str();
191 }
192 
193 #endif // __STOUT_STRINGIFY_HPP__
#define ABORT(...)
Definition: abort.hpp:40
Definition: hashset.hpp:53
Definition: hashmap.hpp:38
Iterable< V > map(F &&f, const Iterable< U, Us...> &input)
Definition: lambda.hpp:45
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
std::string stringify(int flags)