MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
packages.hpp
1#ifndef MSTL_CORE_UTILITY_PACKAGES_HPP__
2#define MSTL_CORE_UTILITY_PACKAGES_HPP__
3#include "../interface/iobject.hpp"
4#include "../interface/ipackage.hpp"
5#include "../string/to_numerics.hpp"
6#include "../string/to_string.hpp"
8
9struct boolean : ipackage<boolean, bool>, iobject<boolean> {
10 using value_type = bool;
11 using base = ipackage<boolean, bool>;
12
13 MSTL_BUILD_PACKAGE_CONSTRUCTOR(boolean)
14
15 MSTL_NODISCARD static MSTL_CONSTEXPR20 string to_string(const value_type value) {
16 return boolean(value).to_string();
17 }
18 MSTL_NODISCARD MSTL_CONSTEXPR20 string to_string() const {
19 return value_ ? "true" : "false";
20 }
21
22 MSTL_NODISCARD static MSTL_CONSTEXPR20 boolean parse(const string_view lower) {
23 boolean obj;
24 string str(lower.trim());
25 try {
26 str.lowercase();
27 if (str == "true" || str == "yes" || str == "y") {
28 obj = true;
29 } else if (str == "false" || str == "no" || str == "n") {
30 obj = false;
31 } else {
32 obj = static_cast<bool>(to_int32(str.view(), nullptr, 10));
33 }
34 } catch (...) {
35 throw_exception(typecast_exception("Convert from string to boolean failed."));
36 }
37 return obj;
38 }
39
40 constexpr boolean operator !() const noexcept {
41 return boolean(!value_);
42 }
43};
44
45template <>
46struct package<bool> {
47 using type = boolean;
48};
49template <>
50struct unpackage<boolean> {
51 using type = bool;
52};
53
54
55#define __MSTL_BUILD_INTEGER_STRUCT(SIGN, UPPER, BYTE) \
56struct SIGN## integer## BYTE : iobject<SIGN## integer## BYTE>, ipackage<SIGN## integer## BYTE, SIGN## int## BYTE## _t> { \
57 using value_type = SIGN## int## BYTE## _t; \
58 using base = ipackage<SIGN## integer## BYTE, SIGN## int## BYTE## _t>; \
59 \
60 MSTL_BUILD_PACKAGE_CONSTRUCTOR(SIGN## integer## BYTE) \
61 \
62 MSTL_NODISCARD constexpr explicit operator bool() const noexcept { \
63 return value_ != _MSTL initialize<value_type>(); \
64 } \
65 \
66 MSTL_NODISCARD static MSTL_CONSTEXPR20 string to_string(const value_type value) { \
67 return _INNER __int_to_string_dispatch(value); \
68 } \
69 \
70 MSTL_NODISCARD MSTL_CONSTEXPR20 string to_string() const { \
71 return _INNER __int_to_string_dispatch(value_); \
72 } \
73 \
74 MSTL_NODISCARD static constexpr SIGN## integer## BYTE parse(const string_view str) { \
75 return SIGN## integer## BYTE{_MSTL to_## SIGN## int## BYTE (str)}; \
76 } \
77}; \
78template <> \
79struct package<SIGN## int## BYTE## _t> { \
80 using type = SIGN## integer## BYTE; \
81}; \
82template <> \
83struct unpackage<SIGN## integer## BYTE> { \
84 using type = SIGN## int## BYTE## _t; \
85};
86
87__MSTL_BUILD_INTEGER_STRUCT(,,16)
88__MSTL_BUILD_INTEGER_STRUCT(,,32)
89__MSTL_BUILD_INTEGER_STRUCT(,,64)
90__MSTL_BUILD_INTEGER_STRUCT(u,U,16)
91__MSTL_BUILD_INTEGER_STRUCT(u,U,32)
92__MSTL_BUILD_INTEGER_STRUCT(u,U,64)
93#undef __MSTL_BUILD_INTEGER_STRUCT
94
95
96#ifdef MSTL_PLATFORM_LINUX64__
97template <>
98struct package<long long> {
99 using type = integer64;
100};
101template <>
102struct package<unsigned long long> {
103 using type = uinteger64;
104};
105#else
106template <>
107struct package<long> {
108 using type = integer32;
109};
110template <>
111struct package<unsigned long> {
112 using type = uinteger32;
113};
114#endif
115
116
117struct float32 : iobject<float32>, ipackage<float32, float32_t> {
118 using value_type = float32_t;
119 using base = ipackage<float32, float32_t>;
120
121 MSTL_BUILD_PACKAGE_CONSTRUCTOR(float32)
122
123 MSTL_NODISCARD constexpr explicit operator bool() const noexcept {
124 return value_ != _MSTL initialize<value_type>();
125 }
126
127 MSTL_NODISCARD static MSTL_CONSTEXPR20 string to_string(const value_type value) {
128 return float32(value).to_string();
129 }
130 MSTL_NODISCARD MSTL_CONSTEXPR20 string to_string() const {
131 return _INNER __float_to_string<char>(value_);
132 }
133
134 MSTL_NODISCARD static constexpr float32 parse(const string_view str) {
135 return float32{_MSTL to_float32(str)};
136 }
137};
138
139template <>
140struct package<float32_t> {
141 using type = float32;
142};
143template <>
144struct unpackage<float32> {
145 using type = float32_t;
146};
147
148
149struct float64 : iobject<float64>, ipackage<float64, float64_t> {
150 using value_type = float64_t;
151 using base = ipackage<float64, float64_t>;
152
153 MSTL_BUILD_PACKAGE_CONSTRUCTOR(float64)
154
155 MSTL_NODISCARD constexpr explicit operator bool() const noexcept {
156 return value_ != _MSTL initialize<value_type>();
157 }
158
159 MSTL_NODISCARD static MSTL_CONSTEXPR20 string to_string(const value_type value) {
160 return float64(value).to_string();
161 }
162 MSTL_NODISCARD MSTL_CONSTEXPR20 string to_string() const {
163 return _INNER __float_to_string<char>(value_);
164 }
165
166 MSTL_NODISCARD static constexpr float64 parse(const string_view str) {
167 return float64{_MSTL to_float64(str)};
168 }
169};
170
171template <>
172struct package<float64_t> {
173 using type = float64;
174};
175template <>
176struct unpackage<float64> {
177 using type = float64_t;
178};
179
180
181struct decimal : iobject<decimal>, ipackage<decimal, decimal_t> {
182 using value_type = decimal_t;
183 using base = ipackage<decimal, decimal_t>;
184
185 MSTL_BUILD_PACKAGE_CONSTRUCTOR(decimal)
186
187 MSTL_NODISCARD constexpr explicit operator bool() const noexcept {
188 return value_ != _MSTL initialize<value_type>();
189 }
190
191 MSTL_NODISCARD static MSTL_CONSTEXPR20 string to_string(const value_type value) {
192 return decimal(value).to_string();
193 }
194 MSTL_NODISCARD MSTL_CONSTEXPR20 string to_string() const {
195 return _INNER __float_to_string<char>(value_);
196 }
197
198 MSTL_NODISCARD static constexpr decimal parse(const string_view str) {
199 return decimal{_MSTL to_decimal(str)};
200 }
201};
202
203template <>
204struct package<decimal_t> {
205 using type = decimal;
206};
207template <>
208struct unpackage<decimal> {
209 using type = decimal_t;
210};
211
213#endif // MSTL_CORE_UTILITY_PACKAGES_HPP__
float float32_t
32位单精度浮点数类型
long double decimal_t
扩展精度浮点数类型
double float64_t
64位双精度浮点数类型
#define _MSTL
全局命名空间MSTL前缀
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr T initialize() noexcept(is_nothrow_default_constructible< T >::value)
返回类型T的默认初始化值
类型包装器模板
类型解包器模板