NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
packages.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_UTILITY_PACKAGES_HPP__
2#define NEFORCE_CORE_UTILITY_PACKAGES_HPP__
3
11
16NEFORCE_BEGIN_NAMESPACE__
17
23
30struct boolean : ipackage<boolean, bool>, iobject<boolean> {
31 using value_type = bool;
32 using base = ipackage;
33
34 constexpr boolean() noexcept = default;
35 NEFORCE_CONSTEXPR20 ~boolean() = default;
36
37 constexpr boolean(const boolean&) noexcept = default;
38 constexpr boolean(boolean&&) noexcept = default;
39
40 constexpr boolean& operator=(const boolean& other) noexcept = default;
41 constexpr boolean& operator=(boolean&& other) noexcept = default;
42
43 explicit constexpr boolean(const value_type value) noexcept :
44 base(value) {}
45
46 constexpr boolean& operator=(const value_type value) noexcept {
47 value_ = value;
48 return *this;
49 }
50
56 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) {
57 return boolean(value).to_string();
58 }
59
64 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return value_ ? "true" : "false"; }
65
75 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 boolean parse(const string_view lower) {
76 boolean obj;
77 string str(lower.trim());
78 try {
79 str.lowercase();
80 if (str == "true" || str == "yes" || str == "y") {
81 obj = true;
82 } else if (str == "false" || str == "no" || str == "n") {
83 obj = false;
84 } else {
85 obj = static_cast<bool>(to_int32(str.view(), nullptr, 10));
86 }
87 } catch (...) {
88 NEFORCE_THROW_EXCEPTION(typecast_exception("Convert from string to boolean failed."));
89 }
90 return obj;
91 }
92
97 constexpr boolean operator!() const noexcept { return boolean(!value_); }
98};
99
100template <>
101struct package<bool> {
102 using type = boolean;
103};
104
105template <>
106struct unpackage<boolean> {
107 using type = bool;
108};
109
110
111#define __NEFORCE_BUILD_INTEGER_STRUCT(SIGN, UPPER, BYTE) \
112 struct SIGN##integer##BYTE : iobject<SIGN##integer##BYTE>, ipackage<SIGN##integer##BYTE, SIGN##int##BYTE##_t> { \
113 using value_type = SIGN##int##BYTE##_t; \
114 using base = ipackage<SIGN##integer##BYTE, SIGN##int##BYTE##_t>; \
115 \
116 constexpr SIGN##integer##BYTE() noexcept = default; \
117 NEFORCE_CONSTEXPR20 ~SIGN##integer##BYTE() = default; \
118 \
119 constexpr SIGN##integer##BYTE(const SIGN##integer##BYTE&) noexcept = default; \
120 constexpr SIGN##integer##BYTE(SIGN##integer##BYTE&&) noexcept = default; \
121 \
122 constexpr SIGN##integer##BYTE& operator=(const SIGN##integer##BYTE& other) noexcept = default; \
123 constexpr SIGN##integer##BYTE& operator=(SIGN##integer##BYTE&& other) noexcept = default; \
124 \
125 explicit constexpr SIGN##integer##BYTE(const value_type value) noexcept : \
126 base(value) {} \
127 \
128 constexpr SIGN##integer##BYTE& operator=(const value_type value) noexcept { \
129 value_ = value; \
130 return *this; \
131 } \
132 \
133 NEFORCE_NODISCARD constexpr explicit operator bool() const noexcept { \
134 return value_ != _NEFORCE initialize<value_type>(); \
135 } \
136 \
137 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) { \
138 return inner::__int_to_string_dispatch(value); \
139 } \
140 \
141 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { \
142 return inner::__int_to_string_dispatch(value_); \
143 } \
144 \
145 NEFORCE_NODISCARD static constexpr SIGN##integer##BYTE parse(const string_view str) { \
146 return SIGN##integer##BYTE{_NEFORCE to_##SIGN##int##BYTE(str)}; \
147 } \
148 }; \
149 template <> \
150 struct package<SIGN##int##BYTE##_t> { \
151 using type = SIGN##integer##BYTE; \
152 }; \
153 template <> \
154 struct unpackage<SIGN##integer##BYTE> { \
155 using type = SIGN##int##BYTE##_t; \
156 };
157
164__NEFORCE_BUILD_INTEGER_STRUCT(, , 8)
165
166
172__NEFORCE_BUILD_INTEGER_STRUCT(, , 16)
173
180__NEFORCE_BUILD_INTEGER_STRUCT(, , 32)
181
188__NEFORCE_BUILD_INTEGER_STRUCT(, , 64)
189
196__NEFORCE_BUILD_INTEGER_STRUCT(u, U, 8)
197
204__NEFORCE_BUILD_INTEGER_STRUCT(u, U, 16)
205
212__NEFORCE_BUILD_INTEGER_STRUCT(u, U, 32)
213
220__NEFORCE_BUILD_INTEGER_STRUCT(u, U, 64)
221
222#undef __NEFORCE_BUILD_INTEGER_STRUCT
223
231
232
233#ifdef NEFORCE_PLATFORM_LINUX64
234template <>
235struct package<long long> {
236 using type = integer64;
237};
238template <>
239struct package<unsigned long long> {
240 using type = uinteger64;
241};
242#else
243template <>
244struct package<long> {
245 using type = integer32;
246};
247template <>
248struct package<unsigned long> {
249 using type = uinteger32;
250};
251#endif
252
253
260struct uinteger128 : iobject<uinteger128>, ipackage<uinteger128, uint128_t> {
261 using value_type = uint128_t;
262 using base = ipackage<uinteger128, uint128_t>;
263
264 constexpr uinteger128() noexcept = default;
265 NEFORCE_CONSTEXPR20 ~uinteger128() = default;
266
267 constexpr uinteger128(const uinteger128&) noexcept = default;
268 constexpr uinteger128(uinteger128&&) noexcept = default;
269
270 constexpr uinteger128& operator=(const uinteger128& other) noexcept = default;
271 constexpr uinteger128& operator=(uinteger128&& other) noexcept = default;
272
277 explicit constexpr uinteger128(const value_type value) noexcept :
278 base(value) {}
279
285 constexpr uinteger128& operator=(const value_type value) noexcept {
286 value_ = value;
287 return *this;
288 }
289
296 explicit NEFORCE_CONSTEXPR20 uinteger128(const string& str, const int base = 10) :
297 uinteger128(str.view(), base) {}
298
305 explicit constexpr uinteger128(string_view str, int base = 10);
306
307 NEFORCE_NODISCARD constexpr explicit operator bool() const noexcept { return value_ != value_type(); }
308
314 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) {
315 return inner::__int_to_string_dispatch(value);
316 }
317
322 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return inner::__int_to_string_dispatch(value_); }
323
330 NEFORCE_NODISCARD static constexpr uinteger128 parse(const string_view str) {
331 return uinteger128{_NEFORCE to_uint128(str)};
332 }
333
339 constexpr uinteger128& operator<<=(const uint32_t shift) noexcept {
340 value_ <<= shift;
341 return *this;
342 }
343
349 constexpr uinteger128& operator>>=(const uint32_t shift) noexcept {
350 value_ >>= shift;
351 return *this;
352 }
353};
354
361struct integer128 : iobject<integer128>, ipackage<integer128, int128_t> {
362 using value_type = int128_t;
363 using base = ipackage<integer128, int128_t>;
364
365 constexpr integer128() noexcept = default;
366 NEFORCE_CONSTEXPR20 ~integer128() = default;
367
368 constexpr integer128(const integer128&) noexcept = default;
369 constexpr integer128(integer128&&) noexcept = default;
370
371 constexpr integer128& operator=(const integer128& other) noexcept = default;
372 constexpr integer128& operator=(integer128&& other) noexcept = default;
373
378 explicit constexpr integer128(const value_type value) noexcept :
379 base(value) {}
380
386 constexpr integer128& operator=(const value_type value) noexcept {
387 value_ = value;
388 return *this;
389 }
390
397 explicit NEFORCE_CONSTEXPR20 integer128(const string& str, const int base = 10) :
398 integer128(str.view(), base) {}
399
406 explicit constexpr integer128(const string_view str, const int base = 10);
407
408 NEFORCE_NODISCARD constexpr explicit operator bool() const noexcept { return value_ != value_type(); }
409
415 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) {
416 return inner::__int_to_string_dispatch(value);
417 }
418
423 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return inner::__int_to_string_dispatch(value_); }
424
431 NEFORCE_NODISCARD static constexpr integer128 parse(const string_view str) {
432 return integer128{_NEFORCE to_int128(str)};
433 }
434
440 constexpr integer128& operator<<=(const uint32_t shift) noexcept {
441 ignore = value_ <<= shift;
442 return *this;
443 }
444
450 constexpr integer128& operator>>=(const uint32_t shift) noexcept {
451 ignore = value_ >>= shift;
452 return *this;
453 }
454};
455
456constexpr uinteger128::uinteger128(const string_view str, const int base) {
457 *this = uinteger128{to_uint128(str, nullptr, base)};
458}
459
460constexpr integer128::integer128(const string_view str, const int base) {
461 *this = integer128{to_int128(str, nullptr, base)};
462}
463
464template <>
465struct package<uint128_t> {
466 using type = uinteger128;
467};
468
469template <>
470struct unpackage<uinteger128> {
471 using type = uint128_t;
472};
473
474template <>
475struct package<int128_t> {
476 using type = integer128;
477};
478
479template <>
480struct unpackage<integer128> {
481 using type = int128_t;
482};
483
484
491struct float32 : iobject<float32>, ipackage<float32, float32_t> {
492 using value_type = float32_t;
493 using base = ipackage<float32, float32_t>;
494
495 constexpr float32() noexcept = default;
496 NEFORCE_CONSTEXPR20 ~float32() = default;
497
498 constexpr float32(const float32&) noexcept = default;
499 constexpr float32(float32&&) noexcept = default;
500
501 constexpr float32& operator=(const float32& other) noexcept = default;
502 constexpr float32& operator=(float32&& other) noexcept = default;
503
504 explicit constexpr float32(const value_type value) noexcept :
505 base(value) {}
506
507 constexpr float32& operator=(const value_type value) noexcept {
508 value_ = value;
509 return *this;
510 }
511
512 NEFORCE_NODISCARD constexpr explicit operator bool() const noexcept {
513 return value_ != _NEFORCE initialize<value_type>();
514 }
515
516 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) {
517 return float32(value).to_string();
518 }
519 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return inner::__float_to_string<char>(value_); }
520
521 NEFORCE_NODISCARD static constexpr float32 parse(const string_view str) {
522 return float32{_NEFORCE to_float32(str)};
523 }
524};
525
526template <>
527struct package<float32_t> {
528 using type = float32;
529};
530
531template <>
532struct unpackage<float32> {
533 using type = float32_t;
534};
535
536
543struct float64 : iobject<float64>, ipackage<float64, float64_t> {
544 using value_type = float64_t;
545 using base = ipackage<float64, float64_t>;
546
547 constexpr float64() noexcept = default;
548 NEFORCE_CONSTEXPR20 ~float64() = default;
549
550 constexpr float64(const float64&) noexcept = default;
551 constexpr float64(float64&&) noexcept = default;
552
553 constexpr float64& operator=(const float64& other) noexcept = default;
554 constexpr float64& operator=(float64&& other) noexcept = default;
555
556 explicit constexpr float64(const value_type value) noexcept :
557 base(value) {}
558
559 constexpr float64& operator=(const value_type value) noexcept {
560 value_ = value;
561 return *this;
562 }
563
564 NEFORCE_NODISCARD constexpr explicit operator bool() const noexcept {
565 return value_ != _NEFORCE initialize<value_type>();
566 }
567
568 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) {
569 return float64(value).to_string();
570 }
571 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return inner::__float_to_string<char>(value_); }
572
573 NEFORCE_NODISCARD static constexpr float64 parse(const string_view str) {
574 return float64{_NEFORCE to_float64(str)};
575 }
576};
577
578template <>
579struct package<float64_t> {
580 using type = float64;
581};
582
583template <>
584struct unpackage<float64> {
585 using type = float64_t;
586};
587
588
595struct decimal : iobject<decimal>, ipackage<decimal, decimal_t> {
596 using value_type = decimal_t;
597 using base = ipackage<decimal, decimal_t>;
598
599 constexpr decimal() noexcept = default;
600 NEFORCE_CONSTEXPR20 ~decimal() = default;
601
602 constexpr decimal(const decimal&) noexcept = default;
603 constexpr decimal(decimal&&) noexcept = default;
604
605 constexpr decimal& operator=(const decimal& other) noexcept = default;
606 constexpr decimal& operator=(decimal&& other) noexcept = default;
607
608 explicit constexpr decimal(const value_type value) noexcept :
609 base(value) {}
610
611 constexpr decimal& operator=(const value_type value) noexcept {
612 value_ = value;
613 return *this;
614 }
615
616 NEFORCE_NODISCARD constexpr explicit operator bool() const noexcept {
617 return value_ != _NEFORCE initialize<value_type>();
618 }
619
620 NEFORCE_NODISCARD static NEFORCE_CONSTEXPR20 string to_string(const value_type value) {
621 return decimal(value).to_string();
622 }
623 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return inner::__float_to_string<char>(value_); }
624
625 NEFORCE_NODISCARD static constexpr decimal parse(const string_view str) {
626 return decimal{_NEFORCE to_decimal(str)};
627 }
628};
629
630template <>
631struct package<decimal_t> {
632 using type = decimal;
633};
634
635template <>
636struct unpackage<decimal> {
637 using type = decimal_t;
638};
639 // Packages
641
642NEFORCE_END_NAMESPACE__
643#endif // NEFORCE_CORE_UTILITY_PACKAGES_HPP__
constexpr basic_string_view trim() const noexcept
去除两侧空白字符
constexpr view_type view() const noexcept
获取字符串视图
constexpr basic_string & lowercase() noexcept(noexcept(_NEFORCE transform(begin(), end(), begin(), _NEFORCE to_lowercase< CharT >)))
转换为小写
字符串格式化功能
float float32_t
32位单精度浮点数类型
unsigned int uint32_t
32位无符号整数类型
long double decimal_t
扩展精度浮点数类型
double float64_t
64位双精度浮点数类型
uinteger8 byte
字节包装类
constexpr int32_t to_int32(const string_view sv, size_t *idx=nullptr, const int base=10)
将字符串转换为32位有符号整数
constexpr float32_t to_float32(const string_view sv, size_t *idx=nullptr)
将字符串转换为32位浮点数
constexpr float64_t to_float64(const string_view sv, size_t *idx=nullptr)
将字符串转换为64位浮点数
constexpr int128_t to_int128(const string_view sv, size_t *idx=nullptr, const int base=10)
将字符串转换为128位有符号整数
constexpr uint128_t to_uint128(const string_view sv, size_t *idx=nullptr, const int base=10)
将字符串转换为128位无符号整数
constexpr decimal_t to_decimal(const string_view sv, size_t *idx=nullptr)
将字符串转换为decimal浮点数
basic_string_view< char > string_view
字符字符串视图
constexpr T initialize() noexcept(is_nothrow_default_constructible< T >::value)
返回类型T的默认初始化值
128位整数类型实现
可解析对象接口
static constexpr boolean parse(const string_view lower)
从字符串解析布尔值
constexpr boolean operator!() const noexcept
逻辑非操作符
static constexpr string to_string(const value_type value)
将布尔值转换为字符串
constexpr string to_string() const
将当前对象转换为字符串
长双精度浮点数包装类
32位浮点数包装类
64位浮点数包装类
128位有符号整数类型
128位有符号整数包装类
constexpr integer128(const value_type value) noexcept
从基础类型构造
constexpr integer128 & operator=(const value_type value) noexcept
从基础类型赋值
constexpr integer128 & operator<<=(const uint32_t shift) noexcept
重写左移赋值以支持128位移位
static constexpr integer128 parse(const string_view str)
从字符串解析
constexpr integer128(const string &str, const int base=10)
从字符串构造
static constexpr string to_string(const value_type value)
将基础类型值转换为字符串
constexpr integer128 & operator>>=(const uint32_t shift) noexcept
重写右移赋值以支持128位移位(算术右移)
constexpr string to_string() const
将当前对象转换为字符串
64位整数包装类
可解析对象接口
constexpr package_type value() const noexcept
类型包装器模板
128位无符号整数类型
128位无符号整数包装类
constexpr uinteger128 & operator>>=(const uint32_t shift) noexcept
重写右移赋值以支持128位移位
constexpr uinteger128(const value_type value) noexcept
从基础类型构造
constexpr string to_string() const
将当前对象转换为字符串
static constexpr uinteger128 parse(const string_view str)
从字符串解析
constexpr uinteger128(const string &str, const int base=10)
从字符串构造
static constexpr string to_string(const value_type value)
将基础类型值转换为字符串
constexpr uinteger128 & operator<<=(const uint32_t shift) noexcept
重写左移赋值以支持128位移位
constexpr uinteger128 & operator=(const value_type value) noexcept
从基础类型赋值
无符号8位整数包装类
字符串到数值的转换函数