SimpleWindow
载入中...
搜索中...
未找到
Utils.h
1#pragma once
2
3#include "Internal.h"
4#include "Property.h"
5#include <map>
6#include <sstream>
7#include <string>
8#include <unordered_map>
9#include <vector>
10
11namespace sw
12{
16 class Utils
17 {
18 private:
22 Utils() = delete;
23
24 public:
29 static void UseUtf8Encoding(bool useUtf8);
30
36 static std::wstring ToWideStr(const std::string &str);
37
43 static std::string ToMultiByteStr(const std::wstring &wstr);
44
51 static std::wstring ToWideStr(const std::string &str, bool utf8);
52
59 static std::string ToMultiByteStr(const std::wstring &wstr, bool utf8);
60
66 static std::wstring Trim(const std::wstring &str);
67
73 static std::wstring TrimStart(const std::wstring &str);
74
80 static std::wstring TrimEnd(const std::wstring &str);
81
88 static std::vector<std::wstring> Split(const std::wstring &str, const std::wstring &delimiter);
89
96 static std::wstring FormatStr(const wchar_t *fmt, ...);
97
98 public:
103 template <typename T>
104 static constexpr auto Max(T a, T b) noexcept
105 -> typename std::enable_if<std::is_scalar<T>::value, T>::type
106 {
107 return a > b ? a : b;
108 }
109
114 template <typename T>
115 static auto Max(const T &a, const T &b)
116 -> typename std::enable_if<!std::is_scalar<T>::value, T>::type
117 {
118 return a > b ? a : b;
119 }
120
125 template <typename T>
126 static constexpr auto Min(T a, T b) noexcept
127 -> typename std::enable_if<std::is_scalar<T>::value, T>::type
128 {
129 return a < b ? a : b;
130 }
131
136 template <typename T>
137 static auto Min(const T &a, const T &b)
138 -> typename std::enable_if<!std::is_scalar<T>::value, T>::type
139 {
140 return a < b ? a : b;
141 }
142
146 template <typename... Args>
147 static std::wstring BuildStr(const Args &...args)
148 {
149 std::wstringstream wss;
150 int _[]{(_BuildStr(wss, args), 0)...};
151 return wss.str();
152 }
153
154 private:
158 template <typename T>
159 static auto _BuildStr(std::wostream &wos, const T &arg)
160 -> typename std::enable_if<!_IsProperty<T>::value && !_HasToString<T>::value>::type
161 {
162 wos << arg;
163 }
164
168 template <typename T>
169 static auto _BuildStr(std::wostream &wos, const T &arg)
170 -> typename std::enable_if<!_IsProperty<T>::value && _HasToString<T>::value>::type
171 {
172 _BuildStr(wos, arg.ToString());
173 }
174
178 template <typename T>
179 static auto _BuildStr(std::wostream &wos, const T &prop)
180 -> typename std::enable_if<_IsProperty<T>::value>::type
181 {
182 _BuildStr(wos, prop.Get());
183 }
184
188 static void _BuildStr(std::wostream &wos, bool b)
189 {
190 wos << (b ? L"true" : L"false");
191 }
192
196 static void _BuildStr(std::wostream &wos, const char *str)
197 {
198 wos << ToWideStr(str);
199 }
200
204 static void _BuildStr(std::wostream &wos, const std::string &str)
205 {
206 wos << ToWideStr(str);
207 }
208
212 template <typename T>
213 static void _BuildStr(std::wostream &wos, const std::vector<T> &vec)
214 {
215 auto beg = vec.begin();
216 auto end = vec.end();
217 wos << L"[";
218 for (auto it = beg; it != end; ++it) {
219 if (it != beg)
220 wos << L", ";
221 _BuildStr(wos, *it);
222 }
223 wos << L"]";
224 }
225
229 template <typename TKey, typename TVal>
230 static void _BuildStr(std::wostream &wos, const std::map<TKey, TVal> &map)
231 {
232 auto beg = map.begin();
233 auto end = map.end();
234 wos << L"{";
235 for (auto it = beg; it != end; ++it) {
236 if (it != beg)
237 wos << L", ";
238 _BuildStr(wos, it->first);
239 wos << L":";
240 _BuildStr(wos, it->second);
241 }
242 wos << L"}";
243 }
244
248 template <typename TKey, typename TVal>
249 static void _BuildStr(std::wostream &wos, const std::unordered_map<TKey, TVal> &map)
250 {
251 auto beg = map.begin();
252 auto end = map.end();
253 wos << L"{";
254 for (auto it = beg; it != end; ++it) {
255 if (it != beg)
256 wos << L", ";
257 _BuildStr(wos, it->first);
258 wos << L":";
259 _BuildStr(wos, it->second);
260 }
261 wos << L"}";
262 }
263 };
264}
值转换器接口
Definition IValueConverter.h:14
工具类
Definition Utils.h:17
static void UseUtf8Encoding(bool useUtf8)
设置Utils类是否使用UTF-8编码进行字符串转换(默认启用)
static std::string ToMultiByteStr(const std::wstring &wstr, bool utf8)
将宽字符串转为窄字符串
static std::wstring FormatStr(const wchar_t *fmt,...)
格式化字符串,类似于 swprintf,但返回一个动态分配的 std::wstring
static constexpr auto Max(T a, T b) noexcept -> typename std::enable_if< std::is_scalar< T >::value, T >::type
取两值中的较大值
Definition Utils.h:104
static auto Max(const T &a, const T &b) -> typename std::enable_if<!std::is_scalar< T >::value, T >::type
取两值中的较大值
Definition Utils.h:115
static std::vector< std::wstring > Split(const std::wstring &str, const std::wstring &delimiter)
对字符串按照指定分隔符进行拆分
static constexpr auto Min(T a, T b) noexcept -> typename std::enable_if< std::is_scalar< T >::value, T >::type
取两值中的较小值
Definition Utils.h:126
static std::wstring TrimEnd(const std::wstring &str)
删除串尾空白字符
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:147
static std::string ToMultiByteStr(const std::wstring &wstr)
将宽字符串转为窄字符串
static std::wstring Trim(const std::wstring &str)
删除首尾空白字符
static std::wstring TrimStart(const std::wstring &str)
删除串首空白字符
static std::wstring ToWideStr(const std::string &str)
将窄字符串转为宽字符串
static std::wstring ToWideStr(const std::string &str, bool utf8)
将窄字符串转为宽字符串
static auto Min(const T &a, const T &b) -> typename std::enable_if<!std::is_scalar< T >::value, T >::type
取两值中的较小值
Definition Utils.h:137
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4
判断一个类型是否有ToString方法
Definition Internal.h:55