DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
da_qstring_cast.h
1#ifndef DA_QSTRING_CAST_H
2#define DA_QSTRING_CAST_H
6#include <cmath>
7#include <QString>
8#include <QByteArray>
9#include <QDate>
10#include <QDateTime>
11#include <QTime>
12#include <type_traits>
13#include <string>
14#ifndef DA_QSTRINGCAST_TOQSTRING_NUMBER
15#define DA_QSTRINGCAST_TOQSTRING_NUMBER(type) \
16 template<> \
17 inline QString toQString< type >(const type& value) \
18 { \
19 return QString::number(value); \
20 }
21#endif
22
23#ifndef DA_QSTRINGCAST_FROMQSTRING_NUMBER
24#define DA_QSTRINGCAST_FROMQSTRING_NUMBER(type, castFunName) \
25 template<> \
26 inline bool fromQString< type >(const QString& str, type& result) \
27 { \
28 bool ok; \
29 auto r = str.castFunName(&ok); \
30 if (ok) { \
31 result = r; \
32 } \
33 return ok; \
34 }
35#endif
36
37#ifndef DA_QSTRINGCAST_FROMQSTRING_DIRECT
38#define DA_QSTRINGCAST_FROMQSTRING_DIRECT(type, castFunName) \
39 template<> \
40 inline bool fromQString< type >(const QString& str, type& result) \
41 { \
42 result = str.castFunName(); \
43 return true; \
44 }
45#endif
46namespace DA
47{
54inline int calculatePrecision(double value);
55
56// 将任何类型转换为QString
57template< typename T >
71inline QString toQString(const T& value)
72{
73 static_assert(true, "You must provide a specialization of toQString for your type.");
74 return QString(); // 这条语句实际上不会被执行,因为静态断言会失败
75}
76// 用户为特定类型提供的特化示例
77DA_QSTRINGCAST_TOQSTRING_NUMBER(int)
78DA_QSTRINGCAST_TOQSTRING_NUMBER(unsigned int)
79DA_QSTRINGCAST_TOQSTRING_NUMBER(long)
80DA_QSTRINGCAST_TOQSTRING_NUMBER(unsigned long)
81DA_QSTRINGCAST_TOQSTRING_NUMBER(long long)
82DA_QSTRINGCAST_TOQSTRING_NUMBER(unsigned long long)
83
84
89template<>
90inline QString toQString< double >(const double& value)
91{
92 // 计算浮点数需要的精度
93 int p = calculatePrecision(value);
94 return QString::number(value, 10, p);
95}
96
102template<>
103inline QString toQString< float >(const float& value)
104{
105 // 计算浮点数需要的精度
106 int p = calculatePrecision(value);
107 return QString::number(value, 10, p);
108}
109
116template< typename T >
117inline bool fromQString(const QString& str, T& result)
118{
119 static_assert(true, "You must provide a specialization of fromQString for your type.");
120 return false; // 这条语句实际上不会被执行,因为静态断言会失败
121}
122
123DA_QSTRINGCAST_FROMQSTRING_NUMBER(short, toShort)
124DA_QSTRINGCAST_FROMQSTRING_NUMBER(unsigned short, toUShort)
125DA_QSTRINGCAST_FROMQSTRING_NUMBER(int, toInt)
126DA_QSTRINGCAST_FROMQSTRING_NUMBER(unsigned int, toUInt)
127DA_QSTRINGCAST_FROMQSTRING_NUMBER(long, toLong)
128DA_QSTRINGCAST_FROMQSTRING_NUMBER(unsigned long, toULong)
129DA_QSTRINGCAST_FROMQSTRING_NUMBER(long long, toLongLong)
130DA_QSTRINGCAST_FROMQSTRING_NUMBER(unsigned long long, toULongLong)
131DA_QSTRINGCAST_FROMQSTRING_NUMBER(float, toFloat)
132DA_QSTRINGCAST_FROMQSTRING_NUMBER(double, toDouble)
133DA_QSTRINGCAST_FROMQSTRING_DIRECT(std::string, toStdString)
134DA_QSTRINGCAST_FROMQSTRING_DIRECT(std::u16string, toStdU16String)
135DA_QSTRINGCAST_FROMQSTRING_DIRECT(std::u32string, toStdU32String)
136DA_QSTRINGCAST_FROMQSTRING_DIRECT(std::wstring, toStdWString)
137
138inline int calculatePrecision(double value)
139{
140 if (value == 0.0) {
141 return 1; // 避免除以零的情况,返回默认精度
142 }
143
144 // 计算浮点数的绝对值和其对数值
145 double absValue = std::fabs(value);
146 double log10Value = std::log10(absValue);
147
148 // 根据浮点数的对数值来确定精度
149 int precision = static_cast< int >(-std::floor(log10Value)) + 3; // 加3是为了保留两位非零有效数字
150 if (precision < 0) {
151 precision = 0; // 精度不能是负数
152 } else if (precision > 15) {
153 precision = 15; // 设置一个精度的上限,避免过高精度
154 }
155
156 return precision;
157}
158}
159#endif // DA_QSTRING_CAST_H
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44
int calculatePrecision(double value)
精度计算,可以通过此函数确认浮点数转换时所需要的精度 由于此函数使用在本文件的各种模板函数中,因此必须定义为内联
Definition da_qstring_cast.h:138
QString toQString< float >(const float &value)
特化float
Definition da_qstring_cast.h:103
QString toQString< double >(const double &value)
特化double
Definition da_qstring_cast.h:90
QString toQString(const T &value)
toQString
Definition da_qstring_cast.h:71
bool fromQString(const QString &str, T &result)
将QString转换为类型T,通过引用返回转换结果,并返回一个bool值表示转换是否成功
Definition da_qstring_cast.h:117