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 { return a > b ? a : b; }
107
112 template <typename T>
113 static auto Max(const T &a, const T &b)
114 -> typename std::enable_if<!std::is_scalar<T>::value, T>::type
115 { return a > b ? a : b; }
116
121 template <typename T>
122 static constexpr auto Min(T a, T b) noexcept
123 -> typename std::enable_if<std::is_scalar<T>::value, T>::type
124 { return a < b ? a : b; }
125
130 template <typename T>
131 static auto Min(const T &a, const T &b)
132 -> typename std::enable_if<!std::is_scalar<T>::value, T>::type
133 { return a < b ? a : b; }
134
138 template <typename... Args>
139 static std::wstring BuildStr(const Args &...args)
140 {
141 std::wstringstream wss;
142 int _[]{(_BuildStr(wss, args), 0)...};
143 return wss.str();
144 }
145
146 private:
150 template <typename T>
151 static auto _BuildStr(std::wostream &wos, const T &arg)
152 -> typename std::enable_if<!_IsProperty<T>::value && !_HasToString<T>::value>::type
153 { wos << arg; }
154
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 { _BuildStr(wos, arg.ToString()); }
162
166 template <typename T>
167 static auto _BuildStr(std::wostream &wos, const T &prop)
168 -> typename std::enable_if<_IsProperty<T>::value>::type
169 { _BuildStr(wos, prop.Get()); }
170
174 static void _BuildStr(std::wostream &wos, bool b)
175 { wos << (b ? L"true" : L"false"); }
176
180 static void _BuildStr(std::wostream &wos, const char *str)
181 { wos << ToWideStr(str); }
182
186 static void _BuildStr(std::wostream &wos, const std::string &str)
187 { wos << ToWideStr(str); }
188
192 template <typename T>
193 static void _BuildStr(std::wostream &wos, const std::vector<T> &vec)
194 {
195 auto beg = vec.begin();
196 auto end = vec.end();
197 wos << L"[";
198 for (auto it = beg; it != end; ++it) {
199 if (it != beg)
200 wos << L", ";
201 _BuildStr(wos, *it);
202 }
203 wos << L"]";
204 }
205
209 template <typename TKey, typename TVal>
210 static void _BuildStr(std::wostream &wos, const std::map<TKey, TVal> &map)
211 {
212 auto beg = map.begin();
213 auto end = map.end();
214 wos << L"{";
215 for (auto it = beg; it != end; ++it) {
216 if (it != beg)
217 wos << L", ";
218 _BuildStr(wos, it->first);
219 wos << L":";
220 _BuildStr(wos, it->second);
221 }
222 wos << L"}";
223 }
224
228 template <typename TKey, typename TVal>
229 static void _BuildStr(std::wostream &wos, const std::unordered_map<TKey, TVal> &map)
230 {
231 auto beg = map.begin();
232 auto end = map.end();
233 wos << L"{";
234 for (auto it = beg; it != end; ++it) {
235 if (it != beg)
236 wos << L", ";
237 _BuildStr(wos, it->first);
238 wos << L":";
239 _BuildStr(wos, it->second);
240 }
241 wos << L"}";
242 }
243 };
244}
值转换器接口
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:113
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:122
static std::wstring TrimEnd(const std::wstring &str)
删除串尾空白字符
static std::wstring BuildStr(const Args &...args)
拼接字符串,也可使用此函数将其他类型转为wstring
Definition Utils.h:139
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:131
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4
判断一个类型是否有ToString方法
Definition Internal.h:55