'dynamic' namespace refactor (step 1/2)

This commit is contained in:
MihailRis
2024-05-07 16:00:52 +03:00
parent 80e7bcb203
commit d33edd4cd9
16 changed files with 248 additions and 392 deletions
+39 -33
View File
@@ -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;
};
}