feat(Resources): Add ResourceDependencyGraph for resource dependency tracking

- Implement dependency graph for resource management
- Add/remove nodes and dependencies
- Reference counting support
- Circular dependency detection
- Add unit tests
This commit is contained in:
2026-03-18 01:13:02 +08:00
parent bd69c3e124
commit d2585f14b3
5 changed files with 414 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
#pragma once
#include "ResourceTypes.h"
#include "../Containers/HashMap.h"
#include "../Containers/Array.h"
#include "../Core/Types.h"
namespace XCEngine {
namespace Resources {
struct DependencyNode {
ResourceGUID guid;
ResourceType type;
Containers::Array<ResourceGUID> dependencies;
Containers::Array<ResourceGUID> dependents;
Core::uint32 refCount = 0;
};
class ResourceDependencyGraph {
public:
ResourceDependencyGraph();
~ResourceDependencyGraph();
void AddNode(ResourceGUID guid, ResourceType type);
void RemoveNode(ResourceGUID guid);
void AddDependency(ResourceGUID owner, ResourceGUID dependency);
void RemoveDependency(ResourceGUID owner, ResourceGUID dependency);
Containers::Array<ResourceGUID> GetDependencies(ResourceGUID guid) const;
Containers::Array<ResourceGUID> GetDependents(ResourceGUID guid) const;
Containers::Array<ResourceGUID> GetAllDependencies(ResourceGUID guid) const;
void IncrementRefCount(ResourceGUID guid);
void DecrementRefCount(ResourceGUID guid);
Core::uint32 GetRefCount(ResourceGUID guid) const;
bool HasCircularDependency(ResourceGUID guid, Containers::Array<ResourceGUID>& outCycle) const;
Containers::Array<ResourceGUID> TopologicalSort() const;
bool Unload(ResourceGUID guid);
void Clear();
bool HasNode(ResourceGUID guid) const;
private:
bool HasCircularDependencyInternal(ResourceGUID guid, Containers::HashMap<ResourceGUID, bool>& visited,
Containers::Array<ResourceGUID>& path, Containers::Array<ResourceGUID>& outCycle) const;
Containers::HashMap<ResourceGUID, DependencyNode> m_nodes;
};
} // namespace Resources
} // namespace XCEngine