Files
GdCpp12/include/sVersion.h

372 lines
6.8 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef _VERSION_STRUCT_ //GdCpp和IGrabber都包含这个文件
#define _VERSION_STRUCT_
#include <stdint.h>
#include <stdio.h>
#include <sstream>
#include <string>
/// u32保存版本号可以与字符串互转
// "1.1.1”对应0x01010001
// "255.255.65535”对应0xFFFFFFFF
struct sVersion
{
sVersion() = default;
sVersion(uint8_t major, uint8_t mid, uint16_t minor)
: Major(major), Mid(mid), Minor(minor)
{
}
sVersion(uint32_t ver)
: u32Ver(ver)
{
}
// 复制构造函数
sVersion(const sVersion& other)
{
u32Ver = other.u32Ver;
}
union
{
uint32_t u32Ver = 0;
struct
{
uint16_t Minor;
uint8_t Mid;
uint8_t Major;
};
};
bool fromStr(const char* str)
{
std::istringstream iss(str);
char delimiter = '.';
int v1=0, v2=0, v3=0;
// 解析Major
if (!(iss >> v1))
return false;
// 检查是否有更多部分
if (iss.peek() != delimiter)
return false; // 如果没有'.',则格式不正确
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Mid
if (!(iss >> v2))
return false; // 至少一个.两个数字
// 检查是否还有Minor部分
if (iss.peek() == delimiter)
{
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Minor不管否成功都继续
iss >> v3;
}
else {
// 如果没有'.'则默认Minor为0
//Minor = 0;
}
Major = uint8_t(v1);
Mid = uint8_t(v2);
Minor = uint16_t(v3);
return true;
}
bool fromStr(const wchar_t* str)
{
std::wistringstream iss(str);
wchar_t delimiter = L'.';
int v1 = 0, v2 = 0, v3 = 0;
// 解析Major
if (!(iss >> v1))
return false;
// 检查是否有更多部分
if (iss.peek() != delimiter)
return false; // 如果没有'.',则格式不正确
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Mid
if (!(iss >> v2))
return false; // 至少一个.两个数字
// 检查是否还有Minor部分
if (iss.peek() == delimiter)
{
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Minor不管否成功都继续
iss >> v3;
}
else {
// 如果没有'.'则默认Minor为0
//Minor = 0;
}
Major = uint8_t(v1);
Mid = uint8_t(v2);
Minor = uint16_t(v3);
return true;
}
std::string toStr() const
{
std::ostringstream oss;
oss << int(Major) << "." << int(Mid) << "." << int(Minor);
return oss.str();
}
std::wstring toWStr() const
{
std::wostringstream oss;
oss << int(Major) << L"." << int(Mid) << L"." << int(Minor);
return oss.str();
}
void clear()
{
u32Ver = 0;
}
inline bool operator==(const sVersion& other) const
{
return u32Ver == other.u32Ver;
}
inline bool operator<(const sVersion& other) const
{
return u32Ver < other.u32Ver;
}
// 利用已重载的 == 和 < 来实现其他运算符
inline bool operator!=(const sVersion& other) const {
return !(*this == other);
}
inline bool operator>(const sVersion& other) const {
return other < *this;
}
inline bool operator<=(const sVersion& other) const {
return !(*this > other);
}
bool operator>=(const sVersion& other) const {
return !(*this < other);
}
};
/// u64保存版本号可以与字符串互转
// "1.1.1.1”对应0x0001000100010001
// "65535.65535.65535.65535”对应0xFFFFFFFFFFFFFFFF
struct sVersion2
{
sVersion2() = default;
sVersion2(uint16_t major, uint16_t mid, uint16_t minor=0, uint16_t build=0)
: Major(major), Mid(mid), Minor(minor), Build(build)
{
}
sVersion2(uint64_t ver)
: u64Ver(ver)
{
}
// 复制构造函数
sVersion2(const sVersion2& other)
{
u64Ver = other.u64Ver;
}
sVersion2(const sVersion& other)
{
Major = other.Major;
Mid = other.Mid;
Minor = other.Minor;
Build = 0;
}
union
{
uint64_t u64Ver = 0;
struct
{
uint16_t Build;
uint16_t Minor;
uint16_t Mid;
uint16_t Major;
};
};
bool fromStr(const char* str)
{
std::istringstream iss(str);
char delimiter = '.';
int v1 = 0, v2 = 0, v3 = 0, v4 = 0;
// 解析Major
if (!(iss >> v1))
return false;
// 检查是否有更多部分
if (iss.peek() != delimiter)
return false; // 如果没有'.',则格式不正确
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Mid
if (!(iss >> v2))
return false; // 至少一个.两个数字
// 检查是否还有Minor部分
if (iss.peek() == delimiter)
{
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Minor
if ((iss >> v3)) {
// 检查是否还有Build部分
if (iss.peek() == delimiter)
{
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Build不管否成功都继续
iss >> v4;
}
else
{
// 如果没有'.'则默认Build为0
//Build = 0;
}
}
}
else {
// 如果没有'.'则默认Minor为0
//Minor = 0;
}
Major = uint8_t(v1);
Mid = uint8_t(v2);
Minor = uint16_t(v3);
Build = uint16_t(v4);
return true;
}
bool fromStr(const wchar_t* str)
{
std::wistringstream iss(str);
wchar_t delimiter = L'.';
int v1 = 0, v2 = 0, v3 = 0, v4 = 0;
// 解析Major
if (!(iss >> v1))
return false;
// 检查是否有更多部分
if (iss.peek() != delimiter)
return false; // 如果没有'.',则格式不正确
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Mid
if (!(iss >> v2))
return false; // 至少一个.两个数字
// 检查是否还有Minor部分
if (iss.peek() == delimiter)
{
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Minor
if ((iss >> v3)) {
// 检查是否还有Build部分
if (iss.peek() == delimiter)
{
// 跳过分隔符
iss.ignore(1, delimiter);
// 解析Build不管否成功都继续
iss >> v4;
}
else
{
// 如果没有'.'则默认Build为0
//Build = 0;
}
}
} else {
// 如果没有'.'则默认Minor为0
//Minor = 0;
}
Major = uint8_t(v1);
Mid = uint8_t(v2);
Minor = uint16_t(v3);
Build = uint16_t(v4);
return true;
}
std::string toStr() const
{
std::ostringstream oss;
oss << int(Major) << "." << int(Mid) << "." << int(Minor) << "." << int(Build);
return oss.str();
}
std::wstring toWStr() const
{
std::wostringstream oss;
oss << int(Major) << L"." << int(Mid) << L"." << int(Minor) << L"." << int(Build);
return oss.str();
}
void clear()
{
u64Ver = 0;
}
inline bool operator==(const sVersion2& other) const
{
return u64Ver == other.u64Ver;
}
inline bool operator<(const sVersion2& other) const
{
return u64Ver < other.u64Ver;
}
// 利用已重载的 == 和 < 来实现其他运算符
inline bool operator!=(const sVersion2& other) const {
return !(*this == other);
}
inline bool operator>(const sVersion2& other) const {
return other < *this;
}
inline bool operator<=(const sVersion2& other) const {
return !(*this > other);
}
bool operator>=(const sVersion2& other) const {
return !(*this < other);
}
};
#endif