dynamic::Value simplified

This commit is contained in:
MihailRis
2024-05-07 18:39:12 +03:00
parent 8e83a07094
commit a4c21984d5
26 changed files with 230 additions and 297 deletions
+72 -98
View File
@@ -2,91 +2,88 @@
using namespace dynamic;
List::~List() {
}
std::string List::str(size_t index) const {
const auto& val = values[index];
switch (static_cast<valtype>(val->value.index())) {
case valtype::string: return std::get<std::string>(val->value);
case valtype::boolean: return std::get<bool>(val->value) ? "true" : "false";
case valtype::number: return std::to_string(std::get<double>(val->value));
case valtype::integer: return std::to_string(std::get<int64_t>(val->value));
const auto& value = values[index];
switch (static_cast<valtype>(value.index())) {
case valtype::string: return std::get<std::string>(value);
case valtype::boolean: return std::get<bool>(value) ? "true" : "false";
case valtype::number: return std::to_string(std::get<double>(value));
case valtype::integer: return std::to_string(std::get<int64_t>(value));
default:
throw std::runtime_error("type error");
}
}
number_t List::num(size_t index) const {
const auto& val = values[index];
switch (static_cast<valtype>(val->value.index())) {
case valtype::number: return std::get<number_t>(val->value);
case valtype::integer: return std::get<integer_t>(val->value);
case valtype::string: return std::stoll(std::get<std::string>(val->value));
case valtype::boolean: return std::get<bool>(val->value);
const auto& value = values[index];
switch (static_cast<valtype>(value.index())) {
case valtype::number: return std::get<number_t>(value);
case valtype::integer: return std::get<integer_t>(value);
case valtype::string: return std::stoll(std::get<std::string>(value));
case valtype::boolean: return std::get<bool>(value);
default:
throw std::runtime_error("type error");
}
}
integer_t List::integer(size_t index) const {
const auto& val = values[index];
switch (static_cast<valtype>(val->value.index())) {
case valtype::number: return std::get<number_t>(val->value);
case valtype::integer: return std::get<integer_t>(val->value);
case valtype::string: return std::stoll(std::get<std::string>(val->value));
case valtype::boolean: return std::get<bool>(val->value);
const auto& value = values[index];
switch (static_cast<valtype>(value.index())) {
case valtype::number: return std::get<number_t>(value);
case valtype::integer: return std::get<integer_t>(value);
case valtype::string: return std::stoll(std::get<std::string>(value));
case valtype::boolean: return std::get<bool>(value);
default:
throw std::runtime_error("type error");
}
}
Map* List::map(size_t index) const {
if (auto* val = std::get_if<Map*>(&values[index]->value)) {
return *val;
if (auto* val = std::get_if<Map_sptr>(&values[index])) {
return val->get();
} else {
throw std::runtime_error("type error");
}
}
List* List::list(size_t index) const {
if (auto* val = std::get_if<List*>(&values[index]->value)) {
return *val;
if (auto* val = std::get_if<List_sptr>(&values[index])) {
return val->get();
} else {
throw std::runtime_error("type error");
}
}
bool List::flag(size_t index) const {
const auto& val = values[index];
switch (static_cast<valtype>(val->value.index())) {
case valtype::integer: return std::get<integer_t>(val->value);
case valtype::boolean: return std::get<bool>(val->value);
const auto& value = values[index];
switch (static_cast<valtype>(value.index())) {
case valtype::integer: return std::get<integer_t>(value);
case valtype::boolean: return std::get<bool>(value);
default:
throw std::runtime_error("type error");
}
}
Value* List::getValueWriteable(size_t index) const {
Value* List::getValueWriteable(size_t index) {
if (index > values.size()) {
throw std::runtime_error("index error");
}
return values.at(index).get();
return &values.at(index);
}
List& List::put(std::unique_ptr<Value> value) {
values.emplace_back(std::move(value));
List& List::put(const Value& value) {
values.emplace_back(value);
return *this;
}
List& List::putList() {
List* arr = new List();
auto arr = std::make_shared<List>();
put(arr);
return *arr;
}
Map& List::putMap() {
Map* map = new Map();
auto map = std::make_shared<Map>();
put(map);
return *map;
}
@@ -95,10 +92,7 @@ void List::remove(size_t index) {
values.erase(values.begin() + index);
}
Map::~Map() {
}
void Map::str(std::string key, std::string& dst) const {
void Map::str(const std::string& key, std::string& dst) const {
dst = get(key, dst);
}
@@ -106,12 +100,12 @@ std::string Map::get(const std::string& key, const std::string def) const {
auto found = values.find(key);
if (found == values.end())
return def;
auto& val = found->second;
switch (static_cast<valtype>(val->value.index())) {
case valtype::string: return std::get<std::string>(val->value);
case valtype::boolean: return std::get<bool>(val->value) ? "true" : "false";
case valtype::number: return std::to_string(std::get<number_t>(val->value));
case valtype::integer: return std::to_string(std::get<integer_t>(val->value));
auto& value = found->second;
switch (static_cast<valtype>(value.index())) {
case valtype::string: return std::get<std::string>(value);
case valtype::boolean: return std::get<bool>(value) ? "true" : "false";
case valtype::number: return std::to_string(std::get<number_t>(value));
case valtype::integer: return std::to_string(std::get<integer_t>(value));
default: throw std::runtime_error("type error");
}
}
@@ -120,12 +114,12 @@ number_t Map::get(const std::string& key, double def) const {
auto found = values.find(key);
if (found == values.end())
return def;
auto& val = found->second;
switch (static_cast<valtype>(val->value.index())) {
case valtype::number: return std::get<number_t>(val->value);
case valtype::integer: return std::get<integer_t>(val->value);
case valtype::string: return std::stoull(std::get<std::string>(val->value));
case valtype::boolean: return std::get<bool>(val->value);
auto& value = found->second;
switch (static_cast<valtype>(value.index())) {
case valtype::number: return std::get<number_t>(value);
case valtype::integer: return std::get<integer_t>(value);
case valtype::string: return std::stoull(std::get<std::string>(value));
case valtype::boolean: return std::get<bool>(value);
default: throw std::runtime_error("type error");
}
}
@@ -134,12 +128,12 @@ integer_t Map::get(const std::string& key, integer_t def) const {
auto found = values.find(key);
if (found == values.end())
return def;
auto& val = found->second;
switch (static_cast<valtype>(val->value.index())) {
case valtype::number: return std::get<number_t>(val->value);
case valtype::integer: return std::get<integer_t>(val->value);
case valtype::string: return std::stoull(std::get<std::string>(val->value));
case valtype::boolean: return std::get<bool>(val->value);
auto& value = found->second;
switch (static_cast<valtype>(value.index())) {
case valtype::number: return std::get<number_t>(value);
case valtype::integer: return std::get<integer_t>(value);
case valtype::string: return std::stoull(std::get<std::string>(value));
case valtype::boolean: return std::get<bool>(value);
default: throw std::runtime_error("type error");
}
}
@@ -148,65 +142,65 @@ bool Map::get(const std::string& key, bool def) const {
auto found = values.find(key);
if (found == values.end())
return def;
auto& val = found->second;
switch (static_cast<valtype>(val->value.index())) {
case valtype::integer: return std::get<integer_t>(val->value);
case valtype::boolean: return std::get<bool>(val->value);
auto& value = found->second;
switch (static_cast<valtype>(value.index())) {
case valtype::integer: return std::get<integer_t>(value);
case valtype::boolean: return std::get<bool>(value);
default: throw std::runtime_error("type error");
}
}
void Map::num(std::string key, double& dst) const {
void Map::num(const std::string& key, double& dst) const {
dst = get(key, dst);
}
void Map::num(std::string key, float& dst) const {
void Map::num(const std::string& key, float& dst) const {
dst = get(key, static_cast<number_t>(dst));
}
void Map::num(std::string key, ubyte& dst) const {
void Map::num(const std::string& key, ubyte& dst) const {
dst = get(key, static_cast<integer_t>(dst));
}
void Map::num(std::string key, int& dst) const {
void Map::num(const std::string& key, int& dst) const {
dst = get(key, static_cast<integer_t>(dst));
}
void Map::num(std::string key, int64_t& dst) const {
void Map::num(const std::string& key, int64_t& dst) const {
dst = get(key, dst);
}
void Map::num(std::string key, uint64_t& dst) const {
void Map::num(const std::string& key, uint64_t& dst) const {
dst = get(key, static_cast<integer_t>(dst));
}
void Map::num(std::string key, uint& dst) const {
void Map::num(const std::string& key, uint& dst) const {
dst = get(key, static_cast<integer_t>(dst));
}
Map* Map::map(std::string key) const {
Map* Map::map(const std::string& key) const {
auto found = values.find(key);
if (found != values.end()) {
if (auto* val = std::get_if<Map*>(&found->second->value)) {
return *val;
if (auto* val = std::get_if<Map_sptr>(&found->second)) {
return val->get();
}
}
return nullptr;
}
List* Map::list(std::string key) const {
List* Map::list(const std::string& key) const {
auto found = values.find(key);
if (found != values.end())
return std::get<List*>(found->second->value);
return std::get<List_sptr>(found->second).get();
return nullptr;
}
void Map::flag(std::string key, bool& dst) const {
void Map::flag(const std::string& key, bool& dst) const {
dst = get(key, dst);
}
Map& Map::put(std::string key, std::unique_ptr<Value> value) {
values.emplace(key, value.release());
Map& Map::put(std::string key, const Value& value) {
values.emplace(key, value);
return *this;
}
@@ -215,13 +209,13 @@ void Map::remove(const std::string& key) {
}
List& Map::putList(std::string key) {
List* arr = new List();
auto arr = std::make_shared<List>();
put(key, arr);
return *arr;
}
Map& Map::putMap(std::string key) {
Map* obj = new Map();
auto obj = std::make_shared<Map>();
put(key, obj);
return *obj;
}
@@ -233,23 +227,3 @@ bool Map::has(const std::string& key) const {
size_t Map::size() const {
return values.size();
}
Value::Value(valvalue value) : value(value) {
}
Value::~Value() {
switch (static_cast<valtype>(value.index())) {
case valtype::map: delete std::get<Map*>(value); break;
case valtype::list: delete std::get<List*>(value); break;
default:
break;
}
}
std::unique_ptr<Value> dynamic::value_of(const valvalue& value) {
return std::make_unique<Value>(value);
}
std::unique_ptr<Value> dynamic::value_of(std::unique_ptr<Map> value) {
return std::make_unique<Value>(value.release());
}