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:
102 template <typename T>
103 static constexpr inline T Max(const T &a, const T &b)
104 {
105 return a > b ? a : b;
106 }
107
111 template <typename T>
112 static constexpr inline T Min(const T &a, const T &b)
113 {
114 return a < b ? a : b;
115 }
116
120 template <typename... Args>
121 static std::wstring BuildStr(const Args &...args)
122 {
123 std::wstringstream wss;
124 int _[]{(_BuildStr(wss, args), 0)...};
125 return wss.str();
126 }
127
128 private:
132 template <typename T>
133 static auto _BuildStr(std::wostream &wos, const T &arg)
134 -> typename std::enable_if<!_IsProperty<T>::value && !_HasToString<T>::value>::type
135 {
136 wos << arg;
137 }
138
142 template <typename T>
143 static auto _BuildStr(std::wostream &wos, const T &arg)
144 -> typename std::enable_if<!_IsProperty<T>::value && _HasToString<T>::value>::type
145 {
146 Utils::_BuildStr(wos, arg.ToString());
147 }
148
152 template <typename T>
153 static auto _BuildStr(std::wostream &wos, const T &prop)
154 -> typename std::enable_if<_IsProperty<T>::value>::type
155 {
156 Utils::_BuildStr(wos, prop.Get());
157 }
158
162 static void _BuildStr(std::wostream &wos, bool b)
163 {
164 wos << (b ? L"true" : L"false");
165 }
166
170 static void _BuildStr(std::wostream &wos, const char *str)
171 {
172 wos << ToWideStr(str);
173 }
174
178 static void _BuildStr(std::wostream &wos, const std::string &str)
179 {
180 wos << ToWideStr(str);
181 }
182
186 template <typename T>
187 static void _BuildStr(std::wostream &wos, const std::vector<T> &vec)
188 {
189 auto beg = vec.begin();
190 auto end = vec.end();
191 wos << L"[";
192 for (auto it = beg; it != end; ++it) {
193 if (it != beg)
194 wos << L", ";
195 _BuildStr(wos, *it);
196 }
197 wos << L"]";
198 }
199
203 template <typename TKey, typename TVal>
204 static void _BuildStr(std::wostream &wos, const std::map<TKey, TVal> &map)
205 {
206 auto beg = map.begin();
207 auto end = map.end();
208 wos << L"{";
209 for (auto it = beg; it != end; ++it) {
210 if (it != beg)
211 wos << L", ";
212 _BuildStr(wos, it->first);
213 wos << L":";
214 _BuildStr(wos, it->second);
215 }
216 wos << L"}";
217 }
218
222 template <typename TKey, typename TVal>
223 static void _BuildStr(std::wostream &wos, const std::unordered_map<TKey, TVal> &map)
224 {
225 auto beg = map.begin();
226 auto end = map.end();
227 wos << L"{";
228 for (auto it = beg; it != end; ++it) {
229 if (it != beg)
230 wos << L", ";
231 _BuildStr(wos, it->first);
232 wos << L":";
233 _BuildStr(wos, it->second);
234 }
235 wos << L"}";
236 }
237 };
238}
值转换器接口
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 T Min(const T &a, const T &b)
取两值中的较小值
Definition Utils.h:112
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:121
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 constexpr T Max(const T &a, const T &b)
取两值中的较大值
Definition Utils.h:103
static std::wstring ToWideStr(const std::string &str)
将窄字符串转为宽字符串
static std::wstring ToWideStr(const std::string &str, bool utf8)
将窄字符串转为宽字符串
判断一个类型是否有ToString方法
Definition Internal.h:55