'dynamic' namespace refactor (step 1/2)
This commit is contained in:
@@ -116,7 +116,7 @@ void AssetsLoader::processPreload(
|
|||||||
switch (tag) {
|
switch (tag) {
|
||||||
case AssetType::sound:
|
case AssetType::sound:
|
||||||
add(tag, path, name, std::make_shared<SoundCfg>(
|
add(tag, path, name, std::make_shared<SoundCfg>(
|
||||||
map->getBool("keep-pcm", false)
|
map->get("keep-pcm", false)
|
||||||
));
|
));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -131,13 +131,13 @@ void AssetsLoader::processPreloadList(AssetType tag, dynamic::List* list) {
|
|||||||
}
|
}
|
||||||
for (uint i = 0; i < list->size(); i++) {
|
for (uint i = 0; i < list->size(); i++) {
|
||||||
auto value = list->get(i);
|
auto value = list->get(i);
|
||||||
switch (value->type) {
|
switch (static_cast<dynamic::valtype>(value->value.index())) {
|
||||||
case dynamic::valtype::string:
|
case dynamic::valtype::string:
|
||||||
processPreload(tag, std::get<std::string>(value->value), nullptr);
|
processPreload(tag, std::get<std::string>(value->value), nullptr);
|
||||||
break;
|
break;
|
||||||
case dynamic::valtype::map: {
|
case dynamic::valtype::map: {
|
||||||
auto map = std::get<dynamic::Map*>(value->value);
|
auto map = std::get<dynamic::Map*>(value->value);
|
||||||
auto name = map->getStr("name");
|
auto name = map->get<std::string>("name");
|
||||||
processPreload(tag, name, map);
|
processPreload(tag, name, map);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-29
@@ -10,7 +10,7 @@ using namespace json;
|
|||||||
using namespace dynamic;
|
using namespace dynamic;
|
||||||
|
|
||||||
static void to_binary(ByteBuilder& builder, const Value* value) {
|
static void to_binary(ByteBuilder& builder, const Value* value) {
|
||||||
switch (value->type) {
|
switch (static_cast<valtype>(value->value.index())) {
|
||||||
case valtype::none:
|
case valtype::none:
|
||||||
throw std::runtime_error("none value is not implemented");
|
throw std::runtime_error("none value is not implemented");
|
||||||
case valtype::map: {
|
case valtype::map: {
|
||||||
@@ -56,8 +56,8 @@ static void to_binary(ByteBuilder& builder, const Value* value) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static List* array_from_binary(ByteReader& reader);
|
static std::unique_ptr<List> array_from_binary(ByteReader& reader);
|
||||||
static Map* object_from_binary(ByteReader& reader);
|
static std::unique_ptr<Map> object_from_binary(ByteReader& reader);
|
||||||
|
|
||||||
std::vector<ubyte> json::to_binary(const Map* obj, bool compress) {
|
std::vector<ubyte> json::to_binary(const Map* obj, bool compress) {
|
||||||
if (compress) {
|
if (compress) {
|
||||||
@@ -83,76 +83,66 @@ std::vector<ubyte> json::to_binary(const Map* obj, bool compress) {
|
|||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Value* value_from_binary(ByteReader& reader) {
|
static std::unique_ptr<Value> value_from_binary(ByteReader& reader) {
|
||||||
ubyte typecode = reader.get();
|
ubyte typecode = reader.get();
|
||||||
valtype type;
|
|
||||||
valvalue val;
|
valvalue val;
|
||||||
switch (typecode) {
|
switch (typecode) {
|
||||||
case BJSON_TYPE_DOCUMENT:
|
case BJSON_TYPE_DOCUMENT:
|
||||||
type = valtype::map;
|
|
||||||
reader.getInt32();
|
reader.getInt32();
|
||||||
val = object_from_binary(reader);
|
val = object_from_binary(reader).release();
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_LIST:
|
case BJSON_TYPE_LIST:
|
||||||
type = valtype::list;
|
val = array_from_binary(reader).release();
|
||||||
val = array_from_binary(reader);
|
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_BYTE:
|
case BJSON_TYPE_BYTE:
|
||||||
type = valtype::integer;
|
|
||||||
val = static_cast<integer_t>(reader.get());
|
val = static_cast<integer_t>(reader.get());
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_INT16:
|
case BJSON_TYPE_INT16:
|
||||||
type = valtype::integer;
|
|
||||||
val = static_cast<integer_t>(reader.getInt16());
|
val = static_cast<integer_t>(reader.getInt16());
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_INT32:
|
case BJSON_TYPE_INT32:
|
||||||
type = valtype::integer;
|
|
||||||
val = static_cast<integer_t>(reader.getInt32());
|
val = static_cast<integer_t>(reader.getInt32());
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_INT64:
|
case BJSON_TYPE_INT64:
|
||||||
type = valtype::integer;
|
|
||||||
val = reader.getInt64();
|
val = reader.getInt64();
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_NUMBER:
|
case BJSON_TYPE_NUMBER:
|
||||||
type = valtype::number;
|
|
||||||
val = reader.getFloat64();
|
val = reader.getFloat64();
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_FALSE:
|
case BJSON_TYPE_FALSE:
|
||||||
case BJSON_TYPE_TRUE:
|
case BJSON_TYPE_TRUE:
|
||||||
type = valtype::boolean;
|
|
||||||
val = (typecode - BJSON_TYPE_FALSE) != 0;
|
val = (typecode - BJSON_TYPE_FALSE) != 0;
|
||||||
break;
|
break;
|
||||||
case BJSON_TYPE_STRING:
|
case BJSON_TYPE_STRING:
|
||||||
type = valtype::string;
|
|
||||||
val = reader.getString();
|
val = reader.getString();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"type "+std::to_string(typecode)+" is not supported");
|
"type "+std::to_string(typecode)+" is not supported"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return new Value(type, val);
|
return std::make_unique<Value>(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
static List* array_from_binary(ByteReader& reader) {
|
static std::unique_ptr<List> array_from_binary(ByteReader& reader) {
|
||||||
auto array = std::make_unique<List>();
|
auto array = std::make_unique<List>();
|
||||||
auto& items = array->values;
|
auto& items = array->values;
|
||||||
while (reader.peek() != BJSON_END) {
|
while (reader.peek() != BJSON_END) {
|
||||||
items.push_back(std::unique_ptr<Value>(value_from_binary(reader)));
|
items.push_back(value_from_binary(reader));
|
||||||
}
|
}
|
||||||
reader.get();
|
reader.get();
|
||||||
return array.release();
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Map* object_from_binary(ByteReader& reader) {
|
static std::unique_ptr<Map> object_from_binary(ByteReader& reader) {
|
||||||
auto obj = std::make_unique<Map>();
|
auto obj = std::make_unique<Map>();
|
||||||
auto& map = obj->values;
|
auto& map = obj->values;
|
||||||
while (reader.peek() != BJSON_END) {
|
while (reader.peek() != BJSON_END) {
|
||||||
const char* key = reader.getCString();
|
const char* key = reader.getCString();
|
||||||
Value* value = value_from_binary(reader);
|
map.insert(std::make_pair(key, value_from_binary(reader)));
|
||||||
map.insert(std::make_pair(key, value));
|
|
||||||
}
|
}
|
||||||
reader.get();
|
reader.get();
|
||||||
return obj.release();
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Map> json::from_binary(const ubyte* src, size_t size) {
|
std::unique_ptr<Map> json::from_binary(const ubyte* src, size_t size) {
|
||||||
@@ -166,11 +156,13 @@ std::unique_ptr<Map> json::from_binary(const ubyte* src, size_t size) {
|
|||||||
} else {
|
} else {
|
||||||
ByteReader reader(src, size);
|
ByteReader reader(src, size);
|
||||||
std::unique_ptr<Value> value (value_from_binary(reader));
|
std::unique_ptr<Value> value (value_from_binary(reader));
|
||||||
if (value->type != valtype::map) {
|
|
||||||
|
if (Map* const* map = std::get_if<Map*>(&value->value)) {
|
||||||
|
std::unique_ptr<Map> obj (*map);
|
||||||
|
value->value = (Map*)nullptr;
|
||||||
|
return obj;
|
||||||
|
} else {
|
||||||
throw std::runtime_error("root value is not an object");
|
throw std::runtime_error("root value is not an object");
|
||||||
}
|
}
|
||||||
std::unique_ptr<Map> obj (std::get<Map*>(value->value));
|
|
||||||
value->value = (Map*)nullptr;
|
|
||||||
return obj;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+68
-69
@@ -11,9 +11,11 @@
|
|||||||
using namespace json;
|
using namespace json;
|
||||||
using namespace dynamic;
|
using namespace dynamic;
|
||||||
|
|
||||||
inline void newline(std::stringstream& ss,
|
inline void newline(
|
||||||
bool nice, uint indent,
|
std::stringstream& ss,
|
||||||
const std::string& indentstr) {
|
bool nice, uint indent,
|
||||||
|
const std::string& indentstr
|
||||||
|
) {
|
||||||
if (nice) {
|
if (nice) {
|
||||||
ss << "\n";
|
ss << "\n";
|
||||||
for (uint i = 0; i < indent; i++) {
|
for (uint i = 0; i < indent; i++) {
|
||||||
@@ -24,29 +26,34 @@ inline void newline(std::stringstream& ss,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stringify(const Value* value,
|
void stringify(
|
||||||
std::stringstream& ss,
|
const Value* value,
|
||||||
int indent,
|
std::stringstream& ss,
|
||||||
const std::string& indentstr,
|
int indent,
|
||||||
bool nice);
|
const std::string& indentstr,
|
||||||
|
bool nice
|
||||||
|
);
|
||||||
|
|
||||||
void stringifyObj(const Map* obj,
|
void stringifyObj(
|
||||||
std::stringstream& ss,
|
const Map* obj,
|
||||||
int indent,
|
std::stringstream& ss,
|
||||||
const std::string& indentstr,
|
int indent,
|
||||||
bool nice);
|
const std::string& indentstr,
|
||||||
|
bool nice
|
||||||
|
);
|
||||||
|
|
||||||
void stringify(const Value* value,
|
void stringify(
|
||||||
std::stringstream& ss,
|
const Value* value,
|
||||||
int indent,
|
std::stringstream& ss,
|
||||||
const std::string& indentstr,
|
int indent,
|
||||||
bool nice) {
|
const std::string& indentstr,
|
||||||
if (value->type == valtype::map) {
|
bool nice
|
||||||
auto map = std::get<Map*>(value->value);
|
) {
|
||||||
stringifyObj(map, ss, indent, indentstr, nice);
|
if (auto map = std::get_if<Map*>(&value->value)) {
|
||||||
|
stringifyObj(*map, ss, indent, indentstr, nice);
|
||||||
}
|
}
|
||||||
else if (value->type == valtype::list) {
|
else if (auto listptr = std::get_if<List*>(&value->value)) {
|
||||||
auto list = std::get<List*>(value->value);
|
auto list = *listptr;
|
||||||
if (list->size() == 0) {
|
if (list->size() == 0) {
|
||||||
ss << "[]";
|
ss << "[]";
|
||||||
return;
|
return;
|
||||||
@@ -66,23 +73,24 @@ void stringify(const Value* value,
|
|||||||
newline(ss, true, indent - 1, indentstr);
|
newline(ss, true, indent - 1, indentstr);
|
||||||
}
|
}
|
||||||
ss << ']';
|
ss << ']';
|
||||||
} else if (value->type == valtype::boolean) {
|
} else if (auto flag = std::get_if<bool>(&value->value)) {
|
||||||
ss << (std::get<bool>(value->value) ? "true" : "false");
|
ss << (*flag ? "true" : "false");
|
||||||
} else if (value->type == valtype::number) {
|
} else if (auto num = std::get_if<number_t>(&value->value)) {
|
||||||
ss << std::setprecision(15);
|
ss << std::setprecision(15) << *num;
|
||||||
ss << std::get<number_t>(value->value);
|
} else if (auto num = std::get_if<integer_t>(&value->value)) {
|
||||||
} else if (value->type == valtype::integer) {
|
ss << *num;
|
||||||
ss << std::get<integer_t>(value->value);
|
} else if (auto str = std::get_if<std::string>(&value->value)) {
|
||||||
} else if (value->type == valtype::string) {
|
ss << util::escape(*str);
|
||||||
ss << util::escape(std::get<std::string>(value->value));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void stringifyObj(const Map* obj,
|
void stringifyObj(
|
||||||
std::stringstream& ss,
|
const Map* obj,
|
||||||
int indent,
|
std::stringstream& ss,
|
||||||
const std::string& indentstr,
|
int indent,
|
||||||
bool nice) {
|
const std::string& indentstr,
|
||||||
|
bool nice
|
||||||
|
) {
|
||||||
if (obj->values.empty()) {
|
if (obj->values.empty()) {
|
||||||
ss << "{}";
|
ss << "{}";
|
||||||
return;
|
return;
|
||||||
@@ -109,9 +117,10 @@ void stringifyObj(const Map* obj,
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string json::stringify(
|
std::string json::stringify(
|
||||||
const Map* obj,
|
const Map* obj,
|
||||||
bool nice,
|
bool nice,
|
||||||
const std::string& indent) {
|
const std::string& indent
|
||||||
|
) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
stringifyObj(obj, ss, 1, indent, nice);
|
stringifyObj(obj, ss, 1, indent, nice);
|
||||||
return ss.str();
|
return ss.str();
|
||||||
@@ -121,7 +130,7 @@ Parser::Parser(const std::string& filename, const std::string& source)
|
|||||||
: BasicParser(filename, source) {
|
: BasicParser(filename, source) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Map* Parser::parse() {
|
std::unique_ptr<Map> Parser::parse() {
|
||||||
char next = peek();
|
char next = peek();
|
||||||
if (next != '{') {
|
if (next != '{') {
|
||||||
throw error("'{' expected");
|
throw error("'{' expected");
|
||||||
@@ -129,7 +138,7 @@ Map* Parser::parse() {
|
|||||||
return parseObject();
|
return parseObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
Map* Parser::parseObject() {
|
std::unique_ptr<Map> Parser::parseObject() {
|
||||||
expect('{');
|
expect('{');
|
||||||
auto obj = std::make_unique<Map>();
|
auto obj = std::make_unique<Map>();
|
||||||
auto& map = obj->values;
|
auto& map = obj->values;
|
||||||
@@ -155,10 +164,10 @@ Map* Parser::parseObject() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pos++;
|
pos++;
|
||||||
return obj.release();
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
List* Parser::parseList() {
|
std::unique_ptr<List> Parser::parseList() {
|
||||||
expect('[');
|
expect('[');
|
||||||
auto arr = std::make_unique<List>();
|
auto arr = std::make_unique<List>();
|
||||||
auto& values = arr->values;
|
auto& values = arr->values;
|
||||||
@@ -167,7 +176,7 @@ List* Parser::parseList() {
|
|||||||
skipLine();
|
skipLine();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
values.push_back(std::unique_ptr<Value>(parseValue()));
|
values.push_back(parseValue());
|
||||||
|
|
||||||
char next = peek();
|
char next = peek();
|
||||||
if (next == ',') {
|
if (next == ',') {
|
||||||
@@ -179,73 +188,63 @@ List* Parser::parseList() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pos++;
|
pos++;
|
||||||
return arr.release();
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value* Parser::parseValue() {
|
std::unique_ptr<Value> Parser::parseValue() {
|
||||||
char next = peek();
|
char next = peek();
|
||||||
dynamic::valvalue val;
|
dynamic::valvalue val;
|
||||||
if (next == '-' || next == '+') {
|
if (next == '-' || next == '+') {
|
||||||
pos++;
|
pos++;
|
||||||
number_u num;
|
number_u num;
|
||||||
valtype type;
|
|
||||||
if (parseNumber(next == '-' ? -1 : 1, num)) {
|
if (parseNumber(next == '-' ? -1 : 1, num)) {
|
||||||
val = std::get<integer_t>(num);
|
val = std::get<integer_t>(num);
|
||||||
type = valtype::integer;
|
|
||||||
} else {
|
} else {
|
||||||
val = std::get<number_t>(num);
|
val = std::get<number_t>(num);
|
||||||
type = valtype::number;
|
|
||||||
}
|
}
|
||||||
return new Value(type, val);
|
return std::make_unique<Value>(val);
|
||||||
}
|
}
|
||||||
if (is_identifier_start(next)) {
|
if (is_identifier_start(next)) {
|
||||||
std::string literal = parseName();
|
std::string literal = parseName();
|
||||||
if (literal == "true") {
|
if (literal == "true") {
|
||||||
val = true;
|
return Value::boolean(true);
|
||||||
return new Value(valtype::boolean, val);
|
|
||||||
} else if (literal == "false") {
|
} else if (literal == "false") {
|
||||||
val = false;
|
return Value::boolean(false);
|
||||||
return new Value(valtype::boolean, val);
|
|
||||||
} else if (literal == "inf") {
|
} else if (literal == "inf") {
|
||||||
val = INFINITY;
|
return Value::of(INFINITY);
|
||||||
return new Value(valtype::number, val);
|
|
||||||
} else if (literal == "nan") {
|
} else if (literal == "nan") {
|
||||||
val = NAN;
|
return Value::of(NAN);
|
||||||
return new Value(valtype::number, val);
|
|
||||||
}
|
}
|
||||||
throw error("invalid literal ");
|
throw error("invalid literal ");
|
||||||
}
|
}
|
||||||
if (next == '{') {
|
if (next == '{') {
|
||||||
val = parseObject();
|
val = parseObject().release();
|
||||||
return new Value(valtype::map, val);
|
return std::make_unique<Value>(val);
|
||||||
}
|
}
|
||||||
if (next == '[') {
|
if (next == '[') {
|
||||||
val = parseList();
|
val = parseList().release();
|
||||||
return new Value(valtype::list, val);
|
return std::make_unique<Value>(val);
|
||||||
}
|
}
|
||||||
if (is_digit(next)) {
|
if (is_digit(next)) {
|
||||||
number_u num;
|
number_u num;
|
||||||
valtype type;
|
|
||||||
if (parseNumber(1, num)) {
|
if (parseNumber(1, num)) {
|
||||||
val = std::get<integer_t>(num);
|
val = std::get<integer_t>(num);
|
||||||
type = valtype::integer;
|
|
||||||
} else {
|
} else {
|
||||||
val = std::get<number_t>(num);
|
val = std::get<number_t>(num);
|
||||||
type = valtype::number;
|
|
||||||
}
|
}
|
||||||
return new Value(type, val);
|
return std::make_unique<Value>(val);
|
||||||
}
|
}
|
||||||
if (next == '"' || next == '\'') {
|
if (next == '"' || next == '\'') {
|
||||||
pos++;
|
pos++;
|
||||||
val = parseString(next);
|
val = parseString(next);
|
||||||
return new Value(valtype::string, val);
|
return std::make_unique<Value>(val);
|
||||||
}
|
}
|
||||||
throw error("unexpected character '"+std::string({next})+"'");
|
throw error("unexpected character '"+std::string({next})+"'");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Map> json::parse(const std::string& filename, const std::string& source) {
|
std::unique_ptr<Map> json::parse(const std::string& filename, const std::string& source) {
|
||||||
Parser parser(filename, source);
|
Parser parser(filename, source);
|
||||||
return std::unique_ptr<Map>(parser.parse());
|
return parser.parse();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Map> json::parse(const std::string& source) {
|
std::unique_ptr<Map> json::parse(const std::string& source) {
|
||||||
|
|||||||
+9
-8
@@ -20,22 +20,23 @@ namespace dynamic {
|
|||||||
|
|
||||||
namespace json {
|
namespace json {
|
||||||
class Parser : public BasicParser {
|
class Parser : public BasicParser {
|
||||||
dynamic::List* parseList();
|
std::unique_ptr<dynamic::List> parseList();
|
||||||
dynamic::Map* parseObject();
|
std::unique_ptr<dynamic::Map> parseObject();
|
||||||
dynamic::Value* parseValue();
|
std::unique_ptr<dynamic::Value> parseValue();
|
||||||
public:
|
public:
|
||||||
Parser(const std::string& filename, const std::string& source);
|
Parser(const std::string& filename, const std::string& source);
|
||||||
|
|
||||||
dynamic::Map* parse();
|
std::unique_ptr<dynamic::Map> parse();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern std::unique_ptr<dynamic::Map> parse(const std::string& filename, const std::string& source);
|
std::unique_ptr<dynamic::Map> parse(const std::string& filename, const std::string& source);
|
||||||
extern std::unique_ptr<dynamic::Map> parse(const std::string& source);
|
std::unique_ptr<dynamic::Map> parse(const std::string& source);
|
||||||
|
|
||||||
extern std::string stringify(
|
std::string stringify(
|
||||||
const dynamic::Map* obj,
|
const dynamic::Map* obj,
|
||||||
bool nice,
|
bool nice,
|
||||||
const std::string& indent);
|
const std::string& indent
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // CODERS_JSON_HPP_
|
#endif // CODERS_JSON_HPP_
|
||||||
|
|||||||
+2
-2
@@ -17,8 +17,8 @@ inline constexpr bool ENGINE_DEBUG_BUILD = true;
|
|||||||
|
|
||||||
inline const std::string ENGINE_VERSION_STRING = "0.21";
|
inline const std::string ENGINE_VERSION_STRING = "0.21";
|
||||||
|
|
||||||
inline constexpr int BLOCK_AIR = 0;
|
inline constexpr blockid_t BLOCK_AIR = 0;
|
||||||
inline constexpr int ITEM_EMPTY = 0;
|
inline constexpr itemid_t ITEM_EMPTY = 0;
|
||||||
|
|
||||||
inline constexpr int CHUNK_W = 16;
|
inline constexpr int CHUNK_W = 16;
|
||||||
inline constexpr int CHUNK_H = 256;
|
inline constexpr int CHUNK_H = 256;
|
||||||
|
|||||||
+38
-163
@@ -1,7 +1,5 @@
|
|||||||
#include "dynamic.hpp"
|
#include "dynamic.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
using namespace dynamic;
|
using namespace dynamic;
|
||||||
|
|
||||||
List::~List() {
|
List::~List() {
|
||||||
@@ -9,7 +7,7 @@ List::~List() {
|
|||||||
|
|
||||||
std::string List::str(size_t index) const {
|
std::string List::str(size_t index) const {
|
||||||
const auto& val = values[index];
|
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::string: return std::get<std::string>(val->value);
|
||||||
case valtype::boolean: return std::get<bool>(val->value) ? "true" : "false";
|
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::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 {
|
number_t List::num(size_t index) const {
|
||||||
const auto& val = values[index];
|
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::number: return std::get<number_t>(val->value);
|
||||||
case valtype::integer: return std::get<integer_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::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 {
|
integer_t List::integer(size_t index) const {
|
||||||
const auto& val = values[index];
|
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::number: return std::get<number_t>(val->value);
|
||||||
case valtype::integer: return std::get<integer_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::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 {
|
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");
|
throw std::runtime_error("type error");
|
||||||
}
|
}
|
||||||
return std::get<Map*>(values[index]->value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List* List::list(size_t index) const {
|
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");
|
throw std::runtime_error("type error");
|
||||||
}
|
}
|
||||||
return std::get<List*>(values[index]->value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool List::flag(size_t index) const {
|
bool List::flag(size_t index) const {
|
||||||
const auto& val = values[index];
|
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::integer: return std::get<integer_t>(val->value);
|
||||||
case valtype::boolean: return std::get<bool>(val->value);
|
case valtype::boolean: return std::get<bool>(val->value);
|
||||||
default:
|
default:
|
||||||
@@ -74,57 +74,11 @@ Value* List::getValueWriteable(size_t index) const {
|
|||||||
return values.at(index).get();
|
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) {
|
List& List::put(std::unique_ptr<Value> value) {
|
||||||
values.emplace_back(std::move(value));
|
values.emplace_back(std::move(value));
|
||||||
return *this;
|
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& List::putList() {
|
||||||
List* arr = new List();
|
List* arr = new List();
|
||||||
put(arr);
|
put(arr);
|
||||||
@@ -145,43 +99,15 @@ Map::~Map() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Map::str(std::string key, std::string& dst) const {
|
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 {
|
std::string Map::get(const std::string& key, const std::string def) 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 {
|
|
||||||
auto found = values.find(key);
|
auto found = values.find(key);
|
||||||
if (found == values.end())
|
if (found == values.end())
|
||||||
return def;
|
return def;
|
||||||
auto& val = found->second;
|
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::string: return std::get<std::string>(val->value);
|
||||||
case valtype::boolean: return std::get<bool>(val->value) ? "true" : "false";
|
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::number: return std::to_string(std::get<number_t>(val->value));
|
||||||
@@ -190,12 +116,12 @@ std::string Map::getStr(std::string key, const std::string& def) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
auto found = values.find(key);
|
||||||
if (found == values.end())
|
if (found == values.end())
|
||||||
return def;
|
return def;
|
||||||
auto& val = found->second;
|
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::number: return std::get<number_t>(val->value);
|
||||||
case valtype::integer: return std::get<integer_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::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);
|
auto found = values.find(key);
|
||||||
if (found == values.end())
|
if (found == values.end())
|
||||||
return def;
|
return def;
|
||||||
auto& val = found->second;
|
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::number: return std::get<number_t>(val->value);
|
||||||
case valtype::integer: return std::get<integer_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::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);
|
auto found = values.find(key);
|
||||||
if (found == values.end())
|
if (found == values.end())
|
||||||
return def;
|
return def;
|
||||||
auto& val = found->second;
|
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::integer: return std::get<integer_t>(val->value);
|
||||||
case valtype::boolean: return std::get<bool>(val->value);
|
case valtype::boolean: return std::get<bool>(val->value);
|
||||||
default: throw std::runtime_error("type error");
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
Map* Map::map(std::string key) const {
|
||||||
auto found = values.find(key);
|
auto found = values.find(key);
|
||||||
if (found != values.end()) {
|
if (found != values.end()) {
|
||||||
auto& val = found->second;
|
if (auto* val = std::get_if<Map*>(&found->second->value)) {
|
||||||
if (val->type != valtype::map)
|
return *val;
|
||||||
return nullptr;
|
}
|
||||||
return std::get<Map*>(val->value);
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -277,57 +202,7 @@ List* Map::list(std::string key) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Map::flag(std::string key, bool& dst) const {
|
void Map::flag(std::string key, bool& dst) const {
|
||||||
dst = getBool(key, dst);
|
dst = get(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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Map& Map::put(std::string key, std::unique_ptr<Value> value) {
|
Map& Map::put(std::string key, std::unique_ptr<Value> value) {
|
||||||
@@ -351,7 +226,7 @@ Map& Map::putMap(std::string key) {
|
|||||||
return *obj;
|
return *obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Map::has(std::string key) {
|
bool Map::has(const std::string& key) const {
|
||||||
return values.find(key) != values.end();
|
return values.find(key) != values.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,11 +234,11 @@ size_t Map::size() const {
|
|||||||
return values.size();
|
return values.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::Value(valtype type, valvalue value) : type(type), value(value) {
|
Value::Value(valvalue value) : value(value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::~Value() {
|
Value::~Value() {
|
||||||
switch (type) {
|
switch (static_cast<valtype>(value.index())) {
|
||||||
case valtype::map: delete std::get<Map*>(value); break;
|
case valtype::map: delete std::get<Map*>(value); break;
|
||||||
case valtype::list: delete std::get<List*>(value); break;
|
case valtype::list: delete std::get<List*>(value); break;
|
||||||
default:
|
default:
|
||||||
@@ -372,21 +247,21 @@ Value::~Value() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Value> Value::boolean(bool 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) {
|
std::unique_ptr<Value> Value::of(number_u value) {
|
||||||
if (std::holds_alternative<integer_t>(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 {
|
} 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) {
|
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) {
|
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 <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
#include <stdexcept>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace dynamic {
|
namespace dynamic {
|
||||||
@@ -15,10 +16,11 @@ namespace dynamic {
|
|||||||
class Value;
|
class Value;
|
||||||
|
|
||||||
enum class valtype {
|
enum class valtype {
|
||||||
none, map, list, number, integer, string, boolean
|
none=0, map, list, string, number, boolean, integer
|
||||||
};
|
};
|
||||||
|
|
||||||
using valvalue = std::variant<
|
using valvalue = std::variant<
|
||||||
|
std::monostate,
|
||||||
Map*,
|
Map*,
|
||||||
List*,
|
List*,
|
||||||
std::string,
|
std::string,
|
||||||
@@ -29,9 +31,8 @@ namespace dynamic {
|
|||||||
|
|
||||||
class Value {
|
class Value {
|
||||||
public:
|
public:
|
||||||
valtype type;
|
|
||||||
valvalue value;
|
valvalue value;
|
||||||
Value(valtype type, valvalue value);
|
Value(valvalue value);
|
||||||
~Value();
|
~Value();
|
||||||
|
|
||||||
static std::unique_ptr<Value> boolean(bool value);
|
static std::unique_ptr<Value> boolean(bool value);
|
||||||
@@ -60,16 +61,11 @@ namespace dynamic {
|
|||||||
return values.at(i).get();
|
return values.at(i).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
List& put(uint value);
|
template<typename T>
|
||||||
List& put(int value);
|
List& put(T value) {
|
||||||
List& put(uint64_t value);
|
return put(std::make_unique<Value>(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);
|
|
||||||
List& put(std::unique_ptr<Value> value);
|
List& put(std::unique_ptr<Value> value);
|
||||||
|
|
||||||
Value* getValueWriteable(size_t index) const;
|
Value* getValueWriteable(size_t index) const;
|
||||||
@@ -85,15 +81,28 @@ namespace dynamic {
|
|||||||
std::unordered_map<std::string, std::unique_ptr<Value>> values;
|
std::unordered_map<std::string, std::unique_ptr<Value>> values;
|
||||||
~Map();
|
~Map();
|
||||||
|
|
||||||
std::string getStr(std::string key) const;
|
template<typename T>
|
||||||
number_t getNum(std::string key) const;
|
T get(const std::string& key) const {
|
||||||
integer_t getInt(std::string key) const;
|
if (!has(key)) {
|
||||||
bool getBool(std::string key) const;
|
throw std::runtime_error("missing key '"+key+"'");
|
||||||
|
}
|
||||||
|
return get(key, T());
|
||||||
|
}
|
||||||
|
|
||||||
std::string getStr(std::string key, const std::string& def) const;
|
std::string get(const std::string& key, const std::string def) const;
|
||||||
number_t getNum(std::string key, double def) const;
|
number_t get(const std::string& key, double def) const;
|
||||||
integer_t getInt(std::string key, integer_t def) const;
|
integer_t get(const std::string& key, integer_t def) const;
|
||||||
bool getBool(std::string key, bool 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 str(std::string key, std::string& dst) const;
|
||||||
void num(std::string key, int& dst) const;
|
void num(std::string key, int& dst) const;
|
||||||
@@ -107,17 +116,14 @@ namespace dynamic {
|
|||||||
List* list(std::string key) const;
|
List* list(std::string key) const;
|
||||||
void flag(std::string key, bool& dst) const;
|
void flag(std::string key, bool& dst) const;
|
||||||
|
|
||||||
Map& put(std::string key, uint value);
|
template<typename T>
|
||||||
Map& put(std::string key, int value);
|
Map& put(std::string key, T value) {
|
||||||
Map& put(std::string key, int64_t value);
|
return put(key, std::make_unique<Value>(value));
|
||||||
Map& put(std::string key, uint64_t value);
|
}
|
||||||
Map& put(std::string key, float value);
|
Map& put(std::string key, uint64_t value) {
|
||||||
Map& put(std::string key, double value);
|
return put(key, Value::of(static_cast<integer_t>(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);
|
|
||||||
Map& put(std::string key, std::unique_ptr<Value> value);
|
Map& put(std::string key, std::unique_ptr<Value> value);
|
||||||
|
|
||||||
void remove(const std::string& key);
|
void remove(const std::string& key);
|
||||||
@@ -125,7 +131,7 @@ namespace dynamic {
|
|||||||
List& putList(std::string key);
|
List& putList(std::string key);
|
||||||
Map& putMap(std::string key);
|
Map& putMap(std::string key);
|
||||||
|
|
||||||
bool has(std::string key);
|
bool has(const std::string& key) const;
|
||||||
size_t size() const;
|
size_t size() const;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -185,6 +185,7 @@ void Engine::processPostRunnables() {
|
|||||||
void Engine::saveSettings() {
|
void Engine::saveSettings() {
|
||||||
logger.info() << "saving settings";
|
logger.info() << "saving settings";
|
||||||
files::write_string(paths->getSettingsFile(), toml::stringify(settingsHandler));
|
files::write_string(paths->getSettingsFile(), toml::stringify(settingsHandler));
|
||||||
|
logger.info() << "saving bindings";
|
||||||
files::write_string(paths->getControlsFile(), Events::writeBindings());
|
files::write_string(paths->getControlsFile(), Events::writeBindings());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+18
-27
@@ -119,18 +119,14 @@ bool SettingsHandler::has(const std::string& name) const {
|
|||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
static void set_numeric_value(T* setting, const dynamic::Value& value) {
|
static void set_numeric_value(T* setting, const dynamic::Value& value) {
|
||||||
switch (value.type) {
|
if (auto num = std::get_if<integer_t>(&value.value)) {
|
||||||
case dynamic::valtype::integer:
|
setting->set(*num);
|
||||||
setting->set(std::get<integer_t>(value.value));
|
} else if (auto num = std::get_if<number_t>(&value.value)) {
|
||||||
break;
|
setting->set(*num);
|
||||||
case dynamic::valtype::number:
|
} else if (auto flag = std::get_if<bool>(&value.value)) {
|
||||||
setting->set(std::get<number_t>(value.value));
|
setting->set(*flag);
|
||||||
break;
|
} else {
|
||||||
case dynamic::valtype::boolean:
|
throw std::runtime_error("type error, numeric value expected");
|
||||||
setting->set(std::get<bool>(value.value));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw std::runtime_error("type error, numeric value expected");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,21 +143,16 @@ void SettingsHandler::setValue(const std::string& name, const dynamic::Value& va
|
|||||||
} else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
|
} else if (auto flag = dynamic_cast<FlagSetting*>(setting)) {
|
||||||
set_numeric_value(flag, value);
|
set_numeric_value(flag, value);
|
||||||
} else if (auto string = dynamic_cast<StringSetting*>(setting)) {
|
} else if (auto string = dynamic_cast<StringSetting*>(setting)) {
|
||||||
switch (value.type) {
|
if (auto num = std::get_if<integer_t>(&value.value)) {
|
||||||
case dynamic::valtype::string:
|
string->set(std::to_string(*num));
|
||||||
string->set(std::get<std::string>(value.value));
|
} else if (auto num = std::get_if<number_t>(&value.value)) {
|
||||||
break;
|
string->set(std::to_string(*num));
|
||||||
case dynamic::valtype::integer:
|
} else if (auto flag = std::get_if<bool>(&value.value)) {
|
||||||
string->set(std::to_string(std::get<integer_t>(value.value)));
|
string->set(*flag ? "true" : "false");
|
||||||
break;
|
} else if (auto str = std::get_if<std::string>(&value.value)) {
|
||||||
case dynamic::valtype::number:
|
string->set(*str);
|
||||||
string->set(std::to_string(std::get<number_t>(value.value)));
|
} else {
|
||||||
break;
|
throw std::runtime_error("not implemented for type");
|
||||||
case dynamic::valtype::boolean:
|
|
||||||
string->set(std::to_string(std::get<bool>(value.value)));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw std::runtime_error("not implemented for type");
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error("type is not implement - setting '"+name+"'");
|
throw std::runtime_error("type is not implement - setting '"+name+"'");
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
static debug::Logger logger("locale");
|
static debug::Logger logger("locale");
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
using namespace std::literals;
|
||||||
|
|
||||||
std::unique_ptr<langs::Lang> langs::current;
|
std::unique_ptr<langs::Lang> langs::current;
|
||||||
std::unordered_map<std::string, langs::LocaleInfo> langs::locales_info;
|
std::unordered_map<std::string, langs::LocaleInfo> langs::locales_info;
|
||||||
@@ -78,12 +79,11 @@ void langs::loadLocalesInfo(const fs::path& resdir, std::string& fallback) {
|
|||||||
auto langInfo = entry.second.get();
|
auto langInfo = entry.second.get();
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
if (langInfo->type == dynamic::valtype::map) {
|
if (auto mapptr = std::get_if<dynamic::Map*>(&langInfo->value)) {
|
||||||
name = std::get<dynamic::Map*>(langInfo->value)->getStr("name", "none");
|
name = (*mapptr)->get("name", "none"s);
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "[" << entry.first << " (" << name << ")] ";
|
std::cout << "[" << entry.first << " (" << name << ")] ";
|
||||||
langs::locales_info[entry.first] = LocaleInfo {entry.first, name};
|
langs::locales_info[entry.first] = LocaleInfo {entry.first, name};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ void Inventory::move(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Inventory::deserialize(dynamic::Map* src) {
|
void Inventory::deserialize(dynamic::Map* src) {
|
||||||
id = src->getNum("id", 1);
|
id = src->get("id", 1);
|
||||||
auto slotsarr = src->list("slots");
|
auto slotsarr = src->list("slots");
|
||||||
size_t slotscount = slotsarr->size();
|
size_t slotscount = slotsarr->size();
|
||||||
while (slots.size() < slotscount) {
|
while (slots.size() < slotscount) {
|
||||||
@@ -58,8 +58,8 @@ void Inventory::deserialize(dynamic::Map* src) {
|
|||||||
}
|
}
|
||||||
for (size_t i = 0; i < slotscount; i++) {
|
for (size_t i = 0; i < slotscount; i++) {
|
||||||
auto item = slotsarr->map(i);
|
auto item = slotsarr->map(i);
|
||||||
itemid_t id = item->getInt("id", ITEM_EMPTY);
|
itemid_t id = item->get("id", ITEM_EMPTY);
|
||||||
itemcount_t count = item->getInt("count", 0);
|
itemcount_t count = item->get("count", 0);
|
||||||
auto& slot = slots[i];
|
auto& slot = slots[i];
|
||||||
slot.set(ItemStack(id, count));
|
slot.set(ItemStack(id, count));
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ void Inventory::convert(dynamic::Map* data, const ContentLUT* lut) {
|
|||||||
auto slotsarr = data->list("slots");
|
auto slotsarr = data->list("slots");
|
||||||
for (size_t i = 0; i < slotsarr->size(); i++) {
|
for (size_t i = 0; i < slotsarr->size(); i++) {
|
||||||
auto item = slotsarr->map(i);
|
auto item = slotsarr->map(i);
|
||||||
itemid_t id = item->getInt("id", ITEM_EMPTY);
|
itemid_t id = item->get("id", ITEM_EMPTY);
|
||||||
itemid_t replacement = lut->getItemId(id);
|
itemid_t replacement = lut->getItemId(id);
|
||||||
item->put("id", replacement);
|
item->put("id", replacement);
|
||||||
if (replacement == 0 && item->has("count")) {
|
if (replacement == 0 && item->has("count")) {
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ static void show_content_missing(
|
|||||||
contentEntry.put("name", entry.name);
|
contentEntry.put("name", entry.name);
|
||||||
}
|
}
|
||||||
std::vector<std::unique_ptr<dynamic::Value>> args;
|
std::vector<std::unique_ptr<dynamic::Value>> args;
|
||||||
args.emplace_back(std::make_unique<Value>(valtype::map, root.release()));
|
args.emplace_back(std::make_unique<Value>(root.release()));
|
||||||
menus::show(engine, "reports/missing_content", std::move(args));
|
menus::show(engine, "reports/missing_content", std::move(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -217,41 +217,32 @@ int lua::LuaState::pushvalue(int idx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int lua::LuaState::pushvalue(const dynamic::Value& value) {
|
int lua::LuaState::pushvalue(const dynamic::Value& value) {
|
||||||
using dynamic::valtype;
|
using namespace dynamic;
|
||||||
switch (value.type) {
|
|
||||||
case valtype::boolean:
|
if (auto* flag = std::get_if<bool>(&value.value)) {
|
||||||
pushboolean(std::get<bool>(value.value));
|
pushboolean(*flag);
|
||||||
break;
|
} else if (auto* num = std::get_if<integer_t>(&value.value)) {
|
||||||
case valtype::integer:
|
pushinteger(*num);
|
||||||
pushinteger(std::get<integer_t>(value.value));
|
} else if (auto* num = std::get_if<number_t>(&value.value)) {
|
||||||
break;
|
pushnumber(*num);
|
||||||
case valtype::number:
|
} else if (auto* str = std::get_if<std::string>(&value.value)) {
|
||||||
pushnumber(std::get<number_t>(value.value));
|
pushstring(str->c_str());
|
||||||
break;
|
} else if (List* const* listptr = std::get_if<List*>(&value.value)) {
|
||||||
case valtype::string:
|
auto list = *listptr;
|
||||||
pushstring(std::get<std::string>(value.value).c_str());
|
lua_createtable(L, list->size(), 0);
|
||||||
break;
|
for (size_t i = 0; i < list->size(); i++) {
|
||||||
case valtype::none:
|
pushvalue(*list->get(i));
|
||||||
pushnil();
|
lua_rawseti(L, -2, i+1);
|
||||||
break;
|
|
||||||
case valtype::list: {
|
|
||||||
auto list = std::get<dynamic::List*>(value.value);
|
|
||||||
lua_createtable(L, list->size(), 0);
|
|
||||||
for (size_t i = 0; i < list->size(); i++) {
|
|
||||||
pushvalue(*list->get(i));
|
|
||||||
lua_rawseti(L, -2, i+1);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
case valtype::map: {
|
} else if (Map* const* mapptr = std::get_if<Map*>(&value.value)) {
|
||||||
auto map = std::get<dynamic::Map*>(value.value);
|
auto map = *mapptr;
|
||||||
lua_createtable(L, 0, map->size());
|
lua_createtable(L, 0, map->size());
|
||||||
for (auto& entry : map->values) {
|
for (auto& entry : map->values) {
|
||||||
pushvalue(*entry.second);
|
pushvalue(*entry.second);
|
||||||
lua_setfield(L, -2, entry.first.c_str());
|
lua_setfield(L, -2, entry.first.c_str());
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
pushnil();
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -318,7 +309,7 @@ std::unique_ptr<dynamic::Value> lua::LuaState::tovalue(int idx) {
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case LUA_TNIL:
|
case LUA_TNIL:
|
||||||
case LUA_TNONE:
|
case LUA_TNONE:
|
||||||
return std::make_unique<Value>(valtype::none, (integer_t)0);
|
return std::make_unique<Value>(std::monostate());
|
||||||
case LUA_TBOOLEAN:
|
case LUA_TBOOLEAN:
|
||||||
return Value::boolean(lua_toboolean(L, idx) == 1);
|
return Value::boolean(lua_toboolean(L, idx) == 1);
|
||||||
case LUA_TNUMBER: {
|
case LUA_TNUMBER: {
|
||||||
@@ -342,7 +333,7 @@ std::unique_ptr<dynamic::Value> lua::LuaState::tovalue(int idx) {
|
|||||||
list->put(tovalue(-1));
|
list->put(tovalue(-1));
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
return std::make_unique<Value>(valtype::list, list.release());
|
return std::make_unique<Value>(list.release());
|
||||||
} else {
|
} else {
|
||||||
// table
|
// table
|
||||||
auto map = std::make_unique<Map>();
|
auto map = std::make_unique<Map>();
|
||||||
@@ -355,7 +346,7 @@ std::unique_ptr<dynamic::Value> lua::LuaState::tovalue(int idx) {
|
|||||||
lua_pop(L, 2);
|
lua_pop(L, 2);
|
||||||
}
|
}
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
return std::make_unique<Value>(valtype::map, map.release());
|
return std::make_unique<Value>(map.release());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -11,22 +11,22 @@ namespace scripting {
|
|||||||
|
|
||||||
static int l_json_stringify(lua_State* L) {
|
static int l_json_stringify(lua_State* L) {
|
||||||
auto value = scripting::state->tovalue(1);
|
auto value = scripting::state->tovalue(1);
|
||||||
if (value->type != dynamic::valtype::map) {
|
|
||||||
|
if (auto mapptr = std::get_if<dynamic::Map*>(&value->value)) {
|
||||||
|
bool nice = lua_toboolean(L, 2);
|
||||||
|
auto string = json::stringify(*mapptr, nice, " ");
|
||||||
|
lua_pushstring(L, string.c_str());
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
luaL_error(L, "table expected");
|
luaL_error(L, "table expected");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
bool nice = lua_toboolean(L, 2);
|
|
||||||
auto string = json::stringify(std::get<dynamic::Map*>(value->value), nice, " ");
|
|
||||||
lua_pushstring(L, string.c_str());
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_json_parse(lua_State* L) {
|
static int l_json_parse(lua_State* L) {
|
||||||
auto string = lua_tostring(L, 1);
|
auto string = lua_tostring(L, 1);
|
||||||
auto element = json::parse("<string>", string);
|
auto element = json::parse("<string>", string);
|
||||||
auto value = std::make_unique<dynamic::Value>(
|
auto value = std::make_unique<dynamic::Value>(element.release());
|
||||||
dynamic::valtype::map, element.release()
|
|
||||||
);
|
|
||||||
scripting::state->pushvalue(*value);
|
scripting::state->pushvalue(*value);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ void Player::deserialize(dynamic::Map *src) {
|
|||||||
|
|
||||||
src->flag("flight", flight);
|
src->flag("flight", flight);
|
||||||
src->flag("noclip", noclip);
|
src->flag("noclip", noclip);
|
||||||
setChosenSlot(src->getInt("chosen-slot", getChosenSlot()));
|
setChosenSlot(src->get("chosen-slot", getChosenSlot()));
|
||||||
|
|
||||||
auto invmap = src->map("inventory");
|
auto invmap = src->map("inventory");
|
||||||
if (invmap) {
|
if (invmap) {
|
||||||
|
|||||||
+4
-4
@@ -174,9 +174,9 @@ const std::vector<ContentPack>& World::getPacks() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void World::deserialize(dynamic::Map* root) {
|
void World::deserialize(dynamic::Map* root) {
|
||||||
name = root->getStr("name", name);
|
name = root->get("name", name);
|
||||||
generator = root->getStr("generator", generator);
|
generator = root->get("generator", generator);
|
||||||
seed = root->getInt("seed", seed);
|
seed = root->get("seed", seed);
|
||||||
|
|
||||||
if(generator == "") {
|
if(generator == "") {
|
||||||
generator = WorldGenerators::getDefaultGeneratorID();
|
generator = WorldGenerators::getDefaultGeneratorID();
|
||||||
@@ -197,7 +197,7 @@ void World::deserialize(dynamic::Map* root) {
|
|||||||
timeobj->num("total-time", totalTime);
|
timeobj->num("total-time", totalTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
nextInventoryId = root->getNum("next-inventory-id", 2);
|
nextInventoryId = root->get("next-inventory-id", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<dynamic::Map> World::serialize() const {
|
std::unique_ptr<dynamic::Map> World::serialize() const {
|
||||||
|
|||||||
Reference in New Issue
Block a user