SimpleWindow
载入中...
搜索中...
未找到
Utils.h
1#pragma once
2
3#include "Property.h"
4#include <map>
5#include <sstream>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10namespace sw
11{
15 class Utils
16 {
17 private:
18 Utils() = delete; // 删除构造函数
19
23 template <typename T, typename = void>
24 struct _HasToString : std::false_type {
25 };
26
30 template <typename T>
31 struct _HasToString<
32 T,
33 decltype(void(std::declval<T>().ToString()))> : std::true_type {
34 };
35
36 public:
43 static std::wstring ToWideStr(const std::string &str, bool utf8 = false);
44
51 static std::string ToMultiByteStr(const std::wstring &wstr, bool utf8 = false);
52
58 static std::wstring Trim(const std::wstring &str);
59
65 static std::wstring TrimStart(const std::wstring &str);
66
72 static std::wstring TrimEnd(const std::wstring &str);
73
80 static std::vector<std::wstring> Split(const std::wstring &str, const std::wstring &delimiter);
81
88 static std::wstring FormatStr(const wchar_t *fmt, ...);
89
90 public:
94 template <typename T>
95 static constexpr inline T Max(const T &a, const T &b)
96 {
97 return a > b ? a : b;
98 }
99
103 template <typename T>
104 static constexpr inline T Min(const T &a, const T &b)
105 {
106 return a < b ? a : b;
107 }
108
112 template <typename... Args>
113 static inline std::wstring BuildStr(const Args &...args)
114 {
115 std::wstringstream wss;
116 int _[]{(Utils::_BuildStr(wss, args), 0)...};
117 return wss.str();
118 }
119
120 private:
124 template <typename T>
125 static inline typename std::enable_if<!_IsProperty<T>::value && !_HasToString<T>::value, void>::type
126 _BuildStr(std::wostream &wos, const T &arg)
127 {
128 wos << arg;
129 }
130
134 template <typename T>
135 static inline typename std::enable_if<!_IsProperty<T>::value && _HasToString<T>::value, void>::type
136 _BuildStr(std::wostream &wos, const T &arg)
137 {
138 Utils::_BuildStr(wos, arg.ToString());
139 }
140
144 template <typename T>
145 static inline typename std::enable_if<_IsProperty<T>::value, void>::type
146 _BuildStr(std::wostream &wos, const T &prop)
147 {
148 Utils::_BuildStr(wos, prop.Get());
149 }
150
154 static inline void _BuildStr(std::wostream &wos, bool b)
155 {
156 wos << (b ? L"true" : L"false");
157 }
158
162 static inline void _BuildStr(std::wostream &wos, const char *str)
163 {
164 wos << Utils::ToWideStr(str);
165 }
166
170 static inline void _BuildStr(std::wostream &wos, const std::string &str)
171 {
172 wos << Utils::ToWideStr(str);
173 }
174
178 template <typename T>
179 static inline void _BuildStr(std::wostream &wos, const std::vector<T> &vec)
180 {
181 auto beg = vec.begin();
182 auto end = vec.end();
183 wos << L"[";
184 for (auto it = beg; it != end; ++it) {
185 if (it != beg)
186 wos << L", ";
187 Utils::_BuildStr(wos, *it);
188 }
189 wos << L"]";
190 }
191
195 template <typename TKey, typename TVal>
196 static inline void _BuildStr(std::wostream &wos, const std::map<TKey, TVal> &map)
197 {
198 auto beg = map.begin();
199 auto end = map.end();
200 wos << L"{";
201 for (auto it = beg; it != end; ++it) {
202 if (it != beg)
203 wos << L", ";
204 Utils::_BuildStr(wos, it->first);
205 wos << L":";
206 Utils::_BuildStr(wos, it->second);
207 }
208 wos << L"}";
209 }
210
214 template <typename TKey, typename TVal>
215 static inline void _BuildStr(std::wostream &wos, const std::unordered_map<TKey, TVal> &map)
216 {
217 auto beg = map.begin();
218 auto end = map.end();
219 wos << L"{";
220 for (auto it = beg; it != end; ++it) {
221 if (it != beg)
222 wos << L", ";
223 Utils::_BuildStr(wos, it->first);
224 wos << L":";
225 Utils::_BuildStr(wos, it->second);
226 }
227 wos << L"}";
228 }
229 };
230}
工具类
Definition Utils.h:16
static std::wstring FormatStr(const wchar_t *fmt,...)
格式化字符串,类似于 swprintf,但返回一个动态分配的 std::wstring
static std::string ToMultiByteStr(const std::wstring &wstr, bool utf8=false)
将宽字符串转为窄字符串
static constexpr T Min(const T &a, const T &b)
取两值中的较小值
Definition Utils.h:104
static std::vector< std::wstring > Split(const std::wstring &str, const std::wstring &delimiter)
对字符串按照指定分隔符进行拆分
static std::wstring TrimEnd(const std::wstring &str)
删除串尾空白字符
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:113
static std::wstring Trim(const std::wstring &str)
删除首尾空白字符
static std::wstring TrimStart(const std::wstring &str)
删除串首空白字符
static constexpr T Max(const T &a, const T &b)
取两值中的较大值
Definition Utils.h:95
static std::wstring ToWideStr(const std::string &str, bool utf8=false)
将窄字符串转为宽字符串