Skip to content

Commit

Permalink
Merge pull request #70 from LimpingPebble/feat/json-creating-no-ptr
Browse files Browse the repository at this point in the history
remove pointer in json
  • Loading branch information
amasson42 authored Nov 5, 2024
2 parents d4154ff + cd95437 commit 72f0cc5
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 194 deletions.
64 changes: 42 additions & 22 deletions Engine/Scene/src/Scene/Assets/AssetResource_AssimpLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,77 +236,97 @@ void loadMaterials(AssetResource &assetResource, const aiScene *scene) {
}
}

std::shared_ptr<Json::Value> loadMetadataEntry(const aiMetadataEntry &entry) {
void loadMetadataEntry(const aiMetadataEntry &entry, Json::Value &out) {
switch (entry.mType) {
case AI_BOOL:
{
const bool data = *static_cast<bool *>(entry.mData);
return Json::boolean(data);
out.value = data;
return;
}
case AI_INT32:
{
const double data = *static_cast<int32_t *>(entry.mData);
return Json::number(data);
out.value = data;
return;
}
case AI_UINT64:
{
const double data = *static_cast<uint64_t *>(entry.mData);
return Json::number(data);
const uint64_t data = *static_cast<uint64_t *>(entry.mData);
out.value = static_cast<double>(data);
return;
}
case AI_FLOAT:
{
const double data = *static_cast<float *>(entry.mData);
return Json::number(data);
out.value = data;
return;
}
case AI_DOUBLE:
{
const double data = *static_cast<double *>(entry.mData);
return Json::number(data);
out.value = data;
return;
}
case AI_AISTRING:
{
const aiString *data = static_cast<aiString *>(entry.mData);
out.value = data->C_Str();
return;
}
case AI_AIVECTOR3D:
{
out.value = Json::Array();
auto &array(std::get<Json::Array>(out.value));
const aiVector3D *data = static_cast<aiVector3D *>(entry.mData);
Json::Array array_data;
array_data.push_back(Json::number(data->x));
array_data.push_back(Json::number(data->y));
array_data.push_back(Json::number(data->z));
return Json::array(array_data);
array.emplace_back(data->x);
array.emplace_back(data->y);
array.emplace_back(data->z);
return;
}
case AI_AIMETADATA:
{
out.value = Json::Object();
auto &object(std::get<Json::Object>(out.value));
const aiMetadata *data = static_cast<aiMetadata *>(entry.mData);
Json::Object object_data;
for (unsigned int i = 0; i < data->mNumProperties; ++i) {
const aiString &key(data->mKeys[i]);
const aiMetadataEntry &value(data->mValues[i]);
object_data[key.C_Str()] = loadMetadataEntry(value);
assert(key.C_Str() != nullptr);
loadMetadataEntry(value, object[key.C_Str()]);
}
return Json::object(object_data);
return;
}
case AI_INT64:
{
const double data = *static_cast<int64_t *>(entry.mData);
return Json::number(data);
const int64_t data = *static_cast<int64_t *>(entry.mData);
out.value = static_cast<double>(data);
return;
}
case AI_UINT32:
{
const double data = *static_cast<uint32_t *>(entry.mData);
return Json::number(data);
out.value = data;
return;
}
default:
{
out = Json::Value();
return;
}
default: break;
}
return Json::null();
}

void loadMetadata(const aiMetadata *metadata, Json::Object &dst) {
void loadMetadata(const aiMetadata *metadata, Json::Object &out) {
if (metadata == nullptr)
return;

for (unsigned int i = 0; i < metadata->mNumProperties; ++i) {
const aiString &key(metadata->mKeys[i]);
const aiMetadataEntry &value(metadata->mValues[i]);

dst[key.C_Str()] = loadMetadataEntry(value);
assert(key.C_Str() != nullptr);
loadMetadataEntry(value, out[key.C_Str()]);
}
}

Expand Down
45 changes: 24 additions & 21 deletions Engine/Utils/include/Utils/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ namespace Stone::Json {

struct Value;

using Object = std::unordered_map<std::string, std::shared_ptr<Value>>;
using Array = std::vector<std::shared_ptr<Value>>;
using Object = std::unordered_map<std::string, Value>;
using Array = std::vector<Value>;

struct Value {

std::variant<Object, Array, std::string, double, bool, std::nullptr_t> value;

explicit Value(Object obj);
explicit Value(Array arr);
explicit Value(std::string str);
explicit Value(double num);
explicit Value(bool b);
explicit Value(std::nullptr_t n = nullptr);
Value() : value(nullptr) {
}

template <typename T, typename = std::enable_if_t<std::is_constructible_v<
std::variant<Object, Array, std::string, double, bool, std::nullptr_t>, T>>>
Value(T &&val) : value(std::forward<T>(val)) {
}

template <typename T>
bool is() const {
Expand All @@ -46,17 +47,17 @@ struct Value {
return std::get<T>(value);
}

static std::shared_ptr<Value> parseString(const std::string &input);
static std::shared_ptr<Value> parseFile(const std::string &path);
static void parseString(const std::string &input, Value &out);
static void parseFile(const std::string &path, Value &out);
std::string serialize() const;
};

std::shared_ptr<Value> object(const Object &obj = {});
std::shared_ptr<Value> array(const Array &arr = {});
std::shared_ptr<Value> string(const std::string &str = "");
std::shared_ptr<Value> number(double num = 0.0);
std::shared_ptr<Value> boolean(bool b = false);
std::shared_ptr<Value> null();
Value object(const Object &obj = {});
Value array(const Array &arr = {});
Value string(const std::string &str = "");
Value number(double num = 0.0);
Value boolean(bool b = false);
Value null();


enum class TokenType {
Expand All @@ -74,6 +75,8 @@ enum class TokenType {
EndOfFile
};

std::string to_string(TokenType type);

struct Token {
TokenType type;
std::string value;
Expand All @@ -98,16 +101,16 @@ class Parser {
public:
explicit Parser(const std::string &input);

std::shared_ptr<Value> parse();
void parse(Value &out);

private:
Lexer _lexer;
Token _currentToken;

std::shared_ptr<Value> _parseValue();
std::shared_ptr<Value> _parseObject();
std::shared_ptr<Value> _parseArray();
std::shared_ptr<Value> _parsePrimitive();
void _parseValue(Value &out);
void _parseObject(Value &out);
void _parseArray(Value &out);
void _parsePrimitive(Value &out);
void _consume(TokenType expected);
};

Expand Down
Loading

0 comments on commit 72f0cc5

Please sign in to comment.