114 lines
3.3 KiB
C++
114 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
struct ikv_node_t;
|
|
|
|
namespace ikv {
|
|
|
|
enum ValueType {
|
|
nullValue = 0,
|
|
stringValue,
|
|
intValue,
|
|
realValue,
|
|
booleanValue,
|
|
objectValue,
|
|
arrayValue
|
|
};
|
|
|
|
enum class Version : unsigned { v1 = 1, v2 = 2 };
|
|
|
|
class Error : public std::runtime_error {
|
|
public:
|
|
using std::runtime_error::runtime_error;
|
|
};
|
|
|
|
class Value {
|
|
public:
|
|
using ArrayIndex = std::uint32_t;
|
|
|
|
explicit Value(ValueType type = objectValue, std::string root_name = "root");
|
|
Value(const Value&) = default;
|
|
Value& operator=(const Value&) = delete;
|
|
|
|
static Value parse(const std::string& text);
|
|
static Value load(const std::string& path);
|
|
static Value fromBinary(const void* data, std::size_t size);
|
|
static Value loadBinary(const std::string& path);
|
|
|
|
ValueType type() const noexcept;
|
|
bool isNull() const noexcept;
|
|
bool isString() const noexcept;
|
|
bool isInt() const noexcept;
|
|
bool isDouble() const noexcept;
|
|
bool isBool() const noexcept;
|
|
bool isObject() const noexcept;
|
|
bool isArray() const noexcept;
|
|
bool empty() const;
|
|
std::size_t size() const;
|
|
std::string name() const;
|
|
|
|
bool isMember(const std::string& key) const;
|
|
Value operator[](const std::string& key);
|
|
Value operator[](const char* key);
|
|
Value operator[](const std::string& key) const;
|
|
Value operator[](const char* key) const;
|
|
Value operator[](ArrayIndex index);
|
|
Value operator[](int index);
|
|
Value operator[](ArrayIndex index) const;
|
|
Value operator[](int index) const;
|
|
Value get(const std::string& key, const Value& fallback) const;
|
|
|
|
Value& operator=(const std::string& value);
|
|
Value& operator=(const char* value);
|
|
Value& operator=(std::int64_t value);
|
|
Value& operator=(int value);
|
|
Value& operator=(double value);
|
|
Value& operator=(bool value);
|
|
|
|
Value makeObject(const std::string& key);
|
|
Value makeArray(const std::string& key, ValueType element_type = nullValue);
|
|
Value append(const std::string& value);
|
|
Value append(const char* value);
|
|
Value append(std::int64_t value);
|
|
Value append(int value);
|
|
Value append(double value);
|
|
Value append(bool value);
|
|
Value appendObject();
|
|
|
|
std::string asString() const;
|
|
std::int64_t asInt64() const;
|
|
int asInt() const;
|
|
double asDouble() const;
|
|
bool asBool() const;
|
|
|
|
void write(const std::string& path, Version version = Version::v2) const;
|
|
void writeBinary(const std::string& path, Version version = Version::v2) const;
|
|
std::vector<std::uint8_t> toBinary(Version version = Version::v2) const;
|
|
void refresh(const std::string& path);
|
|
|
|
private:
|
|
struct Deleter { void operator()(ikv_node_t* node) const noexcept; };
|
|
using Owner = std::shared_ptr<ikv_node_t>;
|
|
|
|
Value(Owner owner, ikv_node_t* node, ikv_node_t* parent, std::string key, bool read_only = false);
|
|
static Value adopt(ikv_node_t* node);
|
|
void require(ValueType expected, const char* operation) const;
|
|
void requireMutable(const char* operation) const;
|
|
void requireObjectParent(const char* operation) const;
|
|
Value appended(std::size_t old_size);
|
|
|
|
Owner owner_;
|
|
ikv_node_t* node_ = nullptr;
|
|
ikv_node_t* parent_ = nullptr;
|
|
std::string key_;
|
|
bool read_only_ = false;
|
|
};
|
|
|
|
} // namespace ikv
|