NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
byte_size类 参考

字节大小类 更多...

#include <byte_size.hpp>

类 byte_size 继承关系图:
[图例]

Public 类型

enum class  unit {
  B , KB , MB , GB ,
  TB , PB , EB , AUTO
}
 字节大小单位枚举 更多...

Public 成员函数

constexpr byte_size () noexcept=default
 默认构造函数
constexpr byte_size (uint64_t bytes) noexcept
 从字节数构造
 byte_size (decimal_t value, unit u, bool binary=true)
 从值和单位构造
constexpr uint64_t bytes () const noexcept
 获取字节数
decimal_t as (unit u, bool binary=true) const
 转换为指定单位的值
string to_string () const
 转换为可读字符串
string to_string (unit u, int precision=2, bool binary=true) const
 转换为指定单位的字符串
constexpr bool is_zero () const noexcept
 检查是否为零
size_t to_hash () const noexcept
 计算哈希值
Public 成员函数 继承自 iobject< byte_size >
constexpr bool try_parse (const string_view str) noexcept
 尝试从字符串解析对象
Public 成员函数 继承自 istringify< byte_size >
constexpr string to_string () const
 转换为字符串
Public 成员函数 继承自 icomparable< byte_size >
constexpr bool operator== (const byte_size &rhs) const noexcept(noexcept(derived().equal_to(rhs)))
 相等比较运算符
constexpr bool operator!= (const byte_size &rhs) const noexcept(noexcept(!(derived().equal_to(rhs))))
 不等比较运算符
constexpr bool operator< (const byte_size &rhs) const noexcept(noexcept(derived().less_than(rhs)))
 小于比较运算符
constexpr bool operator> (const byte_size &rhs) const noexcept(noexcept(rhs.less_than(derived())))
 大于比较运算符
constexpr bool operator<= (const byte_size &rhs) const noexcept(noexcept(!(rhs.less_than(derived()))))
 小于等于比较运算符
constexpr bool operator>= (const byte_size &rhs) const noexcept(noexcept(!(derived().less_than(rhs))))
 大于等于比较运算符
Public 成员函数 继承自 ihashable< byte_size >
constexpr size_t to_ihash () const noexcept(noexcept(derived().to_hash()))
 获取对象的哈希值

静态 Public 成员函数

static byte_size parse (string_view str)
 从字符串解析字节大小
static byte_size parse (string_view str, bool binary)
 从字符串解析字节大小(指定进制)
静态 Public 成员函数 继承自 iobject< byte_size >
static constexpr byte_size parse (const string_view str)
 从字符串解析对象

详细描述

字节大小类

表示一个字节大小值,支持多种单位之间的转换和运算。 提供类型安全的字节大小操作,避免原始整数带来的单位混淆问题。

主要功能:

  • 字节大小的单位表示
  • 从字符串解析字节大小
  • 格式化为可读字符串
  • 单位之间的算术运算
  • 支持二进制(IEC标准)和十进制(SI标准)
  • 哈希支持

使用示例:

// 从字节数构造
byte_size size1(1024);
println(size1); // "1 KB"
// 从值和单位构造
println(size2.bytes()); // 1572864
// 从字符串解析
auto size3 = byte_size::parse("2.5 GB");
auto size4 = byte_size::parse("1 MiB", true); // 二进制模式
// 算术运算
byte_size total = size1 + size2;
byte_size half = total / 2;
// 单位转换
double in_mb = size2.as(byte_size::unit::MB);
// 格式化输出
println(size2.to_string(byte_size::unit::KB, 0)); // "1536 KB"
decimal_t as(unit u, bool binary=true) const
转换为指定单位的值
static byte_size parse(string_view str)
从字符串解析字节大小
constexpr byte_size() noexcept=default
默认构造函数
void println(Args &&... args)
打印多个值并换行
long double decimal_t
扩展精度浮点数类型

TODO: All operations for optimizing byte_size are constexpr

在文件 byte_size.hpp145 行定义.

成员枚举类型说明

◆ unit

enum class byte_size::unit
strong

字节大小单位枚举

枚举值

字节

KB 

千字节

MB 

兆字节

GB 

吉字节

TB 

太字节

PB 

拍字节

EB 

艾字节

AUTO 

自动选择合适单位

在文件 byte_size.hpp151 行定义.

构造及析构函数说明

◆ byte_size() [1/3]

byte_size::byte_size ( )
constexprdefaultnoexcept

默认构造函数

创建大小为0的字节对象。

被这些函数引用 byte_size(), parse() , 以及 parse().

◆ byte_size() [2/3]

byte_size::byte_size ( uint64_t bytes)
inlineexplicitconstexprnoexcept

从字节数构造

参数
bytes字节数

在文件 byte_size.hpp177 行定义.

引用了 byte_size() , 以及 bytes().

◆ byte_size() [3/3]

byte_size::byte_size ( decimal_t value,
unit u,
bool binary = true )

从值和单位构造

参数
value数值
u单位
binary是否使用二进制,默认true
异常
value_exception值为负数或超出范围时抛出

成员函数说明

◆ as()

decimal_t byte_size::as ( unit u,
bool binary = true ) const
nodiscard

转换为指定单位的值

参数
u目标单位
binary是否使用二进制
返回
转换后的浮点值
异常
value_exceptionunit为AUTO时抛出

◆ bytes()

uint64_t byte_size::bytes ( ) const
inlinenodiscardconstexprnoexcept

获取字节数

返回
原始字节数

在文件 byte_size.hpp213 行定义.

被这些函数引用 byte_size().

◆ is_zero()

bool byte_size::is_zero ( ) const
inlinenodiscardconstexprnoexcept

检查是否为零

返回
如果字节数为0返回true

在文件 byte_size.hpp243 行定义.

◆ parse() [1/2]

byte_size byte_size::parse ( string_view str)
inlinestaticnodiscard

从字符串解析字节大小

参数
str字符串
返回
字节大小对象
异常
value_exception解析失败时抛出

支持的单位:B、KB/K、MB/M、GB/G、TB/T、PB/P、EB/E 示例:"1024", "1.5 MB", "2G", "500KB"

在文件 byte_size.hpp198 行定义.

引用了 byte_size() , 以及 parse().

被这些函数引用 parse().

◆ parse() [2/2]

byte_size byte_size::parse ( string_view str,
bool binary )
staticnodiscard

从字符串解析字节大小(指定进制)

参数
str字符串
binary是否使用二进制
返回
字节大小对象
异常
value_exception解析失败时抛出

引用了 byte_size().

◆ to_hash()

size_t byte_size::to_hash ( ) const
inlinenodiscardnoexcept

计算哈希值

返回
哈希值

在文件 byte_size.hpp305 行定义.

◆ to_string() [1/2]

string byte_size::to_string ( ) const
inlinenodiscard

转换为可读字符串

返回
格式化字符串

在文件 byte_size.hpp228 行定义.

引用了 AUTO , 以及 to_string().

被这些函数引用 to_string().

◆ to_string() [2/2]

string byte_size::to_string ( unit u,
int precision = 2,
bool binary = true ) const
nodiscard

转换为指定单位的字符串

参数
u目标单位
precision小数精度
binary是否使用二进制
返回
格式化字符串

该类的文档由以下文件生成: