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 "error.hpp"
33 #include "hashmap.hpp"
34 #include "set.hpp"
35 
36 template <typename T>
37 std::string stringify(const T& t)
38 {
39  std::ostringstream out;
40  out << t;
41  if (!out.good()) {
42  ABORT("Failed to stringify!");
43  }
44  return out.str();
45 }
46 
47 
48 // We provide an explicit overload for strings so we do not incur the overhead
49 // of a stringstream in generic code (e.g., when stringifying containers of
50 // strings below).
51 inline std::string stringify(const std::string& str)
52 {
53  return str;
54 }
55 
56 
57 #ifdef __WINDOWS__
58 inline std::string stringify(const std::wstring& str)
59 {
60  // Convert UTF-16 `wstring` to UTF-8 `string`.
61  static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>
62  converter(
63  "UTF-16 to UTF-8 conversion failed",
64  L"UTF-16 to UTF-8 conversion failed");
65 
66  return converter.to_bytes(str);
67 }
68 
69 
70 inline std::wstring wide_stringify(const std::string& str)
71 {
72  // Convert UTF-8 `string` to UTF-16 `wstring`.
73  static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>
74  converter(
75  "UTF-8 to UTF-16 conversion failed",
76  L"UTF-8 to UTF-16 conversion failed");
77 
78  return converter.from_bytes(str);
79 }
80 #endif // __WINDOWS__
81 
82 
83 inline std::string stringify(bool b)
84 {
85  return b ? "true" : "false";
86 }
87 
88 
89 template <typename T>
90 std::string stringify(const std::set<T>& set)
91 {
92  std::ostringstream out;
93  out << "{ ";
94  typename std::set<T>::const_iterator iterator = set.begin();
95  while (iterator != set.end()) {
96  out << stringify(*iterator);
97  if (++iterator != set.end()) {
98  out << ", ";
99  }
100  }
101  out << " }";
102  return out.str();
103 }
104 
105 
106 template <typename T>
107 std::string stringify(const std::list<T>& list)
108 {
109  std::ostringstream out;
110  out << "[ ";
111  typename std::list<T>::const_iterator iterator = list.begin();
112  while (iterator != list.end()) {
113  out << stringify(*iterator);
114  if (++iterator != list.end()) {
115  out << ", ";
116  }
117  }
118  out << " ]";
119  return out.str();
120 }
121 
122 
123 template <typename T>
124 std::string stringify(const std::vector<T>& vector)
125 {
126  std::ostringstream out;
127  out << "[ ";
128  typename std::vector<T>::const_iterator iterator = vector.begin();
129  while (iterator != vector.end()) {
130  out << stringify(*iterator);
131  if (++iterator != vector.end()) {
132  out << ", ";
133  }
134  }
135  out << " ]";
136  return out.str();
137 }
138 
139 
140 template <typename K, typename V>
141 std::string stringify(const std::map<K, V>& map)
142 {
143  std::ostringstream out;
144  out << "{ ";
145  typename std::map<K, V>::const_iterator iterator = map.begin();
146  while (iterator != map.end()) {
147  out << stringify(iterator->first);
148  out << ": ";
149  out << stringify(iterator->second);
150  if (++iterator != map.end()) {
151  out << ", ";
152  }
153  }
154  out << " }";
155  return out.str();
156 }
157 
158 
159 template <typename T>
160 std::string stringify(const hashset<T>& set)
161 {
162  std::ostringstream out;
163  out << "{ ";
164  typename hashset<T>::const_iterator iterator = set.begin();
165  while (iterator != set.end()) {
166  out << stringify(*iterator);
167  if (++iterator != set.end()) {
168  out << ", ";
169  }
170  }
171  out << " }";
172  return out.str();
173 }
174 
175 
176 template <typename K, typename V>
177 std::string stringify(const hashmap<K, V>& map)
178 {
179  std::ostringstream out;
180  out << "{ ";
181  typename hashmap<K, V>::const_iterator iterator = map.begin();
182  while (iterator != map.end()) {
183  out << stringify(iterator->first);
184  out << ": ";
185  out << stringify(iterator->second);
186  if (++iterator != map.end()) {
187  out << ", ";
188  }
189  }
190  out << " }";
191  return out.str();
192 }
193 
194 
195 // TODO(chhsiao): This overload returns a non-const rvalue for consistency.
196 // Consider the following overloads instead for better performance:
197 // const std::string& stringify(const Error&);
198 // std::string stringify(Error&&);
199 inline std::string stringify(const Error& error)
200 {
201  return error.message;
202 }
203 
204 #endif // __STOUT_STRINGIFY_HPP__
Definition: errorbase.hpp:36
#define ABORT(...)
Definition: abort.hpp:40
Definition: hashset.hpp:53
Definition: hashmap.hpp:38
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
const std::string message
Definition: errorbase.hpp:46
Iterable< V > map(F &&f, const Iterable< U, Us... > &input)
Definition: lambda.hpp:46
std::string error(const std::string &msg, uint32_t code)
std::string stringify(const T &t)
Definition: stringify.hpp:37