upd: first version of atom

This commit is contained in:
2023-11-09 09:40:43 +08:00
parent a2a8c45af1
commit cfdc5e5a1c
5 changed files with 169 additions and 14 deletions

View File

@ -1 +1,79 @@
#include "utils.h"
#include "utils.h"
inline ZYM::int2048 Any2Int(const std::any &value) {
if (auto ptr = std::any_cast<ZYM::int2048>(&value))
return *ptr;
else if (auto ptr = std::any_cast<int>(&value))
return std::move(ZYM::int2048(*ptr));
else if (auto ptr = std::any_cast<double>(&value)) {
std::stringstream buf;
buf << std::fixed << std::setprecision(0) << *ptr;
std::string tmp;
buf >> tmp;
return std::move(ZYM::int2048(tmp));
} else if (auto ptr = std::any_cast<bool>(&value))
return std::move(ZYM::int2048((long long)(*ptr)));
else if (auto ptr = std::any_cast<std::string>(&value)) {
std::string str = *ptr;
size_t dot_position = str.find('.');
if (dot_position != std::string::npos) {
std::string integer_part = str.substr(0, dot_position);
str = integer_part;
}
return std::move(ZYM::int2048(str));
} else
throw FatalError("Any2Int2048: unknown type");
}
double Any2Float(const std::any &value) {
if (auto ptr = std::any_cast<double>(&value))
return *ptr;
else if (auto ptr = std::any_cast<ZYM::int2048>(&value)) {
double res = 0;
std::stringstream buf;
buf << *ptr;
std::string tmp;
buf >> tmp;
int p = 0;
if (tmp[0] == '-') p++;
for (; p < tmp.size(); p++) res = res * 10 + tmp[p] - '0';
if (tmp[0] == '-') res = -res;
return res;
} else if (auto ptr = std::any_cast<bool>(&value))
return (double)(*ptr);
else if (auto ptr = std::any_cast<std::string>(&value))
return std::stod(*ptr);
else
throw FatalError("Any2Float: unknown type");
}
std::string Any2String(const std::any &value) {
std::stringstream buf;
std::string res;
if (auto ptr = std::any_cast<double>(&value)) {
buf << std::setiosflags(std::ios::fixed) << std::setprecision(15) << *ptr;
} else if (auto ptr = std::any_cast<bool>(&value)) {
if (*ptr)
buf << "True";
else
buf << "False";
} else if (auto ptr = std::any_cast<ZYM::int2048>(&value))
buf << *ptr;
else
throw FatalError("Any2String: unknown type");
buf >> res;
return res;
}
bool Any2Bool(const std::any &value) {
if (auto ptr = std::any_cast<ZYM::int2048>(&value)) {
return (*ptr) != 0;
} else if (auto ptr = std::any_cast<double>(&value)) {
return (*ptr) != 0;
} else if (auto ptr = std::any_cast<bool>(&value)) {
return *ptr;
} else if (auto ptr = std::any_cast<std::string>(&value)) {
return (*ptr) != "";
} else
throw FatalError("Any2Bool: unknown type");
}