ppc-lang/include/utils/data.hh

113 lines
2.9 KiB
C++
Raw Normal View History

2022-09-19 07:34:19 +00:00
#pragma once
#include <string>
#include <memory>
#include <vector>
#include <unordered_map>
namespace ppc::data {
class map_t;
class array_t;
using number_t = float;
using string_t = std::string;
using bool_t = bool;
class value_t {
private:
// Variant and the C++ comittee can go fuck themselves
union val_t {
map_t *map;
array_t *arr;
number_t num;
string_t *str;
bool_t bl;
} val;
enum type_t {
Null,
Map,
Arr,
Num,
Str,
Bool,
} type;
public:
bool is_null() const;
bool is_map() const;
bool is_array() const;
bool is_number() const;
bool is_string() const;
bool is_bool() const;
2022-10-14 17:27:45 +00:00
array_t &array(const array_t &arr);
map_t &map(const map_t &map);
number_t &number(number_t num);
string_t &string(const string_t &str);
bool_t &boolean(bool_t bl);
array_t &array();
map_t &map();
number_t &number();
string_t &string();
bool_t &boolean();
const array_t &array() const;
const map_t &map() const;
2022-09-19 07:34:19 +00:00
number_t number() const;
2022-10-14 17:27:45 +00:00
const string_t &string() const;
2022-09-19 07:34:19 +00:00
bool_t boolean() const;
2022-10-09 11:18:38 +00:00
value_t &operator=(const value_t &other);
value_t &operator=(const char *other);
2022-09-19 07:34:19 +00:00
~value_t();
value_t();
2022-09-25 10:45:15 +00:00
value_t(const array_t &val);
value_t(const map_t &val);
2022-10-09 11:18:38 +00:00
value_t(std::initializer_list<std::pair<std::string, value_t>> map);
2022-09-19 07:34:19 +00:00
value_t(number_t val);
2022-09-25 10:45:15 +00:00
value_t(const string_t &val);
value_t(const char *val);
2022-09-19 07:34:19 +00:00
value_t(bool_t val);
2022-09-25 10:45:15 +00:00
value_t(const value_t &other);
2022-10-09 11:18:38 +00:00
2022-09-19 07:34:19 +00:00
};
2022-10-22 12:19:05 +00:00
static const value_t null{};
2022-09-19 07:34:19 +00:00
class map_t {
private:
std::unordered_map<std::string, value_t> values;
public:
value_t &operator[](std::string name){
2022-10-09 11:18:38 +00:00
auto res = values.find(name);
if (res == values.end()) {
res = values.emplace(name, value_t()).first;
2022-09-19 07:34:19 +00:00
}
2022-10-09 11:18:38 +00:00
return res->second;
}
2022-10-19 11:21:05 +00:00
const value_t &operator [](std::string name) const {
2022-10-09 11:18:38 +00:00
auto res = values.find(name);
if (res == values.end()) throw "The map doesn't contain a key '" + name + "'.";
return res->second;
}
2022-09-19 07:34:19 +00:00
2022-10-09 11:18:38 +00:00
bool has(std::string key) const {
return values.find(key) != values.end();
2022-09-19 07:34:19 +00:00
}
std::size_t size() const { return values.size(); }
auto begin() const { return values.begin(); }
auto end() const { return values.end(); }
2022-10-09 11:18:38 +00:00
map_t() { }
map_t(std::initializer_list<std::pair<std::string, value_t>> vals) {
for (const auto &pair : vals) {
values.insert(pair);
}
}
2022-09-19 07:34:19 +00:00
};
class array_t : public std::vector<value_t> { };
2022-09-19 07:34:19 +00:00
}