Have a dynamic loading system using rapidjson and cpp that can work with templated…
- User
Have a dynamic loading system using rapidjson and cpp that can work with templated components to create entities. With this being the component implementation class ComponentBase { public: virtual ~ComponentBase() {} virtual void DestroyData(unsigned char* data) const = 0; virtual void MoveData(unsigned char* source, unsigned char* destination) const = 0; virtual void ConstructData(unsigned char* data) const = 0; virtual std::size_t GetSize() const = 0; };
template class Component : public ComponentBase { public: virtual void DestroyData(unsigned char* data) const override;
virtual void MoveData(unsigned char* source, unsigned char* destination) const override; virtual void ConstructData(unsigned char* data) const override; virtual size_t GetSize() const override; static size_t GetTypeID();};
template void Component::DestroyData(unsigned char* data) const { C* dataLocation = std::launder(reinterpret_cast<C*>(data));
dataLocation->~C();}
template void Component::ConstructData(unsigned char* data) const { new (&data[0]) C(); }
template void Component::MoveData(unsigned char* source, unsigned char* destination) const { new (&destination[0]) C(std::move(std::bit_cast<C>(source))); }
template std::size_t Component::GetSize() const { return sizeof(C); }
//template //std::size_t Component::GetTypeID() //{ // return TypeIdGenerator::GetNewID(); //}
template std::size_t Component::GetTypeID() { return TypeIdGenerator::template GetNewID(); }
template static const std::size_t GetTypeID() { return TypeIdGenerator::GetNewID(); }
struct Position { float x; float y; };
struct Name { std::string name; };
struct Size { int width; int height; };
struct Velocity { int vel; };
- Assistant
- User
Is there a better way of doing this? void Scene::Load(const std::string& filename) { std::ifstream file("scene.json");
if (!file.is_open()) { std::cout << "Failed to open file: " << filename << std::endl; return; } // Read the entire JSON content into a string std::string jsonString((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); // Close the file file.close(); // Parse the JSON string into a RapidJSON document rapidjson::Document document; document.Parse(jsonString.c_str()); // Check if parsing was successful if (document.HasParseError()) { std::cout << "Failed to parse JSON file: " << filename << std::endl; return; } const rapidjson::Value& entitiesArray = document["Scene"]["entities"]; for (rapidjson::SizeType i = 0; i < entitiesArray.Size(); ++i) { const rapidjson::Value& entity = entitiesArray[i]; // Read the entity name std::string entityName = entity["name"].GetString(); // Create the entity in your ECS system (assuming you have an ECS instance called "ecs") Entity newEntity(entityComponentSystem_); // Iterate through each component of the entity for (auto componentIterator = entity.MemberBegin(); componentIterator != entity.MemberEnd(); ++componentIterator) { const std::string& componentName = componentIterator->name.GetString(); const rapidjson::Value& componentValue = componentIterator->value; newEntity.AddComponent<Name>({ entityName }); if (componentName == "Position") { float x = componentValue["x"].GetFloat(); float y = componentValue["y"].GetFloat(); newEntity.AddComponent<Position>({ x, y }); } else if (componentName == "Size") { int width = componentValue["width"].GetInt(); int height = componentValue["height"].GetInt(); newEntity.AddComponent<Size>({ width, height }); } else if (componentName == "Velocity") { int velocity = componentValue["Velocity"].GetInt(); newEntity.AddComponent<Velocity>({ velocity }); } } } - Assistant
- User
template <> void load_component_data(Entity& entity, const rapidjson::Value& component) { if (component.HasMember("x") && component["x"].IsFloat() && component.HasMember("y") && component["y"].IsFloat()) { entity.AddComponent({ component["x"].GetFloat(), component["y"].GetFloat() }); } else { throw std::runtime_error("Invalid component data for Position"); } }
template <> void load_component_data(Entity& entity, const rapidjson::Value& component) { if (component.IsString()) { entity.AddComponent({ component.GetString() }); } else { throw std::runtime_error("Invalid component data for Name"); } }
template <> void load_component_data(Entity& entity, const rapidjson::Value& component) { if (component.HasMember("width") && component["width"].IsInt() && component.HasMember("height") && component["height"].IsInt()) { entity.AddComponent({ component["width"].GetInt(), component["height"].GetInt() }); } else { throw std::runtime_error("Invalid component data for Size"); } }
template <> void load_component_data(Entity& entity, const rapidjson::Value& component) { if (component.HasMember("vel") && component["vel"].IsInt()) { entity.AddComponent({ component["vel"].GetInt() }); } else { throw std::runtime_error("Invalid component data for Velocity"); } } Is there a way to do this dynamically?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.