'dynamic' namespace refactor (step 1/2)
This commit is contained in:
+39
-164
@@ -1,7 +1,5 @@
|
||||
#include "dynamic.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace dynamic;
|
||||
|
||||
List::~List() {
|
||||
@@ -9,7 +7,7 @@ List::~List() {
|
||||
|
||||
std::string List::str(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
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));
|
||||
@@ -21,7 +19,7 @@ std::string List::str(size_t index) const {
|
||||
|
||||
number_t List::num(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
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));
|
||||
@@ -33,7 +31,7 @@ number_t List::num(size_t index) const {
|
||||
|
||||
integer_t List::integer(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
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));
|
||||
@@ -44,22 +42,24 @@ integer_t List::integer(size_t index) const {
|
||||
}
|
||||
|
||||
Map* List::map(size_t index) const {
|
||||
if (values[index]->type != valtype::map) {
|
||||
if (auto* val = std::get_if<Map*>(&values[index]->value)) {
|
||||
return *val;
|
||||
} else {
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
return std::get<Map*>(values[index]->value);
|
||||
}
|
||||
|
||||
List* List::list(size_t index) const {
|
||||
if (values[index]->type != valtype::list) {
|
||||
if (auto* val = std::get_if<List*>(&values[index]->value)) {
|
||||
return *val;
|
||||
} else {
|
||||
throw std::runtime_error("type error");
|
||||
}
|
||||
return std::get<List*>(values[index]->value);
|
||||
}
|
||||
|
||||
bool List::flag(size_t index) const {
|
||||
const auto& val = values[index];
|
||||
switch (val->type) {
|
||||
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);
|
||||
default:
|
||||
@@ -74,57 +74,11 @@ Value* List::getValueWriteable(size_t index) const {
|
||||
return values.at(index).get();
|
||||
}
|
||||
|
||||
List& List::put(std::string value) {
|
||||
values.push_back(std::make_unique<Value>(valtype::string, value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(uint value) {
|
||||
return put((int64_t)value);
|
||||
}
|
||||
|
||||
List& List::put(int value) {
|
||||
return put((int64_t)value);
|
||||
}
|
||||
|
||||
List& List::put(int64_t value) {
|
||||
values.push_back(std::make_unique<Value>(valtype::integer, value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(uint64_t value) {
|
||||
return put((int64_t)value);
|
||||
}
|
||||
|
||||
List& List::put(double value) {
|
||||
values.push_back(std::make_unique<Value>(valtype::number, value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(float value) {
|
||||
return put((double)value);
|
||||
}
|
||||
|
||||
List& List::put(bool value) {
|
||||
values.push_back(std::make_unique<Value>(valtype::boolean, value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(std::unique_ptr<Value> value) {
|
||||
values.emplace_back(std::move(value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(Map* value) {
|
||||
values.push_back(std::make_unique<Value>(valtype::map, value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::put(List* value) {
|
||||
values.push_back(std::make_unique<Value>(valtype::list, value));
|
||||
return *this;
|
||||
}
|
||||
|
||||
List& List::putList() {
|
||||
List* arr = new List();
|
||||
put(arr);
|
||||
@@ -145,57 +99,29 @@ Map::~Map() {
|
||||
}
|
||||
|
||||
void Map::str(std::string key, std::string& dst) const {
|
||||
dst = getStr(key, dst);
|
||||
dst = get(key, dst);
|
||||
}
|
||||
|
||||
std::string Map::getStr(std::string key) const {
|
||||
if (values.find(key) == values.end()) {
|
||||
throw std::runtime_error("missing key '"+key+"'");
|
||||
}
|
||||
return getStr(key, "");
|
||||
}
|
||||
|
||||
double Map::getNum(std::string key) const {
|
||||
if (values.find(key) == values.end()) {
|
||||
throw std::runtime_error("missing key '"+key+"'");
|
||||
}
|
||||
return getNum(key, 0);
|
||||
}
|
||||
|
||||
int64_t Map::getInt(std::string key) const {
|
||||
if (values.find(key) == values.end()) {
|
||||
throw std::runtime_error("missing key '"+key+"'");
|
||||
}
|
||||
return getInt(key, 0);
|
||||
}
|
||||
|
||||
bool Map::getBool(std::string key) const {
|
||||
if (values.find(key) == values.end()) {
|
||||
throw std::runtime_error("missing key '"+key+"'");
|
||||
}
|
||||
return getBool(key, false);
|
||||
}
|
||||
|
||||
std::string Map::getStr(std::string key, const std::string& def) const {
|
||||
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 (val->type) {
|
||||
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));
|
||||
default: throw std::runtime_error("type error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
number_t Map::getNum(std::string key, double def) const {
|
||||
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 (val->type) {
|
||||
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));
|
||||
@@ -204,12 +130,12 @@ number_t Map::getNum(std::string key, double def) const {
|
||||
}
|
||||
}
|
||||
|
||||
integer_t Map::getInt(std::string key, integer_t def) const {
|
||||
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 (val->type) {
|
||||
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));
|
||||
@@ -218,12 +144,12 @@ integer_t Map::getInt(std::string key, integer_t def) const {
|
||||
}
|
||||
}
|
||||
|
||||
bool Map::getBool(std::string key, bool def) const {
|
||||
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 (val->type) {
|
||||
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);
|
||||
default: throw std::runtime_error("type error");
|
||||
@@ -231,40 +157,39 @@ bool Map::getBool(std::string key, bool def) const {
|
||||
}
|
||||
|
||||
void Map::num(std::string key, double& dst) const {
|
||||
dst = getNum(key, dst);
|
||||
dst = get(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, float& dst) const {
|
||||
dst = getNum(key, dst);
|
||||
dst = get(key, static_cast<number_t>(dst));
|
||||
}
|
||||
|
||||
void Map::num(std::string key, ubyte& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
dst = get(key, static_cast<integer_t>(dst));
|
||||
}
|
||||
|
||||
void Map::num(std::string key, int& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
dst = get(key, static_cast<integer_t>(dst));
|
||||
}
|
||||
|
||||
void Map::num(std::string key, int64_t& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
dst = get(key, dst);
|
||||
}
|
||||
|
||||
void Map::num(std::string key, uint64_t& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
dst = get(key, static_cast<integer_t>(dst));
|
||||
}
|
||||
|
||||
void Map::num(std::string key, uint& dst) const {
|
||||
dst = getInt(key, dst);
|
||||
dst = get(key, static_cast<integer_t>(dst));
|
||||
}
|
||||
|
||||
Map* Map::map(std::string key) const {
|
||||
auto found = values.find(key);
|
||||
if (found != values.end()) {
|
||||
auto& val = found->second;
|
||||
if (val->type != valtype::map)
|
||||
return nullptr;
|
||||
return std::get<Map*>(val->value);
|
||||
if (auto* val = std::get_if<Map*>(&found->second->value)) {
|
||||
return *val;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
@@ -277,57 +202,7 @@ List* Map::list(std::string key) const {
|
||||
}
|
||||
|
||||
void Map::flag(std::string key, bool& dst) const {
|
||||
dst = getBool(key, dst);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, uint value) {
|
||||
return put(key, (int64_t)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, int value) {
|
||||
return put(key, (int64_t)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, int64_t value) {
|
||||
values[key] = std::make_unique<Value>(valtype::integer, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, uint64_t value) {
|
||||
return put(key, (int64_t)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, float value) {
|
||||
return put(key, (double)value);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, double value) {
|
||||
values[key] = std::make_unique<Value>(valtype::number, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, std::string value){
|
||||
values[key] = std::make_unique<Value>(valtype::string, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, const char* value) {
|
||||
return put(key, std::string(value));
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, Map* value){
|
||||
values[key] = std::make_unique<Value>(valtype::map, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, List* value){
|
||||
values[key] = std::make_unique<Value>(valtype::list, value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, bool value){
|
||||
values[key] = std::make_unique<Value>(valtype::boolean, value);
|
||||
return *this;
|
||||
dst = get(key, dst);
|
||||
}
|
||||
|
||||
Map& Map::put(std::string key, std::unique_ptr<Value> value) {
|
||||
@@ -351,7 +226,7 @@ Map& Map::putMap(std::string key) {
|
||||
return *obj;
|
||||
}
|
||||
|
||||
bool Map::has(std::string key) {
|
||||
bool Map::has(const std::string& key) const {
|
||||
return values.find(key) != values.end();
|
||||
}
|
||||
|
||||
@@ -359,11 +234,11 @@ size_t Map::size() const {
|
||||
return values.size();
|
||||
}
|
||||
|
||||
Value::Value(valtype type, valvalue value) : type(type), value(value) {
|
||||
Value::Value(valvalue value) : value(value) {
|
||||
}
|
||||
|
||||
Value::~Value() {
|
||||
switch (type) {
|
||||
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:
|
||||
@@ -372,21 +247,21 @@ Value::~Value() {
|
||||
}
|
||||
|
||||
std::unique_ptr<Value> Value::boolean(bool value) {
|
||||
return std::make_unique<Value>(valtype::boolean, value);
|
||||
return std::make_unique<Value>(value);
|
||||
}
|
||||
|
||||
std::unique_ptr<Value> Value::of(number_u value) {
|
||||
if (std::holds_alternative<integer_t>(value)) {
|
||||
return std::make_unique<Value>(valtype::integer, std::get<integer_t>(value));
|
||||
return std::make_unique<Value>(std::get<integer_t>(value));
|
||||
} else {
|
||||
return std::make_unique<Value>(valtype::number, std::get<number_t>(value));
|
||||
return std::make_unique<Value>(std::get<number_t>(value));
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<Value> Value::of(const std::string& value) {
|
||||
return std::make_unique<Value>(valtype::string, value);
|
||||
return std::make_unique<Value>(value);
|
||||
}
|
||||
|
||||
std::unique_ptr<Value> Value::of(std::unique_ptr<Map> value) {
|
||||
return std::make_unique<Value>(valtype::map, value.release());
|
||||
return std::make_unique<Value>(value.release());
|
||||
}
|
||||
|
||||
+39
-33
@@ -7,6 +7,7 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace dynamic {
|
||||
@@ -15,10 +16,11 @@ namespace dynamic {
|
||||
class Value;
|
||||
|
||||
enum class valtype {
|
||||
none, map, list, number, integer, string, boolean
|
||||
none=0, map, list, string, number, boolean, integer
|
||||
};
|
||||
|
||||
using valvalue = std::variant<
|
||||
std::monostate,
|
||||
Map*,
|
||||
List*,
|
||||
std::string,
|
||||
@@ -29,9 +31,8 @@ namespace dynamic {
|
||||
|
||||
class Value {
|
||||
public:
|
||||
valtype type;
|
||||
valvalue value;
|
||||
Value(valtype type, valvalue value);
|
||||
Value(valvalue value);
|
||||
~Value();
|
||||
|
||||
static std::unique_ptr<Value> boolean(bool value);
|
||||
@@ -60,16 +61,11 @@ namespace dynamic {
|
||||
return values.at(i).get();
|
||||
}
|
||||
|
||||
List& put(uint value);
|
||||
List& put(int value);
|
||||
List& put(uint64_t value);
|
||||
List& put(int64_t value);
|
||||
List& put(float value);
|
||||
List& put(double value);
|
||||
List& put(std::string value);
|
||||
List& put(Map* value);
|
||||
List& put(List* value);
|
||||
List& put(bool value);
|
||||
template<typename T>
|
||||
List& put(T value) {
|
||||
return put(std::make_unique<Value>(value));
|
||||
}
|
||||
|
||||
List& put(std::unique_ptr<Value> value);
|
||||
|
||||
Value* getValueWriteable(size_t index) const;
|
||||
@@ -85,15 +81,28 @@ namespace dynamic {
|
||||
std::unordered_map<std::string, std::unique_ptr<Value>> values;
|
||||
~Map();
|
||||
|
||||
std::string getStr(std::string key) const;
|
||||
number_t getNum(std::string key) const;
|
||||
integer_t getInt(std::string key) const;
|
||||
bool getBool(std::string key) const;
|
||||
template<typename T>
|
||||
T get(const std::string& key) const {
|
||||
if (!has(key)) {
|
||||
throw std::runtime_error("missing key '"+key+"'");
|
||||
}
|
||||
return get(key, T());
|
||||
}
|
||||
|
||||
std::string getStr(std::string key, const std::string& def) const;
|
||||
number_t getNum(std::string key, double def) const;
|
||||
integer_t getInt(std::string key, integer_t def) const;
|
||||
bool getBool(std::string key, bool def) const;
|
||||
std::string get(const std::string& key, const std::string def) const;
|
||||
number_t get(const std::string& key, double def) const;
|
||||
integer_t get(const std::string& key, integer_t def) const;
|
||||
bool get(const std::string& key, bool def) const;
|
||||
|
||||
int get(const std::string& key, int def) const {
|
||||
return get(key, static_cast<integer_t>(def));
|
||||
}
|
||||
uint get(const std::string& key, uint def) const {
|
||||
return get(key, static_cast<integer_t>(def));
|
||||
}
|
||||
uint64_t get(const std::string& key, uint64_t def) const {
|
||||
return get(key, static_cast<integer_t>(def));
|
||||
}
|
||||
|
||||
void str(std::string key, std::string& dst) const;
|
||||
void num(std::string key, int& dst) const;
|
||||
@@ -107,17 +116,14 @@ namespace dynamic {
|
||||
List* list(std::string key) const;
|
||||
void flag(std::string key, bool& dst) const;
|
||||
|
||||
Map& put(std::string key, uint value);
|
||||
Map& put(std::string key, int value);
|
||||
Map& put(std::string key, int64_t value);
|
||||
Map& put(std::string key, uint64_t value);
|
||||
Map& put(std::string key, float value);
|
||||
Map& put(std::string key, double value);
|
||||
Map& put(std::string key, const char* value);
|
||||
Map& put(std::string key, std::string value);
|
||||
Map& put(std::string key, Map* value);
|
||||
Map& put(std::string key, List* value);
|
||||
Map& put(std::string key, bool value);
|
||||
template<typename T>
|
||||
Map& put(std::string key, T value) {
|
||||
return put(key, std::make_unique<Value>(value));
|
||||
}
|
||||
Map& put(std::string key, uint64_t value) {
|
||||
return put(key, Value::of(static_cast<integer_t>(value)));
|
||||
}
|
||||
|
||||
Map& put(std::string key, std::unique_ptr<Value> value);
|
||||
|
||||
void remove(const std::string& key);
|
||||
@@ -125,7 +131,7 @@ namespace dynamic {
|
||||
List& putList(std::string key);
|
||||
Map& putMap(std::string key);
|
||||
|
||||
bool has(std::string key);
|
||||
bool has(const std::string& key) const;
|
||||
size_t size() const;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user